2

次のクエリがあり、結果も以下に示します。

  SELECT      
      SUM(atm) atm,SUM(cc) cc,SUM(csh) csh ,SUM(chk) chk
    FROM  sales                 
    WHERE repId= 9000
          AND DATEADD(dd,0,DATEDIFF(dd,0,idate)) ='2013-08-25 00:00:00'

クエリ結果

以下に示すように、PIVOT を使用せずにクエリを変更して結果を再実行する方法。 クエリ結果

4

1 に答える 1

2

これはうまくいくはずです

SELECT  'atm' type, SUM(atm) Amount  FROM  sales                 
  WHERE repId= 9000 AND DATEADD(dd,0,DATEDIFF(dd,0,idate)) ='2013-08-25 00:00:00'
UNION ALL
SELECT  'cc' type, SUM(cc) Amount  FROM  sales                 
  WHERE repId= 9000 AND DATEADD(dd,0,DATEDIFF(dd,0,idate)) ='2013-08-25 00:00:00'
UNION ALL
SELECT  'csh' type, SUM(csh) Amount  FROM  sales                 
  WHERE repId= 9000 AND DATEADD(dd,0,DATEDIFF(dd,0,idate)) ='2013-08-25 00:00:00'
UNION ALL
SELECT  'chk' type, SUM(chk) Amount  FROM  sales                 
  WHERE repId= 9000 AND DATEADD(dd,0,DATEDIFF(dd,0,idate)) ='2013-08-25 00:00:00'

または、CTE を使用して:

;WITH CTE(atm,cc,csh,chk) AS
   (SELECT  SUM(atm),SUM(cc),SUM(csh),SUM(chk) FROM sales                 
    WHERE repId= 9000 AND DATEADD(dd,0,DATEDIFF(dd,0,idate)) ='2013-08-25 00:00:00' )
SELECT  'atm' Type,atm Amount FROM CTE
UNION ALL SELECT 'cc' ,cc  FROM CTE
UNION ALL SELECT 'csh',csh FROM CTE
UNION ALL SELECT 'chk',chk FROM CTE
于 2013-08-26T10:14:12.590 に答える