USE [GYNCW]
GO
/****** 对象: Table [dbo].[PriceInfo] 脚本日期: 04/22/2009 10:40:34 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE TABLE [dbo].[PriceInfo](
[PriceID] [uniqueidentifier] NOT NULL,
[ProductID] [uniqueidentifier] NOT NULL, --产品ID
[PriceTypeID] [int] NOT NULL,--价格类型
[MarketID] [uniqueidentifier] NOT NULL,--交易市场ID
[Price] [money] NOT NULL,--价格
[PriceDate] [datetime] NOT NULL,--发布日期
[PriceDescription] [nvarchar](max) COLLATE Chinese_PRC_CI_AS NULL,
[AddDate] [datetime] NOT NULL,
[UserName] [nvarchar](50) COLLATE Chinese_PRC_CI_AS NOT NULL,
[LotNo] [uniqueidentifier] NOT NULL
) ON [PRIMARY]这是一张产品价格表,里面保存了所有农贸市场里全部产品的价格(包括历史价格)请问如何才能查询出每个产品在每个农贸市场的最新价格呢。最新价格就是 PriceDate对应的记录。
GO
/****** 对象: Table [dbo].[PriceInfo] 脚本日期: 04/22/2009 10:40:34 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE TABLE [dbo].[PriceInfo](
[PriceID] [uniqueidentifier] NOT NULL,
[ProductID] [uniqueidentifier] NOT NULL, --产品ID
[PriceTypeID] [int] NOT NULL,--价格类型
[MarketID] [uniqueidentifier] NOT NULL,--交易市场ID
[Price] [money] NOT NULL,--价格
[PriceDate] [datetime] NOT NULL,--发布日期
[PriceDescription] [nvarchar](max) COLLATE Chinese_PRC_CI_AS NULL,
[AddDate] [datetime] NOT NULL,
[UserName] [nvarchar](50) COLLATE Chinese_PRC_CI_AS NOT NULL,
[LotNo] [uniqueidentifier] NOT NULL
) ON [PRIMARY]这是一张产品价格表,里面保存了所有农贸市场里全部产品的价格(包括历史价格)请问如何才能查询出每个产品在每个农贸市场的最新价格呢。最新价格就是 PriceDate对应的记录。
-- Author: liangCK 小梁
-- Title : 查每个分组前N条记录
-- Date : 2008-11-13 17:19:23
---------------------------------
--> 生成测试数据: #T
IF OBJECT_ID('tempdb.dbo.#T') IS NOT NULL DROP TABLE #T
CREATE TABLE #T (ID VARCHAR(3),GID INT,Author VARCHAR(29),Title VARCHAR(39),Date DATETIME)
INSERT INTO #T
SELECT '001',1,'邹建','深入浅出SQLServer2005开发管理与应用实例','2008-05-10' UNION ALL
SELECT '002',1,'胡百敬','SQLServer2005性能调校','2008-03-22' UNION ALL
SELECT '003',1,'格罗夫Groff.J.R.','SQL完全手册','2009-07-01' UNION ALL
SELECT '004',1,'KalenDelaney','SQLServer2005技术内幕存储引擎','2008-08-01' UNION ALL
SELECT '005',2,'Alex.Kriegel.Boris.M.Trukhnov','SQL宝典','2007-10-05' UNION ALL
SELECT '006',2,'飞思科技产品研发中心','SQLServer2000高级管理与开发','2007-09-10' UNION ALL
SELECT '007',2,'胡百敬','SQLServer2005数据库开发详解','2008-06-15' UNION ALL
SELECT '008',3,'陈浩奎','SQLServer2000存储过程与XML编程','2005-09-01' UNION ALL
SELECT '009',3,'赵松涛','SQLServer2005系统管理实录','2008-10-01' UNION ALL
SELECT '010',3,'黄占涛','SQL技术手册','2006-01-01'--SQL查询如下:--按GID分组,查每个分组中Date最新的前2条记录
--1.字段ID唯一时:
SELECT * FROM #T AS T WHERE ID IN(SELECT TOP 2 ID FROM #T WHERE GID=T.GID ORDER BY Date DESC)--2.如果ID不唯一时:
SELECT * FROM #T AS T WHERE 2>(SELECT COUNT(*) FROM #T WHERE GID=T.GID AND Date>T.Date)--SQL Server 2005 使用新方法--3.使用ROW_NUMBER()进行排位分组
SELECT ID,GID,Author,Title,Date
FROM
(
SELECT rid=ROW_NUMBER() OVER(PARTITION BY GID ORDER BY Date DESC),*
FROM #T
) AS T
WHERE rid<=2--4.使用APPLY
SELECT DISTINCT b.*
FROM #T AS a
CROSS APPLY
(
SELECT TOP(2) * FROM #T WHERE a.GID=GID ORDER BY Date DESC
) AS b
--结果
/*ID GID Author Title Date
---- ----------- ----------------------------- --------------------------------------- -----------------------
003 1 格罗夫Groff.J.R. SQL完全手册 2009-07-01 00:00:00.000
004 1 KalenDelaney SQLServer2005技术内幕存储引擎 2008-08-01 00:00:00.000
005 2 Alex.Kriegel.Boris.M.Trukhnov SQL宝典 2007-10-05 00:00:00.000
007 2 胡百敬 SQLServer2005数据库开发详解 2008-06-15 00:00:00.000
009 3 赵松涛 SQLServer2005系统管理实录 2008-10-01 00:00:00.000
010 3 黄占涛 SQL技术手册 2006-01-01 00:00:00.000(6 行受影响)
*/
select t.* from PriceInfo t where PriceDate = (select max(PriceDate) from PriceInfo where ProductID = t.ProductID and MarketID = t.MarketID)--2
select t.* from PriceInfo t where not exists (select 1 from PriceInfo where ProductID = t.ProductID and MarketID = t.MarketID and PriceDate > t.PriceDate)
(
select *
,rn=row_number() over(partition by ProductID,MarketID order by PriceDate desc)
from PriceInfo
) t
where rn=1
where not exists (select 1 from PriceInfo where ProductID = t.ProductID and MarketID = t.MarketID and PriceDate > t.PriceDate)
select t.* from PriceInfo t
where not exists (select 1 from PriceInfo where ProductID = t.ProductID and MarketID = t.MarketID and PriceDate > t.PriceDate)這種not exists 的方法效率最好了.
大烏龜出來了.
select * from PriceInfo a ,
(select MarketID, ProductID, max(PriceDate) as pdate from PriceInfo group by MarketID, ProductID) b where a.MarketID = b.MarketID and a.ProductID=b.ProductID and a.PriceDate=b.pdate