一条比较难的sql语句

xiaoliuvv 2011-03-09 09:41:23
有很多产品的不同价格放在一个表里 且每个产品 的每个价格都有一个日期
例如
id 产品名称 产品价格 价格日期
1 白糖 3 2010-1-1
2 白糖 4 2010-1-2
3 白糖 5 2010-1-3

4 砂糖 3 2010-1-1
5 砂糖 5 2010-1-2
6 砂糖 6 2010-1-3
7 砂糖 7 2010-1-4

8 红糖 2 2010-1-1
9 红糖 3 2010-1-2
10 红糖 4 2010-1-3
11 红糖 5 2010-1-4
12 红糖 6 2010-1-5

以上表中可以看出 不是每一天所有产品都有价格的
现在客户想同时列出三个产品的价格 如果有的产品在某一天没有价格就都不列出了,也就是三种产品的价格在某天都存在才列出来,如何实现呢?
列出的表格样式要如下样子的
价格日期 白糖 红糖 砂糖
2010-1-1 3 3 2
2010-1-2 4 3 2
2010-1-3 5 3 2

能列出来吗???



...全文
65 12 打赏 收藏 转发到动态 举报
写回复
用AI写文章
12 条回复
切换为时间正序
请发表友善的回复…
发表回复
ljking0731 2011-03-09
  • 打赏
  • 举报
回复

--建表
create table #Price(ID int, Pname varchar(20), price int, PDate varchar(20))
--数据
insert into #Price
select '1','白糖','3','2010-1-1'
union all select '2','白糖','4','2010-1-2'
union all select '3','白糖','5','2010-1-3'
union all select '4','砂糖','3','2010-1-1'
union all select '5','砂糖','5','2010-1-2'
union all select '6','砂糖','6','2010-1-3'
union all select '7','砂糖','7','2010-1-4'
union all select '8','红糖','2','2010-1-1'
union all select '9','红糖','3','2010-1-2'
union all select '10','红糖','4','2010-1-3'
union all select '11','红糖','5','2010-1-4'
union all select '12','红糖','6','2010-1-5'

--显示
select Pdate,max(case pname when '白糖' then Price end) as [白糖],
max(case pname when '红糖' then Price end) as [红糖],max(case pname when '砂糖' then Price end) as [砂糖]
from #Price where Pdate in
(
select Pdate from #Price
group by Pdate having count(0)>2
) group by Pdate
--结果
Pdate 白糖 红糖 砂糖
-------------------- ----------- ----------- -----------
2010-1-1 3 2 3
2010-1-2 4 3 5
2010-1-3 5 4 6
xiaoliuvv 2011-03-09
  • 打赏
  • 举报
回复
[Quote=引用 2 楼 xiao_ai_mei 的回复:]
SQL code
--> 测试数据: #tb
if object_id('tempdb.dbo.#tb') is not null drop table #tb
go
create table #tb (id int,产品名称 varchar(4),产品价格 int,价格日期 datetime)
insert into #tb
select 1,'白糖',3,'2010-1-1' u……
[/Quote]
简单易懂,数据获取正确 多谢 分全给你了
xiaoliuvv 2011-03-09
  • 打赏
  • 举报
回复
[Quote=引用 6 楼 acherat 的回复:]
SQL code

create table tb (id int,产品名称 varchar(4),产品价格 int,价格日期 datetime)
insert into tb
select 1,'白糖',3,'2010-1-1' union all
select 2,'白糖',4,'2010-1-2' union all
select 3,'白糖',5,'2010-1-3' unio……
[/Quote]
您的这个列出了没有价格数据的日期
xiaoliuvv 2011-03-09
  • 打赏
  • 举报
回复
[Quote=引用 3 楼 haiwer 的回复:]
应该加多个条件


SQL code
select a.*
from tab a,(
select 价格日期 from tab
where 产品名称 in ('白糖','红糖','砂糖')
group by 价格日期
having count(1)=3
) as t
where a.价格日期 = t.价格日期
and a.产品名称 in ('白糖','红糖','砂糖')
……
[/Quote]
您的这个是无法列出的,即使没有行列转换,连记录都列不出
Shawn 2011-03-09
  • 打赏
  • 举报
回复
CREATE TABLE #temp
(
id INT,
产品名称 NVARCHAR(100),
产品价格 INT,
价格日期 DATETIME
)
INSERT #temp
select '1', N'白糖', '3', '2010-1-1' union all
select '2', N'白糖', '4', '2010-1-2' union all
select '3', N'白糖', '5', '2010-1-3' union all
select '4', N'砂糖', '3', '2010-1-1' union all
select '5', N'砂糖', '5', '2010-1-2' union all
select '6', N'砂糖', '6', '2010-1-3' union all
select '7', N'砂糖', '7', '2010-1-4' union all
select '8', N'红糖', '2', '2010-1-1' union all
select '9', N'红糖', '3', '2010-1-2' union all
select '10', N'红糖', '4', '2010-1-3' union all
select '11', N'红糖', '5', '2010-1-4' union all
select '12', N'红糖', '6', '2010-1-5'

SELECT * FROM #temp
--SQL:
SELECT * FROM
(
SELECT b.价格日期, b.产品名称, 产品价格=MAX(产品价格) FROM
(SELECT 价格日期 FROM #temp GROUP BY 价格日期 HAVING COUNT(DISTINCT 产品名称) = 3) a
CROSS APPLY
(SELECT * FROM #temp WHERE 价格日期 = a.价格日期) b
GROUP BY b.价格日期, b.产品名称
) m
PIVOT
(MAX(产品价格) FOR 产品名称 IN ([白糖], [红糖], [砂糖])) n
/*
价格日期 白糖 红糖 砂糖
2010-01-01 00:00:00.000 3 2 3
2010-01-02 00:00:00.000 4 3 5
2010-01-03 00:00:00.000 5 4 6
*/
快溜 2011-03-09
  • 打赏
  • 举报
回复
[Quote=引用 2 楼 xiao_ai_mei 的回复:]
SQL code
--> 测试数据: #tb
if object_id('tempdb.dbo.#tb') is not null drop table #tb
go
create table #tb (id int,产品名称 varchar(4),产品价格 int,价格日期 datetime)
insert into #tb
select 1,'白糖',3,'2010-1-1' u……
[/Quote]
.
AcHerat 2011-03-09
  • 打赏
  • 举报
回复

create table tb (id int,产品名称 varchar(4),产品价格 int,价格日期 datetime)
insert into tb
select 1,'白糖',3,'2010-1-1' union all
select 2,'白糖',4,'2010-1-2' union all
select 3,'白糖',5,'2010-1-3' union all
select 4,'砂糖',3,'2010-1-1' union all
select 5,'砂糖',5,'2010-1-2' union all
select 6,'砂糖',6,'2010-1-3' union all
select 7,'砂糖',7,'2010-1-4' union all
select 8,'红糖',2,'2010-1-1' union all
select 9,'红糖',3,'2010-1-2' union all
select 10,'红糖',4,'2010-1-3' union all
select 11,'红糖',5,'2010-1-4' union all
select 12,'红糖',6,'2010-1-5'
go

declare @sql varchar(max)
set @sql = 'select convert(varchar(10),价格日期,120)价格日期'
select @sql = @sql + ',max(case 产品名称 when ''' + 产品名称 + ''' then 产品价格 else 0 end)[' + 产品名称 + ']'
from (select distinct 产品名称 from tb)t
select @sql = @sql + ' from tb group by convert(varchar(10),价格日期,120)'
exec(@sql)

drop table tb

/*

价格日期 白糖 红糖 砂糖
---------- ----------- ----------- -----------
2010-01-01 3 2 3
2010-01-02 4 3 5
2010-01-03 5 4 6
2010-01-04 0 5 7
2010-01-05 0 6 0

(5 行受影响)
AcHerat 2011-03-09
  • 打赏
  • 举报
回复

declare @sql varchar(max)
set @sql = 'select convert(varchar(7),价格日期,120)价格日期'
select @sql = @sql + ',max(case 产品名称 when ''' + 产品名称 + ''' then 产品价格 else 0 end)[' + 产品名称 + ']'
from (select distinct 产品名称 from tb)t
select @sql = @sql + ' from tb group by convert(varchar(7),价格日期,120)'
exec(@sql)
DEATH64 2011-03-09
  • 打赏
  • 举报
回复
神马数据库?2005直接PIVOT
昵称被占用了 2011-03-09
  • 打赏
  • 举报
回复
应该加多个条件

select a.*
from tab a,(
select 价格日期 from tab
where 产品名称 in ('白糖','红糖','砂糖')
group by 价格日期
having count(1)=3
) as t
where a.价格日期 = t.价格日期
and a.产品名称 in ('白糖','红糖','砂糖')
Xiao_Ai_Mei 2011-03-09
  • 打赏
  • 举报
回复
--> 测试数据: #tb
if object_id('tempdb.dbo.#tb') is not null drop table #tb
go
create table #tb (id int,产品名称 varchar(4),产品价格 int,价格日期 datetime)
insert into #tb
select 1,'白糖',3,'2010-1-1' union all
select 2,'白糖',4,'2010-1-2' union all
select 3,'白糖',5,'2010-1-3' union all
select 4,'砂糖',3,'2010-1-1' union all
select 5,'砂糖',5,'2010-1-2' union all
select 6,'砂糖',6,'2010-1-3' union all
select 7,'砂糖',7,'2010-1-4' union all
select 8,'红糖',2,'2010-1-1' union all
select 9,'红糖',3,'2010-1-2' union all
select 10,'红糖',4,'2010-1-3' union all
select 11,'红糖',5,'2010-1-4' union all
select 12,'红糖',6,'2010-1-5'

select * from
(
select 价格日期,
白糖=max(case when 产品名称='白糖' then 产品价格 end),
红糖=max(case when 产品名称='红糖' then 产品价格 end),
砂糖=max(case when 产品名称='砂糖' then 产品价格 end)
from #tb
group by 价格日期
)t
where 白糖+红糖+砂糖 is not null


价格日期 白糖 红糖 砂糖
----------------------- ----------- ----------- -----------
2010-01-01 00:00:00.000 3 2 3
2010-01-02 00:00:00.000 4 3 5
2010-01-03 00:00:00.000 5 4 6

(3 row(s) affected)
昵称被占用了 2011-03-09
  • 打赏
  • 举报
回复
select a.*
from tab a,(
select 价格日期 from tab
where 产品名称 in ('白糖','红糖','砂糖')
group by 价格日期
having count(1)=3
) as t
where a.价格日期 = t.价格日期


这是选择记录的,行列转换论坛很多,自己写写吧

22,210

社区成员

发帖
与我相关
我的任务
社区描述
MS-SQL Server 疑难问题
社区管理员
  • 疑难问题社区
  • 尘觉
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

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