想了2天,此sql语句是写不出来了.特写出来,让众位高手试试!如能解决,真是救小弟了.

jxf_yx 2004-11-27 09:00:24
PortShip表:

产品品种 当前港口 数量 时间
Port_ProductID Port_CurrHaven Port_Number Port_InsertTime
1 宁波港 123 2004-01-11
1 宁波港 232 2004-01-12
2 上海港 4023 2004-07-11
3 上海港 423 2004-07-25
4 宁波港 423 2004-12-11
..
..
==
Product表:
产品名称
Product_ID Product_Name
1 苯乙烯
2 乙二醇 
3 正构烷烃
4 基础油
..
..
============================要求的============================================
查询效果:
(查询出某一港口如:(宁波港)的所有品种1-12月份的数量)
产品名称 一月份 二月份 三月份 四月份 五月份 6月份 ....12月份 (列一直到12月份)
苯乙烯 (123+232)的和 空 空 空 空 空 空
基础油 空 空 .. 423
..
..

难点:
1.查询结果的产品名称不能重复
2.增加12个月份列
3.每个品种都要列出1-12月份的数量.不论此月份是否有数量
4.每个月份的数量,都是在此月份里的和.
=================================================================

请大家帮帮忙.

...全文
193 14 打赏 收藏 转发到动态 举报
写回复
用AI写文章
14 条回复
切换为时间正序
请发表友善的回复…
发表回复
solidpanther 2004-11-30
  • 打赏
  • 举报
回复
.............

"12月"=(select sum(Port_Number) from #PortShip where
Port_ProductID=t1.Port_ProductID and convert(varchar(7),Port_InsertTime,120)=convert(varchar(7),'2004-12',120)),
"合计"=(select sum(Port_Number) from #PortShip where
Port_ProductID=t1.Port_ProductID and convert(varchar(4),Port_InsertTime,120)= '2004' )
from #PortShip t1 right outer join #Product t2
on t1.Port_ProductID=t2.Product_ID group by t1.Port_ProductID,t2.产品名称
...............
chenyuandxm 2004-11-28
  • 打赏
  • 举报
回复
sorry,上面case语句少了end。

加上就可以了。
chenyuandxm 2004-11-28
  • 打赏
  • 举报
回复
create procedure pro_port @port,@year
as
select product_name,
select 一月份=sum(case when month(port_inserttime)=1 then port_number else 0) from portship where year(port_inserttime)=@year,
二月份=sum(case when month(port_inserttime)=2 then port_number else 0) from portship where year(port_inserttime)=@year,
三月份=sum(case when month(port_inserttime)=3 then port_number else 0) from portship where year(port_inserttime)=@year,
四月份=sum(case when month(port_inserttime)=4 then port_number else 0) from portship where year(port_inserttime)=@year,
五月份=sum(case when month(port_inserttime)=5 then port_number else 0) from portship where year(port_inserttime)=@year,
六月份=sum(case when month(port_inserttime)=6 then port_number else 0) from portship where year(port_inserttime)=@year,
七月份=sum(case when month(port_inserttime)=7 then port_number else 0) from portship where year(port_inserttime)=@year,
八月份=sum(case when month(port_inserttime)=8 then port_number else 0) from portship where year(port_inserttime)=@year,
九月份=sum(case when month(port_inserttime)=9 then port_number else 0) from portship where year(port_inserttime)=@year,
十月份=sum(case when month(port_inserttime)=10 then port_number else 0) from portship where year(port_inserttime)=@year,
十一月份=sum(case when month(port_inserttime)=11 then port_number else 0) from portship where year(port_inserttime)=@year,
十二月份=sum(case when month(port_inserttime)=12 then port_number else 0) from portship where year(port_inserttime)=@year
from product where Port_CurrHaven=@port group by product_id
********************************
exec pro_port '宁波港',2004
tina2elva 2004-11-28
  • 打赏
  • 举报
回复
学习
dazhu2 2004-11-28
  • 打赏
  • 举报
回复
study
solidpanther 2004-11-28
  • 打赏
  • 举报
回复
...........
from #PortShip t1 right outer join #Product t2 on t1.Port_ProductID=t2.Product_ID group by
.............
jxf_yx 2004-11-28
  • 打赏
  • 举报
回复
solidpanther(╃╄╃电影迷╄╃╄)

忘了,还有问题。 就是如果这个品种1-12月份都没数量,不列出来.
谢谢.非常感谢
didoleo 2004-11-28
  • 打赏
  • 举报
回复
create table #PortShip (Port_ProductID int,Port_CurrHaven varchar(100),Port_Number numeric,Port_InsertTime datetime )

create table #Product (Product_ID int,产品名称 varchar(100))

insert into #PortShip values (1,'宁波港',123,'2004-01-11')
insert into #PortShip values (1,'宁波港',232,'2004-01-12')
insert into #PortShip values (2,'上海港',4023,'2004-07-11')
insert into #PortShip values (3,'上海港',423,'2004-12-11')
insert into #PortShip values (4,'宁波港',423,'2004-5-26')
insert into #PortShip values (1,'别的港',456,'2004-4-2')
insert into #PortShip values (3,'上海港',456,'2004-2-4')
insert into #PortShip values (2,'别的港',456,'2004-01-8')

insert into #Product values (1,'苯乙烯')
insert into #Product values (2,'乙二醇 ')
insert into #Product values (3,'正构烷烃')
insert into #Product values (4,'基础油')




select b.产品名称,
sum(case when month(a.port_insertTime)=1 then port_number else 0 end) as '01月份',
sum(case when month(a.port_insertTime)=2 then port_number else 0 end) as '02月份',
sum(case when month(a.port_insertTime)=3 then port_number else 0 end) as '03月份',
sum(case when month(a.port_insertTime)=4 then port_number else 0 end) as '04月份',
sum(case when month(a.port_insertTime)=5 then port_number else 0 end) as '05月份',
sum(case when month(a.port_insertTime)=6 then port_number else 0 end) as '06月份',
sum(case when month(a.port_insertTime)=7 then port_number else 0 end) as '07月份',
sum(case when month(a.port_insertTime)=8 then port_number else 0 end) as '08月份',
sum(case when month(a.port_insertTime)=9 then port_number else 0 end) as '09月份',
sum(case when month(a.port_insertTime)=10 then port_number else 0 end) as '10月份',
sum(case when month(a.port_insertTime)=11 then port_number else 0 end) as '11月份',
sum(case when month(a.port_insertTime)=12 then port_number else 0 end) as '12月份'
from #portship a left join #product b on a.port_productid=b.Product_ID
where a.port_currhaven='宁波港'
group by b.产品名称
xiaosong8584 2004-11-27
  • 打赏
  • 举报
回复
学习
greatProject 2004-11-27
  • 打赏
  • 举报
回复
mark
lanbaibai 2004-11-27
  • 打赏
  • 举报
回复
不对应该是
建立一个表month
表中有两个字段
F_Column varchar
F_Name varchar
内容是
1 1月份
2 2月份
3 3月份
4 4月份
5 5月份
6 6月份
7 7月份
8 8月份
9 9月份
10 10月份
11 11月份
12 12月份
然后执行下列语句
declare @var nvarchar(2000)
set @var=''
select @var=@var+ 'sum(case month(Port_InsertTime) when '''+ a.F_column +''' then Port_Number else 0 end) as '''+ a.F_name +''',' from t_month a
set @var=left(@var,len(@var)-1)
set @var='select port_productid, '+ @var +' from PortShip where Port_CurrHaven=''宁波港'' group by port_productid '
exec sp_executesql @var
lanbaibai 2004-11-27
  • 打赏
  • 举报
回复
建立一个表month
表中有两个字段
F_Column varchar
F_Name varchar
内容是
1 1月份
2 2月份
3 3月份
4 4月份
5 5月份
6 6月份
7 7月份
8 8月份
9 9月份
10 10月份
11 11月份
12 12月份
然后执行下列语句
declare @var nvarchar(2000)
set @var=''
select @var=@var+ 'sum(case month(Port_InsertTime) when '''+ a.F_column +''' then Port_Number else 0 end) as '''+ a.F_name +''',' from t_month a
set @var=left(@var,len(@var)-1)
set @var='select port_productid, '+ @var +' from PortShip where Port_CurrHaven=''宁波港'' group by port_productid '
exec sp_executesql @var
select port_productid, sum(case month(Port_InsertTime) when '1' then Port_Number else 0 end) as '1月份',sum(case month(Port_InsertTime) when '2' then Port_Number else 0 end) as '2月份',sum(case month(Port_InsertTime) when '3' then Port_Number else 0 end) as '3月份',sum(case month(Port_InsertTime) when '4' then Port_Number else 0 end) as '4月份',sum(case month(Port_InsertTime) when '5' then Port_Number else 0 end) as '5月份',sum(case month(Port_InsertTime) when '6' then Port_Number else 0 end) as '6月份',sum(case month(Port_InsertTime) when '7' then Port_Number else 0 end) as '7月份',sum(case month(Port_InsertTime) when '8' then Port_Number else 0 end) as '8月份',sum(case month(Port_InsertTime) when '9' then Port_Number else 0 end) as '9月份',sum(case month(Port_InsertTime) when '10' then Port_Number else 0 end) as '10月份',sum(case month(Port_InsertTime) when '11' then Port_Number else 0 end) as '11月份',sum(case month(Port_InsertTime) when '12' then Port_Number else 0 end) as '12月份' from PortShip where Port_CurrHaven='宁波港' group by port_productid
jxf_yx 2004-11-27
  • 打赏
  • 举报
回复
高手就是高手.厉害.佩服.明天给分.先放这给大家看看
solidpanther 2004-11-27
  • 打赏
  • 举报
回复

------建立环境
create table #PortShip (Port_ProductID int,Port_CurrHaven varchar(100),Port_Number numeric,Port_InsertTime datetime )

create table #Product (Product_ID int,产品名称 varchar(100))

insert into #PortShip values (1,'宁波港',456,'2004-01-11')
insert into #PortShip values (1,'宁波港',456,'2004-01-12')
insert into #PortShip values (2,'上海港',456,'2004-07-11')
insert into #PortShip values (3,'上海港',456,'2004-12-11')
insert into #PortShip values (4,'宁波港',456,'2004-5-26')
insert into #PortShip values (1,'别的港',456,'2004-4-2')
insert into #PortShip values (3,'上海港',456,'2004-2-4')
insert into #PortShip values (2,'别的港',456,'2004-01-8')

insert into #Product values (1,'苯乙烯')
insert into #Product values (2,'乙二醇 ')
insert into #Product values (3,'正构烷烃')
insert into #Product values (4,'基础油')

--------你要的
select t2.产品名称,
"1月"=(select sum(Port_Number) from #PortShip where
Port_ProductID=t1.Port_ProductID and convert(varchar(7),Port_InsertTime,120)=convert(varchar(7),'2004-01',120)),
"2月"=(select sum(Port_Number) from #PortShip where
Port_ProductID=t1.Port_ProductID and convert(varchar(7),Port_InsertTime,120)=convert(varchar(7),'2004-02',120)),
"3月"=(select sum(Port_Number) from #PortShip where
Port_ProductID=t1.Port_ProductID and convert(varchar(7),Port_InsertTime,120)=convert(varchar(7),'2004-03',120)),
"4月"=(select sum(Port_Number) from #PortShip where
Port_ProductID=t1.Port_ProductID and convert(varchar(7),Port_InsertTime,120)=convert(varchar(7),'2004-04',120)),
"5月"=(select sum(Port_Number) from #PortShip where
Port_ProductID=t1.Port_ProductID and convert(varchar(7),Port_InsertTime,120)=convert(varchar(7),'2004-05',120)),
"6月"=(select sum(Port_Number) from #PortShip where
Port_ProductID=t1.Port_ProductID and convert(varchar(7),Port_InsertTime,120)=convert(varchar(7),'2004-06',120)),
"7月"=(select sum(Port_Number) from #PortShip where
Port_ProductID=t1.Port_ProductID and convert(varchar(7),Port_InsertTime,120)=convert(varchar(7),'2004-07',120)),
"8月"=(select sum(Port_Number) from #PortShip where
Port_ProductID=t1.Port_ProductID and convert(varchar(7),Port_InsertTime,120)=convert(varchar(7),'2004-08',120)),
"9月"=(select sum(Port_Number) from #PortShip where
Port_ProductID=t1.Port_ProductID and convert(varchar(7),Port_InsertTime,120)=convert(varchar(7),'2004-09',120)),
"10月"=(select sum(Port_Number) from #PortShip where
Port_ProductID=t1.Port_ProductID and convert(varchar(7),Port_InsertTime,120)=convert(varchar(7),'2004-10',120)),
"11月"=(select sum(Port_Number) from #PortShip where
Port_ProductID=t1.Port_ProductID and convert(varchar(7),Port_InsertTime,120)=convert(varchar(7),'2004-11',120)),
"12月"=(select sum(Port_Number) from #PortShip where
Port_ProductID=t1.Port_ProductID and convert(varchar(7),Port_InsertTime,120)=convert(varchar(7),'2004-12',120))
from #PortShip t1,#Product t2 where t1.Port_ProductID=t2.Product_ID group by t1.Port_ProductID,t2.产品名称
---删除环境
drop table #PortShip
drop table #Product

34,594

社区成员

发帖
与我相关
我的任务
社区描述
MS-SQL Server相关内容讨论专区
社区管理员
  • 基础类社区
  • 二月十六
  • 卖水果的net
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧