SQLSERVER的问题.在线等,追加分..

来源:百度知道 编辑:UC知道 时间:2024/05/31 11:10:09
先有表T1,字段A1(统计),A2(人数),A3(国别),A4(次数),M1,M2,M3,M4,M5,M6,M7,M8,M9,M10,M11,M12
(M1-M12表示12个月不能重复)
我希望查询出来的结果是:
A1 M1 M2 M3 M4 M5 M6 M7 M8 M9 M10 M11 M12
A2
A3
A4
A2,A3,A4的数据对应在M1-M12.
麻烦写下完整的语句啊..

问题我自己已经解决了.谢谢大家了.

你好,我为测试就随便建了个表,在我这可以查出,但不知道是不是你要的效果,你看下create table txt_person
(
pID int primary key identity(1,1),
pmonth varchar(20),--月份
pnum int default 1,--次数
pcountry varchar(20),--国别
pcol int--人数
)

insert into txt_person(pmonth,pcountry, pcol) select pmonth, pcountry,pcol from txt_person --values('中国',2)
insert into txt_person(pmonth,pcountry, pcol) values('六月','美国',2)
select * from txt_person

select pmonth, sum(pnum) as 次数,pcountry,sum(pcol) as 人数 from txt_person group by pmonth,pcountry

用union all实现:

select A1,M1,M2,M3,M4,M5,M6,M7,M8,M9,M10,M11,M12 from T1
union all
select A2,M1,M2,M3,M4,M5,M6,M7,M8,M9,M10,M11,M12 from T1
union all
select A3,M1,M2,M3,M4,M5,M6,M7,M8,M9,M10,M11,M12 from T1
union all
select A4,M1,M2,M3,M4,M5,M6,M7,M8,M9,M10,M11,M12 from T1;

---
以上,希望对你有所帮助。

select A1,count(M1) as M1,......... from table group by A1