SQL查询排序为0

时间:2018-06-12 12:45:35

标签: sql-server

我有以下查询:

SELECT distinct sending_organisation AS [Supplier], t1.date, COUNT(Status) AS [Transactions],  Month(t1.Date) SO_Month, Year(t1.date) SO_Year
 FROM tx

   CROSS APPLY ( VALUES (CONCAT(left(DATENAME(mm, Date_Reported),3),' ',
                             DATENAME(yyyy, Date_Reported)),
                      DATEPART(mm, Date_Reported)
                     )
            ) t1 (Date, Morder)

where  Date >= DATEADD(MONTH, -13, CAST(GETDATE() AS DATE))
GROUP BY t1.date, t1.Morder, sending_organisation
ORDER BY sending_organisation, date DESC;

该查询为我提供了去年的供应商及其交易清单。我想要的是一个动态查询来改变上述内容,并允许我只为那些拥有' 0'的供应商选择去年的数据。当月的交易价值。

有什么想法吗?

2 个答案:

答案 0 :(得分:3)

这就是你追求的吗?所以我使用了动态SQL,SQL服务器中的任何字符串都可以使用EXECUTE/EXEC作为SQL命令执行。我将您的确切查询添加为带有占位符值的字符串,以便在IF块之后添加额外的where子句。在这里,我为你添加条件添加你的条件是否要在你的语句中添加额外的where子句,如果没有,那么删除占位符,这样选择查询仍然可以运行。

DECLARE @sql NVARCHAR(MAX)=
'SELECT DISTINCT sending_organisation AS [Supplier], 
                 t1.date, 
                 Count(status)  AS [Transactions], 
                 Month(t1.date) so_month, 
                 Year(t1.date)  so_year 
 FROM            tx 
 CROSS apply     ( VALUES 
                 ( 
                                                 Concat(LEFT(Datename(mm, date_reported),3),'' '', Datename(yyyy, date_reported)), 
                                 Datepart(mm, date_reported) 
                 ) 
                 ) t1 (date, morder) 
 WHERE           date >= Dateadd(month, -13, Cast(Getdate() AS DATE)) 
 placeHolder 
 GROUP BY        t1.date, 
                 t1.morder, 
                 sending_organisation 
 ORDER BY        sending_organisation, 
                 date DESC; ';
IF(condition)
BEGIN
    SET @sql = replace(@sql,'placeHolder','AND WHERE transactionValue = 0 ');
END
ELSE 
BEGIN
    SET @sql = replace(@sql,'placeHolder',' ');
END
EXEC(@sql);

答案 1 :(得分:1)

确定哪些供应商在当前月份与NOT EXISTS进行了交易,然后加入以前的数据。

;WITH Data AS 
(
    -- This is your query without modifications
    SELECT distinct 
        sending_organisation AS [Supplier], 
        t1.date, 
        COUNT(Status) AS [Transactions],  
        Month(t1.Date) SO_Month, 
        Year(t1.date) SO_Year
    FROM 
        tx
        CROSS APPLY ( VALUES (CONCAT(left(DATENAME(mm, Date_Reported),3),' ',
                             DATENAME(yyyy, Date_Reported)),
                      DATEPART(mm, Date_Reported)
                     )
            ) t1 (Date, Morder)
    where  
        Date >= DATEADD(MONTH, -13, CAST(GETDATE() AS DATE))
    GROUP BY 
        t1.date, 
        t1.Morder, 
        sending_organisation
),
AvailableSuppliers AS
(
    SELECT DISTINCT
        Supplier = sending_organisation
    FROM
        tx AS D
),
CurrentMonthTransactionZero AS
(
    -- Determine which sending_organisation have no transactions on the current month
    SELECT
        A.Supplier
    FROM
        AvailableSuppliers AS A
    WHERE
        NOT EXISTS (
            SELECT
                'no transaction for current month'
            FROM
                tx AS T
            WHERE
                A.Supplier = T.sending_organisation AND
                Date_Reported >= DATEADD(DAY, 1, EOMONTH(GETDATE(), -1)) AND -- First day of current month
                Date_Reported <  DATEADD(DAY, 1, EOMONTH(GETDATE())) -- First day of next month
            )
)
SELECT
    D.*
FROM
    CurrentMonthTransactionZero AS D
    INNER JOIN CurrentMonthTransactionZero AS C ON D.Supplier = C.Supplier