时间 产品名称 产品型号 交易价格
2011-7-23 A 2*3*5 0.5
2011-7-25 A 2*3*5 2.3
2011-7-28 B 2*3*7 1.3
2011-8-3 A 2*3*5 4
2011-8-8 C 2*3*9 2
2011-8-23 B 2*3*7 3
2011-9-1 C 2*3*9 6
2011-9-12 C 2*3*9 8
2011-9-21 B 2*3*7 7
找出每种产品在一个月当中最后一次交易的价格,如何写SQL语句
2011-7-23 A 2*3*5 0.5
2011-7-25 A 2*3*5 2.3
2011-7-28 B 2*3*7 1.3
2011-8-3 A 2*3*5 4
2011-8-8 C 2*3*9 2
2011-8-23 B 2*3*7 3
2011-9-1 C 2*3*9 6
2011-9-12 C 2*3*9 8
2011-9-21 B 2*3*7 7
找出每种产品在一个月当中最后一次交易的价格,如何写SQL语句
解决方案 »
- 表有2列,第2列是数字,要求select中+3列,根据第二列的数字改变这三列,返回结果集,如结果集id=1,price=12,小于5=null,5-10=null,大于12=12
- mdf与ldf
- 关于SQL server 中计算时间的长短
- 今日21分: 有关数据库升级问题
- 字符串邊界符??
- SQL Server master..xp_cmdshell 'c:\test.bat'?????
- 请教一统计语句
- 如何备份Access数据库???
- 急!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
- 在master库中建一存储过程A,再用sp_procoption令其自动执行,但如何让这个A实现在tempdb中自动建用户,并db_owner角色?
- 如何写一个按月动态生成表的存储过程,进行历史数据移值................
- 不知道有没有64位的sqlsever2000 ?请前辈指导!
select *
from tb t
where not exists (select 1 from tb where 产品名称=t.产品名称
and convert(varchar(6),时间,112)=convert(varchar(6),t.时间,112) and 时间>t.时间)
where not exists (
select 1 from tab
where convert(varchar(7),时间,120) = convert(varchar(7),a.时间,120)
and 产品名称= a.产品名称 and 产品型号= a.产品型号
and 时间 > a.时间
)
from tb t
where 时间 (select max(时间) from tb where 产品名称=t.产品名称
and convert(varchar(6),时间,112)=convert(varchar(6),t.时间,112))
drop table #
Go
Create table #([时间] Datetime,[产品名称] nvarchar(1),[产品型号] nvarchar(5),[交易价格] decimal(18,1))
Insert #
select '2011-7-23',N'A',N'2*3*5',0.5 union all
select '2011-7-25',N'A',N'2*3*5',2.3 union all
select '2011-7-28',N'B',N'2*3*7',1.3 union all
select '2011-8-3',N'A',N'2*3*5',4 union all
select '2011-8-8',N'C',N'2*3*9',2 union all
select '2011-8-23',N'B',N'2*3*7',3 union all
select '2011-9-1',N'C',N'2*3*9',6 union all
select '2011-9-12',N'C',N'2*3*9',8 union all
select '2011-9-21',N'B',N'2*3*7',7
Go
Select *
from # t
where not exists(select 1
from #
where datediff(month,[时间],t.[时间])=0 and day([时间])>day(t.[时间]))
/*
时间 产品名称 产品型号 交易价格
----------------------- ---- ----- ---------------------------------------
2011-07-28 00:00:00.000 B 2*3*7 1.3
2011-08-23 00:00:00.000 B 2*3*7 3.0
2011-09-21 00:00:00.000 B 2*3*7 7.0(3 row(s) affected)*/
*
from
tb t
where
时间=(select max(时间) from tb where 产品名称=t.产品名称 and convert(varchar(7),时间,120)=convert(varchar(7),t.时间,120))
-- Author :fredrickhu(小F,向高手学习)
-- Date :2011-08-10 17:40:53
-- Verstion:
-- Microsoft SQL Server 2008 (RTM) - 10.0.1600.22 (Intel X86)
-- Jul 9 2008 14:43:34
-- Copyright (c) 1988-2008 Microsoft Corporation
-- Enterprise Evaluation Edition on Windows NT 5.1 <X86> (Build 2600: Service Pack 3)
--
----------------------------------------------------------------
--> 测试数据:[tb]
if object_id('[tb]') is not null drop table [tb]
go
create table [tb]([时间] datetime,[产品名称] varchar(1),[产品型号] varchar(5),[交易价格] numeric(2,1))
insert [tb]
select '2011-7-23','A','2*3*5',0.5 union all
select '2011-7-25','A','2*3*5',2.3 union all
select '2011-7-28','B','2*3*7',1.3 union all
select '2011-8-3','A','2*3*5',4 union all
select '2011-8-8','C','2*3*9',2 union all
select '2011-8-23','B','2*3*7',3 union all
select '2011-9-1','C','2*3*9',6 union all
select '2011-9-12','C','2*3*9',8 union all
select '2011-9-21','B','2*3*7',7
--------------开始查询--------------------------
select
*
from
tb t
where
时间=(select max(时间) from tb where datediff(mm,[时间],t.[时间])=0)
select
*
from
tb t
where
时间=(select max(时间) from tb where convert(varchar(7),时间,120)=convert(varchar(7),t.时间,120))
----------------结果----------------------------
/* 时间 产品名称 产品型号 交易价格
----------------------- ---- ----- ---------------------------------------
2011-07-28 00:00:00.000 B 2*3*7 1.3
2011-08-23 00:00:00.000 B 2*3*7 3.0
2011-09-21 00:00:00.000 B 2*3*7 7.0(3 行受影响)*/