在WHERE子句中具有多个日期参数的SELECT SUM查询

时间:2018-11-09 10:38:17

标签: sql sql-server

我试图在SQL Server中返回信息,在这些信息中,对值进行求和,但仅在两个日期参数为MAX日期的情况下。

表看起来像这样:

HH间隔 HH Intervals

但是,我试图用SETTLEMENT_DATE来求和,而ENTRY_DATE是MAX ENTRY_DATE和MAX ODS_Load_Date的总和,所以我写了以下查询:

SELECT [SETTLEMENT_DAY],
       [COUNTERPARTY_NAME],
       [LINE_ITEM_NAME],
       SUM([VARIABLE_VAL]) AS [PPA>10MW(OP)],
       [STATEMENT_TYPE],
       [ODS_LoadDate]
FROM [ODS].[dbo].[ODS_MM_PPA_FINANCIAL_RESULTS] A
WHERE [LINE_ITEM_NAME] = 'PPA > 10 MW (OP)'
  AND [VARIABLE_NAME] = 'CHARGE_AMOUNT'
  AND [SETTLEMENT_DAY] BETWEEN '2018-10-01' AND '2018-10-31'
  AND [COUNTERPARTY_NAME] IN ('IBIL_405145')
  AND [ODS_LoadDate] = (SELECT MAX([ODS_LoadDate])
                        FROM [ODS].[dbo].[ODS_MM_PPA_FINANCIAL_RESULTS] B
                        WHERE A.[SETTLEMENT_DAY] = B.[SETTLEMENT_DAY]
                          AND A.[COUNTERPARTY_NAME] = B.[COUNTERPARTY_NAME]
                          AND A.[LINE_ITEM_NAME] = B.[LINE_ITEM_NAME]
                          AND A.[VARIABLE_VAL] = B.[VARIABLE_VAL]
                          AND A.[STATEMENT_TYPE] = B.[STATEMENT_TYPE]
                          AND A.[STATEMENT_TYPE] = B.[STATEMENT_TYPE]
                          AND A.[VARIABLE_NAME] = B.[VARIABLE_NAME]
                          AND A.[ENTRY_DATE] = B.[ENTRY_DATE]
                          AND A.[ODS_LoadDate] = B.[ODS_LoadDate]
                          AND [ENTRY_DATE] = (SELECT MAX([ENTRY_DATE])
                                              FROM [ODS].[dbo].[ODS_MM_PPA_FINANCIAL_RESULTS] C
                                              WHERE B.[SETTLEMENT_DAY] = C.[SETTLEMENT_DAY]
                                                AND B.[COUNTERPARTY_NAME] = C.[COUNTERPARTY_NAME]
                                                AND B.[LINE_ITEM_NAME] = C.[LINE_ITEM_NAME]
                                                AND B.[VARIABLE_VAL] = C.[VARIABLE_VAL]
                                                AND B.[STATEMENT_TYPE] = C.[STATEMENT_TYPE]
                                                AND B.[STATEMENT_TYPE] = C.[STATEMENT_TYPE]
                                                AND B.[VARIABLE_NAME] = C.[VARIABLE_NAME]
                                                AND B.[ENTRY_DATE] = C.[ENTRY_DATE]
                                                AND B.[ODS_LoadDate] = C.[ODS_LoadDate]))
GROUP BY [SETTLEMENT_DAY],
         [COUNTERPARTY_NAME],
         [LINE_ITEM_NAME],
         [STATEMENT_TYPE],
         [ODS_LoadDate]
ORDER BY [SETTLEMENT_DAY] ASC;

但是我发现每个SETTLEMENT_DATE的返回结果都有两个记录,我只希望其中一个记录是MAX ENTRY_DATE,而ODS_Load_Date也是MAX { {1}}。

有什么想法可以解决这个问题吗?

2 个答案:

答案 0 :(得分:0)

尝试以下方法。基本上我正在内部块中获得具有最大ods_load_date和max_entry_date的所有记录。之后,我总结并根据现有查询进行分组?

SELECT [SETTLEMENT_DAY]
     , [COUNTERPARTY_NAME]
     , [LINE_ITEM_NAME]
     , [STATEMENT_TYPE]
     , [ODS_LoadDate] 
     , SUM([VARIABLE_VAL]) AS [PPA>10MW(OP)]     
  (SELECT
        [SETTLEMENT_DAY],
        [COUNTERPARTY_NAME], 
        [LINE_ITEM_NAME],
        --SUM([VARIABLE_VAL]) AS [PPA>10MW(OP)],
        [STATEMENT_TYPE],
        [ODS_LoadDate],
        [ENTRY_DATE],
        MAX(A.[ODS_LoadDate]) OVER(PARTITION BY [SETTLEMENT_DAY]
                                              ,[COUNTERPARTY_NAME]
                                              ,[LINE_ITEM_NAME]
                                              ,[VARIABLE_VAL]
                                              ,[STATEMENT_TYPE]                                           
                                              ,[VARIABLE_NAME]                                            
                                              ,[ENTRY_DATE]
                                              ,[ODS_LoadDate]
                                              ) as max_ods_load_date
        MAX(A.[ENTRY_DATE]) OVER(PARTITION BY [SETTLEMENT_DAY]
                                              ,[COUNTERPARTY_NAME]
                                              ,[LINE_ITEM_NAME]
                                              ,[VARIABLE_VAL]
                                              ,[STATEMENT_TYPE]                                           
                                              ,[VARIABLE_NAME]                                            
                                              ,[ODS_LoadDate]
                                              ,[ENTRY_DATE]
                                              ) as max_entry_date
  FROM [ODS].[dbo].[ODS_MM_PPA_FINANCIAL_RESULTS] AS A
 WHERE [LINE_ITEM_NAME]='PPA > 10 MW (OP)' 
   AND [VARIABLE_NAME]='CHARGE_AMOUNT'
   AND [SETTLEMENT_DAY] BETWEEN '2018-10-01' AND '2018-10-31'
   AND [COUNTERPARTY_NAME] IN ('IBIL_405145')    
  )
WHERE x.max_ods_load_date = ods_load_date
  AND x.max_entry_date= x.entry_date  
GROUP BY  [SETTLEMENT_DAY]
        , [COUNTERPARTY_NAME]
        , [LINE_ITEM_NAME]
        , [STATEMENT_TYPE]
        , [ODS_LoadDate]
ORDER BY [SETTLEMENT_DAY] asc

答案 1 :(得分:0)

您要在[ODS_LoadDate]上加入A和B,当您需要MAX [ODS_LoadDate]时,它将包含两个表中的所有行。与在[ENTRY_DATE]加入B和C相同。我认为,如果删除这些条目,它将起作用。

例如,如果您有两个表

ExampleTable

| Col1 | Col2       |
| 1    | 2018-01-01 |
| 1    | 2018-01-02 |

并运行:

SELECT Col1, Col2
FROM ExampleTable a
WHERE Col2 = (SELECT MAX(Col2) FROM ExampleTable b WHERE a.Col1 = b.Col1 AND a.Col2 = b.Col2)

您将获得:

| Col1 | Col2       |
| 1    | 2018-01-01 |
| 1    | 2018-01-02 |

当您想要的时候是:

SELECT Col1, Col2
FROM ExampleTable a
WHERE Col2 = (SELECT MAX(Col2) FROM ExampleTable b WHERE a.Col1 = b.Col1)

| Col1 | Col2       |
| 1    | 2018-01-01 |

我认为这就是您所需要的(已编辑以选择MAX([SETTLEMENT_DAY]):

SELECT MAX([SETTLEMENT_DAY]),
       [COUNTERPARTY_NAME],
       [LINE_ITEM_NAME],
       SUM([VARIABLE_VAL]) AS [PPA>10MW(OP)],
       [STATEMENT_TYPE],
       [ODS_LoadDate]
FROM [ODS].[dbo].[ODS_MM_PPA_FINANCIAL_RESULTS] A
WHERE [LINE_ITEM_NAME] = 'PPA > 10 MW (OP)'
  AND [VARIABLE_NAME] = 'CHARGE_AMOUNT'
  AND [SETTLEMENT_DAY] BETWEEN '2018-10-01' AND '2018-10-31'
  AND [COUNTERPARTY_NAME] IN ('IBIL_405145')
  AND [ODS_LoadDate] = (SELECT MAX([ODS_LoadDate])
                        FROM [ODS].[dbo].[ODS_MM_PPA_FINANCIAL_RESULTS] B
                        WHERE A.[COUNTERPARTY_NAME] = B.[COUNTERPARTY_NAME]
                          AND A.[LINE_ITEM_NAME] = B.[LINE_ITEM_NAME]
                          AND A.[VARIABLE_VAL] = B.[VARIABLE_VAL]
                          AND A.[STATEMENT_TYPE] = B.[STATEMENT_TYPE]
                          AND A.[STATEMENT_TYPE] = B.[STATEMENT_TYPE]
                          AND A.[VARIABLE_NAME] = B.[VARIABLE_NAME]
                          AND A.[ENTRY_DATE] = B.[ENTRY_DATE]
                          AND [ENTRY_DATE] = (SELECT MAX([ENTRY_DATE])
                                              FROM [ODS].[dbo].[ODS_MM_PPA_FINANCIAL_RESULTS] C
                                              WHERE B.[COUNTERPARTY_NAME] = C.[COUNTERPARTY_NAME]
                                                AND B.[LINE_ITEM_NAME] = C.[LINE_ITEM_NAME]
                                                AND B.[VARIABLE_VAL] = C.[VARIABLE_VAL]
                                                AND B.[STATEMENT_TYPE] = C.[STATEMENT_TYPE]
                                                AND B.[STATEMENT_TYPE] = C.[STATEMENT_TYPE]
                                                AND B.[VARIABLE_NAME] = C.[VARIABLE_NAME]))
GROUP BY [COUNTERPARTY_NAME],
         [LINE_ITEM_NAME],
         [STATEMENT_TYPE],
         [ODS_LoadDate]
ORDER BY [SETTLEMENT_DAY] ASC;