Sql 上一可用年度的联接表中的组信息
因此,我有一个具有唯一标识符及其组的表;我想根据另一个表的组得到它的总数 表1 UNIQUE_ID ! Group 1 West 2 West 3 West 4 West 5 West 6 East 7 East 独一无二!团体 1西 2西 3西 4西 5西 6东区 7东 然后我有了第二个表,我加入了它 表2 UNIQUE_ID ! NET PROFIT ! ASSETS ! EQUITY ! YEAR 1 100 100 100 2016 1 100 100 100 2015 2 100 100 100 2016 2 100 100 100 2015 3 100 100 100 2016 3 100 100 100 2015 ***4 10 10 10 2015*** 5 100 100 100 2016 5 100 100 100 2015 ***6 10 10 10 2014*** 7 100 100 100 2016 7 100 100 100 2015 7 100 100 100 2014 独一无二!净利润!资产!公平!年 1 100 100 100 2016 1 100 100 100 2015 2 100 100 100 2016 2 100 100 100 2015 3 100 100 100 2016 3 100 100 100 2015 ***4 10 10 10 2015*** 5 100 100 100 2016 5 100 100 100 2015 ***6 10 10 10 2014*** 7 100 100 100 2016 7 100 100 100 2015 7 100 100 100 2014 我将前面的表格链接起来,然后一组一组地给我提供净利润、资产和权益的总计,问题是它汇总了表2中的所有可用年份,或者我做了一个计算,其中年份是2016年,我只得到2016年的总计,不包括最新年份为2015年或2014年的行 我需要它按组分组,只对每个唯一ID的上一个可用年份求和,以便得到下表 你能给我这张桌子吗 Group ! NET PROFIT ! ASSETS ! EQUITY East 410 410 410 West 110 110 110 组!净利润!资产!衡平法 东410 410 410 西110 有人能帮我吗?我到处都找过,试过很多组合,但都没有成功,这就是你想要的吗Sql 上一可用年度的联接表中的组信息,sql,ms-access,Sql,Ms Access,因此,我有一个具有唯一标识符及其组的表;我想根据另一个表的组得到它的总数 表1 UNIQUE_ID ! Group 1 West 2 West 3 West 4 West 5 West 6 East 7 East 独一无二!团体 1西 2西 3西 4西 5西 6东区 7东 然后我有了第二个表,我加入了它 表2 UNIQUE_ID ! NET P
SELECT Group_,
SUM(NET_PROFIT) AS NET_PROFIT_YR,
SUM(ASSETS) AS ASSETS_YR,
SUM(EQUITY) As EQUITY_YR
FROM Table1 AS T1
INNER
JOIN (SELECT T2_RAW.*
FROM ( SELECT Unique_ID,
MAX(year) AS m_year
FROM Table2 AS T2
GROUP
BY Unique_ID
) AS MYR
INNER
JOIN Table2 AS T2_RAW
ON MYR.unique_id = T2_RAW.unique_id
AND MYR.m_year = T2_RAW.year
) AS TMP
ON T1.unique_id = TMP.unique_id
GROUP
BY group_;
这是你想要的吗
SELECT Group_,
SUM(NET_PROFIT) AS NET_PROFIT_YR,
SUM(ASSETS) AS ASSETS_YR,
SUM(EQUITY) As EQUITY_YR
FROM Table1 AS T1
INNER
JOIN (SELECT T2_RAW.*
FROM ( SELECT Unique_ID,
MAX(year) AS m_year
FROM Table2 AS T2
GROUP
BY Unique_ID
) AS MYR
INNER
JOIN Table2 AS T2_RAW
ON MYR.unique_id = T2_RAW.unique_id
AND MYR.m_year = T2_RAW.year
) AS TMP
ON T1.unique_id = TMP.unique_id
GROUP
BY group_;
I.完整的SQL语句:
二,。说明:
内部查询:
SELECT
unique_id,
max(year) as maxYear
FROM table_b
GROUP BY unique_id
SELECT
ta.group,
SUM(IFNULL(tb.net_profit, 0)) as sumNetProfit,
SUM(IFNULL(tb.assets, 0)) as sumAssets,
SUM(IFNULL(tb.equity, 0)) as sumEquity
FROM table_a AS ta
LEFT JOIN (
<THE-INNER-QUERY-RESULTS>
) AS tbMaxYears ON tbMaxYears.unique_id = ta.unique_id
LEFT JOIN table_b AS tb ON
tb.unique_id = ta.unique_id
AND tb.year = tbMaxYears.maxYear
GROUP BY ta.group;
LEFT JOIN table_b AS tb ON
tb.unique_id = ta.unique_id
AND tb.year = tbMaxYears.maxYear
- 从
中选择表b
s,并为每个唯一id
选择相应的最新年份,例如最大年份李>唯一id
- 按
对获取的记录进行分组李>唯一_id
- 在别名
下的maxYears
语句中使用,其结果由从LEFT JOIN
获取的表a
进行过滤唯一\u id
unique_id maxYear
-------------------
1 2016
2 2016
3 2016
4 2015
5 2016
6 2014
7 2016
外部查询:
SELECT
unique_id,
max(year) as maxYear
FROM table_b
GROUP BY unique_id
SELECT
ta.group,
SUM(IFNULL(tb.net_profit, 0)) as sumNetProfit,
SUM(IFNULL(tb.assets, 0)) as sumAssets,
SUM(IFNULL(tb.equity, 0)) as sumEquity
FROM table_a AS ta
LEFT JOIN (
<THE-INNER-QUERY-RESULTS>
) AS tbMaxYears ON tbMaxYears.unique_id = ta.unique_id
LEFT JOIN table_b AS tb ON
tb.unique_id = ta.unique_id
AND tb.year = tbMaxYears.maxYear
GROUP BY ta.group;
LEFT JOIN table_b AS tb ON
tb.unique_id = ta.unique_id
AND tb.year = tbMaxYears.maxYear
外部查询中的第二个左联接:
SELECT
unique_id,
max(year) as maxYear
FROM table_b
GROUP BY unique_id
SELECT
ta.group,
SUM(IFNULL(tb.net_profit, 0)) as sumNetProfit,
SUM(IFNULL(tb.assets, 0)) as sumAssets,
SUM(IFNULL(tb.equity, 0)) as sumEquity
FROM table_a AS ta
LEFT JOIN (
<THE-INNER-QUERY-RESULTS>
) AS tbMaxYears ON tbMaxYears.unique_id = ta.unique_id
LEFT JOIN table_b AS tb ON
tb.unique_id = ta.unique_id
AND tb.year = tbMaxYears.maxYear
GROUP BY ta.group;
LEFT JOIN table_b AS tb ON
tb.unique_id = ta.unique_id
AND tb.year = tbMaxYears.maxYear
- 将
详细信息附加(连接)到从表_b
获取的记录李>表_a
- 仅附加
记录,这些记录与表b
中相应的表a
值具有相同的唯一id
,并且(!)与唯一id
表中相应的最大年份
值具有相同的最大年份
年
CREATE TABLE `table_a` (
`unique_id` int(11) DEFAULT NULL,
`group` varchar(255) DEFAULT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
CREATE TABLE `table_b` (
`unique_id` int(11) DEFAULT NULL,
`net_profit` int(11) DEFAULT NULL,
`assets` int(11) DEFAULT NULL,
`equity` int(11) DEFAULT NULL,
`year` int(11) DEFAULT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
我使用了和你一样的数据
祝你好运 I.完整的SQL语句:
二,。说明:
内部查询:
SELECT
unique_id,
max(year) as maxYear
FROM table_b
GROUP BY unique_id
SELECT
ta.group,
SUM(IFNULL(tb.net_profit, 0)) as sumNetProfit,
SUM(IFNULL(tb.assets, 0)) as sumAssets,
SUM(IFNULL(tb.equity, 0)) as sumEquity
FROM table_a AS ta
LEFT JOIN (
<THE-INNER-QUERY-RESULTS>
) AS tbMaxYears ON tbMaxYears.unique_id = ta.unique_id
LEFT JOIN table_b AS tb ON
tb.unique_id = ta.unique_id
AND tb.year = tbMaxYears.maxYear
GROUP BY ta.group;
LEFT JOIN table_b AS tb ON
tb.unique_id = ta.unique_id
AND tb.year = tbMaxYears.maxYear
- 从
中选择表b
s,并为每个唯一id
选择相应的最新年份,例如最大年份李>唯一id
- 按
对获取的记录进行分组李>唯一_id
- 在别名
下的maxYears
语句中使用,其结果由从LEFT JOIN
获取的表a
进行过滤唯一\u id
unique_id maxYear
-------------------
1 2016
2 2016
3 2016
4 2015
5 2016
6 2014
7 2016
外部查询:
SELECT
unique_id,
max(year) as maxYear
FROM table_b
GROUP BY unique_id
SELECT
ta.group,
SUM(IFNULL(tb.net_profit, 0)) as sumNetProfit,
SUM(IFNULL(tb.assets, 0)) as sumAssets,
SUM(IFNULL(tb.equity, 0)) as sumEquity
FROM table_a AS ta
LEFT JOIN (
<THE-INNER-QUERY-RESULTS>
) AS tbMaxYears ON tbMaxYears.unique_id = ta.unique_id
LEFT JOIN table_b AS tb ON
tb.unique_id = ta.unique_id
AND tb.year = tbMaxYears.maxYear
GROUP BY ta.group;
LEFT JOIN table_b AS tb ON
tb.unique_id = ta.unique_id
AND tb.year = tbMaxYears.maxYear
外部查询中的第二个左联接:
SELECT
unique_id,
max(year) as maxYear
FROM table_b
GROUP BY unique_id
SELECT
ta.group,
SUM(IFNULL(tb.net_profit, 0)) as sumNetProfit,
SUM(IFNULL(tb.assets, 0)) as sumAssets,
SUM(IFNULL(tb.equity, 0)) as sumEquity
FROM table_a AS ta
LEFT JOIN (
<THE-INNER-QUERY-RESULTS>
) AS tbMaxYears ON tbMaxYears.unique_id = ta.unique_id
LEFT JOIN table_b AS tb ON
tb.unique_id = ta.unique_id
AND tb.year = tbMaxYears.maxYear
GROUP BY ta.group;
LEFT JOIN table_b AS tb ON
tb.unique_id = ta.unique_id
AND tb.year = tbMaxYears.maxYear
- 将
详细信息附加(连接)到从表_b
获取的记录李>表_a
- 仅附加
记录,这些记录与表b
中相应的表a
值具有相同的唯一id
,并且(!)与唯一id
表中相应的最大年份
值具有相同的最大年份
年
CREATE TABLE `table_a` (
`unique_id` int(11) DEFAULT NULL,
`group` varchar(255) DEFAULT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
CREATE TABLE `table_b` (
`unique_id` int(11) DEFAULT NULL,
`net_profit` int(11) DEFAULT NULL,
`assets` int(11) DEFAULT NULL,
`equity` int(11) DEFAULT NULL,
`year` int(11) DEFAULT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
我使用了和你一样的数据
祝你好运 考虑一个聚合派生表与其他两个表的联接,其中最后一个
内部联接
实质上充当一个where
子句,用于筛选每个唯一id的选择年份
请注意用于包装MS Access中所需的表的第一个联接对的括号。考虑将聚合派生表联接到其他两个表的联接,其中最后一个
内部联接本质上充当where
子句,以筛选每个唯一id的选择年份
请注意用于包装MS Access中所需的表的第一个联接对的括号。首先找出您正在使用的RDBMS。然后,请参阅MicrosoftAccessFirst了解您正在使用的RDBMS。然后,看看微软AccessLove你的答案,超级详细!现在我明白了为什么它不起作用,我应该如何询问它谢谢你,我很感激。喜欢你的回答,非常详细!现在我明白了为什么它不起作用,我应该如何质疑它谢谢你,我很感激。