数据透视表排序依据

时间:2012-08-14 10:50:44

标签: sql sql-server-2008

我已经为每月的销售额创建了一个数据透视表,我想按当月订购数据。我想知道我是否可以使用案例陈述和命令;我在下面尝试了这个(虽然没有工作);

数据库服务器: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

3 个答案:

答案 0 :(得分: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

答案 1 :(得分: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

答案 2 :(得分: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