在MS Access SQL中使用交叉表查询 怎样像这样变换表
===========================
|IND |LBL |TICKETS|
===========================
|1 |AM |101 |
|2 |AM |1 |
|1 |HR |102 |
|1 |TV |100 |
|2 |TV |2 |
===========================
对此
=======================
|LBL |IND1 |IND2|
=======================
|AM |101 |1 |
|HR |102 |0 |
|TV |100 |2 |
=======================
这是我尝试过的查询。
我认为我在交叉表查询中犯了一些错误,如果可以指出的话,这会有所帮助
TRANSFORM First(TBL1.TICKETS) AS TCK
SELECT TBL1.LBL, First(TBL1.TICKETS) AS TCK1
from
(
SELECT IIF(A.AM= '[AM]' OR A.AM='[AM2]',1,2) AS IND
, 'AM' AS LBL
, COUNT(A.AM) AS TICKETS
FROM shree_main AS A GROUP BY IIF(A.AM='[AM]' OR A.AM='[AM2]',1,2), 'AM'
union all
SELECT IIF(A.Newpri= '[HR]' OR A.Newpri='[NML]' OR A.Newpri='[N2H]',1 ,2) AS IND, 'HR' AS LBL,
count([A].Newpri) AS TICKETS FROM shree_main AS A
GROUP BY IIF(A.Newpri= '[HR]' OR A.Newpri='[NML]' OR A.Newpri='[N2H]',1,2), 'HR'
union all
SELECT IIF(A.TV= '[SC]' OR A.TV='[TV]' OR A.TV='[TV][SC]' OR A.TV='[VE]',1 ,2) AS IND, 'TV' AS LBL, COUNT(A.TV) AS TICKETS FROM shree_main AS A GROUP BY IIF(A.TV= '[SC]' OR A.TV='[TV]' OR A.TV='[TV][SC]' OR A.TV='[VE]',1 ,2), 'TV'
) AS TBL1
GROUP BY TBL1.LBL, TBL1.LBL, TBL1.LBL, TBL1.LBL
PIVOT TBL1.LBL;
这是查询的结果
LBL TCK1 AM HR TV
AM 1 101
HR 102 102
TV 2 100