使用子查询过滤查询

时间:2016-09-23 09:57:02

标签: sql sql-server sql-server-2008

为了理解这个问题,我将解释预期的结果。 我有一个db表,我保存了当天活动的一些数据。然后我想总结一些数字字段和文本字段的最后一个寄存器,使用两个日期之间的过滤器。

示例:

       •DB TABLE

  ID|CALLS|RESULT  |  DATE

  1 | 2   |FAIL    |15/09/16
  1 | 1   |ERROR   |16/09/16
  1 | 3   |OK      |17/09/16


      •SUM BETWEEN 15 and 17

 ID|TOTAL CALLS|LAST RESULT   
 1 |    6      |    OK   

     •SUM BETWEEN 15 and 16

 ID|TOTAL CALLS|LAST RESULT   
 1 |    3      |  ERROR   

- 这会是解决方案吗?

 SELECT DISTINCT ID,
 TOTAL_CALLS=SUM(CALLS),
 LAST_RESULT= (
     SELECT RESULT FROM TABLE T2 where T2.DATE between MIN(T1.DATE) and MAX (T1.DATE) and T1.ID=T2.ID
)

FROM TABLE T1
WHERE
TIME BETWEEN 15/09/16 and 17/09/16
GROUP BY ID

非常感谢!

此致

4 个答案:

答案 0 :(得分:1)

使用以下查询。

;WITH cte_1
AS
(SELECT  ID,SUM(CALLS)OVER( PARTITION BY ID) [TOTAL CALLS] 
 ,Result [LAST RESULT]
 ,ROW_NUMBER()OVER( PARTITION BY ID ORDER BY [DATE] desc) RNO
from #YourTable T
WHERE [DATE] between '09/15/2016' AND '09/16/16')
SELECT ID,[TOTAL CALLS],[LAST RESULT] 
FROM cte_1 
WHERE Rno=1

答案 1 :(得分:0)

SELECT ID, SUM(CALLS), MAX(CASE WHEN RNO=1 THEN RESULT END)
FROM (
  SELECT *, ROW_NUMBER()OVER(PARTITION BY ID ORDER BY [DATE] desc) RNO
  FROM tbl T1
  WHERE
  DATE BETWEEN '15/09/16' and '17/09/16'
) A
GROUP BY ID

此查询有一个更好的计划,然后由 Unnikrishnan R

提供解决方案

比较一下:

;with tbl(ID, CALLS, RESULT, DATE) as (
  select 1, 2, 'FAIL', '15/09/16' union all
  select 1, 1, 'ERROR', '16/09/16' union all
  select 1, 3, 'OK', '17/09/16' union all
  select 2, 1, 'OK', '17/09/16'
)
, cte_1
AS
(SELECT  ID,SUM(CALLS)OVER( PARTITION BY ID) [TOTAL CALLS] 
 ,Result [LAST RESULT]
 ,ROW_NUMBER()OVER( PARTITION BY ID ORDER BY [DATE] desc) RNO
from tbl T
WHERE [DATE] between '15/09/16' AND '16/09/16')
SELECT ID,[TOTAL CALLS],[LAST RESULT] 
FROM cte_1 
WHERE Rno=1

费用 0,02278908

;with tbl(ID, CALLS, RESULT, DATE) as (
  select 1, 2, 'FAIL', '15/09/16' union all
  select 1, 1, 'ERROR', '16/09/16' union all
  select 1, 3, 'OK', '17/09/16' union all
  select 2, 1, 'OK', '17/09/16'
)
SELECT ID, SUM(CALLS), MAX(CASE WHEN RNO=1 THEN RESULT END)
FROM (
  SELECT *, ROW_NUMBER()OVER(PARTITION BY ID ORDER BY [DATE] desc) RNO
  FROM tbl T1
  WHERE
  DATE BETWEEN '15/09/16' and '17/09/16'
) A
GROUP BY ID

费用 0,01138172

答案 2 :(得分:0)

你可以使用TOP 1 WITH TIES和ORDERING来获得你需要的东西:

DECLARE @dFrom date = '2016-09-15',
        @dTo date = '2016-09-16'

SELECT TOP 1 WITH TIES 
                    ID,
                    SUM(CALLS) OVER (PARTITION BY ID) [TOTAL CALLS],
                    RESULT [LAST RESULT]
FROM YourTable
WHERE [DATE] between @dFrom and @dTo
ORDER BY ROW_NUMBER() OVER (PARTITION BY ID ORDER BY [DATE]) DESC

答案 3 :(得分:0)

您可以使用最大日期来获取结果列值。

DECLARE @FromDate DATETIME= '15 SEP 2016'

DECLARE @ToDate DATETIME= '16 SEP 2016'

SELECT ID , SUM(CALLS) , ( SELECT RESULT
                           FROM #yourTable
                           WHERE [DATE] = @ToDate
                         )  RESULT
FROM **#yourTable**
WHERE [DATE] BETWEEN @FromDate AND @ToDate
GROUP BY ID