请参阅SQL Fiddle,以获取可用的示例:http://sqlfiddle.com/#!3/6d04f/1
我有4个表,TableA,TableB,TableC和TableD。
TableA数据和结构:
+--------------------+
| ID | Name |
+--------------------+
| 987 | Mississauga |
| 454 | Hoboken |
| 343 | Berkeley |
+--------------------+
TableB数据和结构:
+------------------------------------------------------------+
| City1ID | City1Name | City2ID | City2Name | Factor |
+------------------------------------------------------------+
| 343 | Berkeley | 19 | Oakland | 0.5 |
| 987 | Mississauga | 23 | Toronto | 1.0 |
| 66 | Redmond | 13 | Seattle | 1.0 |
| 343 | Berkeley | 14 | San Jose | 0.5 |
| 454 | Hoboken | 55 | New York City | 0.9 |
| 454 | Hoboken | 44 | Philadelphia | 0.1
| 19 | Oakland | 9 | San Francisco | 1.0 |
...
+------------------------------------------------------------+
TableC:是一个表,其中包含一年中每一天的城市每小时数据。并非所有城市都在TableC中。 TableA表示TableB中存在映射的城市的一小部分。在我的例子中,TableA有439个城市(行),TableB有7000行映射。
TableC的结构如下:
+------------------------------------------------------------------------+
|Date | CID | Name | Blah1 | Blah2 | Hour1 | Hour2 | Hour3 | ... | Hour24 |
+------------------------------------------------------------------------+
以下是TableC的一天数据的示例:
2006-08-01 00:00:00 9 San Francisco Blah1 Blah2 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1
2006-08-01 00:00:00 23 Toronto Blah1 Blah2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2
2006-08-01 00:00:00 13 Seattle Blah1 Blah2 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3
2006-08-01 00:00:00 14 San Jose Blah1 Blah2 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4
2006-08-01 00:00:00 55 New York City Blah1 Blah2 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5
2006-08-01 00:00:00 44 Philadelphia Blah1 Blah2 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6
为了说明,TableC中没有奥克兰市。然而,伯克利的每小时观测数据是通过求和得出的(圣何塞0.5 x数据和奥克兰0.5 x数据)。
TableD结构:
+----------------------------------+
| Date | ID | Name | Hour | Value |
+----------------------------------+
TableD是最终结果,应该使用TableA中每个城市的每天每小时观察数据填充。
在我的例子中,HOUR 1应如下所示,Mississauga = 2.0(1 x Toronto = 1 x 2 = 2),Hoboken = 5.10(0.9 x纽约市+ 0.1 x费城= 0.9 x 5 + 0.1 x 6 = 5.10),伯克利= 2.50(0.5 x圣何塞+ 0.5 x奥克兰= 0.5 x 4 + 0.5 x旧金山= 0.5 x 4 + 0.5 x 1 = 2.50):
+--------------------------------------------------------+
| Date | ID | Name | Hour | Value |
+--------------------------------------------------------+
| 2006-08-01 00:00:00 | 987 | Mississauga | 1 | 2.00 |
| 2006-08-01 00:00:00 | 454 | Hoboken | 1 | 5.10 |
| 2006-08-01 00:00:00 | 343 | Berkeley | 1 | 2.50 |
+--------------------------------------------------------+
现在,棘手的部分是奥克兰没有出现在TableC中,而是映射到旧金山,因此任何映射到奥克兰的城市都需要引用它所映射到的城市。在我的情况下,有很多情况发生这种情况所以我想确保我正确处理这个问题。
我得到解包/规范化TableC并将其加入表B但不确定如何继续。我知道我需要使用Group By,但不知道如何使用我所描述的警告。
select * from
(
select Date, CID, Name, replace(Details, 'Hour', '') as Hour, ObservationValue
from TableC
as Result
unpivot
(
ObservationValue for Details in ([Hour1], [Hour2], [Hour3], [Hour4], [Hour5], [Hour6],
[Hour7],[Hour8],[Hour9],[Hour10],[Hour11],[Hour12],[Hour13],[Hour14],
[Hour15],[Hour16],[Hour17],[Hour18],[Hour19],[Hour20],[Hour21],[Hour22],
[Hour23],[Hour24])
)
as UnPvt
) as resultX
join TableB as b on (b.City2ID = resultX.CID)
如果你无法访问下面的SQL Fiddle链接,那么就是创建Schema的代码:
CREATE TABLE TableA
([Id] int, [name] varchar(20));
INSERT INTO TableA
([Id], [name])
VALUES
(987, 'Mississauga'),
(454, 'Hoboken'),
(343, 'Berkeley');
CREATE TABLE TableB
([City1ID] int, [City1Name] varchar(20),
[City2ID] int, [City2Name] varchar(20), [Factor] varchar(20))
INSERT INTO TableB
([City1ID], [City1Name],[City2ID], [City2Name], [Factor])
VALUES
(343, 'Berkeley', 19, 'Oakland', 0.5),
(987, 'Mississauga', 23, 'Toronto', 1.0),
(66, 'Redmond', 13, 'Seattle', 1.0),
(343, 'Berkeley', 14, 'San Jose', 0.5),
(454, 'Hoboken', 55, 'New York City', 0.9),
(454, 'Hoboken', 44, 'Philadephia', 0.1),
(19, 'Oakland', 9, 'San Francisco', 1.0);
CREATE TABLE TableC
([date] datetime, [CId] int, [name] varchar(50), [blah1] varchar(10),
[blah2] varchar(10), [hour1] int, [hour2] int, [hour3] int, [hour4] int,
[hour5] int, [hour6] int, [hour7] int, [hour8] int, [hour9] int,
[hour10] int,[hour11] int, [hour12] int, [hour13] int, [hour14] int,
[hour15] int, [hour16] int, [hour17] int, [hour18] int, [hour19] int,
[hour20] int, [hour21] int, [hour22] int, [hour23] int, [hour24] int);
INSERT INTO TableC
([date], [CId], [name], [blah1],
[blah2], [hour1], [hour2], [hour3], [hour4],
[hour5], [hour6], [hour7], [hour8], [hour9],
[hour10],[hour11], [hour12], [hour13], [hour14],
[hour15], [hour16], [hour17], [hour18], [hour19],
[hour20], [hour21], [hour22], [hour23], [hour24])
VALUES
('2006-08-01 00:00:00', 9, 'San Francisco', 'Blah1', 'Blah2', 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1),
('2006-08-01 00:00:00', 23,'Toronto', 'Blah1', 'Blah2', 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2),
('2006-08-01 00:00:00', 13,'Seattle', 'Blah1', 'Blah2', 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3),
('2006-08-01 00:00:00', 14,'San Jose', 'Blah1', 'Blah2', 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4),
('2006-08-01 00:00:00', 55,'New York City', 'Blah1', 'Blah2', 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5),
('2006-08-01 00:00:00', 44,'Philadelphia', 'Blah1', 'Blah2', 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6);
答案 0 :(得分:1)
这将with your data and structure
select date,cityid, cityname,hr, sum(hour1) as total
from
(
select c.Date as date,
isnull(a.id,parent.city1id) as cityid,
isnull(a.name,parent.city1name) as cityname,
isnull(parent.factor,1) * --- parent factor
b.factor * --- actual factor
isnull(c.hour1,0) as hour1 ,
1 as hr
from tablea a
right outer join tableb b on b.city1id = a.[id]
left outer join tableb parent on parent.city2id=b.city1id
left outer join tablec c on c.cid = isnull(b.city2id,b.city1id)
) final
where date is not null and cityid is not null
group by cityid,cityname,date,hr