查询Gridview sqldatasource

时间:2014-06-04 01:34:36

标签: sql sql-server

在我的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 |

我想要两个不同的查询。

  1. 我在我的架构中提到的同样的查询没有Offr Engr(Offr Engr不应该出现结果)但序列应该是Jr Engr然后是Othres。
  2. 与Offr Engr一起推出(输出Offr Engr然后Jr Supdt,然后Others.i使用代码为公司中不同年级的人使用代码。
  3. KDPR 1-3是Offr Engrs,KDPR 4-5是Jr Supdt,KDPR 6及以上是其他人。

1 个答案:

答案 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列上的自定义排序。