结合SQL查询

时间:2015-02-13 15:18:39

标签: sql sql-server

我正在尝试将两个非常相似的SQL查询与不同的日期范围组合以生成单个输出表。 (比较本周与去年相应周的结果。)

我有一些SO的拖网并发现了一些类似的问题(例如this one),但仍未设法使其正常工作:

这两个查询是:

SELECT
    [arrpoint]  
    ,COUNT([arrpoint]) AS NumberOfTimesTW
FROM  [groups] tb1
    INNER JOIN [fileinfo] tb2 ON tb1.op_name = tb2.[operator]
    INNER JOIN [costs] tb4 ON tb2.[fileno] = tb4.[fileno]
WHERE 
    [bedbank] = 1 
    AND [booked] >= DATEADD(wk, DATEDIFF(wk,0,GETDATE()), 0)
GROUP BY [ arrpoint] HAVING (COUNT([arrpoint])>1)
ORDER BY NumberOfTimesTW DESC

SELECT
    [arrpoint]  
    ,COUNT([arrpoint]) AS NumberOfTimesTW
FROM  [groups] tb1
    INNER JOIN [fileinfo] tb2 ON tb1.op_name = tb2.[operator]
    INNER JOIN [costs] tb4 ON tb2.[fileno] = tb4.[fileno]
WHERE 
    [bedbank] = 1 
    AND [booked] >= DateAdd(wk,-52,DATEADD(wk, DATEDIFF(wk,0,GETDATE()), 0))
    AND [booked] <= DateAdd(wk,-51,DATEADD(wk, DATEDIFF(wk,0,GETDATE()), 0))
GROUP BY [ arrpoint] HAVING (COUNT([arrpoint])>1)
ORDER BY NumberOfTimesTW DESC

这些输出:

arrpoint | NumberOfTimesTW
  abc    |        3
  def    |        2

arrpoint | NumberOfTimesTWLY
  ghi    |        5
  klm    |        4
  abc    |        1

我希望得到的是:

arrpoint | NumberOfTimesTW | NumberOfTimesTWLY
  abc    |       3         |          1
  def    |       2         |
  ghi    |                 |          5
  klm    |                 |          4

我不太了解SQL我最初认为只要在两个查询之间插入UNION就可以实现这一点,但没有运气。

有人能给我一些关于如何实现这个目标的指示吗?

3 个答案:

答案 0 :(得分:3)

您可以在第二个查询

上使用完全连接(假设您需要来自两个表的所有值)
select  a.[arrpoint], NumberOfTimesTW, NumberOfTimesTW1 
from
(
(SELECT
    [arrpoint]  
    ,COUNT([arrpoint]) AS NumberOfTimesTW
FROM  [groups] tb1
    INNER JOIN [fileinfo] tb2 ON tb1.op_name = tb2.[operator]
    INNER JOIN [costs] tb4 ON tb2.[fileno] = tb4.[fileno]
WHERE 
    [bedbank] = 1 
    AND [booked] >= DATEADD(wk, DATEDIFF(wk,0,GETDATE()), 0)
GROUP BY [ arrpoint] HAVING (COUNT([arrpoint])>1)
ORDER BY NumberOfTimesTW DESC) as a 
full join
(SELECT
    [arrpoint]  
    ,COUNT([arrpoint]) AS NumberOfTimesTW1
FROM  [groups] tb1
    INNER JOIN [fileinfo] tb2 ON tb1.op_name = tb2.[operator]
    INNER JOIN [costs] tb4 ON tb2.[fileno] = tb4.[fileno]
WHERE 
    [bedbank] = 1 
    AND [booked] >= DateAdd(wk,-52,DATEADD(wk, DATEDIFF(wk,0,GETDATE()), 0))
    AND [booked] <= DateAdd(wk,-51,DATEADD(wk, DATEDIFF(wk,0,GETDATE()), 0))
GROUP BY [ arrpoint] HAVING (COUNT([arrpoint])>1)
ORDER BY NumberOfTimesTW DESC) as b)
on a.[arrpoint] =b.[arrpoint] 

答案 1 :(得分:3)

在聚合中使用Case来简化查询

SELECT
    [arrpoint]  
    ,COUNT( case when [booked] >= DATEADD(wk, DATEDIFF(wk,0,GETDATE()), 0)
        THEN [arrpoint]
        END) AS NumberOfTimesTW
    , COUNT(CASE WHEN ([booked] >= DateAdd(wk,-52,DATEADD(wk, DATEDIFF(wk,0,GETDATE()), 0))
    AND [booked] <= DateAdd(wk,-51,DATEADD(wk, DATEDIFF(wk,0,GETDATE()), 0)))
        THEN [arrpoint]
        END) AS NumberOfTimesTWLY
FROM  [groups] tb1
    INNER JOIN [fileinfo] tb2 ON tb1.op_name = tb2.[operator]
    INNER JOIN [costs] tb4 ON tb2.[fileno] = tb4.[fileno]
WHERE 
    [bedbank] = 1 
AND (
    [booked] >= DATEADD(wk, DATEDIFF(wk, 0, GETDATE()), 0)
    OR(
            [booked] >= DateAdd(wk, - 52, DATEADD(wk, DATEDIFF(wk, 0, GETDATE()), 0))
            AND [booked] <= DateAdd(wk, - 51, DATEADD(wk, DATEDIFF(wk, 0, GETDATE()), 0))
            )
        )
GROUP BY [ arrpoint] HAVING (COUNT([arrpoint])>1)
ORDER BY NumberOfTimesTW DESC

答案 2 :(得分:1)

with a as (
SELECT
    [arrpoint]  
    ,COUNT([arrpoint]) AS NumberOfTimesTW
FROM  [groups] tb1
    INNER JOIN [fileinfo] tb2 ON tb1.op_name = tb2.[operator]
    INNER JOIN [costs] tb4 ON tb2.[fileno] = tb4.[fileno]
WHERE 
    [bedbank] = 1 
    AND [booked] >= DATEADD(wk, DATEDIFF(wk,0,GETDATE()), 0)
GROUP BY [ arrpoint] HAVING (COUNT([arrpoint])>1)
)
,b as (
SELECT
    [arrpoint]  
    ,COUNT([arrpoint]) AS NumberOfTimesTW
FROM  [groups] tb1
    INNER JOIN [fileinfo] tb2 ON tb1.op_name = tb2.[operator]
    INNER JOIN [costs] tb4 ON tb2.[fileno] = tb4.[fileno]
WHERE 
    [bedbank] = 1 
    AND [booked] >= DateAdd(wk,-52,DATEADD(wk, DATEDIFF(wk,0,GETDATE()), 0))
    AND [booked] <= DateAdd(wk,-51,DATEADD(wk, DATEDIFF(wk,0,GETDATE()), 0))
GROUP BY [ arrpoint] HAVING (COUNT([arrpoint])>1)
)
select 
  [arrpoint]
  ,a.NumberOfTimesTW
  ,b.NumberOfTimesTW as NumberOfTimesTWLY
from a
full join b
  on a.[arrpoint] = b.[arrpoint]