1

我正在尝试在我们的票务网站上汇总用户的实时视图。

目前,我在网站上有 2 个会话(Session col)。我当前的查询返回以下内容:

EVENTID     EVENT      Session    SessionOpened           SessionLength   NumberofTix
1619        BB King     11         2013-01-28 09:34:16.300   -2            2
1622        BB King     12         2013-01-28 09:36:55.073    0            1
1622        BB King     12         2013-01-28 09:36:55.077    0            3

但是,它应该如下所示:

EVENTID     EVENT      Session    SessionOpened           SessionLength   NumberofTix
1619        BB King     11         2013-01-28 09:34:16.300   -2            2
1622        BB King     12         2013-01-28 09:36:55.073    0            4

为什么第 12 次会议返回 2 行——1 行有 1 张票,1 行有 3 张票?

查询:

SELECT distinct set_eSRO_Event as EventID, shName as EventName, setSRO_UID as SRO_SessionID, setDateTime as SessionStarted, 
DATEDIFF(mi, getDate(), setDateTime) as SessionTimer, Count(*) AS NumberofTickets FROM SessionTickets
INNER JOIN Shows on shCode = setShowCode
WHERE setStatus = 0
GROUP BY setsro_UID, shName, set_eSRO_Event, setDateTime
4

2 回答 2

1

您的结果的列和查询的别名不连贯,很难说。我想 setDateTime 是不同的(= SessionStarted 或 SessionOpened)。当您按 setDatetime 分组时,如果它们不同 => 两行。

因此,向 setDatetime 添加一个MIN聚合函数(如您的“想要的”结果),并从 GROUP BY 中删除 setDatetime

SELECT set_eSRO_Event as EventID, 
shName as EventName, 
setSRO_UID as SRO_SessionID, 
MIN(setDateTime) as SessionStarted, 
DATEDIFF(mi, getDate(), MIN(setDateTime)) as SessionTimer, 
COUNT(*) AS NumberofTickets 
FROM SessionTickets
INNER JOIN Shows on shCode = setShowCode
WHERE setStatus = 0
GROUP BY setsro_UID, shName, set_eSRO_Event
于 2013-01-28T14:59:49.040 回答
1

在不知道 wich 列属于 wich 表的情况下,以及 SQL Server 的版本,您可以尝试以下查询:

SELECT  EventID, 
        EventName, 
        SRO_SessionID, 
        MIN(SessionStarted) SessionStarted, 
        SessionTimer,
        SUM(NumberofTickets) NumberofTickets
FROM (  SELECT  set_eSRO_Event as EventID, 
                shName as EventName, 
                setSRO_UID as SRO_SessionID, 
                setDateTime as SessionStarted, 
                DATEDIFF(mi, getDate(), setDateTime) as SessionTimer, 
                Count(*) AS NumberofTickets 
        FROM SessionTickets 
        INNER JOIN Shows 
            ON shCode = setShowCode
        WHERE setStatus = 0
        GROUP BY set_eSRO_Event, shName, setSRO_UID, setDateTime,
                 DATEDIFF(mi, getDate(), setDateTime)) A
GROUP BY EventID, EventName, SRO_SessionID, SessionTimer
于 2013-01-28T15:00:04.950 回答