-1

我为每月的销售额创建了一个数据透视表,我想按当月对数据进行排序。我想知道是否可以使用 case 语句和 order by;我在下面试过这个(虽然不工作);

数据库服务器:SQL Server 2008

SELECT txt_web_name, 
SUM(CASE WHEN MONTH(dte_pay_paydate) = 1 AND txt_pay_type <> 'Credit' THEN mon_pay_amount ELSE 0 END) AS 'January',
SUM(CASE WHEN MONTH(dte_pay_paydate) = 2 AND txt_pay_type <> 'Credit' THEN mon_pay_amount ELSE 0 END) AS 'February',
SUM(CASE WHEN MONTH(dte_pay_paydate) = 3 AND txt_pay_type <> 'Credit' THEN mon_pay_amount ELSE 0 END) AS 'March',
SUM(CASE WHEN MONTH(dte_pay_paydate) = 4 AND txt_pay_type <> 'Credit' THEN mon_pay_amount ELSE 0 END) AS 'April',
SUM(CASE WHEN MONTH(dte_pay_paydate) = 5 AND txt_pay_type <> 'Credit' THEN mon_pay_amount ELSE 0 END) AS 'May',
SUM(CASE WHEN MONTH(dte_pay_paydate) = 6 AND txt_pay_type <> 'Credit' THEN mon_pay_amount ELSE 0 END) AS 'June',
SUM(CASE WHEN MONTH(dte_pay_paydate) = 7 AND txt_pay_type <> 'Credit' THEN mon_pay_amount ELSE 0 END) AS 'July',
SUM(CASE WHEN MONTH(dte_pay_paydate) = 8 AND txt_pay_type <> 'Credit' THEN mon_pay_amount ELSE 0 END) AS 'August',
SUM(CASE WHEN MONTH(dte_pay_paydate) = 9 AND txt_pay_type <> 'Credit' THEN mon_pay_amount ELSE 0 END) AS 'September',
SUM(CASE WHEN MONTH(dte_pay_paydate) = 10 AND txt_pay_type <> 'Credit' THEN mon_pay_amount ELSE 0 END) AS 'October',
SUM(CASE WHEN MONTH(dte_pay_paydate) = 11 AND txt_pay_type <> 'Credit' THEN mon_pay_amount ELSE 0 END) AS 'November',
SUM(CASE WHEN MONTH(dte_pay_paydate) = 12 AND txt_pay_type <> 'Credit' THEN mon_pay_amount ELSE 0 END) AS 'December'
FROM tbl_payment
INNER JOIN dbo.tbl_orders ON (uid_pay_orderid = uid_orders)
INNER JOIN dbo.tbl_websites ON (uid_order_webid = uid_websites)
WHERE dbo.tbl_payment.bit_pay_paid = 1
GROUP BY txt_web_name
ORDER BY 
        CASE 

         WHEN MONTH(GETDATE()) = 1 THEN 'January'
         WHEN MONTH(GETDATE()) = 2 THEN 'February'
         WHEN MONTH(GETDATE()) = 3 THEN 'March'
         WHEN MONTH(GETDATE()) = 4 THEN 'April'
         WHEN MONTH(GETDATE()) = 5 THEN 'May'
         WHEN MONTH(GETDATE()) = 6 THEN 'June'
         WHEN MONTH(GETDATE()) = 7 THEN 'July'
         WHEN MONTH(GETDATE()) = 8 THEN 'August'
         WHEN MONTH(GETDATE()) = 9 THEN 'September'
         WHEN MONTH(GETDATE()) = 10 THEN 'October'
         WHEN MONTH(GETDATE()) = 11 THEN 'November'
         WHEN MONTH(GETDATE()) = 12 THEN 'December' 

         END  DESC
4

3 回答 3

0

试试这个查询:

SELECT txt_web_name, 
SUM(CASE WHEN MONTH(dte_pay_paydate) = 1 AND txt_pay_type <> 'Credit' THEN mon_pay_amount ELSE 0 END) AS January,
SUM(CASE WHEN MONTH(dte_pay_paydate) = 2 AND txt_pay_type <> 'Credit' THEN mon_pay_amount ELSE 0 END) AS February,
SUM(CASE WHEN MONTH(dte_pay_paydate) = 3 AND txt_pay_type <> 'Credit' THEN mon_pay_amount ELSE 0 END) AS March,
SUM(CASE WHEN MONTH(dte_pay_paydate) = 4 AND txt_pay_type <> 'Credit' THEN mon_pay_amount ELSE 0 END) AS April,
SUM(CASE WHEN MONTH(dte_pay_paydate) = 5 AND txt_pay_type <> 'Credit' THEN mon_pay_amount ELSE 0 END) AS May,
SUM(CASE WHEN MONTH(dte_pay_paydate) = 6 AND txt_pay_type <> 'Credit' THEN mon_pay_amount ELSE 0 END) AS June,
SUM(CASE WHEN MONTH(dte_pay_paydate) = 7 AND txt_pay_type <> 'Credit' THEN mon_pay_amount ELSE 0 END) AS July,
SUM(CASE WHEN MONTH(dte_pay_paydate) = 8 AND txt_pay_type <> 'Credit' THEN mon_pay_amount ELSE 0 END) AS August,
SUM(CASE WHEN MONTH(dte_pay_paydate) = 9 AND txt_pay_type <> 'Credit' THEN mon_pay_amount ELSE 0 END) AS September,
SUM(CASE WHEN MONTH(dte_pay_paydate) = 10 AND txt_pay_type <> 'Credit' THEN mon_pay_amount ELSE 0 END) AS October,
SUM(CASE WHEN MONTH(dte_pay_paydate) = 11 AND txt_pay_type <> 'Credit' THEN mon_pay_amount ELSE 0 END) AS November,
SUM(CASE WHEN MONTH(dte_pay_paydate) = 12 AND txt_pay_type <> 'Credit' THEN mon_pay_amount ELSE 0 END) AS December
FROM tbl_payment
INNER JOIN dbo.tbl_orders ON (uid_pay_orderid = uid_orders)
INNER JOIN dbo.tbl_websites ON (uid_order_webid = uid_websites)
WHERE dbo.tbl_payment.bit_pay_paid = 1
GROUP BY txt_web_name
ORDER BY 
        CASE MONTH(GETDATE()) 
         WHEN 1 THEN January
         WHEN 2 THEN February
         WHEN 3 THEN March
         WHEN 4 THEN April
         WHEN 5 THEN May
         WHEN 6 THEN June
         WHEN 7 THEN July
         WHEN 8 THEN August
         WHEN 9 THEN September
         WHEN 10 THEN October
         WHEN 11 THEN November
         WHEN 12 THEN December 

         END  DESC
于 2012-08-14T10:57:32.960 回答
0

您当前正在按一个常量值(即月份的名称)进行排序,正如您所发现的那样,它不会产生任何影响。

你需要类似的东西

 ORDER BY
     CASE MONTH(GETDATE()) 
     WHEN 1 THEN January
     WHEN 2 THEN February
     ...
     END

PS。你知道SQL Server有一个PIVOT命令...

select * 
from 
(
    select txt_web_name, mon_pay_amount, datename(month,dte_pay_paydate) as mpd 
    FROM tbl_payment  
    INNER JOIN dbo.tbl_orders ON (uid_pay_orderid = uid_orders)  
    INNER JOIN dbo.tbl_websites ON (uid_order_webid = uid_websites)  
    WHERE dbo.tbl_payment.bit_pay_paid = 1  
    AND txt_pay_type<>'credit'
) t
pivot
(
    sum(mon_pay_amount) for mpd in 
    ([January],[February],[March],[April],[May],[June],[July],[August], .....)
)p
order by
   case MONTH(getdate())
       when 1 then [January]
       when 2 then [February]
        ...
   end
于 2012-08-14T10:52:46.400 回答
0

我会改变它来使用一个PIVOT函数。这将清理查询以不使用所有CASE语句:

SELECT *
FROM 
(
  SELECT txt_web_name, 
    txt_pay_type, 
    DATENAME(month, dte_pay_paydate) mth,  -- get the month name instead of the number
    mon_pay_amount
  FROM tbl_payment
  INNER JOIN dbo.tbl_orders 
    ON (uid_pay_orderid = uid_orders)
  INNER JOIN dbo.tbl_websites 
    ON (uid_order_webid = uid_websites)
  WHERE dbo.tbl_payment.bit_pay_paid = 1
    and txt_pay_type <> 'Credit'
) x
PIVOT
(
  sum(mon_pay_amount)
  for mth in([January], [February], [March], [April], [May],
            [June], [July], [August], September, [October], 
            [November], [December])
)p
order by 
  case datename(month, getdate()) 
    when 'January' then January 
    when 'February' then February  
    when 'March' then March  
    when 'April' then April  
    when 'May' then May  
    when 'June' then June  
    when 'July' then July  
    when 'August' then August  
    when 'September' then September  
    when 'October' then October  
    when 'November' then November  
    when 'December' then December  
  END Desc
于 2012-08-14T11:07:02.607 回答