小编Sam*_*Sam的帖子

我需要知道如何创建交叉表查询

我需要帮助创建以下结果.我想到了一个sql pivot,但我不知道如何使用它.看了几个例子,无法想出解决方案.关于如何实现这一目标的任何其他想法也是受欢迎的.必须动态生成状态列.

有三个表,资产,资产类型,assetstatus

Table: assets
assetid     int
assettag    varchar(25)
assettype   int
assetstatus int

Table: assettypes
id         int
typename   varchar(20)  (ex: Desktop, Laptop, Server, etc.)

Table: assetstatus
id         int
statusname varchar(20)  (ex: Deployed, Inventory, Shipped, etc.)

期望的结果:

AssetType     Total   Deployed   Inventory  Shipped     ...
-----------------------------------------------------------
Desktop         100       75        20          5       ...
Laptop           75       56        19          1       ...
Server           60       50        10          0       ...

一些数据:

assets table:
1,hol1234,1,1
2,hol1233,1,2
3,hol3421,2,3
4,svr1234,3,1

assettypes table:
1,Desktop
2,Laptop
3,Server

assetstatus table:
1,Deployed
2,Inventory
3,Shipped

sql pivot crosstab

24
推荐指数
1
解决办法
6万
查看次数

标签 统计

crosstab ×1

pivot ×1

sql ×1