SQL为每个连接选择一个额外的“假”行

时间:2015-11-11 16:22:02

标签: sql sql-server join

我有以下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

有什么想法吗?

3 个答案:

答案 0 :(得分: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

答案 1 :(得分:1)

用这样的GROUP BY写下您的ROLLUP声明:

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

答案 2 :(得分: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