2015-11-11 28 views
0

我有以下SQL查詢:SQL選擇一個額外的「假」行每加入

SELECT 
    O.Id AS OrganisationId, o.Name as OrganistionName, 
    pt.product_type_name as ProductType 
FROM 
    [Content].[Organisations] O (nolock) 
JOIN 
    [Content].[ProductOrganisations] PO (nolock) ON PO.OrganisationId = O.Id 
JOIN 
    [dbo].[report_info] R (nolock) ON PO.ProductId = R.report_id 
JOIN 
    [dbo].[CONTENT_ProductTypes] PT (nolock) ON R.[product_type_id] = PT.PRODUCTTYPEID 
WHERE 
    O.ShowCompanyPage = 1 
GROUP BY 
    O.Id , o.Name, PT.PRODUCTTYPEID, pt.product_type_name 
ORDER BY 
    o.Name, pt.product_type_name 

它返回的結果如下:

OrganisationId OrganistionName ProductType 
1  Coca Cola Company Article 
1  Coca Cola Company Book 
1  Coca Cola Company Company Profile 
2  PepsiCo Inc.  Audio Conference 
2  PepsiCo Inc.  Book 
2  PepsiCo Inc.  Company Profile 
3  Pfizer, Inc.  Article 
3  Pfizer, Inc.  Company Profile 
3  Pfizer, Inc.  Credit Rating Report 

但我需要到一個額外的「加假「行每個組織。

此行應作爲第一行出現,且ProductType應爲空白。

例如:

OrganisationId OrganistionName ProductType 
1  Coca Cola Company 
1  Coca Cola Company Article 
1  Coca Cola Company Book 
1  Coca Cola Company Company Profile 
2  PepsiCo Inc.   
2  PepsiCo Inc.  Audio Conference 
2  PepsiCo Inc.  Book 
2  PepsiCo Inc.  Company Profile 
3  Pfizer, Inc. 
3  Pfizer, Inc.  Article 
3  Pfizer, Inc.  Company Profile 
3  Pfizer, Inc.  Credit Rating Report 

任何想法?

+1

您可以添加'UNION SELECT DISTINCT OrganisationId,OrganisationName '' 爲[ProductType] FROM [[[相同的分組查詢這裏] ]' – CactusCake

+0

由於看起來準確性很重要,因此在繼續使用nolock拋出查詢之前,可能需要查看本文。這不是一個神奇的「走得快」按鈕。它有許多人不理解的非常嚴重的副作用。 http://blogs.sqlsentry.com/aaronbertrand/bad-habits-nolock-everywhere/ –

+0

@JoeMalpass - 不幸的是,你的解決方案每隔一行就給我一個空白的ProductType。我需要一個空白的ProductType作爲每個組織的起始行。有任何想法嗎? – fourbeatcoder

回答

2

可以使用聯合創建此:

SELECT O.Id AS OrganisationId, o.Name as OrganistionName, '' as ProductType 
FROM [Content].[Organisations] O (nolock) 
JOIN [Content].[ProductOrganisations] PO (nolock) 
ON PO.OrganisationId = O.Id 
JOIN [dbo].[report_info] R (nolock) 
ON PO.ProductId = R.report_id 
WHERE O.ShowCompanyPage = 1 
GROUP BY O.Id , o.Name 

union 

SELECT DISTINCT O.Id AS OrganisationId, o.Name as OrganistionName, '' as ProductType 
FROM [Content].[Organisations] O (nolock) 
JOIN [Content].[ProductOrganisations] PO (nolock) 
ON PO.OrganisationId = O.Id 
JOIN [dbo].[report_info] R (nolock) 
ON PO.ProductId = R.report_id 
WHERE O.ShowCompanyPage = 1 
GROUP BY O.Id , o.Name 
ORDER BY o.Name, pt.product_type_name 
+0

您的解決方案將無法工作。 它只會給我組織列表,每行都有空白ProductType。請回顧一下我原來的帖子,看看我需要返回的數據並編輯你的答案。 – fourbeatcoder

0

在上述運算評論從@JoeMalpass建議類似,這裏是我如何解決它。

SELECT DISTINCT O.Id AS OrganisationId, o.Name as OrganisationName, 0, '' as [product_type_name], 0 AS ProductsInType 
FROM [Content].[Organisations] O (nolock) 
WHERE O.ShowCompanyPage = 1 

UNION 

SELECT O.Id AS OrganisationId, o.Name as OrganistionName, PT.PRODUCTTYPEID, pt.product_type_name, COUNT(R.report_id) AS ProductsInType 
FROM [Content].[Organisations] O (nolock) 
JOIN [Content].[ProductOrganisations] PO (nolock) 
ON PO.OrganisationId = O.Id 
JOIN [dbo].[report_info] R (nolock) 
ON PO.ProductId = R.report_id 
JOIN .[dbo].[CONTENT_ProductTypes] PT (nolock) 
ON R.[product_type_id] = PT.PRODUCTTYPEID 
WHERE O.ShowCompanyPage = 1 
GROUP BY O.Id , o.Name, PT.PRODUCTTYPEID, pt.product_type_name 
+0

這個查詢看起來不像你想要的結果在原來的問題..你的'例子'只有3列..該信息是重要的。 – JamieD77

+0

@ JamieD77你是不正確的 - 列數與 – fourbeatcoder

+0

這個問題無關,所以如果你告訴我們你想要一個3列結果..我們應該假設你想要5? – JamieD77

1

撰寫您GROUP BY語句ROLLUP這樣的:

GROUP BY O.Id, o.Name, ROLLUP(PT.PRODUCTTYPEID, pt.product_type_name) 
+0

我試圖弄清楚如何用rollup來做到這一點..不錯的工作 – JamieD77

+0

這不起作用,它給了我每個組織50%以上的行數,全部使用null o.Name,並且在組織開始時都是如此。查看我爲解決方案提供的答案。 – fourbeatcoder

+0

@ JamieD77這不是正確的解決方案,即使我使用'ROLLUP(pt.product_type_name)',每個組織的行數也會增加50%,每個額外的行在組織開始時都會有一個空的pt.product_type_name。我希望每個組織都有一個額外的行。看到我的答案,它的工作原理:http://stackoverflow.com/a/33655766/976537 – fourbeatcoder