在我的asp.net + vb + sql web中。我有http://sqlfiddle.com/#!3/1fcdb/8
的查询和架构我目前正在使用:
with T1 as (
SELECT KNO, Pl, case when KDPR in (4,5) then 'Jr Supdt'
when KDPR >= 6 then 'Others' else '?' end as rank,
Status
from STAFF
)
SELECT Pl, rank, Held= (Present+AL+CL), Present, AL, CL
from T1 pivot (Count(KNO) for Status in (Present, AL, CL)) as P
order by Pl desc, rank desc;
产生:
| PL | RANK | HELD | PRESENT | AL | CL | |------|----------|------|---------|----|----| | SN | Others | 1 | 0 | 0 | 1 | | SN | Jr Supdt | 2 | 1 | 1 | 0 | | SH | Others | 1 | 0 | 0 | 1 | | SH | Jr Supdt | 1 | 0 | 1 | 0 | | Offr | ? | 3 | 2 | 1 | 0 | | HS | Others | 1 | 0 | 1 | 0 | | HS | Jr Supdt | 1 | 1 | 0 | 0 |
我想要两个不同的查询。
KDPR 1-3是Offr Engrs,KDPR 4-5是Jr Supdt,KDPR 6及以上是其他人。
答案 0 :(得分:0)
with T1 as (
SELECT KNO, Pl, case
when KDPR in (1,2,3) then 'Offr Engr'
when KDPR in (4,5) then 'Jr Supdt'
when KDPR >= 6 then 'Others' else '?' end as rank,
Status
from STAFF
)
SELECT Pl, rank, Held= (Present+AL+CL), Present, AL, CL
from T1 pivot (Count(KNO) for Status in (Present, AL, CL)) as P
WHERE rank <> 'Offr Engr'
order by Pl desc, rank desc;
上面的答案是针对问题1:据我所知,您想过滤掉“Offr Engr”&#39;来自您的查询。
with T1 as (
SELECT KNO, Pl, case
when KDPR in (1,2,3) then 'Offr Engr'
when KDPR in (4,5) then 'Jr Supdt'
when KDPR >= 6 then 'Others' else '?' end as rank,
Status
from STAFF
)
SELECT Pl, rank, Held= (Present+AL+CL), Present, AL, CL
from T1 pivot (Count(KNO) for Status in (Present, AL, CL)) as P
order by
CASE rank WHEN 'Offr Engr' THEN 1
WHEN 'Jr Supdt' THEN 2
WHEN 'Others' THEN 3 END;
上面的答案适用于问题2:据我所知,您需要rank
列上的自定义排序。