DECLARE temptable CURSOR local FOR SELECT * FROM 货品基础资料表 OPEN temptable FETCH NEXT FROM temptable INTO .................... while @@FETCH_STATUS = 0 begin 一个个处理,也可以定进临时表,相信不会很难了吧。 end
这个好像以前有人问过,差不多的:select case when ordertype=1 then 货品编码 when ordertype=4 '小计' else null end as 货品编码,case when ordertype=1 then 货品名称 else null end as 货品名称,case when ordertype=2 or ordertype=3 then 仓库名称 else null end as 仓库名称,case when ordertype=1 then 库存下限 else null end as 库存下限,case when ordertype=4 then null else 当前库存 end as 当前库存,case when ordertype=1 then 库存上限 else null end as 库存上限 from ( select a.货品编码,a.货品名称,null as 仓库名称,a.库存下限,isnull(b.数量,0) as 当前库存,a.库存上限 ,1 as ordertype from 货品基础资料表 left join ( select 货品编码,sum(数量) as 数量 from 从表 group by 货品编码 ) as b on a.货品编码=b.货品编码 union all select c.货品编码,null as 货品名称,d.仓库名称,null as 库存下限,sum(c.数量) as 当前库存,null as 库存上限 ,2 as ordertype from 从表 c,主表 d where c.仓库编号=d.仓库编号 group by c.货品编码,d.仓库名称 union all select c.货品编码,null as 货品名称,max(仓库名称) as 仓库名称,null as 库存下限,sum(c.数量) as 当前库存,null as 库存上限 ,3 as ordertype from 从表 c,主表 d where c.仓库编号=d.仓库编号 group by c.货品编码 union all select c.货品编码,null as 货品名称,max(仓库名称) as 仓库名称,null as 库存下限,sum(c.数量) as 当前库存,null as 库存上限 ,4 as ordertype from 从表 c,主表 d where c.仓库编号=d.仓库编号 group by c.货品编码) as x order by 货品编码,ordertype,仓库名称呵呵庞大的语句! 没有测试!
更正:select case when ordertype=1 then 货品编码 when ordertype=3 then '小计' else null end as 货品编码,case when ordertype=1 then 货品名称 else null end as 货品名称,case when ordertype=2 then 仓库名称 else null end as 仓库名称,case when ordertype=1 then 库存下限 else null end as 库存下限,case when ordertype=4 then null else 当前库存 end as 当前库存,case when ordertype=1 then 库存上限 else null end as 库存上限 from ( select a.货品编码,a.货品名称,null as 仓库名称,a.库存下限,isnull(b.数量,0) as 当前库存,a.库存上限 ,1 as ordertype from 货品基础资料表 a left join ( select 货品编码,sum(数量) as 数量 from 从表 group by 货品编码 ) as b on a.货品编码=b.货品编码 union all select c.货品编码,null as 货品名称,d.仓库名称,null as 库存下限,sum(c.数量) as 当前库存,null as 库存上限 ,2 as ordertype from 从表 c,主表 d where c.仓库编号=d.仓库编号 group by c.货品编码,d.仓库名称 union all select c.货品编码,null as 货品名称,max(仓库名称) as 仓库名称,null as 库存下限,sum(c.数量) as 当前库存,null as 库存上限 ,3 as ordertype from 从表 c,主表 d where c.仓库编号=d.仓库编号 group by c.货品编码 union all select c.货品编码,null as 货品名称,max(仓库名称) as 仓库名称,null as 库存下限,sum(c.数量) as 当前库存,null as 库存上限 ,4 as ordertype from 从表 c,主表 d where c.仓库编号=d.仓库编号 group by c.货品编码 ) as x order by rtrim(货品编码),ordertype
1 as ordertype是用来辅助排序的,以前回答过差不多的题,也用这个方法,所以还记得,现在是现写的。不知道怎么回事,我的sql server order by 货品编码,ordertype 就是不对, 改成 order by rtrim(货品编码),ordertype就对了。
select c.货品编码,null as 货品名称,max(仓库名称) as 仓库名称,null as 库存下限,sum(c.数量) as 当前库存,null as 库存上限 ,3 as ordertype from 从表 c,主表 d where c.仓库编号=d.仓库编号 group by c.货品编码 union all 为什么要用max(仓库名称) as 仓库名称, max为什么要用
,max(仓库名称) as 仓库名称 这里不用max也可以的! 可以换成:,min(仓库名称) as 仓库名称 or: ,null as 仓库名称
我想应该是, 终于理解了一点点。4 as ordertype是不是那一行空行。才起作有。
select c.货品编码,null as 货品名称,max(仓库名称) as 仓库名称,null as 库存下限,sum(c.数量) as 当前库存,null as 库存上限 ,4 as ordertype from 从表 c,主表 d where c.仓库编号=d.仓库编号 group by c.货品编码这一句因为是空行,可不可以直接写成select c.货口编码,null as 货品名称,null as 仓库名称,null as 库存下限,null as 当前库存,null as 库存上限, 4 as ordertype
在我这里执行通不过 select c.货口编码,null as 货品名称,null as 仓库名称,null as 库存下限,null as 当前库存,null as 库存上限, 4 as ordertypeform ****,这里应该是什么
那几个case 语句真的理解不了。
select 货品编码, 货品名称, 仓库名称, 库存下限,当前库存,库存上限 From ( select a.货品编码,a.货品名称 as 仓库名称,a.库存下限,isnull(b.数量,0) as 当前库存,a.库存上限 ,1 as ordertype from 货品基础资料表 a left join ( select 货品编码,sum(数量) as 数量 from 从表 group by 货品编码 ) as b on a.货品编码=b.货品编码) as x order by rtrim(货品编码),ordertype这样我得到了 第一行 货品编码, 货品名称, 仓库名称, 库存下限,当前库存,库存上限 001 酒 null -10 10 2接下来,我想把下面的继续 接上去,头痛。
必须用case语句,因为你要排序,而且要插入空行。 你可以把表的原始结构贴出,我调试一下。(不方便的话,可以发短消息) 你上面的结构我下午是试过的下午测试:create table 货品基础资料表 ( [货品编码] char(20), [货品名称] char(20), 库存上限 int, 库存下限 int )insert 货品基础资料表 values( '001', '酒' , 2 , -10) insert 货品基础资料表 values( '002' , '烟', 10 , 3)create table 主表( 仓库编号 int,仓库名称 char(30) )insert 主表 values( 1, '仓库1') insert 主表 values( 2, '仓库2') create table 从表( 仓库编号 int,货品编码 char(20),数量 int)insert 从表 values( 1, '001', 3) insert 从表 values( 1 , '002' , 5) insert 从表 values( 2 , '001' , 6) insert 从表 values( 2 , '001' , 1)select case when ordertype=1 then 货品编码 when ordertype=3 then '小计' else null end as 货品编码,case when ordertype=1 then 货品名称 else null end as 货品名称,case when ordertype=2 then 仓库名称 else null end as 仓库名称,case when ordertype=1 then 库存下限 else null end as 库存下限,case when ordertype=4 then null else 当前库存 end as 当前库存,case when ordertype=1 then 库存上限 else null end as 库存上限 from ( select a.货品编码,a.货品名称,null as 仓库名称,a.库存下限,isnull(b.数量,0) as 当前库存,a.库存上限 ,1 as ordertype from 货品基础资料表 a left join ( select 货品编码,sum(数量) as 数量 from 从表 group by 货品编码 ) as b on a.货品编码=b.货品编码 union all select c.货品编码,null as 货品名称,d.仓库名称,null as 库存下限,sum(c.数量) as 当前库存,null as 库存上限 ,2 as ordertype from 从表 c,主表 d where c.仓库编号=d.仓库编号 group by c.货品编码,d.仓库名称 union all select c.货品编码,null as 货品名称,max(仓库名称) as 仓库名称,null as 库存下限,sum(c.数量) as 当前库存,null as 库存上限 ,3 as ordertype from 从表 c,主表 d where c.仓库编号=d.仓库编号 group by c.货品编码 union all select c.货品编码,null as 货品名称,max(仓库名称) as 仓库名称,null as 库存下限,sum(c.数量) as 当前库存,null as 库存上限 ,4 as ordertype from 从表 c,主表 d where c.仓库编号=d.仓库编号 group by c.货品编码 ) as x order by rtrim(货品编码),ordertype,仓库名称 结果: 货品编码 货品名称 仓库名称 库存下限 当前库存 库存上限 -------------------- -------------------- ------------------------------ ----------- ----------- ----------- 001 酒 NULL -10 10 2 NULL NULL 仓库1 NULL 3 NULL NULL NULL 仓库2 NULL 7 NULL 小计 NULL NULL NULL 10 NULL NULL NULL NULL NULL NULL NULL 002 烟 NULL 3 5 10 NULL NULL 仓库1 NULL 5 NULL 小计 NULL NULL NULL 5 NULL NULL NULL NULL NULL NULL NULL(所影响的行数为 9 行)
Server: Msg 170, Level 15, State 1, Line 22 Line 22: Incorrect syntax near ' '. 我把你上面那段代码拷过来,出现上面的错误
DECLARE temptable CURSOR local FOR
SELECT * FROM 货品基础资料表
OPEN temptable
FETCH NEXT FROM temptable
INTO ....................
while @@FETCH_STATUS = 0
begin
一个个处理,也可以定进临时表,相信不会很难了吧。
end
case when ordertype=1 then 货品编码 when ordertype=4 '小计' else null end as 货品编码,case when ordertype=1 then 货品名称 else null end as 货品名称,case when ordertype=2 or ordertype=3 then 仓库名称 else null end as 仓库名称,case when ordertype=1 then 库存下限 else null end as 库存下限,case when ordertype=4 then null else 当前库存 end as 当前库存,case when ordertype=1 then 库存上限 else null end as 库存上限
from
(
select
a.货品编码,a.货品名称,null as 仓库名称,a.库存下限,isnull(b.数量,0) as 当前库存,a.库存上限 ,1 as ordertype
from 货品基础资料表 left join
(
select 货品编码,sum(数量) as 数量 from 从表 group by 货品编码
) as b on a.货品编码=b.货品编码
union all
select
c.货品编码,null as 货品名称,d.仓库名称,null as 库存下限,sum(c.数量) as 当前库存,null as 库存上限 ,2 as ordertype
from 从表 c,主表 d
where c.仓库编号=d.仓库编号
group by c.货品编码,d.仓库名称
union all
select
c.货品编码,null as 货品名称,max(仓库名称) as 仓库名称,null as 库存下限,sum(c.数量) as 当前库存,null as 库存上限 ,3 as ordertype
from 从表 c,主表 d
where c.仓库编号=d.仓库编号
group by c.货品编码
union all
select
c.货品编码,null as 货品名称,max(仓库名称) as 仓库名称,null as 库存下限,sum(c.数量) as 当前库存,null as 库存上限 ,4 as ordertype
from 从表 c,主表 d
where c.仓库编号=d.仓库编号
group by c.货品编码) as x
order by 货品编码,ordertype,仓库名称呵呵庞大的语句!
没有测试!
你可以使用SQL 语句查出数据,到前台再作矩阵转换
when ordertype=1 then 货品编码
when ordertype=3 then '小计'
else null end as 货品编码,case when ordertype=1 then 货品名称 else null end as 货品名称,case when ordertype=2 then 仓库名称 else null end as 仓库名称,case when ordertype=1 then 库存下限 else null end as 库存下限,case when ordertype=4 then null else 当前库存 end as 当前库存,case when ordertype=1 then 库存上限 else null end as 库存上限
from
(
select
a.货品编码,a.货品名称,null as 仓库名称,a.库存下限,isnull(b.数量,0) as 当前库存,a.库存上限 ,1 as ordertype
from 货品基础资料表 a left join
(
select 货品编码,sum(数量) as 数量 from 从表 group by 货品编码
) as b on a.货品编码=b.货品编码
union all
select
c.货品编码,null as 货品名称,d.仓库名称,null as 库存下限,sum(c.数量) as 当前库存,null as 库存上限 ,2 as ordertype
from 从表 c,主表 d
where c.仓库编号=d.仓库编号
group by c.货品编码,d.仓库名称
union all
select
c.货品编码,null as 货品名称,max(仓库名称) as 仓库名称,null as 库存下限,sum(c.数量) as 当前库存,null as 库存上限 ,3 as ordertype
from 从表 c,主表 d
where c.仓库编号=d.仓库编号
group by c.货品编码
union all
select
c.货品编码,null as 货品名称,max(仓库名称) as 仓库名称,null as 库存下限,sum(c.数量) as 当前库存,null as 库存上限 ,4 as ordertype
from 从表 c,主表 d
where c.仓库编号=d.仓库编号
group by c.货品编码
) as x
order by rtrim(货品编码),ordertype
order by 货品编码,ordertype 就是不对,
改成
order by rtrim(货品编码),ordertype就对了。
c.货品编码,null as 货品名称,max(仓库名称) as 仓库名称,null as 库存下限,sum(c.数量) as 当前库存,null as 库存上限 ,3 as ordertype
from 从表 c,主表 d
where c.仓库编号=d.仓库编号
group by c.货品编码
union all
为什么要用max(仓库名称) as 仓库名称, max为什么要用
这里不用max也可以的!
可以换成:,min(仓库名称) as 仓库名称
or:
,null as 仓库名称
c.货品编码,null as 货品名称,max(仓库名称) as 仓库名称,null as 库存下限,sum(c.数量) as 当前库存,null as 库存上限 ,4 as ordertype
from 从表 c,主表 d
where c.仓库编号=d.仓库编号
group by c.货品编码这一句因为是空行,可不可以直接写成select c.货口编码,null as 货品名称,null as 仓库名称,null as 库存下限,null as 当前库存,null as 库存上限, 4 as ordertype
select c.货口编码,null as 货品名称,null as 仓库名称,null as 库存下限,null as 当前库存,null as 库存上限, 4 as ordertypeform ****,这里应该是什么
From (
select
a.货品编码,a.货品名称 as 仓库名称,a.库存下限,isnull(b.数量,0) as 当前库存,a.库存上限 ,1 as ordertype
from 货品基础资料表 a left join
(
select 货品编码,sum(数量) as 数量 from 从表 group by 货品编码
) as b on a.货品编码=b.货品编码) as x
order by rtrim(货品编码),ordertype这样我得到了
第一行
货品编码, 货品名称, 仓库名称, 库存下限,当前库存,库存上限
001 酒 null -10 10 2接下来,我想把下面的继续 接上去,头痛。
你可以把表的原始结构贴出,我调试一下。(不方便的话,可以发短消息)
你上面的结构我下午是试过的下午测试:create table 货品基础资料表 (
[货品编码] char(20),
[货品名称] char(20),
库存上限 int,
库存下限 int
)insert 货品基础资料表 values(
'001', '酒' , 2 , -10)
insert 货品基础资料表 values(
'002' , '烟', 10 , 3)create table 主表(
仓库编号 int,仓库名称 char(30)
)insert 主表 values(
1, '仓库1')
insert 主表 values(
2, '仓库2')
create table 从表(
仓库编号 int,货品编码 char(20),数量 int)insert 从表 values(
1, '001', 3)
insert 从表 values(
1 , '002' , 5)
insert 从表 values(
2 , '001' , 6)
insert 从表 values(
2 , '001' , 1)select case
when ordertype=1 then 货品编码
when ordertype=3 then '小计'
else null end as 货品编码,case when ordertype=1 then 货品名称 else null end as 货品名称,case when ordertype=2 then 仓库名称 else null end as 仓库名称,case when ordertype=1 then 库存下限 else null end as 库存下限,case when ordertype=4 then null else 当前库存 end as 当前库存,case when ordertype=1 then 库存上限 else null end as 库存上限
from
(
select
a.货品编码,a.货品名称,null as 仓库名称,a.库存下限,isnull(b.数量,0) as 当前库存,a.库存上限 ,1 as ordertype
from 货品基础资料表 a left join
(
select 货品编码,sum(数量) as 数量 from 从表 group by 货品编码
) as b on a.货品编码=b.货品编码
union all
select
c.货品编码,null as 货品名称,d.仓库名称,null as 库存下限,sum(c.数量) as 当前库存,null as 库存上限 ,2 as ordertype
from 从表 c,主表 d
where c.仓库编号=d.仓库编号
group by c.货品编码,d.仓库名称
union all
select
c.货品编码,null as 货品名称,max(仓库名称) as 仓库名称,null as 库存下限,sum(c.数量) as 当前库存,null as 库存上限 ,3 as ordertype
from 从表 c,主表 d
where c.仓库编号=d.仓库编号
group by c.货品编码
union all
select
c.货品编码,null as 货品名称,max(仓库名称) as 仓库名称,null as 库存下限,sum(c.数量) as 当前库存,null as 库存上限 ,4 as ordertype
from 从表 c,主表 d
where c.仓库编号=d.仓库编号
group by c.货品编码
) as x
order by rtrim(货品编码),ordertype,仓库名称
结果:
货品编码 货品名称 仓库名称 库存下限 当前库存 库存上限
-------------------- -------------------- ------------------------------ ----------- ----------- -----------
001 酒 NULL -10 10 2
NULL NULL 仓库1 NULL 3 NULL
NULL NULL 仓库2 NULL 7 NULL
小计 NULL NULL NULL 10 NULL
NULL NULL NULL NULL NULL NULL
002 烟 NULL 3 5 10
NULL NULL 仓库1 NULL 5 NULL
小计 NULL NULL NULL 5 NULL
NULL NULL NULL NULL NULL NULL(所影响的行数为 9 行)
Line 22: Incorrect syntax near ' '.
我把你上面那段代码拷过来,出现上面的错误