加入问题,输出错误

时间:2014-04-29 14:27:52

标签: sql mysqli

我想计算每个AudiEvent的总销售额,包括没有销售的活动。销售额将按BasePrice * PriceMultiplier计算。因此,分组将基于EventName,并且应该获得总销售额。

SELECT  AudiEvent.EventName, 
        AudiEvent.EventDate, 
        AudiEvent.StartTime,
        SUM(Cast(AuditoriumRows.PriceMultiplier*AudiEvent.BasePrice as DECIMAL(4,1))) as "Total Sales"
FROM    AudiEvent
LEFT OUTER JOIN Reserver 
    ON AudiEvent.AuditoriumID=Reserver.AuditoriumID
INNER JOIN AuditoriumRows 
    ON Reserver.AuditoriumID=AuditoriumRows.AuditoriumID
INNER JOIN Auditorium 
    ON Reserver.AuditoriumID=Auditorium.AuditoriumID
GROUP BY AudiEvent.EventName;

OUTPUT -

EventName       EventDate  StartTime Total Sales
Big Bash        29/6/2014  20:00      24480
County Cricket  06/07/2014 11:00       0
IPL             05/05/2014 03:00       0

INPUT -

礼堂

AudiID  AudiName 
Audi01  London Theatre 
Audi02  Brentwood Hall 
Audi03  County Hall

AudiEvent

EventID AudiID EventName       BasePrice
1    Audi01 Big Bash          300
2    Audi01 County Cricket    400
3    Audi01 Big Bash          300
4    Audi01 Big Bash          300
5    Audi01 County Cricket    300
6    Audi01 Big Bash          300
7    Audi01 Big Bash          310
8    Audi01 County Cricket    400
9    Audi01 County Cricket    400
10   Audi01 County Cricket    400
11   Audi02 County Cricket    500
12   Audi02 County Cricket    520
13   Audi02 County Cricket    500
14   Audi02 County Cricket    500
15   Audi02 County Cricket    500
16   Audi03 IPL               1000
17   Audi03 IPL               1000
18   Audi03 IPL               1000
19   Audi03 Big Bash          350
20   Audi03 Big Bash          350

AuditoriumRows

AudiID   PriceMultiplier SeatRows
Audi01      1.0          8
Audi01      1.5          7
Audi01      2.0          6
Audi01      2.5          5
Audi01      3.0          4
Audi01      3.5          3
Audi01      4.0          2
Audi01      4.5          1
Audi01      5.0          0
Audi02      1.0          8
Audi02      1.5          7
Audi02      2.0          6
Audi02      2.5          5
Audi02      3.0          4
Audi02      3.5          3
Audi02      4.0          2
Audi02      4.5          1
Audi02      5.0          0
Audi03      1.0          8
Audi03      1.5          7
Audi03      2.0          6
Audi03      2.5          5
Audi03      3.0          4
Audi03      3.5          3
Audi03      4.0          2
Audi03      4.5          1
Audi03      5.0          0

储池

AudiID EventID SeatNumber
Audi01  1           7
Audi01  3           2
Audi01  4           4
Audi01  5           0
Audi01  6           1
Audi01  7           6
Audi01  8           3
Audi01  10          7
Audi01  2           0
Audi01  3           0
Audi01  5           1
Audi01  7           3
Audi01  1           4
Audi01  1           8
Audi01  1           4
Audi03  19          0
Audi03  20          1
Audi03  19          2
Audi03  20          3        

我在这里没有得到正确的总数,也没有得到总销售额为0的其他2个事件。我不确定这里有什么问题。

2 个答案:

答案 0 :(得分:0)

除非你知道自己在做什么,否则LEFT JOINS上的INNER JOINING会给你意想不到的结果。我已将它们全部更改为左连接,并在group by语句中添加了一些字段以使该语句有效。不完全确定这对你有帮助吗??

这为您提供每个单独事件的总销售额(即按日期和开始时间)。

SELECT AudiEvent.EventName, 
       AudiEvent.EventDate, 
       AudiEvent.StartTime, 
       SUM(Cast(AuditoriumRows.PriceMultiplier*AudiEvent.BasePrice as DECIMAL(4,1))) as "Total Sales" 

FROM AudiEvent 
LEFT JOIN Reserver ON AudiEvent.AuditoriumID=Reserver.AuditoriumID 
LEFT JOIN AuditoriumRows ON Reserver.AuditoriumID=AuditoriumRows.AuditoriumID 
LEFT JOIN Auditorium ON Reserver.AuditoriumID=Auditorium.AuditoriumID 
GROUP BY AudiEvent.EventName, AudiEvent.EventDate, AudiEvent.StartTime;

这为您提供每个单独活动的总销售额。

SELECT AudiEvent.EventName 
       SUM(Cast(AuditoriumRows.PriceMultiplier*AudiEvent.BasePrice as DECIMAL(4,1))) as "Total Sales" 

FROM AudiEvent 
LEFT JOIN Reserver ON AudiEvent.AuditoriumID=Reserver.AuditoriumID 
LEFT JOIN AuditoriumRows ON Reserver.AuditoriumID=AuditoriumRows.AuditoriumID 
LEFT JOIN Auditorium ON Reserver.AuditoriumID=Auditorium.AuditoriumID 
GROUP BY AudiEvent.EventName;

答案 1 :(得分:0)

试试这个:

SELECT  AudiEvent.EventName, 
        MAX(AudiEvent.EventDate), 
        MAX(AudiEvent.StartTime),
        COALESCE(SUM(Cast(AuditoriumRows.PriceMultiplier*AudiEvent.BasePrice as DECIMAL(4,1))), 0) as "Total Sales"
        from AudiEvent
        LEFT OUTER JOIN Reserver 
        ON AudiEvent.AuditoriumID=Reserver.AudiID
        AND Reserver.EventID = AudiEvent.EventID
        INNER JOIN AuditoriumRows 
        ON Reserver.AudiID=AuditoriumRows.AudiID
        AND Reserver.SeatNumber=AuditoriumRows.SeatRows
        INNER JOIN Auditorium 
        ON Auditorium.AuditoriumID = AudiEvent.AudiID
        GROUP BY AudiEvent.EventName;

我添加了额外的加入条件(Reserver.EventID = AudiEvent.EventID)和(Reserver.SeatNumber=AuditoriumRows.AudiID)。