SQL Server:加入两个表和最后一列计数总计(按​​年份,zip)

发布于 2025-02-12 10:45:56 字数 1960 浏览 0 评论 0 原文

我有一个 housing 和我的SQL Server数据库中的 rimel_reports 表。我试图计算房屋邮政编码中犯罪的总出现,并带有一个查询。

这些表中有很多值,但是以下是我加入ZIP和Year的匹配的值(每个表之间的“唯一” ID都是完全不同的,以下只是一个示例)。

我的住房表示例数据:

UID Zipcode Saleyear Soldprice
1 12345 2018 2018 300000
2 23345 2019 200000 200000
3 56777 2018 2018 500000
4 65789 2021 350000

犯罪 我的 MY >
1 12345 2018
2 23345 2019
3 56777 2018
4 65789 2020

我想创建一个视图表(从单个查询),犯罪发生在那年发生的房屋邮政编码中计算犯罪:

UID Zipcode Saleyear Saleyear Soldprice 犯罪发生在ZIP
1 12345 2018 2018 300000 23223345
2019 2019 200000 200000 200000 4
3 3 3 3 56777 2018 500000 50
4 65789 2021 350000 2

我尝试了以下内容,但这不是工作:

SELECT 
    h.uID, h.zipcode, h.saleyear, 
    COUNT(c.IncidentID) AS Crime Occurrences At Zip
FROM 
    Housing h
INNER JOIN 
    Crime_Reports c ON h.zipcode = c.zipcode
                    AND h.saleyear = c.occurredyear
GROUP BY 
    1
ORDER BY 
    1

编辑

测试并发布了下面的答案

I have a Housing and a Crime_Reports table in my SQL Server database. I am trying to count the total occurrences of crimes in the zipcode of a house was sold in with a single query.

There are many values in these tables, but the below is what I am joining on and matching by zip and year (the 'unique' ids between each table are totally different, the below is just an example).

My Housing table sample data:

uID zipcode saleyear soldprice
1 12345 2018 300000
2 23345 2019 200000
3 56777 2018 500000
4 65789 2021 350000

My Crime Reports table sample data:

IncidentID zipcode occurredyear
1 12345 2018
2 23345 2019
3 56777 2018
4 65789 2020

I want to create a view table (from single query) with the crime occurrences counting the crimes at the house zip code that occurred that year:

uID zipcode saleyear soldprice Crime Occurrences At Zip
1 12345 2018 300000 23
2 23345 2019 200000 4
3 56777 2018 500000 50
4 65789 2021 350000 2

I tried the following, but this did not work:

SELECT 
    h.uID, h.zipcode, h.saleyear, 
    COUNT(c.IncidentID) AS Crime Occurrences At Zip
FROM 
    Housing h
INNER JOIN 
    Crime_Reports c ON h.zipcode = c.zipcode
                    AND h.saleyear = c.occurredyear
GROUP BY 
    1
ORDER BY 
    1

EDIT

Answers tested and posted below

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

謌踐踏愛綪 2025-02-19 10:45:56

更新答案

解决方案1 ​​多亏了@Charlieface(问题是使用1或1,2,3在该条款中)

 SELECT h.uID, h.zipcode, h.saleyear, COUNT(c.IncidentID) AS Crime Occurrences At Zip
   FROM Housing h
   INNER JOIN Crime_Reports c
            ON h.zipcode = c.zipcode
            AND h.saleyear = c.occurredyear
 GROUP BY h.uID, h.zipcode, h.saleyear
 ORDER BY h.uID, h.zipcode, h.saleyear

解决方案2 多亏了 @艾登或

WITH CrimesCTE AS (
    SELECT COUNT(IncidentID) AS CrimeOccurrencesAtZip, occurredyear, zipcode 
    FROM Crime_Reports
    GROUP BY occurredyear, zipcode
)
    
SELECT * 
FROM 
    (SELECT * FROM Housing) h
    LEFT JOIN 
    (SELECT CrimeOccurrencesAtZip, occurredyear, zipcode FROM CrimesCTE) c
        ON h.zipcode = c.zipcode 
        AND h.saleyear = c.occurredyear

UPDATE ANSWERS

SOLUTION 1 Thanks to @Charlieface (The problem was using 1 or 1,2,3 in BY Clauses)

 SELECT h.uID, h.zipcode, h.saleyear, COUNT(c.IncidentID) AS Crime Occurrences At Zip
   FROM Housing h
   INNER JOIN Crime_Reports c
            ON h.zipcode = c.zipcode
            AND h.saleyear = c.occurredyear
 GROUP BY h.uID, h.zipcode, h.saleyear
 ORDER BY h.uID, h.zipcode, h.saleyear

SOLUTION 2 Thanks to @Aiden Or

WITH CrimesCTE AS (
    SELECT COUNT(IncidentID) AS CrimeOccurrencesAtZip, occurredyear, zipcode 
    FROM Crime_Reports
    GROUP BY occurredyear, zipcode
)
    
SELECT * 
FROM 
    (SELECT * FROM Housing) h
    LEFT JOIN 
    (SELECT CrimeOccurrencesAtZip, occurredyear, zipcode FROM CrimesCTE) c
        ON h.zipcode = c.zipcode 
        AND h.saleyear = c.occurredyear
鸢与 2025-02-19 10:45:56

我认为您可以在这种情况下使用常见的表格表达式(CTE):

 With CTE AS (
    SELECT 
    COUNT(IncidentID) AS Crime Occurrences, 
    occurredyear, 
    zipcode 
    FROM Crime_Reports 
    GROUP BY occurredyear, zipcode)
    SELECT * 
    FROM ( 
        SELECT * FROM Housing
    ) tb1 
    LEFT JOIN (
        SELECT  
            Crime Occurrences,
            occurredyear, 
            zipcode 
        FROM CTE
   ) ON tb1.zipcode = tb2.zipcode 
        AND tb1.saleyear = tb2.occurredyear

I think you can use Common Table Expressions (CTE) in this circumstances :

SQL Server Common Table Expressions (CTE)

 With CTE AS (
    SELECT 
    COUNT(IncidentID) AS Crime Occurrences, 
    occurredyear, 
    zipcode 
    FROM Crime_Reports 
    GROUP BY occurredyear, zipcode)
    SELECT * 
    FROM ( 
        SELECT * FROM Housing
    ) tb1 
    LEFT JOIN (
        SELECT  
            Crime Occurrences,
            occurredyear, 
            zipcode 
        FROM CTE
   ) ON tb1.zipcode = tb2.zipcode 
        AND tb1.saleyear = tb2.occurredyear
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文