3个连接和where子句在一起

时间:2009-06-26 17:21:02

标签: sql database

我有3张桌子

bl_main (bl_id UNIQUE, bl_area)
bl_details (bl_id UNIQUE, name) 
bl_data(bl_id, month, paper_tons, bottles_tons)

bl_id在最后一个表中不是唯一的。将有多行相同的bl_id

我正在尝试以下列方式检索数据

bl_id | name | bl_area | sum(paper_tons) | sum (bottles_tons) | paper_tons | bottles_tons

sum(paper_tons)应该返回相同bl_id的所有纸张总和,例如1月到12月。

使用以下查询,我能够正确地检索所有数据,除了结果,bl_ids(From bl_data table)有多次出现。

SELECT bl_main.bl_id,name,bl_area,sums.SummedPaper, sums.SummedBottles,paper_tons,bottles_tons
FROM bl_main
JOIN bl_details ON 
    bl_main.bl_id= bl_details.bl_id
left outer JOIN bl_data ON
    bl_data.bl_id= bl_main.bl_id
left outer JOIN (
    SELECT bl_id, SUM(Paper_tons) As SummedPaper, SUM(bottle_tons) As SummedBottles 
FROM bl_data

GROUP by bl_id)总和ON sums.bl_id = bl_main.bl_id

我想只检索bl_ids的唯一值而不重复,它应该包含bl_id,它具有最大月份而不是相同bl_id的所有月份。

例如:

INCORRECT


**0601**    University Hall     75.76   17051   1356    4040    1154    **11**     
**0601**    University Hall     75.76   17051   1356    9190    101     **12**  
**0605**    UIC Student     22.86   3331    14799   0   356   **8**   

CORRECT   
**0601**    University Hall     75.76   17051   1356    9190    101     **12**  
**0605**    UIC Student     22.86   3331    14799   0   356   **8**  

我知道我可以使用

获得最大值
WHERE Month = (SELECT MAX(Month)

但是我应该在查询中添加这个,我应该更改连接定义。 任何帮助都非常感谢,因为我是sql的新手。提前谢谢。

5 个答案:

答案 0 :(得分:1)

您有两个表可能应合并为一个表(bl_mainbl_details)。但是把它放在一边,你需要的是一个自连接子查询来选择具有最大月份的行。类似于以下内容(未经测试):

SELECT bl_main.bl_id, bl_details.name, bl_main.bl_area, sums.sum_paper_tons,
       sums.sum_bottles_tons, maxmonth.paper_tons, maxmonth.bottles_tons
FROM bl_main
INNER JOIN bl_details ON bl_main.bl_id = bl_details.bl_id
LEFT OUTER JOIN (SELECT bl_id, SUM(paper_tons) AS sum_paper_tons, 
                        SUM(bottles_tons) AS sum_bottles_tons
                 FROM bl_data
                 GROUP BY bl_id) sums ON bl_main.bl_id = sums.bl_id
LEFT OUTER JOIN (SELECT bl_id, paper_tons, bottles_tons
                 FROM bl_data data2
                 INNER JOIN (SELECT bl_id, MAX(month) AS max_month
                             FROM bl_data
                             GROUP BY bl_id) m
                    ON m.bl_id = data2.bl_id
                    AND m.max_month = data2.month) maxmonth
    ON bl_main.bl_id = maxmonth.bl_id

答案 1 :(得分:0)

您可以使用以下形式的子查询加入包含月份的表:

Select *
From mytable m
    Inner Join (Select max(Month) as Month, myId
                From mytable
                Group By myId) mnth
        On mnth.myId = m.myId and mnth.Month = m.Month

答案 2 :(得分:0)

你的JOIN条款

left outer JOIN bl_data ON
  bl_data.bl_id= bl_main.bl_id

没有指定使用paper_tons和bottles_tons为您显示的数据选择哪个月。

您可以将该JOIN更新为仅包含最大月份,这应该限制条目,如下所示:

left outer JOIN (SELECT bl_id, MAX(Month) as Month from bl_data GROUP BY bl_id) as Month
  ON Month.bl_id = bl_main.bl_id
left outer JOIN bl_data ON
  bl_data.bl_id = bl_main.bl_id AND bl_data.Month = Month.bl_Month

答案 3 :(得分:0)

我认为这个查询就是你要找的东西

SELECT bl_main.bl_id,name, bl_area, sums.SummedPaper, sums.SummedBottles, paper_tons, bottles_tons
FROM bl_main
    JOIN bl_details ON bl_main.bl_id= bl_details.bl_id
    left outer JOIN bl_data ON    bl_data.bl_id= bl_main.bl_id
    left outer JOIN
    (
        SELECT bl_id, month, SUM(Paper_tons) As SummedPaper, SUM(bottle_tons) As SummedBottles 
        FROM bl_data
        WHERE month in
        (SELECT MAX(month) FROM bl_data GROUP BY bl_id)
        GROUP BY bl_id, month
    ) sums ON sums.bl_id = bl_main.bl_id

答案 4 :(得分:0)

我想在lc给出的答案中添加评论,但我还没有50个声望点。这是一篇文章的链接,我相信这个问题解释了这个问题,并添加了为什么lc给出的解决方案是正确的。

http://www.sqlteam.com/article/how-to-use-group-by-with-distinct-aggregates-and-derived-tables