如何在sql的不同列中显示月份名称?

时间:2014-03-28 07:12:00

标签: sql-server tsql

我有这个问题:

DECLARE @month INT
SET @month=1

SELECT 

    CLOI_ClientOrderItems.cl_Id,

    NoOfInv = SUM(CASE WHEN DATEPART(mm, in_date_issued) <= @month
                        AND DATEPART(yyyy, in_date_issued) = 2014
                       THEN 1 ELSE 0 END),

    MonthTotal = SUM(CASE WHEN DATEPART(mm, in_date_issued) <= @month 
                           AND DATEPART(yyyy, in_date_issued) = 2014
                          THEN in_total ELSE 0 END),

    Grandtotal = SUM(in_total),

    RemainingAmount = SUM(in_total) - SUM(CASE 
                          WHEN DATEPART(mm, in_date_issued) <= @month 
                          THEN in_total ELSE 0 END)


FROM (SELECT 
          DISTINCT MasterOrderId, cl_Id 
      FROM 
          CLOI_ClientOrderItems) as CLOI_ClientOrderItems 

INNER JOIN 
      IN_Invoices 
ON
      IN_Invoices.MasterOrderId = CLOI_ClientOrderItems.MasterOrderId
GROUP BY 
      CLOI_ClientOrderItems.cl_id

输出我想要那样:

  noofinv |amt      |clid       | grandtotal | jan        | feb     |march  | remainingamt
    5     |7.00     |100000_Pri | 245.00     | 0.00       | 238.00  |7.00   | 238.00
    12    |2510.12  |100001_pro | 181110.29  | 138891.92  | 9708.25 |510.12 | 178600.17

如果我将月份数字传递为3,则应将其显示为1月2日和3月以及相应月份的相关记录。 sql

0 个答案:

没有答案