求一条或一组有点难度的MS SQL语句
A表各字段记录如下(称入货表)
id code numb numbname type datas mons
1 IN07010009 G001 天和可乐 月结 2007/01/09 900
2 IN07010010 G003 天虹制衣 月结 2007/01/09 448
3 IN07010011 G003 天虹制衣 月结 2007/01/08 22
4 IN07010012 G003 天虹制衣 现金 2007/01/10 36
5 IN07010013 G003 天虹制衣 现金 2007/01/09 92
B表各字段记录如下(称入货付款表)
id code datas numb numbname mons
1 IN07010009 2007/01/09 G001 天和可乐 900
2 IN07010010 2007/01/09 G003 天虹制衣 200
3 IN07010010 2007/01/09 G003 天虹制衣 248
4 IN07010011 2007/01/08 G003 天虹制衣 22
表说明:
1、以A表为主表分离出相同供应商只列出一个名称
2、所要的结果如下排列,结果中,其实空白处为NULL的,为使看得清楚固把NULL去掉了
3、特别注意“天虹制衣”的排列结果,即同一供应商下的记录是要按日期由小到大排列的
4、特别注意“天虹制衣”的排列结果如下三行
2007/01/09 448.00 月结 IN07010010 --> A表的记录
NULL 200.00 NULL IN07010010 --> 此记录在B表按id小到大排列
NULL 248.00 NULL IN07010010
如下为想要的结果
G001 天和可乐
2007/01/09 900.00 月结 IN07010009 --> A表的记录
NULL 900.00 NULL IN07010009 --> B表的记录
G003 天虹制衣
2007/01/08 22.00 月结 IN07010011
NULL 22.00 NULL IN07010011
2007/01/09 448.00 月结 IN07010010 --> A表的记录
NULL 200.00 NULL IN07010010 --> B表的记录
NULL 248.00 NULL IN07010010 --> B表的记录
2007/01/09 92.00 现金 IN07010013
2007/01/10 36.00 现金 IN07010012
目前我用 case when 加上 select top 100 percent 方法可以得到大至结果,但是上面“表说明”中的3和4点的排序就怎也搞不定,在此求一条或一组SQL语句(不知不用select top 100 percent这类的方法还会不会有更好的写法),望大家出手(由于有点复杂,大家请看清楚些,谢谢)
[解决办法]
关注
[解决办法]
只能order by case when 字段满足某个条件 then 1 esle 0 end,case when……
这样拼一下试试喽,如果还不行就给每条记录加个标识列,标识它是
G001 天和可乐、还是A表的记录还是B表的记录,再用case when 拼一下。
[解决办法]
create table GetGoods(id int,code varchar(20),numb varchar(10),numbname varchar(50),type varchar(20),datas datetime,mons money)
insert GetGoods select 1, ' IN07010009 ', 'G001 ', '天和可乐 ', '月结 ', '2007/01/09 ',900
union all select 2, ' IN07010010 ', 'G003 ', '天虹制衣 ', '月结 ', '2007/01/09 ',448
union all select 3, ' IN07010011 ', 'G003 ', '天虹制衣 ', '月结 ', '2007/01/08 ',22
union all select 4, ' IN07010012 ', 'G003 ', '天虹制衣 ', '现金 ', '2007/01/10 ',36
union all select 5, ' IN07010013 ', 'G003 ', '天虹制衣 ', '现金 ', '2007/01/09 ',92
create table PayGoods(id int,code varchar(20),datas datetime,numb varchar(10),numbname varchar(50),mons money)
insert PayGoods select 1, ' IN07010009 ', '2007/01/09 ', 'G001 ', '天和可乐 ',900
union all select 2, ' IN07010010 ', '2007/01/09 ', 'G003 ', '天虹制衣 ',200
union all select 3, ' IN07010010 ', '2007/01/09 ', 'G003 ', '天虹制衣 ',248
union all select 4, ' IN07010011 ', '2007/01/08 ', 'G003 ', '天虹制衣 ',22
select identity(int,1,1) id,* into # from
(
select numb,numbname,datas,mons,type,code from GetGoods
union
select numb,numbname,datas,mons,null type,code from PayGoods
)A order by numb,numbname,datas,code,type desc
select case when id=(select top 1 id from # where numb=a.numb and numbname=a.numbname order by id) then numb else ' ' end numb,
case when id=(select top 1 id from # where numb=a.numb and numbname=a.numbname order by id) then numbname else ' ' end numb,
case when id=(select top 1 id from # where numb=a.numb and numbname=a.numbname and datas=a.datas and code=a.code order by id) then datas else null end datas,
datas,
mons,type,code
from # a
drop table GetGoods,#,PayGoods
[解决办法]
借用楼上的数据
SELECT numb,numbname,
CASE sort WHEN 1 THEN NULL ELSE datas END AS datas1,
mons,type,code
FROM
(SELECT numb,numbname,
CONVERT(NVARCHAR(20),datas,111) AS datas,
mons,type,code, 0 AS sort,id
FROM GetGoods
UNION ALL
SELECT numb,numbname,
CONVERT(NVARCHAR(20),datas,111),
mons,NULL ,code, 1 AS sort,id
FROM PayGoods) a
ORDER BY numb,datas,code,sort,id
numb numbname datas1 mons type code
---------- -------------------------------------------------- -------------------- --------------------- -------------------- --------------------
G001 天和可乐 2007/01/09 900.0000 月结 IN07010009
G001 天和可乐 NULL 900.0000 NULL IN07010009
G003 天虹制衣 2007/01/08 22.0000 月结 IN07010011
G003 天虹制衣 NULL 22.0000 NULL IN07010011
G003 天虹制衣 2007/01/09 448.0000 月结 IN07010010
G003 天虹制衣 NULL 200.0000 NULL IN07010010
G003 天虹制衣 NULL 248.0000 NULL IN07010010
G003 天虹制衣 2007/01/09 92.0000 现金 IN07010013
G003 天虹制衣 2007/01/10 36.0000 现金 IN07010012
(9 row(s) affected)
[解决办法]
akuzou(启航) =akuzou(拔剑四顾心茫然)
這兩個一個人啊,把樓主蒙了:)
[解决办法]
create table GetGoods(id int,code varchar(20),numb varchar(10),numbname varchar(50),type varchar(20),datas datetime,mons money)
insert GetGoods select 1, ' IN07010009 ', 'G001 ', '天和可乐 ', '月结 ', '2007/01/09 ',900
union all select 2, ' IN07010010 ', 'G003 ', '天虹制衣 ', '月结 ', '2007/01/09 ',448
union all select 3, ' IN07010011 ', 'G003 ', '天虹制衣 ', '月结 ', '2007/01/08 ',22
union all select 4, ' IN07010012 ', 'G003 ', '天虹制衣 ', '现金 ', '2007/01/10 ',36
union all select 5, ' IN07010013 ', 'G003 ', '天虹制衣 ', '现金 ', '2007/01/09 ',92
create table PayGoods(id int,code varchar(20),datas datetime,numb varchar(10),numbname varchar(50),mons money)
insert PayGoods select 1, ' IN07010009 ', '2007/01/09 ', 'G001 ', '天和可乐 ',900
union all select 2, ' IN07010010 ', '2007/01/09 ', 'G003 ', '天虹制衣 ',200
union all select 3, ' IN07010010 ', '2007/01/09 ', 'G003 ', '天虹制衣 ',248
union all select 4, ' IN07010011 ', '2007/01/08 ', 'G003 ', '天虹制衣 ',22
GO
SELECT (CASE WHEN b.numbname= 'temp ' then a.numb else NULL END),
(CASE WHEN b.numbname= 'temp ' then a.numbname else NULL END),
CASE sort WHEN 1 THEN NULL ELSE datas END AS datas1,
mons,type,code
FROM
(SELECT DISTINCT numb,numbname FROM GetGoods) a RIGHT JOIN
(SELECT numb,numbname,
CONVERT(NVARCHAR(20),datas,111) AS datas,
mons,type,code, 1 AS sort,id
FROM GetGoods
UNION ALL
SELECT numb,numbname,
CONVERT(NVARCHAR(20),datas,111),
mons,NULL ,code, 2 AS sort,id
FROM PayGoods
UNION ALL SELECT DISTINCT numb, 'temp ',NULL,NULL,NULL,NULL,-1,NULL FROM GetGoods
) b
ON a.numb=b.numb
ORDER BY a.numb,b.datas,code,sort,id