日期:2014-05-17  浏览次数:20484 次

一个复杂的sql语句,求指点,带表结构和测试数据,+挑战性能问题
本帖最后由 weicongjin 于 2013-05-17 23:26:48 编辑
两个表,产品表Products和价格表Prices. 一个产品对应多个价格。隔几天添加一次价格,并更新Products表的UpdateTime字段。
表结构如下:

CREATE TABLE Products(
    [Id] [int] IDENTITY(1,1) PRIMARY KEY NOT NULL,
    [Name] [nvarchar](80) NOT NULL,
    [Image_url] [varchar](400) NULL,
[ClassId] [int] NULL,
    [UpdateTime] [datetime] NOT NULL
)
GO
CREATE TABLE Prices(
    [Id] [int] IDENTITY(1,1) PRIMARY KEY NOT NULL,
    [Price] [money] NOT NULL,
    [PId] [int] NOT NULL,--产品表的Id
    [Time] [datetime] NOT NULL
)
GO
INSERT INTO Products VALUES ('剃须刀','http://images.csdn.net/20130517/11101.jpg',6,'2012-4-11 10:16:12')
INSERT INTO Products VALUES ('连衣裙','http://images.csdn.net/20130517/11101.jpg',6,'2012-4-12 10:16:12')
INSERT INTO Products VALUES ('小米手机','http://images.csdn.net/20130517/11101.jpg',6,'2012-4-13 10:16:12')
INSERT INTO Products VALUES ('笔记本','http://images.csdn.net/20130517/11101.jpg',6,'2012-5-11 10:16:12')
INSERT INTO Products VALUES ('联想电脑','http://images.csdn.net/20130517/11101.jpg',6,'2012-5-12 10:16:12')
INSERT INTO Products VALUES ('毛巾','http://images.csdn.net/20130517/11101.jpg',6,'2012-5-13 10:16:12')
INSERT INTO Products VALUES ('赛车','http://images.csdn.net/20130517/11101.jpg',6,'2012-5-14 10:16:12')
INSERT INTO Products VALUES ('电冰箱','http://images.csdn.net/20130517/11101.jpg',6,'2012-5-18 10:16:12')
INSERT INTO Products VALUES ('平板电脑','http://images.csdn.net/20130517/11101.jpg',6,'2012-5-22 10:16:12')
INSERT INTO Products VALUES ('冰与火','http://images.csdn.net/20130517/11101.jpg',6,'2012-5-17 20:16:12')
GO
INSERT INTO Prices VALUES (20,1,'2012-6-11 10:16:12')
INSERT INTO Prices VALUES (29,2,'2012-6-12 10:16:12')
INSERT INTO Prices VALUES (21,2,'2012-6-13 10:16:12')
INSERT INTO Prices VALUES (29,4,'2012-6-12 10:16:12')
INSERT INTO Prices VALUES (29,4,'2012-6-13 10:16:12')
INSERT INTO Prices VALUES (30,2,'2012-6-14 10:16:12')
INSERT INTO Prices VALUES (31,3,'2012-6-11 10:16:12')
INSERT INTO Prices VALUES (29,3,'2012-6-12 10:16:12')
INSERT INTO Prices VALUES (21,3,'2012-6-13 10:16:12')
INSERT INTO Prices VALUES (22,3,'2012-6-14 10:16:12')
INSERT INTO Prices VALUES (20,4,'2012-6-11 10:16:12')
INSERT INTO Prices VALUES (20,6,'2012-6-14 10:16:12')
INSERT INTO Prices VALUES (20,7,'2012-6-11 10:16:12')
INSERT INTO Prices VALUES (20,4,'2012-6-14 10:16:12')
INSERT INTO Prices VALUES (50,5,'2012-6-14 10:16:12')
INSERT INTO Prices VALUES (20,6,'2012-6-11 10:16:12')
INSERT INTO Prices VALUES (20,6,'2012-6-12 10:16:12')
INSERT INTO Prices VALUES (21,1,'2012-6-13 10:16:12')
INSERT INTO Prices VALUES (20,6,'2