转置数据SQL

时间:2017-05-01 22:53:15

标签: sql ms-access

数据与此类似:

+----+------+-----------+-------+---------+---------+--------+
| ID | Unit | Floorplan | Sq Ft |  Name   | Amenity | Charge |
+----+------+-----------+-------+---------+---------+--------+
|  1 |  110 | A1        |   750 |  Alan   | GARAGE  |     50 |
|  2 |      |           |       |         | RENT    |    850 |
|  3 |      |           |       |         | PEST    |      2 |
|  4 |      |           |       |         | TRASH   |     15 |
|  5 |      |           |       |         | TOTAL   |     20 |
|  6 |  111 | A2        |   760 |  Bill   | STORAGE |     35 |
|  7 |      |           |       |         | GARAGE  |     50 |
|  8 |      |           |       |         | RENT    |    850 |
|  9 |      |           |       |         | PEST    |      2 |
| 10 |      |           |       |         | TOTAL   |     15 |
| 11 |  112 | A3        |   770 | Charlie | PETRENT |     20 |
| 12 |      |           |       |         | STORAGE |     35 |
| 13 |      |           |       |         | GARAGE  |     50 |
| 14 |      |           |       |         | RENT    |    850 |
| 15 |      |           |       |         | TOTAL   |      2 |
+----+------+-----------+-------+---------+---------+--------+

我是SQL的新手,并尝试使用Microsoft Access,但我需要帮助。

数据需要如下所示:

Ending Picture

我的第一步是用

将单位与其余单位分开
SELECT * FROM table WHERE Unit <> NULL;

之后我通常只是硬输入其余部分。

我的想法如下:

INSERT INTO table

VALUES (NULL,NULL,...,'Pest',$2)

FROM table

WHERE NOT EXIST 'Pest' BETWEEN x AND y

/* where x = Total 1 and y = Total 2*/

我是否在正确的轨道上?我可能需要循环或连接,但我还没有达到那个级别。

2 个答案:

答案 0 :(得分:1)

您可以使用交叉表查询,但有点复杂的是:

TRANSFORM 
    Sum(TableUnit.Charge) AS SumOfCharge
SELECT 
    S.Unit, 
    S.Floorplan, 
    S.SqFt, 
    S.Name, 
    S.Amenity
FROM 
    TableUnit, 
        (SELECT 
            Q.Id, 
            Val(DMax("Id","TableUnit","Id<=" & Q.[Id] & " And Unit Is Not Null")) AS ParentId
        FROM TableUnit As Q) AS T, 
        (SELECT 
            TableUnit.Id, 
            TableUnit.Unit, 
            TableUnit.Floorplan, 
            TableUnit.SqFt, 
            TableUnit.Name, 
            TableUnit.Amenity
        FROM 
            TableUnit
        WHERE 
            TableUnit.Unit Is Not Null) AS S
WHERE 
    TableUnit.Id=[T].[Id] 
    AND 
    T.ParentId)=[S].[Id]
GROUP BY 
    T.ParentId, 
    S.Unit, 
    S.Floorplan, 
    S.SqFt, 
    S.Name, 
    S.Amenity
PIVOT 
    TableUnit.Amenity In
        ("Garage","Pest","Trash","PetRent","Storage","Rent");

您的测试数据与预期输出略有不同,因此:

Output

答案 1 :(得分:0)

我的MSAccess相当生疏,但这样的事情应该有效:

SELECT t0.Unit, t0.Floorplan, t0.[Sq Ft], t0.Name, t0.Amenity
   , SUM(IIF(tM.Amenity = 'GARAGE', Charge, 0)) AS [Garage]
   , SUM(IIF(tM.Amenity = 'PEST', Charge, 0)) AS [Pest]
FROM (
   SELECT t1.id AS id0, MIN(t2.id) AS idN
   FROM t AS t1
   INNER JOIN t AS t2 ON t1.id < t2.id
   WHERE t1.Unit <> '' AND t2.Unit <> ''
) AS groups
INNER JOIN t AS t0 ON t0.id = groups.id0
LEFT JOIN t AS tM ON tM.id > groups.id0 AND tm.id < groups.idN
GROUP BY t0.Unit, t0.Floorplan, t0.[Sq Ft], t0.Name, t0.Amenity
;

但是,如果我没记错的话,它在新版本中没有改变;您不能拥有真正的子查询,并且需要使groups成为可以加入的单独查询,就像它是一个表/视图一样。