【问题标题】:SQL Join, Aggregate with PivotSQL 连接,使用 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")
)

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

【问题讨论】:

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

标签: sql sql-server pivot


【解决方案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])
    )
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2021-08-19
      • 2010-11-17
      • 2016-10-16
      • 2021-10-08
      • 2016-10-04
      • 2014-12-30
      • 1970-01-01
      相关资源
      最近更新 更多