我有以下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
任何想法?
您可以添加'UNION SELECT DISTINCT OrganisationId,OrganisationName '' 爲[ProductType] FROM [[[相同的分組查詢這裏] ]' – CactusCake
由於看起來準確性很重要,因此在繼續使用nolock拋出查詢之前,可能需要查看本文。這不是一個神奇的「走得快」按鈕。它有許多人不理解的非常嚴重的副作用。 http://blogs.sqlsentry.com/aaronbertrand/bad-habits-nolock-everywhere/ –
@JoeMalpass - 不幸的是,你的解決方案每隔一行就給我一個空白的ProductType。我需要一個空白的ProductType作爲每個組織的起始行。有任何想法嗎? – fourbeatcoder