求1条SQL SERVER的语句 在线等!!!!

wjfxyj 2008-03-03 12:56:14
求1条SQL SERVER的语句
我有一表如下

dm mc sl dj zj
001 aaaaa 3 10.00 30.00
002 bbbbb 2 20.00 40.00
003 ccccc 4 30.00 120.00
001 aaaaa 2 10.00 20.00
002 bbbbb 1 20.00 20.00
003 ccccc 3 30.00 90.00
001 aaaaa 2 15.00 30.00
002 bbbbb 4 22.00 88.00
003 ccccc 2 30.00 60.00
......
......

dm 001 mc aaaaa dj 从10.00上涨到15.00
dm 002 mc bbbbb dj 从20.00上涨到22.00
......
......
我想得到一个新表,反映涨价情况

dm mc dj
001 aaaaa 10.00
001 aaaaa 15.00
002 bbbbb 20.00
002 bbbbb 22.00
......
......

1、不涨价的不需要
2、可能一个周期内同一dm,mc的物品超过一次涨价,都能一一显示。

...全文
67 点赞 收藏 5
写回复
5 条回复
切换为时间正序
当前发帖距今超过3年,不再开放新的回复
发表回复
wjpath23 2008-03-03
create TABLE mytable (dm CHAR(20),mc CHAR(20),sl INT,dj FLOAT,zj FLOAT)
INSERT mytable SELECT '001','aaaaa',3,10.00,30.00
UNION ALL SELECT '002','bbbbb',2,20.00,40.00
UNION ALL SELECT '003','ccccc',4,30.00,120.00
UNION ALL SELECT '001','aaaaa',2,10.00,20.00
UNION ALL SELECT '002','bbbbb',1,20.00,20.00
UNION ALL SELECT '003','ccccc',3,30.00,90.00
UNION ALL SELECT '001','aaaaa',2,15.00,30.00
UNION ALL SELECT '002','bbbbb',4,22.00,88.00
UNION ALL SELECT '003','ccccc',2,30.00,60.00
-------------
select *
from mytable

SELECT * into newtable FROM (SELECT DISTINCT dm,mc,dj FROM mytable) a
WHERE (SELECT count(dm) FROM (SELECT DISTINCT dm,mc,dj FROM mytable) aa WHERE dm=a.dm AND mc=a.mc)>1
-------------
select *
from newtable
-------------
001 aaaaa 10
001 aaaaa 15
002 bbbbb 20
002 bbbbb 22
回复
山之魂2 2008-03-03
select dm,mc ,dj
from tb where dm in
(select dm from tb where count(dm)> 1 group by dm)
order by dm, mc,dj
回复
QQQQAnnie 2008-03-03
你判断商品涨价的条件是mc 还是dj 还是什么啊
回复
威尔亨特 2008-03-03
select dm,mc ,dj
from tb where dm in (select dm from tb where count(dm)>1 group by dm)
order by dm, mc,dj
回复
chuifengde 2008-03-03
DECLARE @a TABLE(dm VARCHAR(20),mc VARCHAR(20),sl INT,dj FLOAT,zj FLOAT)
INSERT @a SELECT '001','aaaaa',3,10.00,30.00
UNION ALL SELECT '002','bbbbb',2,20.00,40.00
UNION ALL SELECT '003','ccccc',4,30.00,120.00
UNION ALL SELECT '001','aaaaa',2,10.00,20.00
UNION ALL SELECT '002','bbbbb',1,20.00,20.00
UNION ALL SELECT '003','ccccc',3,30.00,90.00
UNION ALL SELECT '001','aaaaa',2,15.00,30.00
UNION ALL SELECT '002','bbbbb',4,22.00,88.00
UNION ALL SELECT '003','ccccc',2,30.00,60.00

SELECT * FROM (SELECT DISTINCT dm,mc,dj FROM @a) a
WHERE (SELECT count(1) FROM (SELECT DISTINCT dm,mc,dj FROM @a) aa WHERE dm=a.dm AND mc=a.mc)>1

--result
/*dm mc dj
-------------------- -------------------- -----------
001 aaaaa 10.0
001 aaaaa 15.0
002 bbbbb 20.0
002 bbbbb 22.0

(所影响的行数为 4 行)
*/
回复
相关推荐
发帖
MS-SQL Server
创建于2007-09-28

3.3w+

社区成员

MS-SQL Server相关内容讨论专区
申请成为版主
帖子事件
创建了帖子
2008-03-03 12:56
社区公告
暂无公告