易截截图软件、单文件、免安装、纯绿色、仅160KB

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


相关文档:

SQL 面试题 一

题目一:
 有两张表:部门表department  部门编号dept_id  部门名称dept_name            
               员工表employee     员工编号emp_id ......

sql server 索引入门

    最近做了一网站,但是在外面访问太慢。本想找下原因。看看是程序原因还是数据库原因。在网上逛逛。看了一下数据库索引。现在总结一下。方便下次查看。本文比较基础是入门级别的。
     首先,什么是索引?从BookOnline上search了一下:    
  &nbs ......

在c#中的取得sql server存储过程的output参数

private void Create_Pz(string Sourcename)
        {
            string OutMsg;
            SqlParameter[] sortPara = { new SqlParameter("@ps_IsType", Pz ......

简单的SQL面试题

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中使用convert函数进行日期的查询

曾经遇到这样的情况,在数据库的Meeting表中有PublishTime (DateTime,8)字段,用来存储一个开会时间,在存入时由于要指明开会具体时间,故格式为yyyy-mm-dd hh:mm:ss,而我们查询时是通过yyyy-mm-dd来进行的,即查询某一天的所有会议信息,这样如果通过select * from Meeting where PublishTime=@PublishTime (参数@PublishTime为y ......
© 2009 ej38.com All Rights Reserved. 关于E健网联系我们 | 站点地图 | 赣ICP备09004571号