我有一个SQL表,有两列我感兴趣; Event_Time和Event_ID。 Event_ID存储开始和结束事件的数值(1表示开始,2表示结束)每个事件都有关联的Event_Time。我想要做的是在SSRS报告中将这些行显示为列:
Event_ID Event_Time
1 5/11/2017 9:50 AM
2 5/11/2017 9:55 AM
我想看看:
Start Time End Time
5/11/2017 9:50 AM 5/11/2017 9:55 AM
编辑:
详细说明,该报告将显示按Container_ID分组的数据。目前报告显示此表:
Container ID Event ID Event Time
17080430-002 1 5/11/2017 9:50 AM
2 5/11/2017 9:55 AM
我们希望看到:
Container ID Start Time End Time
17080430-002 5/11/2017 9:50 AM 5/11/2017 9:55 AM
编辑2:
到目前为止,我对此感到非常幸运:
select t1.Container_ID, t1.Event_Time Start, t2.Event_Time [End]
from (
select Container_ID, Event_Time,
ROW_NUMBER() Over(Partition by Container_ID order by Event_Time) EventID
FROM dbo.Custom_EventLog WHERE Event_ID = '1'
) t1
LEFT JOIN (
SELECT Container_ID, Event_Time,
ROW_NUMBER() Over(Partition by Container_ID order by Event_Time) EventID
from dbo.Custom_EventLog WHERE Event_ID = '2'
) t2
on (t1.Container_ID = t2.Container_ID and t1.EventID = t2.EventID)
但是返回的表格的错位时间如图所示:
答案 0 :(得分:0)
我不确定该表结构是否可以告诉开始和结束时间对应于同一事件(除非您的问题中没有显示另一列建立关系)。
话虽如此,您可以使用Event_ID
过滤的两个子查询执行某些操作:
SELECT (SELECT Event_Time FROM tableName WHERE Event_ID = 1) AS [Start Time],
(SELECT Event_Time FROM tableName WHERE Event_ID = 2) AS [End Time]
答案 1 :(得分:0)
我认为这就是你所追求的。我会在SQL而不是SSRS中解决这个问题。临时表仅供我们实际处理一些数据。
if object_id('tempdb.dbo.#events') is not null drop table #events
create table #events
(
mainEventId int,
eventId int,
Event_Time datetime
)
insert into #events
select 200, 1, '5/11/2017 9:50 AM'
union all select 200, 2, '5/11/2017 9:55 AM'
union all select 300, 1, '5/11/2017 9:50 AM'
union all select 300, 2, '5/11/2017 9:55 AM'
select startTime.mainEventId, startTime.Event_Time, endTime.Event_Time
from #events startTime
left join #events as endTime
on startTime.mainEventId = endTime.mainEventId
and endTime.eventId = 2
where
startTime.eventId = 1
答案 2 :(得分:0)
我肯定会在SQL而不是SSRS中执行此操作。在我看来,你可以通过一个连接来逃避这个 - 我的例子下面是一个内连接,但你可以轻松地修改它以使用左连接(或其他)
select t1.container_id, t1.startTime, t2.endTime
from (select container_id, event_time as startTime from tableName as t1
where event_id = 1) as t1
join (select container_id, event_time as endTime from tableName as t2
where event_id = 2) as t2 on t2.container_id = t1.container_id
希望有所帮助。