简体   繁体   English

SQL-查询加入3个表,按特定列分组并按年份过滤?

[英]SQL- Query Join 3 Tables, Group by a certain column and filter by Year?

Question:问题:

What are the total distributions in 2020 from properties that were acquired in 2019 broken down by state?按 state 细分的 2019 年收购的物业在 2020 年的总分配是多少?

So far this is what I have and seems like I am not getting it right to sum up?到目前为止,这就是我所拥有的,似乎我没有得到正确的总结? what step am I missing.我错过了什么步骤。 I am not getting an error but not getting an answer either?我没有收到错误但也没有得到答案? Any ideas?有任何想法吗?

CREATE TABLE Property
(
    hmy INT   NOT NULL,
    scode VARCHAR   NOT NULL,
    saddr1 VARCHAR NOT NULL,
    saddr2 VARCHAR NOT NULL,
    scity VARCHAR NOT NULL,
    sstate VARCHAR NOT NULL,
    szipcode INT NOT NULL,
    PRIMARY KEY (hmy) 
);

CREATE TABLE PropInfo
(
    hmy INT   NOT NULL,
    hcode INT   NOT NULL,
    acquisitiondate DATE   NOT NULL,
    unitcount INT   NOT NULL,
    yearbuilt INT   NOT NULL,
    distributionentitycode VARCHAR   NOT NULL,
    PRIMARY KEY (hcode)
);

CREATE TABLE DistributionLog
(
    hmy INT   NOT NULL,
    hcode INT   NOT NULL,
    distributionDate DATE   NOT NULL,
    distributiontype VARCHAR   NOT NULL,
    distributionamount INT   NOT NULL,
    PRIMARY KEY (hmy)
);

SELECT SUM(DL.distributionamount) AS Total, PY.sstate, PI.acquisitiondate, DL.distributionamount 
FROM DistributionLog AS DL
JOIN PropInfo AS PI
ON PI.hmy = DL.hcode
JOIN Property AS PY
ON PY.hmy = PI.hmy
WHERE distributionDate BETWEEN '2020-1-1' AND '2020-12-31'
GROUP BY PY.sstate, PI.acquisitiondate, DL.distributionamount
ORDER BY PY.sstate;

Here's what was tried:这是尝试过的:

在此处输入图像描述

You can try something like this:你可以尝试这样的事情:

select p.sstate, sum(distributionamount)
from distributionlog d
inner join propinfo pi
  on d.hcode = pi.hcode
  and extract(year from acquisitiondate) = 2019
  and extract(year from distributiondate) = 2020
inner join property p on pi.hmy = p.hmy
group by p.sstate

In this SQL, we are joining the 3 tables as you have been doing also.在这个 SQL 中,我们将加入 3 个表格,就像您一直在做的那样。 We are specifying during the join to ensure that acquisition date should be in 2019 and distribution date should be from 2020.我们在加入期间指定,以确保收购日期应在 2019 年,分发日期应从 2020 年开始。

Since you want the sum(DL.distributionamount) , DL.distributionamount should not be in group by clause.由于您想要sum(DL.distributionamount)因此 DL.distributionamount不应在 group by 子句中。 Below query should return you sstate wise sum(DL.distributionamount) for the properties that have been acquired in 2019 and distributed in 2020.(relation between tables is not clear to me without sample data. So I am assuming that you did the joining right)下面的查询应该返回您在 2019 年获得并在 2020 年分发的属性的状态明智总和(DL.distributionamount)。(没有样本数据,我不清楚表之间的关系。所以我假设你做了加入权)

SELECT  PY.sstate, SUM(DL.distributionamount) AS Total
FROM DistributionLog AS DL
JOIN PropInfo AS PI
ON PI.hmy = DL.hcode and PI.acquisitiondate BETWEEN '2019-1-1' AND '2019-12-31'
JOIN Property AS PY
ON PY.hmy = PI.hmy
WHERE distributionDate BETWEEN '2020-1-1' AND '2020-12-31'
GROUP BY PY.sstate
ORDER BY PY.sstate;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM