sql行转列
写一个存储过程,将表一按照表二的形式进行查询。
仓库名称 商品名称 数量
A
S001 12
A S002 17
A
S003 10
B S001 21
B
S002 5
B S003 0
C S001 100
C S002 11
C S003 25
(表一)
商
品名称 总库存 A B C
S001 133 12 21 100
S002 33 17 5 11
S003 35 10 0
25
(表二)
create
table
tb(仓库名称
varchar
(
10
),商品名称
varchar
(
10
),数量
int
)
insert
into
tb
values
(
'
A
'
,
'
S001
'
,
12
)
insert
into
tb
values
(
'
A
'
,
'
S002
'
,
17
)
insert
into
tb
values
(
'
A
'
,
'
S003
'
,
10
)
insert
into
tb
values
(
'
B
'
,
'
S001
'
,
21
)
insert
into
tb
values
(
'
B
'
,
'
S002
'
,
5
)
insert
into
tb
values
(
'
B
'
,
'
S003
'
,
0
)
insert
into
tb
values
(
'
C
'
,
'
S001
'
,
100
)
insert
into
tb
values
(
'
C
'
,
'
S002
'
,
11
)
insert
into
tb
values
(
'
C
'
,
'
S003
'
,
25
)
go
--
如果只有A,B,C,则使用静态SQL。
select
商品名称,
sum
(数量) 总库存,
sum
(
case
仓库名称
when
'
A
'
then
数量
else
0
end
)
[
A
]
,
sum
(
case
仓库名称
when
'
B
'
then
数量
else
0
end
)
[
B
]
,
sum
(
case
仓库名称
when
'
C
'
then
数量
else
0
end
)
[
C
]
from
tb
group
by
商品名称
/*
商品名称 总库存 A B C
---------- ----------- ----------- ----------- -----------
S001 133 12 21 100
S002 33
相关文档:
CREATE proc [dbo].[proc_DeleteTemplet] (@templeId varchar(15),@errorMessage varchar(50) output)
as
begin
declare @error int
set @error =0
begin tran
delete from tc_templet_Head where fBillNo=@templeId
set @error=@error+@@error
delete from tc_templet_ ......
一、情况说明
sql server 2000以前的版本,例如7.0一般不存在多个版本,只有标准版跟桌面版,用户如果不清楚该装什么版本的话,可按安装上的
安装先决条件指示安装,一般在WIN2000 服务器版上装标准版,其他的系统装桌面版的就可以;而SQL Server 2000安装问题就比较大,时常
见到的问题如下:
......
1. Q. What is a join?
A. Join is a process of retrieve pieces of data from different sets (tables) and returns them to the user or program as one joined collection of data.
2. Q. Can a table have more than one foreign key defined?
A. A table can have any number of foreig ......
你的SQL Server 2000必须打上SP3的补,然后安装Microsoft的JDBC驱动,将以下三个jar包复制到WebContent\WEB-INF\lib
msbase.jar
mssqlserver.jar
msutil.jar
下面是测试代码,没有报错数据库就能正常连接
public class DbcTest {
/**
* @param args
*/
public static void main(String[] args) {
Str ......
在运行窗口输入regedit,打开注册表编辑器,在HKEY_LOCAL_MACHINE\SYSTEM\ControlSet001\Control\Session Manager中找到PendingFileRenameOperations,删除该键值,关闭注册表编辑器。重新安装SQL Server 2000,此时,就可以正常进入期待的安装界面了。 ......