0

请在此处查看 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    |
...
+------------------------------------------------------------+

表 C:是一个表格,其中包含一年中每一天的城市每小时数据。并非所有城市都在表 C 中。表 A 表示在表 B 中存在映射的城市的一小部分。在我的例子中,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

例如,奥克兰市不在表 C 中。但是,伯克利的每小时观测数据是通过求和得出的(圣何塞的 0.5 x 数据和奥克兰的 0.5 x 数据)。

表D结构:

+----------------------------------+
| Date | ID | Name |  Hour | Value |
+----------------------------------+

表 D 是最终结果,应填充表 A 中每个城市每天的每小时观测数据。

在我的示例中,HOUR 1 应该如下所示,其中 Mississauga = 2.0 (1 x Toronto = 1 x 2 = 2),Hoboken = 5.10 (0.9 x New York City + 0.1 x Philadelphia = 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 并将其与 Table 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);
4

1 回答 1

1

这将与您的数据和结构有关

   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
于 2013-04-28T13:35:36.227 回答