我是SQL新手。我正在寻找一个简单的SQL解决方案来组合行/列,其行包含相同的数据,在本例中为邮政编码。例如,数据看起来像这样:
state, county, city, zip, count "CA","ALAMEDA","HAYWARD","94541",5371 "CA","ALAMEDA","HAYWARD","94542",2209 "CA","ALAMEDA","HAYWARD","94544",7179 "CA","ALAMEDA","HAYWARD","94545",4209 "CA","ALAMEDA","CASTRO VALLEY","94546",7213 "CA","ALAMEDA","HAYWARD","94546",37 "CA","ALAMEDA","LIVERMORE","94550",9809 "CA","ALAMEDA","LIVERMORE","94551",6558 "CA","ALAMEDA","CASTRO VALLEY","94552",3121 "CA","ALAMEDA","HAYWARD","94552",12 "CA","ALAMEDA","FREMONT","94555",5392
我想最终将数据看起来像这样:
state, county, city, zip, count "CA","ALAMEDA","HAYWARD","94541",5371 "CA","ALAMEDA","HAYWARD","94542",2209 "CA","ALAMEDA","HAYWARD","94544",7179 "CA","ALAMEDA","HAYWARD","94545",4209 "CA","ALAMEDA","CASTRO VALLEY / HAYWARD","94546",7250 "CA","ALAMEDA","LIVERMORE","94550",9809 "CA","ALAMEDA","LIVERMORE","94551",6558 "CA","ALAMEDA","CASTRO VALLEY HAYWARD","94552",3133 "CA","ALAMEDA","FREMONT","94555",5392
您可以看到,在两行中数据已合并或汇总。对于包含完全相同的邮政编码的行,城市名称(两者)都显示在城市列中,而计数是每行的计数总和。
有没有办法用SQL做到这一点?即使它需要两个不同的SQL语句也没问题。
答案 0 :(得分:2)
假设SQL Server,您可以使用FOR XML
来获得所需的结果。
select distinct t.state,t.county,t.zip,t2.sumcount,
STUFF(
(
SELECT '/' + city AS [text()]
FROM mytable t3
WHERE t.zip = t3.zip
FOR XML PATH('')
), 1, 1, '') AS ColList
from mytable t
join (select zip, sum(count) as sumcount
from mytable
group by zip) t2 on t.zip=t2.zip
还有一些SQL Fiddle。
如果您使用的是MySQL,请查看使用GROUP_CONCAT
:
select distinct t.state,t.county,t.zip,t2.sumcount,
GROUP_CONCAT(t.city) as cities
from mytable t
join (select zip, sum(count) as sumcount
from mytable
group by zip) t2 on t.zip=t2.zip
GROUP BY t.state,t.county,t.zip,t2.sumcount
更多Fiddle。
祝你好运。答案 1 :(得分:0)
我调整了sgeddes的精湛答案,实际得到了计数并避免重复输入,并添加了一个支持脚本,以便您可以测试它。这确实假定SQL Server。
IF EXISTS(SELECT * FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_NAME = 'MYTABLE') BEGIN
drop table MYTABLE;
END;
go
create table MYTABLE
(
state nvarchar(2)
,county nvarchar(100)
,city nvarchar(100)
,zip nvarchar(10)
)
go
insert into MYTABLE(state,county,city,zip) values('CA','ALAMEDA','HAYWARD','94541');
insert into MYTABLE(state,county,city,zip) values('CA','ALAMEDA','HAYWARD','94541');
insert into MYTABLE(state,county,city,zip) values('CA','ALAMEDA','HAYWARD','94544');
insert into MYTABLE(state,county,city,zip) values('CA','ALAMEDA','HAYWARD','94545');
insert into MYTABLE(state,county,city,zip) values('CA','ALAMEDA','CASTRO VALLEY','94546');
insert into MYTABLE(state,county,city,zip) values('CA','ALAMEDA','CASTRO VALLEY','94546');
insert into MYTABLE(state,county,city,zip) values('CA','ALAMEDA','CASTRO VALLEY','94546');
insert into MYTABLE(state,county,city,zip) values('CA','ALAMEDA','CASTRO VALLEY','94546');
insert into MYTABLE(state,county,city,zip) values('CA','ALAMEDA','CASTRO VALLEY','94546');
insert into MYTABLE(state,county,city,zip) values('CA','ALAMEDA','HAYWARD','94546');
insert into MYTABLE(state,county,city,zip) values('CA','ALAMEDA','HAYWARD','94546');
insert into MYTABLE(state,county,city,zip) values('CA','ALAMEDA','HAYWARD','94546');
insert into MYTABLE(state,county,city,zip) values('CA','ALAMEDA','HAYWARD','94546');
insert into MYTABLE(state,county,city,zip) values('CA','ALAMEDA','LIVERMORE','94550');
insert into MYTABLE(state,county,city,zip) values('CA','ALAMEDA','LIVERMORE','94551');
insert into MYTABLE(state,county,city,zip) values('CA','ALAMEDA','CASTRO VALLEY','94552');
insert into MYTABLE(state,county,city,zip) values('CA','ALAMEDA','HAYWARD','94552');
insert into MYTABLE(state,county,city,zip) values('CA','ALAMEDA','FREMONT','94555');
select distinct
t.state
,t.county
,t.zip
,t2.sumcount
,STUFF((
SELECT distinct '/' + city AS [text()]
FROM mytable t3
WHERE t.zip = t3.zip
FOR XML PATH('')
), 1, 1, '') AS ColList
from
mytable t
inner join
(
select zip, sum(count) as sumcount
from
(
select zip,count(*) as count
from mytable
group by zip
) x
group by zip
) t2
on t.zip=t2.zip
输出如下:
state county zip sumcount ColList
CA ALAMEDA 94541 2 HAYWARD
CA ALAMEDA 94544 1 HAYWARD
CA ALAMEDA 94545 1 HAYWARD
CA ALAMEDA 94546 9 CASTRO VALLEY/HAYWARD
CA ALAMEDA 94550 1 LIVERMORE
CA ALAMEDA 94551 1 LIVERMORE
CA ALAMEDA 94552 2 CASTRO VALLEY/HAYWARD
CA ALAMEDA 94555 1 FREMONT