不同的记录数量合并,相同的记录数量取其一 SQL语句怎么写
SELECT ML009,SUM(ML005) AS 超入数量 FROM INVML GROUP BY ML009
--如果ML009,ML005 相同那么 数量等于ML005任何一笔,如果ML009不同,那么数量等于SUM(ML005)
不同的记录数量合并,相同的记录数量取其一 ,我自己琢磨语句如下
SELECT A.ML009,SUM(超入数量) FROM
(SELECT distinct ML009,超入数量 FROM XXX ) A GROUP BY A.ML009
还有没有更好的例子,大家推荐一下,谢谢.
[解决办法]
if OBJECT_ID('INVML') is not null drop table INVML
create table INVML
(
ML009 int,
ML005 int
)
insert into INVML
select 22,24 union
select 50,50 union
select 50,50 union
select 10,22 union
select 20,20 union
select 22,23 union
select 22,24 union
select 21,24 union
select 30,21
SELECT ML009,SUM(ML005) AS 超入数量 FROM
(
select ML009,ML005 from INVML where ML009<>ML005
union all
select distinct ML009,ML005 from INVML where ML009=ML005
) A GROUP BY ML009
if OBJECT_ID('INVML') is not null drop table INVML
create table INVML
(
ML009 int,
ML005 int
)
insert into INVML
select 22,24 union
select 50,50 union
select 50,50 union
select 10,22 union
select 20,20 union
select 22,23 union
select 22,24 union
select 21,24 union
select 30,21
SELECT ML009,SUM(ML005) AS 超入数量 FROM
(
select ML009,ML005 from INVML where ML009<>ML005
union all
select top 1 ML009,ML005 from INVML where ML009=ML005 order by NEWID() --随机一个
) A GROUP BY ML009