SQLpivot/Unpivot?
SQL pivot/Unpivot?
真的希望有人能帮助我,我有一个 table 结构如下:
检索此数据的查询是:
SELECT
[Pattern Number]
,Sunday
,Monday
,Tuesday
,Wednesday
,Thursday
,Friday
,Saturday
FROM Access.Pattern_Details
WHERE [Pattern Number] = 4
+----------------+--------+--------+---------+-----------+----------+--------+----------+
| Pattern Number | Sunday | Monday | Tuesday | Wednesday | Thursday | Friday | Saturday |
+----------------+--------+--------+---------+-----------+----------+--------+----------+
| 4 | 0 | 1 | 1 | 1 | 1 | 1 | 0 |
+----------------+--------+--------+---------+-----------+----------+--------+----------+
我希望看起来像这样:
+----------------+------------+-----------+
| Pattern Number | WorkingDay | Day |
+----------------+------------+-----------+
| 4 | 0 | Sunday |
| 4 | 1 | Monday |
| 4 | 1 | Tuesday |
| 4 | 1 | Wednesday |
| 4 | 1 | Thursday |
| 4 | 1 | Friday |
| 4 | 0 | Saturday |
+----------------+------------+-----------+
我已经尝试了 PIVOT
/UNPIVOT
,这感觉是正确的方向,但我真的很纠结如何得到结果:(
在SQL服务器中,我会推荐cross apply
和values()
:
select pd.[Pattern Number], x.*
from Access.Pattern_Details pd
cross apply (values
('Sunday', Sunday),
('Monday', Monday),
('Tuesday', Tuesday),
('Wednesday', Wednesday),
('Thursday', Thursday),
('Friday', Friday),
('Saturday', Saturday)
) x(day, workingDay)
where pd.[Pattern Number] = 4
您可以使用 UNPIVOT
:
SELECT [Pattern Number], [Working Day], [Day]
FROM
(
SELECT 4 [Pattern Number], 0 Sunday, 1 Monday, 1 Tuesday, 1 Wednesday, 1 Thursday, 1 Friday, 0 Saturday
) pvt
UNPIVOT([Working Day] FOR [Day] IN ([Sunday], [Monday], [Tuesday], [Wednesday], [Thursday], [Friday], [Saturday])) unpvt
UNPIVOT
有很好的记录和解释。参见:Using Pivot and Unpivot - SQL Server
真的希望有人能帮助我,我有一个 table 结构如下:
检索此数据的查询是:
SELECT
[Pattern Number]
,Sunday
,Monday
,Tuesday
,Wednesday
,Thursday
,Friday
,Saturday
FROM Access.Pattern_Details
WHERE [Pattern Number] = 4
+----------------+--------+--------+---------+-----------+----------+--------+----------+
| Pattern Number | Sunday | Monday | Tuesday | Wednesday | Thursday | Friday | Saturday |
+----------------+--------+--------+---------+-----------+----------+--------+----------+
| 4 | 0 | 1 | 1 | 1 | 1 | 1 | 0 |
+----------------+--------+--------+---------+-----------+----------+--------+----------+
我希望看起来像这样:
+----------------+------------+-----------+
| Pattern Number | WorkingDay | Day |
+----------------+------------+-----------+
| 4 | 0 | Sunday |
| 4 | 1 | Monday |
| 4 | 1 | Tuesday |
| 4 | 1 | Wednesday |
| 4 | 1 | Thursday |
| 4 | 1 | Friday |
| 4 | 0 | Saturday |
+----------------+------------+-----------+
我已经尝试了 PIVOT
/UNPIVOT
,这感觉是正确的方向,但我真的很纠结如何得到结果:(
在SQL服务器中,我会推荐cross apply
和values()
:
select pd.[Pattern Number], x.*
from Access.Pattern_Details pd
cross apply (values
('Sunday', Sunday),
('Monday', Monday),
('Tuesday', Tuesday),
('Wednesday', Wednesday),
('Thursday', Thursday),
('Friday', Friday),
('Saturday', Saturday)
) x(day, workingDay)
where pd.[Pattern Number] = 4
您可以使用 UNPIVOT
:
SELECT [Pattern Number], [Working Day], [Day]
FROM
(
SELECT 4 [Pattern Number], 0 Sunday, 1 Monday, 1 Tuesday, 1 Wednesday, 1 Thursday, 1 Friday, 0 Saturday
) pvt
UNPIVOT([Working Day] FOR [Day] IN ([Sunday], [Monday], [Tuesday], [Wednesday], [Thursday], [Friday], [Saturday])) unpvt
UNPIVOT
有很好的记录和解释。参见:Using Pivot and Unpivot - SQL Server