【发布时间】: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