是否可以查询 SQL Pivot 报告并将输出连接到另一个 select 语句中?
Posted
技术标签:
【中文标题】是否可以查询 SQL Pivot 报告并将输出连接到另一个 select 语句中?【英文标题】:Is it possible to query an SQL Pivot report and join the output in another select statement? 【发布时间】:2015-09-12 23:58:24 【问题描述】:我有一个 SQL 中的数据透视类型报告(创建为视图)。
工单编号为 ROWS,而工单状态为 COLUMNS。
数据中的值是工单在该状态下花费的总时间的时间戳 (hh:mm:ss)。
SQL Pivot 视图示例(注意:未列出所有状态(请参阅 SQL Pivot Query for all):
ticket_number closed completed new waiting-1
22 0:00:00 0:00:00 0:00:00 0:00:00
32 0:00:00 0:00:00 6:00:01 0:00:00
SQL 透视查询:
v_pivottime
WITH t AS (
select
ticket_number,
ISNULL(status,'null') status,
update_date,
row_number() OVER (PARTITION BY ticket_number ORDER BY update_date) rn
from xxx.dbo.report
), s AS (
SELECT
t1.ticket_number,
t1.status,
t1.update_date,
t2.update_date prevdate,
case when t2.status=t1.status then DATEDIFF(s, t2.update_date, t1.update_date) end dif
FROM t t1
LEFT JOIN t t2 ON t1.ticket_number=t2.ticket_number AND t1.rn=t2.rn+1
)
SELECT *
FROM (
SELECT
ticket_number,
[status],
cast(dateadd(s,sum(dif),0) as time(0)) [time]
FROM s
GROUP BY ticket_number,status ) src
PIVOT
(
min([time])
for [status] in ([Closed], [Complete], [New], [Waiting-1], [Waiting-2], [Waiting-3], [stoptime], [Research-1], [Research-2], [Resolved], [Special Request], [Opened Request], [null])
) piv;
查询视图(下)我想添加(加入?)枢轴输出,并在顶部(列)添加状态名称以及 hh:mm:ss 中的值输出
v_last_update
select
ctr1.ticket_number,
ctr1.status,
ctr1.create_date,
ctr1.created_by,
ctr1.customer,
ctr1.description,
ctr1.update_sequence,
ctr1.update_date
FROM
xx.dbo.report ctr1
JOIN (
select ticket_number as ticket_number_d,
MAX(update_date) as max_update_date,
MAX(update_sequence) as max_ update_sequence
from xx.dbo.report
group by ticket_number
) ctr2
ON ctr1.ticket_number = ctr2.ticket_number_d
AND ctr1.update_date = ctr2.max_update_date
AND ctr1.update_sequence = ctr2.max_update_sequence
WHERE customer <> ‘Bread’
ORDER BY max_update_date ASC;
*想看看是否有一种方法可以在某种程度上从 v_last_update 查询中的票号执行 vlookup 类型的交易,并将列添加到输出中列的末尾。想为每个状态添加一列并插入该票状态的时间。这有可能让我发疯吗!
谢谢!
-P
【问题讨论】:
假设您的所有查询都按预期交付,只需从您的数据透视查询创建一个 VIEW 或(内联!)-TVF 并将其连接起来,就好像它是一个表一样...... 【参考方案1】:除了我的评论之外,您还可以让 Pivot 部分成为 CTE:
(未经测试)
WITH t AS (
select
ticket_number,
ISNULL(status,'null') status,
update_date,
row_number() OVER (PARTITION BY ticket_number ORDER BY update_date) rn
from xxx.dbo.report
), s AS (
SELECT
t1.ticket_number,
t1.status,
t1.update_date,
t2.update_date prevdate,
case when t2.status=t1.status then DATEDIFF(s, t2.update_date, t1.update_date) end dif
FROM t t1
LEFT JOIN t t2 ON t1.ticket_number=t2.ticket_number AND t1.rn=t2.rn+1
)
,pivotPart AS
(
SELECT *
FROM (
SELECT
ticket_number,
[status],
cast(dateadd(s,sum(dif),0) as time(0)) [time]
FROM s
GROUP BY ticket_number,status ) src
PIVOT
(
min([time])
for [status] in ([Closed], [Complete], [New], [Waiting-1], [Waiting-2], [Waiting-3], [stoptime], [Research-1], [Research-2], [Resolved], [Special Request], [Opened Request], [null])
) piv
)
select
ctr1.ticket_number,
ctr1.status,
ctr1.create_date,
ctr1.created_by,
ctr1.customer,
ctr1.description,
ctr1.update_sequence,
ctr1.update_date,
pivotPart.*
FROM
xx.dbo.report ctr1
JOIN pivotPart ON ctr1.ticket_number=pivotPart.ticket_number
JOIN (
select ticket_number as ticket_number_d,
MAX(update_date) as max_update_date,
MAX(update_sequence) as max_ update_sequence
from xx.dbo.report
group by ticket_number
) ctr2
ON ctr1.ticket_number = ctr2.ticket_number_d
AND ctr1.update_date = ctr2.max_update_date
AND ctr1.update_sequence = ctr2.max_update_sequence
WHERE customer <> ‘Bread’
ORDER BY max_update_date ASC;
【讨论】:
谢谢俊哥!查询有效,但没有显示数据透视表时间戳的列 @Panthersfball234,在“pivotPart”中选择ticket_number,状态和时间,上面用“SELECT *”取这三个……不知道还需要什么信息……顺便说一句:如果您想让枢轴具有多于一列,那将变得非常棘手。如果有帮助,请投票和/或标记为已接受或继续询问...【参考方案2】:实际上,弄清楚这比我想象的要容易得多。
我所做的是将 select 语句和 pivot 查询转换为单独的视图。之后,将选择视图与透视视图 ON ticket_number 结合起来。它返回了与 select 语句票号内联的数据透视视图中的所有列。现场检查和它的点!
感谢大家的帮助,不胜感激!
【讨论】:
因为是我的评论和我的回答使您走上了正确的道路,您非常好心地支持我的回答和/或标记为已接受,谢谢! 完成 :) 再次感谢您以上是关于是否可以查询 SQL Pivot 报告并将输出连接到另一个 select 语句中?的主要内容,如果未能解决你的问题,请参考以下文章
SQL(横表和纵表)行列转换,PIVOT与UNPIVOT的区别和使用方法举例