我们正在开展任务和假期项目。
表结果是:
Rn TicketNo task Assigned emp
1 a Holdiay sunday emp_A
2 a Task A completed monday emp_A
1 b Holiday sunday emp_B
2 b Task A completed monday emp_B
我想要以下结果。
TicketNo emp sunday monday
a emp_A Holdiay Task A completed
b emp_B Holiday Task A completed
我在不使用数据透视的情况下尝试查询。 不要求帮助。
答案 0 :(得分:0)
您可以使用以下quries将行数据转换为列。试一试。
1.使用支点
select distinct TicketNo,emp,sunday,monday
from
(
select distinct TicketNo ,task, Assigned,emp from <tbl_name>
) src
pivot
(
max(task)
for Assigned in (sunday, monday)
) piv
2.使用联接:(没有Pivot)
select a.ticketno,
a.emp
,a.task as sunday
,b.task as monday
from <tbl_name> a join <tbl_name> b
on a.assigned='sunday' and b.assigned='monday'
and a.ticketno=b.ticketno
答案 1 :(得分:0)
我使用子查询和连接以不同的方式尝试了它。无论如何,你已经有了答案。
SELECT * INTO YOUR_TABLE FROM
( SELECT 1 AS Rn,'a' AS TicketNo,'Holiday' as task,'sunday' as Assigned,'emp_A' as emp
UNION ALL
SELECT 2,'a','Task A completed','monday','emp_A' UNION ALL
SELECT 1,'b','Holiday','sunday','emp_B' UNION ALL
SELECT 2,'b','Task A completed','monday','emp_B'
) X
SELECT * FROM YOUR_TABLE
SELECT T1.TicketNo
,T1.emp
,T1.Sunday
,T2.Monday
FROM
(SELECT TicketNo
,emp
,CASE WHEN task='Holiday' then 'Holiday' end as Sunday
FROM YOUR_TABLE
WHERE CASE WHEN task='Holiday' then 'Holiday' end IS NOT NULL) T1
JOIN
(SELECT TicketNo
,emp
,CASE WHEN task='Task A completed' then 'Task A completed' end as Monday
FROM YOUR_TABLE
WHERE CASE WHEN task='Task A completed' then 'Task A completed' end IS NOT NULL) T2
ON T1.TicketNo = T2.TicketNo