SQL Server查询连接多个表

时间:2012-08-30 12:01:07

标签: sql sql-server tsql

我有一个查询,我认为不应该那么难,但是,我现在花了很多时间在它上面仍然无法按照我想要的方式得到它,所以我希望有人在这里可以帮助我。

基本上,我需要创建一个报告,为每个区域提供每个月的值。但是,并非所有地区每个月都提供数据;在这种情况下,视图应该为该月份和区域返回NULL。因此,视图需要看起来像这样:

Month      Area    Value
2012-08-01 Area1   2
2012-08-01 Area2   3
2012-09-01 Area1   3
2012-09-01 Area2   NULL

我的数据表看起来像这样

Date       Area    Value
2012-08-01 Area1   2
2012-08-01 Area2   3
2012-09-01 Area1   3 -- Notice that Area2 is not present for September here

我有一张包含所有可用区域的表格 此外,我创建了一个表值函数,它返回从给定日期到现在的所有日期。

例如这句话

SELECT * FROM Periods_Months('2012-01-01')

将返回8条记录,如:

DateValue           Year    Month   YearMonth
2012-01-01 00:00:00.000 2012    1   20121
2012-02-01 00:00:00.000 2012    2   20122
2012-03-01 00:00:00.000 2012    3   20123
2012-04-01 00:00:00.000 2012    4   20124
2012-05-01 00:00:00.000 2012    5   20125
2012-06-01 00:00:00.000 2012    6   20126
2012-07-01 00:00:00.000 2012    7   20127
2012-08-01 00:00:00.000 2012    8   20128

根据建议,我的查询现在看起来像这样:

WITH months AS (
    SELECT DateValue, YearMonth FROM Periods_Months('2011-01-01')
)
select m.DateValue
       ,CAST(DATEADD(s,-1,DATEADD(mm, DATEDIFF(m,0,m.DateValue)+1,0)) AS Date) AS DateReported -- Get last day in month
       ,ResponseTime AS Value
       ,g.ExternalId 
from GISDB.dbo.GisObjects g
CROSS JOIN months m
LEFT OUTER JOIN
( -- SELECT data from data table, grouped by area and month
SELECT dbo.YearMonth(CloseDate) AS YearMonth
       ,MAX(CloseDate) AS LastDate
       ,GisObjectId
       ,SUM(DATEDIFF(HH,RegDate,CloseDate)) AS ResponseTime -- calculate response time between start and end data (the value we need)
FROM DataTable 
WHERE   CloseDate IS NOT NULL
AND     GisObjectId IS NOT NULL
GROUP BY GisObjectId, dbo.YearMonth(CloseDate)  -- group by area and month
) c
ON g.ObjectId = c.GisObjectId AND c.YearMonth = m.YearMonth 
WHERE g.CompanyId = 3 AND g.ObjectTypeId = 1 -- reduce the GIS objects that we compare to
ORDER BY m.DateValue, g.ObjectId 

但结果是这个(值总是为NULL):

DateValue                   DateReported    Value   ExternalId
2011-01-01 00:00:00.000 31-01-2011  NULL    9994
2011-01-01 00:00:00.000 31-01-2011  NULL    9993
2011-01-01 00:00:00.000 31-01-2011  NULL    9992
2011-01-01 00:00:00.000 31-01-2011  NULL    9991
2011-01-01 00:00:00.000 31-01-2011  NULL    2339
2011-01-01 00:00:00.000 31-01-2011  NULL    2338
2011-01-01 00:00:00.000 31-01-2011  NULL    2337
2011-01-01 00:00:00.000 31-01-2011  NULL    2336
2011-01-01 00:00:00.000 31-01-2011  NULL    2335
2011-01-01 00:00:00.000 31-01-2011  NULL    2334
2011-01-01 00:00:00.000 31-01-2011  NULL    2327
2011-01-01 00:00:00.000 31-01-2011  NULL    2326
2011-01-01 00:00:00.000 31-01-2011  NULL    2325
2011-01-01 00:00:00.000 31-01-2011  NULL    2324
2011-01-01 00:00:00.000 31-01-2011  NULL    2323
2011-01-01 00:00:00.000 31-01-2011  NULL    2322

2 个答案:

答案 0 :(得分:2)

我想你有一张包含所有区域的表格,我称之为area_table。

WITH month_table AS (
    SELECT dateValue FROM Periods_Months('2012-01-01')
)
select * from area_table 
CROSS JOIN month_table
LEFT OUTER JOIN myValueTable 
ON area_table.name = myValueTable.area 
AND myValueTable.date = left(convert(varchar(30),month_table.dateValue,120),10) 
ORDER BY myValueTable.Month, myValueTable.area 

答案 1 :(得分:1)

假设Areas是您所有可用区域的表格,t - 是您的数据表:

SELECT pm.dateValue,Ar.Area, t.value 
FROM Periods_Months('2012-01-01') pm, Areas ar
  left join t on (pm.dateValue=t.Date) and (ar.Area=t.Area)
order by pm.DateValue,ar.Area