Tsql - 有一种有效的方法来查询吗?

时间:2012-09-20 15:37:46

标签: sql tsql

有一种有效或简单的方法来查询吗?例如,最小化服务器负载。谢谢!

db table

qId     WKDT    WK_ENDT                  SUM_LVL_Sort  ADOCD  ocd  OfficeName     WKLY_AVG  WeekCode
------  ------  -----------------------  ------------  -----  ---  -------------  --------  --------
201112  201110  2011-10-28 00:00:00.000  19.00         NULL   NAT  NATION         2.23      n
201112  201110  2011-10-28 00:00:00.000  13.00         NULL   F09  SAN FRANCISCO  2.20      n
201112  201111  2011-11-25 00:00:00.000  19.00         NULL   NAT  NATION         2.39      n
201112  201111  2011-11-25 00:00:00.000  13.00         NULL   F09  SAN FRANCISCO  2.14      n
201112  201112  2011-12-30 00:00:00.000  19.00         NULL   NAT  NATION         2.37      n
201112  201112  2011-12-30 00:00:00.000  13.00         NULL   F09  SAN FRANCISCO  2.18      n

这是我需要的布局

WK_ENDT                  adocd  ocd  OfficeName     SUM_LVL_Sort  10    11    12
-----------------------  -----  ---  -------------  ------------  ----  ----  ----
2011-10-07 00:00:00.000  NULL   F09  SAN FRANCISCO  13.00         2.2   2.14  2.18
2011-10-07 00:00:00.000  NULL   NAT  NATION         19.00         2.23  2.39  2.37

tql查询

 select TOP 1 (WK_ENDT),
     ,SUM_LVL_Sort
     ,ADOCD     as adocd
     ,Alias   as ocd
     ,OfficeName
     ,(select WKLY_AVG from kpi.dbo.tb_ssi_rzli where SUM_LVL_Sort = 19.00 and RIGHT(wkdt,2) = '10' and weekcode = 'n'  ) as [10]
     ,(select WKLY_AVG from kpi.dbo.tb_ssi_rzli where SUM_LVL_Sort = 19.00 and RIGHT(wkdt,2) = '11' and weekcode = 'n'  ) as [11]
     ,(select WKLY_AVG from kpi.dbo.tb_ssi_rzli where SUM_LVL_Sort = 19.00 and RIGHT(wkdt,2) = '12' and weekcode = 'n'  ) as [12]
from kpi.dbo.tb_ssi_rzli
where SUM_LVL_Sort = 19.00 and right(qId,2) = '12' and WeekCode = 'n'
order by WK_ENDT desc

bluefeet的查询结果

WK_ENDT                  SUM_LVL_Sort  adocd  ocd  OfficeName  10    11    12
-----------------------  ------------  -----  ---  ----------  ----  ----  ----
2011-12-30 00:00:00.000  19.00         NULL   NAT  NATION      NULL  NULL  2.37
2011-11-25 00:00:00.000  19.00         NULL   NAT  NATION      NULL  2.39  NULL
2011-10-28 00:00:00.000  19.00         NULL   NAT  NATION      2.23  NULL  NULL

1 个答案:

答案 0 :(得分:1)

你试过这个:

select  (WK_ENDT),
     ,SUM_LVL_Sort
     ,ADOCD   as adocd
     ,Alias   as ocd
     ,OfficeName
     , CASE WHEN RIGHT(wkdt,2) = '10' THEN WKLY_AVG END As [10]
     , CASE WHEN RIGHT(wkdt,2) = '11' THEN WKLY_AVG END As [11]
     , CASE WHEN RIGHT(wkdt,2) = '12' THEN WKLY_AVG END As [12]
from kpi.dbo.tb_ssi_rzli
where SUM_LVL_Sort = 19.00 
    and right(qId,2) = '12'
    --and RIGHT(wkdt,2) IN ('10', '11', '12')
    and WeekCode = 'n'
order by WK_ENDT desc 

编辑尝试此操作,看起来您希望根据月份获取值:

<击> select TOP 1 (WK_ENDT), ,SUM_LVL_Sort ,ADOCD as adocd ,Alias as ocd ,OfficeName , CASE WHEN month(wkdt) = '10' THEN WKLY_AVG END As [10] , CASE WHEN month(wkdt) = '11' THEN WKLY_AVG END As [11] , CASE WHEN month(wkdt) = '12' THEN WKLY_AVG END As [12] from kpi.dbo.tb_ssi_rzli where SUM_LVL_Sort = 19.00 and right(qId,2) = '12' and WeekCode = 'n' order by WK_ENDT desc