1

これを照会するための効率的または簡単な方法はありますか?たとえば、サーバーの負荷を最小限に抑えるためです。ありがとう!

dbテーブル

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
4

1 に答える 1

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

于 2012-09-20T15:49:33.763 に答える