SQL 连接,使用 Pivot 聚合

Posted

技术标签:

【中文标题】SQL 连接,使用 Pivot 聚合【英文标题】:SQL Join, Aggregate with Pivot 【发布时间】:2021-12-11 04:02:33 【问题描述】:

我正在尝试从库存系统创建自定义报告。不幸的是,在系统 UI 中,只有一个选项可以将查询用于自定义报告。我想生成带有资产状态的资产报告,例如有故障,新 - 店内,二手。

SELECT  productType.COMPONENTTYPENAME AS "Product Type", "state"."displaystate" AS "Asset State", count("resource"."resourcename" ) AS "Asset Count" FROM Resources resource
left JOIN ComponentDefinition product ON resource.COMPONENTID=product.COMPONENTID
left JOIN ComponentType productType ON product.COMPONENTTYPEID=productType.COMPONENTTYPEID
LEFT JOIN "resourcestate" "state" ON "resource"."resourcestateid" = "state"."resourcestateid"
LEFT JOIN "resourcetype" "rtype" ON "productType"."resourcetypeid" = "rtype"."resourcetypeid"
GROUP BY  state.displaystate,productType.COMPONENTTYPENAME

通过这个查询,我得到了这种格式的数据

Product Type Asset State Asset Count
Workstation In Use 30
Keyb_Mouse In Use 30
Workstation New - In Store 10
Keyb_Mouse Used - In Store 20
Workstation Used - In Store 20

我想将此资产状态按行转换为(Excel 数据透视表)之类的列以获取摘要。

我尝试更改查询以将结果更改为这种格式。

Product Type In Use New - In Store Used - In Store
Workstation 30 10 20
Keyb_Mouse 30 20
SELECT  productType.COMPONENTTYPENAME AS "Product Type", "state"."displaystate" AS "Asset State", count("resource"."resourcename" ) AS "Asset Count" FROM Resources resource
left JOIN ComponentDefinition product ON resource.COMPONENTID=product.COMPONENTID
left JOIN ComponentType productType ON product.COMPONENTTYPEID=productType.COMPONENTTYPEID
LEFT JOIN "resourcestate" "state" ON "resource"."resourcestateid" = "state"."resourcestateid"
LEFT JOIN "resourcetype" "rtype" ON "productType"."resourcetypeid" = "rtype"."resourcetypeid"
GROUP BY  state.displaystate,productType.COMPONENTTYPENAME

SELECT "Asset State", "In Use", "Used - In Store", "In Store", "New - In Store", "Damaged","Faulty" FROM Resources resource
PIVOT(
sum("Asset Count") for "Asset State" in ("In Use", "Used - In Store", "In Store", "New - In Store", "Damaged", "Faulty")
)

但是这个查询没有返回任何东西。请参考任何链接或视频以了解和解决此问题。提前致谢。

【问题讨论】:

如果没有样本数据、预期输出和表模式,这是无法回答的。旁白:理想情况下,您不应该使用" 进行引用,而是使用[] 【参考方案1】:

下面的查询解决了这个问题。欢迎提出改进查询的建议。

SELECT  "Product Type", "In Use", "Used - In Store", "In Store", "New - In Store", "Damaged","Faulty" FROM (
SELECT  productType.COMPONENTTYPENAME AS "Product Type", "state"."displaystate" AS "Asset State", count("resource"."resourcename" ) AS "Asset Count" FROM Resources resource
left JOIN ComponentDefinition product ON resource.COMPONENTID=product.COMPONENTID
left JOIN ComponentType productType ON product.COMPONENTTYPEID=productType.COMPONENTTYPEID
LEFT JOIN "resourcestate" "state" ON "resource"."resourcestateid" = "state"."resourcestateid"
LEFT JOIN "resourcetype" "rtype" ON "productType"."resourcetypeid" = "rtype"."resourcetypeid"
GROUP BY  state.displaystate,productType.COMPONENTTYPENAME )d
pivot
(
  max("Asset Count")
  for "Asset State" in ("In Use", "Used - In Store", "In Store", "New - In Store", "Damaged", "Faulty")
) piv

【讨论】:

【参考方案2】:

您必须将查询的第一部分命名为 CTE,所以它应该是:

;with Resources as (
    SELECT productType.COMPONENTTYPENAME AS [Product Type],
    [state].[displaystate] AS [Asset State],
    count([resource].[resourcename] ) AS [Asset Count]
    FROM Resources resource
        left JOIN ComponentDefinition product ON resource.COMPONENTID=product.COMPONENTID
        left JOIN ComponentType productType ON product.COMPONENTTYPEID=productType.COMPONENTTYPEID
        LEFT JOIN [resourcestate] [state] ON [resource].[resourcestateid] = [state].[resourcestateid]
        LEFT JOIN [resourcetype] [rtype] ON [productType].[resourcetypeid] = [rtype].[resourcetypeid]
    GROUP BY  state.displaystate,productType.COMPONENTTYPENAME
)
SELECT [Asset State], [In Use], [Used - In Store], [In Store], [New - In Store], [Damaged], [Faulty]
FROM Resources resource
PIVOT(
    sum([Asset Count]) for [Asset State] in ([In Use], [Used - In Store], [In Store], [New - In Store], [Damaged], [Faulty])
)

【讨论】:

以上是关于SQL 连接,使用 Pivot 聚合的主要内容,如果未能解决你的问题,请参考以下文章

SQL Server PIVOT - 多个聚合

同一列上具有多个聚合的 T-SQL Pivot

具有动态生成列、聚合函数和无聚合列的 SQL Pivot

没有聚合的 Pivot Sql

PIVOT:行转列函数

SQL Server中行列转换 Pivot UnPivot