I need to extract data from this table to find the lowest prices of
each product as of today. The product will be listed/grouped by the
name only, discarding the product code - I use SUBSTRING(Produ ctName,
1, CHARINDEX('(', ProductName)-2).
I can get this result, but I had to use several views (totally
inefficient). I think this can be done in one efficient/fast query,
but I can't think of one.
In the case that one query is not possible, is there other efficient
way to get the results? There are about 300K records in the table.
Any help is appreciated! Thanks a lot in advance!
Thanks,
Netpurpose
-----------------------START TABLE INFO-------------------------
Here is the desired result (on 8/29/03):
ProductName Price
---------------------------------------- ---------------------
Product One 33.5000
Product Three 40.4500
Product Two 44.7500
Here is the table info:
CREATE TABLE [ProductList] (
[Distributor] [nvarchar] (5) NULL ,
[ProductName] [nvarchar] (40) NULL ,
[Price] [money] NULL ,
[EffectiveDate] [smalldatetime] NULL
) ON [PRIMARY]
Table Data (comma delimited):
*Sorry I can't list in insert commands as there are too many.
"Manufacturer", "ProductName"," Price","Effecti veDate"
"AAA","Prod uct One (MXT234)",1000, 2003-08-30 00:00:00
"BBB","Prod uct One (MXT234)",1000, 2003-07-29 00:00:00
"BBB","Prod uct One (MXT234)",28.15 ,2003-07-30 00:00:00
"BBB","Prod uct One (MXT234)",35.22 ,2003-08-04 00:00:00
"BBB","Prod uct One (MXT234)",35.22 ,2003-08-04 00:00:00
"BBB","Prod uct One (MXT234)",1000, 2003-08-16 00:00:00
"BBB","Prod uct One (MXT234)",33.8, 2003-08-27 00:00:00
"CCC","Prod uct One (MXT234)",33.25 ,2003-08-31 00:00:00
"DDD","Prod uct One (MXT234)",46.25 ,2003-01-02 00:00:00
"EEE","Prod uct One (MXT234)",1000, 2003-08-30 00:00:00
"FFF","Prod uct One (MXT234)",1000, 2003-08-30 00:00:00
"GGG","Prod uct One (MXT234)",1000, 2003-09-01 00:00:00
"HHH","Prod uct One (MXT234)",33.8, 2003-08-04 00:00:00
"III","Prod uct One (MXT234)",1000, 2003-01-13 00:00:00
"JJJ","Prod uct One (MXT234)",34.35 ,2003-07-30 00:00:00
"JJJ","Prod uct One (MXT234)",34.35 ,2003-09-01 00:00:00
"KKK","Prod uct One (MXT234)",1000, 2003-08-30 00:00:00
"ZZZ","Prod uct One (MXT205)",54,20 03-01-13 00:00:00
"AAA","Prod uct One (MXT205)",1000, 2003-08-30 00:00:00
"BBB","Prod uct One (MXT205)",33.95 ,2003-07-27 00:00:00
"BBB","Prod uct One (MXT205)",33.95 ,2003-07-29 00:00:00
"BBB","Prod uct One (MXT205)",35.22 ,2003-08-04 00:00:00
"BBB","Prod uct One (MXT205)",33.8, 2003-08-16 00:00:00
"BBB","Prod uct One (MXT205)",33.5, 2003-08-27 00:00:00
"CCC","Prod uct One (MXT205)",1000, 2003-08-30 00:00:00
"DDD","Prod uct One (MXT205)",46.25 ,2003-01-02 00:00:00
"EEE","Prod uct One (MXT205)",1000, 2003-08-30 00:00:00
"FFF","Prod uct One (MXT205)",1000, 2003-08-30 00:00:00
"GGG","Prod uct One (MXT205)",1000, 2003-09-01 00:00:00
"HHH","Prod uct One (MXT205)",33.95 ,2003-08-04 00:00:00
"III","Prod uct One (MXT205)",1000, 2003-01-13 00:00:00
"JJJ","Prod uct One (MXT205)",34.35 ,2003-07-30 00:00:00
"JJJ","Prod uct One (MXT205)",34.35 ,2003-09-01 00:00:00
"KKK","Prod uct One (MXT205)",1000, 2003-08-30 00:00:00
"ZZZ","Prod uct One (MXT548)",54,20 03-01-13 00:00:00
"AAA","Prod uct One (MXT548)",25.04 ,2003-08-31 00:00:00
"BBB","Prod uct One (MXT548)",33.95 ,2003-07-22 00:00:00
"BBB","Prod uct One (MXT548)",33.95 ,2003-07-27 00:00:00
"BBB","Prod uct One (MXT548)",35.22 ,2003-08-04 00:00:00
"BBB","Prod uct One (MXT548)",33.8, 2003-08-16 00:00:00
"BBB","Prod uct One (MXT548)",33.8, 2003-08-27 00:00:00
"CCC","Prod uct One (MXT548)",1000, 2003-08-30 00:00:00
"DDD","Prod uct One (MXT548)",46.25 ,2003-01-02 00:00:00
"EEE","Prod uct One (MXT548)",1000, 2003-08-30 00:00:00
"FFF","Prod uct One (MXT548)",1000, 2003-08-30 00:00:00
"GGG","Prod uct One (MXT548)",1000, 2003-09-01 00:00:00
"HHH","Prod uct One (MXT548)",33.8, 2003-08-04 00:00:00
"III","Prod uct One (MXT548)",1000, 2003-01-13 00:00:00
"JJJ","Prod uct One (MXT548)",34.35 ,2003-07-30 00:00:00
"JJJ","Prod uct One (MXT548)",34.35 ,2003-09-01 00:00:00
"KKK","Prod uct One (MXT548)",1000, 2003-08-30 00:00:00
"ZZZ","Prod uct Two (DGT6789)",54,2 003-01-13 00:00:00
"AAA","Prod uct Two (DGT6789)",1000 ,2003-08-30 00:00:00
"BBB","Prod uct Two (DGT6789)",1000 ,2003-07-22 00:00:00
"BBB","Prod uct Two (DGT6789)",44.7 5,2003-07-27 00:00:00
"BBB","Prod uct Two (DGT6789)",44.7 5,2003-07-29 00:00:00
"BBB","Prod uct Two (DGT6789)",44.3 5,2003-07-30 00:00:00
"BBB","Prod uct Two (DGT6789)",44.7 5,2003-08-04 00:00:00
"BBB","Prod uct Two (DGT6789)",44.7 5,2003-08-04 00:00:00
"BBB","Prod uct Two (DGT6789)",1000 ,2003-08-23 00:00:00
"BBB","Prod uct Two (DGT6789)",44.7 5,2003-08-27 00:00:00
"CCC","Prod uct Two (DGT6789)",1000 ,2003-08-30 00:00:00
"DDD","Prod uct Two (DGT6789)",46.2 5,2003-05-08 00:00:00
"EEE","Prod uct Two (DGT6789)",1000 ,2003-08-30 00:00:00
"FFF","Prod uct Two (DGT6789)",1000 ,2003-08-30 00:00:00
"GGG","Prod uct Two (DGT6789)",1000 ,2003-09-01 00:00:00
"HHH","Prod uct Two (DGT6789)",46.1 ,2003-08-04 00:00:00
"III","Prod uct Two (DGT6789)",1000 ,2003-01-13 00:00:00
"JJJ","Prod uct Two (DGT6789)",47.4 5,2003-08-04 00:00:00
"JJJ","Prod uct Two (DGT6789)",47.4 5,2003-09-01 00:00:00
"KKK","Prod uct Two (DGT6789)",44.7 5,2003-07-14 00:00:00
"ZZZ","Prod uct Two (DGT6704)",54,2 003-01-13 00:00:00
"AAA","Prod uct Two (DGT6704)",1000 ,2003-08-30 00:00:00
"BBB","Prod uct Two (DGT6704)",45.5 ,2003-07-22 00:00:00
"BBB","Prod uct Two (DGT6704)",46.1 ,2003-08-04 00:00:00
"BBB","Prod uct Two (DGT6704)",46.1 ,2003-08-16 00:00:00
"BBB","Prod uct Two (DGT6704)",44.7 5,2003-08-27 00:00:00
"CCC","Prod uct Two (DGT6704)",1000 ,2003-08-30 00:00:00
"DDD","Prod uct Two (DGT6704)",46.2 5,2003-05-08 00:00:00
"EEE","Prod uct Two (DGT6704)",1000 ,2003-08-30 00:00:00
"FFF","Prod uct Two (DGT6704)",1000 ,2003-08-30 00:00:00
"GGG","Prod uct Two (DGT6704)",1000 ,2003-09-01 00:00:00
"HHH","Prod uct Two (DGT6704)",46.1 ,2003-08-04 00:00:00
"III","Prod uct Two (DGT6704)",1000 ,2003-01-13 00:00:00
"JJJ","Prod uct Two (DGT6704)",47.4 5,2003-08-04 00:00:00
"JJJ","Prod uct Two (DGT6704)",47.4 5,2003-09-01 00:00:00
"KKK","Prod uct Two (DGT6704)",44.7 5,2003-07-14 00:00:00
"ZZZ","Prod uct Three (QAT6785)",52,2 003-01-13 00:00:00
"AAA","Prod uct Three (QAT6785)",1000 ,2003-08-30 00:00:00
"BBB","Prod uct Three (QAT6785)",45.5 ,2003-07-22 00:00:00
"BBB","Prod uct Three (QAT6785)",532. 25,2003-07-29 00:00:00
"BBB","Prod uct Three (QAT6785)",1000 ,2003-07-30 00:00:00
"BBB","Prod uct Three (QAT6785)",1000 ,2003-08-04 00:00:00
"BBB","Prod uct Three (QAT6785)",46.1 ,2003-08-16 00:00:00
"BBB","Prod uct Three (QAT6785)",44.7 5,2003-08-27 00:00:00
"CCC","Prod uct Three (QAT6785)",42.6 5,2003-08-31 00:00:00
"DDD","Prod uct Three (QAT6785)",46.2 5,2003-05-08 00:00:00
"EEE","Prod uct Three (QAT6785)",1000 ,2003-08-30 00:00:00
"FFF","Prod uct Three (QAT6785)",1000 ,2003-08-30 00:00:00
"GGG","Prod uct Three (QAT6785)",1000 ,2003-09-01 00:00:00
"HHH","Prod uct Three (QAT6785)",46.1 ,2003-08-04 00:00:00
"III","Prod uct Three (QAT6785)",1000 ,2003-01-13 00:00:00
"JJJ","Prod uct Three (QAT6785)",40.4 5,2003-08-04 00:00:00
"JJJ","Prod uct Three (QAT6785)",40.4 5,2003-09-01 00:00:00
"KKK","Prod uct Three (QAT6785)",44.7 5,2003-07-14 00:00:00
"ZZZ","Prod uct Three (QAT556)",50.23 ,2003-01-13 00:00:00
"AAA","Prod uct Three (QAT556)",1000, 2003-08-30 00:00:00
"BBB","Prod uct Three (QAT556)",44.75 ,2003-07-22 00:00:00
"BBB","Prod uct Three (QAT556)",532.2 5,2003-07-29 00:00:00
"BBB","Prod uct Three (QAT556)",1000, 2003-07-30 00:00:00
"BBB","Prod uct Three (QAT556)",1000, 2003-08-03 00:00:00
"BBB","Prod uct Three (QAT556)",1000, 2003-08-04 00:00:00
"BBB","Prod uct Three (QAT556)",44.75 ,2003-08-16 00:00:00
"BBB","Prod uct Three (QAT556)",44.75 ,2003-08-27 00:00:00
"CCC","Prod uct Three (QAT556)",1000, 2003-08-30 00:00:00
"DDD","Prod uct Three (QAT556)",46.25 ,2003-05-08 00:00:00
"EEE","Prod uct Three (QAT556)",1000, 2003-08-30 00:00:00
"FFF","Prod uct Three (QAT556)",1000, 2003-08-30 00:00:00
"GGG","Prod uct Three (QAT556)",1000, 2003-09-01 00:00:00
"HHH","Prod uct Three (QAT556)",46.1, 2003-08-04 00:00:00
"III","Prod uct Three (QAT556)",1000, 2003-01-13 00:00:00
"JJJ","Prod uct Three (QAT556)",40.45 ,2003-08-04 00:00:00
"JJJ","Prod uct Three (QAT556)",40.45 ,2003-09-01 00:00:00
"KKK","Prod uct Three (QAT556)",44.75 ,2003-07-14 00:00:00
"JJJ","Prod uct Three (QAT556)",40.15 ,2003-08-01 00:00:00 9 3136
"netpurpose " <ne********@hot mail.com> wrote in message
news:31******** *************** ***@posting.goo gle.com... I need to extract data from this table to find the lowest prices of each product as of today. The product will be listed/grouped by the name only, discarding the product code - I use SUBSTRING(Produ ctName, 1, CHARINDEX('(', ProductName)-2).
I can get this result, but I had to use several views (totally inefficient). I think this can be done in one efficient/fast query, but I can't think of one.
In the case that one query is not possible, is there other efficient way to get the results? There are about 300K records in the table.
Any help is appreciated! Thanks a lot in advance!
Thanks, Netpurpose
SELECT P1.ProductName, P1.Price
FROM ProductList AS P1
WHERE EffectiveDate <= CURRENT_TIMESTA MP AND
NOT EXISTS
(SELECT *
FROM ProductList AS P2
WHERE SUBSTRING(P1.Pr oductName, 1,
CHARINDEX('(', P1.ProductName) - 2) =
SUBSTRING(P2.Pr oductName, 1,
CHARINDEX('(', P2.ProductName) - 2) AND
P2.EffectiveDat e <= CURRENT_TIMESTA MP AND
P2.Price < P1.Price)
Regards,
jag -----------------------START TABLE INFO-------------------------
Here is the desired result (on 8/29/03):
ProductName Price ---------------------------------------- --------------------- Product One 33.5000 Product Three 40.4500 Product Two 44.7500
Here is the table info:
CREATE TABLE [ProductList] ( [Distributor] [nvarchar] (5) NULL , [ProductName] [nvarchar] (40) NULL , [Price] [money] NULL , [EffectiveDate] [smalldatetime] NULL ) ON [PRIMARY]
Table Data (comma delimited): *Sorry I can't list in insert commands as there are too many.
"Manufacturer", "ProductName"," Price","Effecti veDate" "AAA","Prod uct One (MXT234)",1000, 2003-08-30 00:00:00 "BBB","Prod uct One (MXT234)",1000, 2003-07-29 00:00:00 "BBB","Prod uct One (MXT234)",28.15 ,2003-07-30 00:00:00 "BBB","Prod uct One (MXT234)",35.22 ,2003-08-04 00:00:00 "BBB","Prod uct One (MXT234)",35.22 ,2003-08-04 00:00:00 "BBB","Prod uct One (MXT234)",1000, 2003-08-16 00:00:00 "BBB","Prod uct One (MXT234)",33.8, 2003-08-27 00:00:00 "CCC","Prod uct One (MXT234)",33.25 ,2003-08-31 00:00:00 "DDD","Prod uct One (MXT234)",46.25 ,2003-01-02 00:00:00 "EEE","Prod uct One (MXT234)",1000, 2003-08-30 00:00:00 "FFF","Prod uct One (MXT234)",1000, 2003-08-30 00:00:00 "GGG","Prod uct One (MXT234)",1000, 2003-09-01 00:00:00 "HHH","Prod uct One (MXT234)",33.8, 2003-08-04 00:00:00 "III","Prod uct One (MXT234)",1000, 2003-01-13 00:00:00 "JJJ","Prod uct One (MXT234)",34.35 ,2003-07-30 00:00:00 "JJJ","Prod uct One (MXT234)",34.35 ,2003-09-01 00:00:00 "KKK","Prod uct One (MXT234)",1000, 2003-08-30 00:00:00 "ZZZ","Prod uct One (MXT205)",54,20 03-01-13 00:00:00 "AAA","Prod uct One (MXT205)",1000, 2003-08-30 00:00:00 "BBB","Prod uct One (MXT205)",33.95 ,2003-07-27 00:00:00 "BBB","Prod uct One (MXT205)",33.95 ,2003-07-29 00:00:00 "BBB","Prod uct One (MXT205)",35.22 ,2003-08-04 00:00:00 "BBB","Prod uct One (MXT205)",33.8, 2003-08-16 00:00:00 "BBB","Prod uct One (MXT205)",33.5, 2003-08-27 00:00:00 "CCC","Prod uct One (MXT205)",1000, 2003-08-30 00:00:00 "DDD","Prod uct One (MXT205)",46.25 ,2003-01-02 00:00:00 "EEE","Prod uct One (MXT205)",1000, 2003-08-30 00:00:00 "FFF","Prod uct One (MXT205)",1000, 2003-08-30 00:00:00 "GGG","Prod uct One (MXT205)",1000, 2003-09-01 00:00:00 "HHH","Prod uct One (MXT205)",33.95 ,2003-08-04 00:00:00 "III","Prod uct One (MXT205)",1000, 2003-01-13 00:00:00 "JJJ","Prod uct One (MXT205)",34.35 ,2003-07-30 00:00:00 "JJJ","Prod uct One (MXT205)",34.35 ,2003-09-01 00:00:00 "KKK","Prod uct One (MXT205)",1000, 2003-08-30 00:00:00 "ZZZ","Prod uct One (MXT548)",54,20 03-01-13 00:00:00 "AAA","Prod uct One (MXT548)",25.04 ,2003-08-31 00:00:00 "BBB","Prod uct One (MXT548)",33.95 ,2003-07-22 00:00:00 "BBB","Prod uct One (MXT548)",33.95 ,2003-07-27 00:00:00 "BBB","Prod uct One (MXT548)",35.22 ,2003-08-04 00:00:00 "BBB","Prod uct One (MXT548)",33.8, 2003-08-16 00:00:00 "BBB","Prod uct One (MXT548)",33.8, 2003-08-27 00:00:00 "CCC","Prod uct One (MXT548)",1000, 2003-08-30 00:00:00 "DDD","Prod uct One (MXT548)",46.25 ,2003-01-02 00:00:00 "EEE","Prod uct One (MXT548)",1000, 2003-08-30 00:00:00 "FFF","Prod uct One (MXT548)",1000, 2003-08-30 00:00:00 "GGG","Prod uct One (MXT548)",1000, 2003-09-01 00:00:00 "HHH","Prod uct One (MXT548)",33.8, 2003-08-04 00:00:00 "III","Prod uct One (MXT548)",1000, 2003-01-13 00:00:00 "JJJ","Prod uct One (MXT548)",34.35 ,2003-07-30 00:00:00 "JJJ","Prod uct One (MXT548)",34.35 ,2003-09-01 00:00:00 "KKK","Prod uct One (MXT548)",1000, 2003-08-30 00:00:00 "ZZZ","Prod uct Two (DGT6789)",54,2 003-01-13 00:00:00 "AAA","Prod uct Two (DGT6789)",1000 ,2003-08-30 00:00:00 "BBB","Prod uct Two (DGT6789)",1000 ,2003-07-22 00:00:00 "BBB","Prod uct Two (DGT6789)",44.7 5,2003-07-27 00:00:00 "BBB","Prod uct Two (DGT6789)",44.7 5,2003-07-29 00:00:00 "BBB","Prod uct Two (DGT6789)",44.3 5,2003-07-30 00:00:00 "BBB","Prod uct Two (DGT6789)",44.7 5,2003-08-04 00:00:00 "BBB","Prod uct Two (DGT6789)",44.7 5,2003-08-04 00:00:00 "BBB","Prod uct Two (DGT6789)",1000 ,2003-08-23 00:00:00 "BBB","Prod uct Two (DGT6789)",44.7 5,2003-08-27 00:00:00 "CCC","Prod uct Two (DGT6789)",1000 ,2003-08-30 00:00:00 "DDD","Prod uct Two (DGT6789)",46.2 5,2003-05-08 00:00:00 "EEE","Prod uct Two (DGT6789)",1000 ,2003-08-30 00:00:00 "FFF","Prod uct Two (DGT6789)",1000 ,2003-08-30 00:00:00 "GGG","Prod uct Two (DGT6789)",1000 ,2003-09-01 00:00:00 "HHH","Prod uct Two (DGT6789)",46.1 ,2003-08-04 00:00:00 "III","Prod uct Two (DGT6789)",1000 ,2003-01-13 00:00:00 "JJJ","Prod uct Two (DGT6789)",47.4 5,2003-08-04 00:00:00 "JJJ","Prod uct Two (DGT6789)",47.4 5,2003-09-01 00:00:00 "KKK","Prod uct Two (DGT6789)",44.7 5,2003-07-14 00:00:00 "ZZZ","Prod uct Two (DGT6704)",54,2 003-01-13 00:00:00 "AAA","Prod uct Two (DGT6704)",1000 ,2003-08-30 00:00:00 "BBB","Prod uct Two (DGT6704)",45.5 ,2003-07-22 00:00:00 "BBB","Prod uct Two (DGT6704)",46.1 ,2003-08-04 00:00:00 "BBB","Prod uct Two (DGT6704)",46.1 ,2003-08-16 00:00:00 "BBB","Prod uct Two (DGT6704)",44.7 5,2003-08-27 00:00:00 "CCC","Prod uct Two (DGT6704)",1000 ,2003-08-30 00:00:00 "DDD","Prod uct Two (DGT6704)",46.2 5,2003-05-08 00:00:00 "EEE","Prod uct Two (DGT6704)",1000 ,2003-08-30 00:00:00 "FFF","Prod uct Two (DGT6704)",1000 ,2003-08-30 00:00:00 "GGG","Prod uct Two (DGT6704)",1000 ,2003-09-01 00:00:00 "HHH","Prod uct Two (DGT6704)",46.1 ,2003-08-04 00:00:00 "III","Prod uct Two (DGT6704)",1000 ,2003-01-13 00:00:00 "JJJ","Prod uct Two (DGT6704)",47.4 5,2003-08-04 00:00:00 "JJJ","Prod uct Two (DGT6704)",47.4 5,2003-09-01 00:00:00 "KKK","Prod uct Two (DGT6704)",44.7 5,2003-07-14 00:00:00 "ZZZ","Prod uct Three (QAT6785)",52,2 003-01-13 00:00:00 "AAA","Prod uct Three (QAT6785)",1000 ,2003-08-30 00:00:00 "BBB","Prod uct Three (QAT6785)",45.5 ,2003-07-22 00:00:00 "BBB","Prod uct Three (QAT6785)",532. 25,2003-07-29 00:00:00 "BBB","Prod uct Three (QAT6785)",1000 ,2003-07-30 00:00:00 "BBB","Prod uct Three (QAT6785)",1000 ,2003-08-04 00:00:00 "BBB","Prod uct Three (QAT6785)",46.1 ,2003-08-16 00:00:00 "BBB","Prod uct Three (QAT6785)",44.7 5,2003-08-27 00:00:00 "CCC","Prod uct Three (QAT6785)",42.6 5,2003-08-31 00:00:00 "DDD","Prod uct Three (QAT6785)",46.2 5,2003-05-08 00:00:00 "EEE","Prod uct Three (QAT6785)",1000 ,2003-08-30 00:00:00 "FFF","Prod uct Three (QAT6785)",1000 ,2003-08-30 00:00:00 "GGG","Prod uct Three (QAT6785)",1000 ,2003-09-01 00:00:00 "HHH","Prod uct Three (QAT6785)",46.1 ,2003-08-04 00:00:00 "III","Prod uct Three (QAT6785)",1000 ,2003-01-13 00:00:00 "JJJ","Prod uct Three (QAT6785)",40.4 5,2003-08-04 00:00:00 "JJJ","Prod uct Three (QAT6785)",40.4 5,2003-09-01 00:00:00 "KKK","Prod uct Three (QAT6785)",44.7 5,2003-07-14 00:00:00 "ZZZ","Prod uct Three (QAT556)",50.23 ,2003-01-13 00:00:00 "AAA","Prod uct Three (QAT556)",1000, 2003-08-30 00:00:00 "BBB","Prod uct Three (QAT556)",44.75 ,2003-07-22 00:00:00 "BBB","Prod uct Three (QAT556)",532.2 5,2003-07-29 00:00:00 "BBB","Prod uct Three (QAT556)",1000, 2003-07-30 00:00:00 "BBB","Prod uct Three (QAT556)",1000, 2003-08-03 00:00:00 "BBB","Prod uct Three (QAT556)",1000, 2003-08-04 00:00:00 "BBB","Prod uct Three (QAT556)",44.75 ,2003-08-16 00:00:00 "BBB","Prod uct Three (QAT556)",44.75 ,2003-08-27 00:00:00 "CCC","Prod uct Three (QAT556)",1000, 2003-08-30 00:00:00 "DDD","Prod uct Three (QAT556)",46.25 ,2003-05-08 00:00:00 "EEE","Prod uct Three (QAT556)",1000, 2003-08-30 00:00:00 "FFF","Prod uct Three (QAT556)",1000, 2003-08-30 00:00:00 "GGG","Prod uct Three (QAT556)",1000, 2003-09-01 00:00:00 "HHH","Prod uct Three (QAT556)",46.1, 2003-08-04 00:00:00 "III","Prod uct Three (QAT556)",1000, 2003-01-13 00:00:00 "JJJ","Prod uct Three (QAT556)",40.45 ,2003-08-04 00:00:00 "JJJ","Prod uct Three (QAT556)",40.45 ,2003-09-01 00:00:00 "KKK","Prod uct Three (QAT556)",44.75 ,2003-07-14 00:00:00 "JJJ","Prod uct Three (QAT556)",40.15 ,2003-08-01 00:00:00
"netpurpose " <ne********@hot mail.com> wrote in message
news:31******** *************** ***@posting.goo gle.com... Hi Jag,
Thank you for your response. I tried the query, but it gives me a different pricing result:
ProductName Price ---------------------------------------- --------------------- Product One (MXT234) 28.1500 Product Two (DGT6789) 44.3500 Product Three (QAT556) 40.1500
The correct result should be:
ProductName Price ---------------------------------------- --------------------- Product One 33.5000 Product Two 44.7500 Product Three 40.4500
The reason is because for Product One, Manufacturer "BBB" has a price of 28.15 on 2003-07-30, this price is no longer valid because "BBB" has more recent price of 33.5 starting on 2003-08-27. Same case with Product Two and Three (manufacturer "JJJ" overrides the 40.15 on 2003-08-01 with 40.45 on 2003-08-04).
I have to take the most recent prices from each manufacturer for a particular product (price at the latest date as of today), and then take the minimum price from the result.
Again, thank you for your help. Sorry I might not explain it well in the beginning. Any further help is appreciated. Thank you very much in advance!
Regards, Netpurpose
I was a little too quick on the draw and misread your requirements.
SELECT ProductName, MIN(Price)
FROM (SELECT P1.Distributor,
SUBSTRING(P1.Pr oductName, 1,
CHARINDEX('(', P1.ProductName) - 2) AS
ProductName,
P1.EffectiveDat e AS LatestDate,
P1.price
FROM ProductList AS P1
LEFT OUTER JOIN
ProductList AS P2
ON P1.Distributor = P2.Distributor AND
P1.ProductName = P2.ProductName AND
P1.EffectiveDat e <= CURRENT_TIMESTA MP AND
P2.EffectiveDat e <= CURRENT_TIMESTA MP AND
P2.EffectiveDat e > P1.EffectiveDat e
WHERE P2.EffectiveDat e IS NULL) AS LatestPrices
GROUP BY ProductName
Regards,
jag
"John Gilson" <ja*@acm.org> wrote in message news:<_u******* **************@ twister.nyc.rr. com>... "netpurpose " <ne********@hot mail.com> wrote in message news:31******** *************** ***@posting.goo gle.com... I need to extract data from this table to find the lowest prices of each product as of today. The product will be listed/grouped by the name only, discarding the product code - I use SUBSTRING(Produ ctName, 1, CHARINDEX('(', ProductName)-2).
I can get this result, but I had to use several views (totally inefficient). I think this can be done in one efficient/fast query, but I can't think of one.
In the case that one query is not possible, is there other efficient way to get the results? There are about 300K records in the table.
Any help is appreciated! Thanks a lot in advance!
Thanks, Netpurpose
SELECT P1.ProductName, P1.Price FROM ProductList AS P1 WHERE EffectiveDate <= CURRENT_TIMESTA MP AND NOT EXISTS (SELECT * FROM ProductList AS P2 WHERE SUBSTRING(P1.Pr oductName, 1, CHARINDEX('(', P1.ProductName) - 2) = SUBSTRING(P2.Pr oductName, 1, CHARINDEX('(', P2.ProductName) - 2) AND P2.EffectiveDat e <= CURRENT_TIMESTA MP AND P2.Price < P1.Price)
Regards, jag
-----------------------START TABLE INFO-------------------------
Here is the desired result (on 8/29/03):
ProductName Price ---------------------------------------- --------------------- Product One 33.5000 Product Three 40.4500 Product Two 44.7500
Here is the table info:
CREATE TABLE [ProductList] ( [Distributor] [nvarchar] (5) NULL , [ProductName] [nvarchar] (40) NULL , [Price] [money] NULL , [EffectiveDate] [smalldatetime] NULL ) ON [PRIMARY]
Table Data (comma delimited): *Sorry I can't list in insert commands as there are too many.
"Manufacturer", "ProductName"," Price","Effecti veDate" "AAA","Prod uct One (MXT234)",1000, 2003-08-30 00:00:00 "BBB","Prod uct One (MXT234)",1000, 2003-07-29 00:00:00 "BBB","Prod uct One (MXT234)",28.15 ,2003-07-30 00:00:00 "BBB","Prod uct One (MXT234)",35.22 ,2003-08-04 00:00:00 "BBB","Prod uct One (MXT234)",35.22 ,2003-08-04 00:00:00 "BBB","Prod uct One (MXT234)",1000, 2003-08-16 00:00:00 "BBB","Prod uct One (MXT234)",33.8, 2003-08-27 00:00:00 "CCC","Prod uct One (MXT234)",33.25 ,2003-08-31 00:00:00 "DDD","Prod uct One (MXT234)",46.25 ,2003-01-02 00:00:00 "EEE","Prod uct One (MXT234)",1000, 2003-08-30 00:00:00 "FFF","Prod uct One (MXT234)",1000, 2003-08-30 00:00:00 "GGG","Prod uct One (MXT234)",1000, 2003-09-01 00:00:00 "HHH","Prod uct One (MXT234)",33.8, 2003-08-04 00:00:00 "III","Prod uct One (MXT234)",1000, 2003-01-13 00:00:00 "JJJ","Prod uct One (MXT234)",34.35 ,2003-07-30 00:00:00 "JJJ","Prod uct One (MXT234)",34.35 ,2003-09-01 00:00:00 "KKK","Prod uct One (MXT234)",1000, 2003-08-30 00:00:00 "ZZZ","Prod uct One (MXT205)",54,20 03-01-13 00:00:00 "AAA","Prod uct One (MXT205)",1000, 2003-08-30 00:00:00 "BBB","Prod uct One (MXT205)",33.95 ,2003-07-27 00:00:00 "BBB","Prod uct One (MXT205)",33.95 ,2003-07-29 00:00:00 "BBB","Prod uct One (MXT205)",35.22 ,2003-08-04 00:00:00 "BBB","Prod uct One (MXT205)",33.8, 2003-08-16 00:00:00 "BBB","Prod uct One (MXT205)",33.5, 2003-08-27 00:00:00 "CCC","Prod uct One (MXT205)",1000, 2003-08-30 00:00:00 "DDD","Prod uct One (MXT205)",46.25 ,2003-01-02 00:00:00 "EEE","Prod uct One (MXT205)",1000, 2003-08-30 00:00:00 "FFF","Prod uct One (MXT205)",1000, 2003-08-30 00:00:00 "GGG","Prod uct One (MXT205)",1000, 2003-09-01 00:00:00 "HHH","Prod uct One (MXT205)",33.95 ,2003-08-04 00:00:00 "III","Prod uct One (MXT205)",1000, 2003-01-13 00:00:00 "JJJ","Prod uct One (MXT205)",34.35 ,2003-07-30 00:00:00 "JJJ","Prod uct One (MXT205)",34.35 ,2003-09-01 00:00:00 "KKK","Prod uct One (MXT205)",1000, 2003-08-30 00:00:00 "ZZZ","Prod uct One (MXT548)",54,20 03-01-13 00:00:00 "AAA","Prod uct One (MXT548)",25.04 ,2003-08-31 00:00:00 "BBB","Prod uct One (MXT548)",33.95 ,2003-07-22 00:00:00 "BBB","Prod uct One (MXT548)",33.95 ,2003-07-27 00:00:00 "BBB","Prod uct One (MXT548)",35.22 ,2003-08-04 00:00:00 "BBB","Prod uct One (MXT548)",33.8, 2003-08-16 00:00:00 "BBB","Prod uct One (MXT548)",33.8, 2003-08-27 00:00:00 "CCC","Prod uct One (MXT548)",1000, 2003-08-30 00:00:00 "DDD","Prod uct One (MXT548)",46.25 ,2003-01-02 00:00:00 "EEE","Prod uct One (MXT548)",1000, 2003-08-30 00:00:00 "FFF","Prod uct One (MXT548)",1000, 2003-08-30 00:00:00 "GGG","Prod uct One (MXT548)",1000, 2003-09-01 00:00:00 "HHH","Prod uct One (MXT548)",33.8, 2003-08-04 00:00:00 "III","Prod uct One (MXT548)",1000, 2003-01-13 00:00:00 "JJJ","Prod uct One (MXT548)",34.35 ,2003-07-30 00:00:00 "JJJ","Prod uct One (MXT548)",34.35 ,2003-09-01 00:00:00 "KKK","Prod uct One (MXT548)",1000, 2003-08-30 00:00:00 "ZZZ","Prod uct Two (DGT6789)",54,2 003-01-13 00:00:00 "AAA","Prod uct Two (DGT6789)",1000 ,2003-08-30 00:00:00 "BBB","Prod uct Two (DGT6789)",1000 ,2003-07-22 00:00:00 "BBB","Prod uct Two (DGT6789)",44.7 5,2003-07-27 00:00:00 "BBB","Prod uct Two (DGT6789)",44.7 5,2003-07-29 00:00:00 "BBB","Prod uct Two (DGT6789)",44.3 5,2003-07-30 00:00:00 "BBB","Prod uct Two (DGT6789)",44.7 5,2003-08-04 00:00:00 "BBB","Prod uct Two (DGT6789)",44.7 5,2003-08-04 00:00:00 "BBB","Prod uct Two (DGT6789)",1000 ,2003-08-23 00:00:00 "BBB","Prod uct Two (DGT6789)",44.7 5,2003-08-27 00:00:00 "CCC","Prod uct Two (DGT6789)",1000 ,2003-08-30 00:00:00 "DDD","Prod uct Two (DGT6789)",46.2 5,2003-05-08 00:00:00 "EEE","Prod uct Two (DGT6789)",1000 ,2003-08-30 00:00:00 "FFF","Prod uct Two (DGT6789)",1000 ,2003-08-30 00:00:00 "GGG","Prod uct Two (DGT6789)",1000 ,2003-09-01 00:00:00 "HHH","Prod uct Two (DGT6789)",46.1 ,2003-08-04 00:00:00 "III","Prod uct Two (DGT6789)",1000 ,2003-01-13 00:00:00 "JJJ","Prod uct Two (DGT6789)",47.4 5,2003-08-04 00:00:00 "JJJ","Prod uct Two (DGT6789)",47.4 5,2003-09-01 00:00:00 "KKK","Prod uct Two (DGT6789)",44.7 5,2003-07-14 00:00:00 "ZZZ","Prod uct Two (DGT6704)",54,2 003-01-13 00:00:00 "AAA","Prod uct Two (DGT6704)",1000 ,2003-08-30 00:00:00 "BBB","Prod uct Two (DGT6704)",45.5 ,2003-07-22 00:00:00 "BBB","Prod uct Two (DGT6704)",46.1 ,2003-08-04 00:00:00 "BBB","Prod uct Two (DGT6704)",46.1 ,2003-08-16 00:00:00 "BBB","Prod uct Two (DGT6704)",44.7 5,2003-08-27 00:00:00 "CCC","Prod uct Two (DGT6704)",1000 ,2003-08-30 00:00:00 "DDD","Prod uct Two (DGT6704)",46.2 5,2003-05-08 00:00:00 "EEE","Prod uct Two (DGT6704)",1000 ,2003-08-30 00:00:00 "FFF","Prod uct Two (DGT6704)",1000 ,2003-08-30 00:00:00 "GGG","Prod uct Two (DGT6704)",1000 ,2003-09-01 00:00:00 "HHH","Prod uct Two (DGT6704)",46.1 ,2003-08-04 00:00:00 "III","Prod uct Two (DGT6704)",1000 ,2003-01-13 00:00:00 "JJJ","Prod uct Two (DGT6704)",47.4 5,2003-08-04 00:00:00 "JJJ","Prod uct Two (DGT6704)",47.4 5,2003-09-01 00:00:00 "KKK","Prod uct Two (DGT6704)",44.7 5,2003-07-14 00:00:00 "ZZZ","Prod uct Three (QAT6785)",52,2 003-01-13 00:00:00 "AAA","Prod uct Three (QAT6785)",1000 ,2003-08-30 00:00:00 "BBB","Prod uct Three (QAT6785)",45.5 ,2003-07-22 00:00:00 "BBB","Prod uct Three (QAT6785)",532. 25,2003-07-29 00:00:00 "BBB","Prod uct Three (QAT6785)",1000 ,2003-07-30 00:00:00 "BBB","Prod uct Three (QAT6785)",1000 ,2003-08-04 00:00:00 "BBB","Prod uct Three (QAT6785)",46.1 ,2003-08-16 00:00:00 "BBB","Prod uct Three (QAT6785)",44.7 5,2003-08-27 00:00:00 "CCC","Prod uct Three (QAT6785)",42.6 5,2003-08-31 00:00:00 "DDD","Prod uct Three (QAT6785)",46.2 5,2003-05-08 00:00:00 "EEE","Prod uct Three (QAT6785)",1000 ,2003-08-30 00:00:00 "FFF","Prod uct Three (QAT6785)",1000 ,2003-08-30 00:00:00 "GGG","Prod uct Three (QAT6785)",1000 ,2003-09-01 00:00:00 "HHH","Prod uct Three (QAT6785)",46.1 ,2003-08-04 00:00:00 "III","Prod uct Three (QAT6785)",1000 ,2003-01-13 00:00:00 "JJJ","Prod uct Three (QAT6785)",40.4 5,2003-08-04 00:00:00 "JJJ","Prod uct Three (QAT6785)",40.4 5,2003-09-01 00:00:00 "KKK","Prod uct Three (QAT6785)",44.7 5,2003-07-14 00:00:00 "ZZZ","Prod uct Three (QAT556)",50.23 ,2003-01-13 00:00:00 "AAA","Prod uct Three (QAT556)",1000, 2003-08-30 00:00:00 "BBB","Prod uct Three (QAT556)",44.75 ,2003-07-22 00:00:00 "BBB","Prod uct Three (QAT556)",532.2 5,2003-07-29 00:00:00 "BBB","Prod uct Three (QAT556)",1000, 2003-07-30 00:00:00 "BBB","Prod uct Three (QAT556)",1000, 2003-08-03 00:00:00 "BBB","Prod uct Three (QAT556)",1000, 2003-08-04 00:00:00 "BBB","Prod uct Three (QAT556)",44.75 ,2003-08-16 00:00:00 "BBB","Prod uct Three (QAT556)",44.75 ,2003-08-27 00:00:00 "CCC","Prod uct Three (QAT556)",1000, 2003-08-30 00:00:00 "DDD","Prod uct Three (QAT556)",46.25 ,2003-05-08 00:00:00 "EEE","Prod uct Three (QAT556)",1000, 2003-08-30 00:00:00 "FFF","Prod uct Three (QAT556)",1000, 2003-08-30 00:00:00 "GGG","Prod uct Three (QAT556)",1000, 2003-09-01 00:00:00 "HHH","Prod uct Three (QAT556)",46.1, 2003-08-04 00:00:00 "III","Prod uct Three (QAT556)",1000, 2003-01-13 00:00:00 "JJJ","Prod uct Three (QAT556)",40.45 ,2003-08-04 00:00:00 "JJJ","Prod uct Three (QAT556)",40.45 ,2003-09-01 00:00:00 "KKK","Prod uct Three (QAT556)",44.75 ,2003-07-14 00:00:00 "JJJ","Prod uct Three (QAT556)",40.15 ,2003-08-01 00:00:00
Did I miscopy John's query? It gives me the future price of $25.04
for Product One, but is easy to fix by adding
AND P1.EffectiveDat e <= CURRENT_TIMESTA MP
to the where clause of the derived table LatestPrices
John's initial idea of NOT EXISTS still works, too, in part. (Note
that I blew off the ProductName parsing by taking a hatchet to that
column with an update...
-- Be lazy
UPDATE ProductList SET
ProductName =
SUBSTRING(Produ ctName, 1,CHARINDEX('(' , ProductName) - 2)
GO
-- For each product, select the lowest non-future
-- price that has not been superseded.
SELECT ProductName, MIN(Price) as Price -- get the lowest price
FROM (
SELECT ProductName, Price -- from all non-future prices
FROM ProductList P1
WHERE P1.EffectiveDat e <= CURRENT_TIMESTA MP
AND NOT EXISTS ( -- that have not been superseded
SELECT * FROM ProductList P2
WHERE P2.Distributor = P1.Distributor
AND P2.ProductName = P1.ProductName
AND P2.EffectiveDat e <= CURRENT_TIMESTA MP
AND P2.EffectiveDat e > P1.EffectiveDat e
)
) T
GROUP BY ProductName -- for each product
go
By the way, the delimited data was no trouble. I pasted it into
Excel, did Text To Columns, named the data region, and inserted it with:
insert into [ProductList]
select Manufacturer, ProductName, Price, EffectiveDate
from OpenRowset(
'Microsoft.Jet. OLEDB.4.0',
'Excel 8.0;Database=e: \excel\ProductL ist.xls',
'select * from ProductList')
Be sure to close the Excel file before inserting.
-- Steve Kass
-- Drew University
-- Ref: 2498EA13-1756-4C81-9307-25AFD5BB451D
netpurpose wrote: Hi Jag,
Thanks a lot!! It works like a charm! Your query saves me a lot of headache.
I have struggled with this for quite a while now and I am glad I find help here.
Again, thank you very much! I really appreciate your help in such a short time!
Regards, Netpurpose
"John Gilson" <ja*@acm.org> wrote in message news:<am******* **************@ twister.nyc.rr. com>...
"netpurpose " <ne********@hot mail.com> wrote in message news:31****** *************** *****@posting.g oogle.com...
Hi Jag,
Thank you for your response. I tried the query, but it gives me a different pricing result:
ProductNam e Price ---------------------------------------- --------------------- Product One (MXT234) 28.1500 Product Two (DGT6789) 44.3500 Product Three (QAT556) 40.1500
The correct result should be:
ProductNam e Price ---------------------------------------- --------------------- Product One 33.5000 Product Two 44.7500 Product Three 40.4500
The reason is because for Product One, Manufacturer "BBB" has a price of 28.15 on 2003-07-30, this price is no longer valid because "BBB" has more recent price of 33.5 starting on 2003-08-27. Same case with Product Two and Three (manufacturer "JJJ" overrides the 40.15 on 2003-08-01 with 40.45 on 2003-08-04).
I have to take the most recent prices from each manufacturer for a particular product (price at the latest date as of today), and then take the minimum price from the result.
Again, thank you for your help. Sorry I might not explain it well in the beginning. Any further help is appreciated. Thank you very much in advance!
Regards, Netpurpose
I was a little too quick on the draw and misread your requirements.
SELECT ProductName, MIN(Price) FROM (SELECT P1.Distributor, SUBSTRING(P1.Pr oductName, 1, CHARINDEX('(', P1.ProductName) - 2) AS ProductName, P1.EffectiveDat e AS LatestDate, P1.price FROM ProductList AS P1 LEFT OUTER JOIN ProductList AS P2 ON P1.Distributor = P2.Distributor AND P1.ProductName = P2.ProductName AND P1.EffectiveDat e <= CURRENT_TIMESTA MP AND P2.EffectiveDat e <= CURRENT_TIMESTA MP AND P2.EffectiveDat e > P1.EffectiveDat e WHERE P2.EffectiveDat e IS NULL) AS LatestPrices GROUP BY ProductName
Regards, jag
"John Gilson" <ja*@acm.org> wrote in message news:<_u******* **************@ twister.nyc.rr. com>...
"netpurpose " <ne********@hot mail.com> wrote in message news:31**** *************** *******@posting .google.com...
>I need to extract data from this table to find the lowest prices of >each product as of today. The product will be listed/grouped by the >name only, discarding the product code - I use SUBSTRING(Produ ctName, >1, CHARINDEX('(', ProductName)-2). > >I can get this result, but I had to use several views (totally >inefficien t). I think this can be done in one efficient/fast query, >but I can't think of one. > >In the case that one query is not possible, is there other efficient >way to get the results? There are about 300K records in the table. > >Any help is appreciated! Thanks a lot in advance! > >Thanks, >Netpurpo se
SELECT P1.ProductName, P1.Price FROM ProductList AS P1 WHERE EffectiveDate <= CURRENT_TIMESTA MP AND NOT EXISTS (SELECT * FROM ProductList AS P2 WHERE SUBSTRING(P1.Pr oductName, 1, CHARINDEX('(', P1.ProductName) - 2) = SUBSTRING(P2.Pr oductName, 1, CHARINDEX('(', P2.ProductName) - 2) AND P2.EffectiveDat e <= CURRENT_TIMESTA MP AND P2.Price < P1.Price)
Regards, jag
>-----------------------START TABLE INFO------------------------- > >Here is the desired result (on 8/29/03): > >ProductNam e Price >---------------------------------------- --------------------- >Product One 33.5000 >Product Three 40.4500 >Product Two 44.7500 > > >Here is the table info: > >CREATE TABLE [ProductList] ( >[Distributor] [nvarchar] (5) NULL , >[ProductName] [nvarchar] (40) NULL , >[Price] [money] NULL , >[EffectiveDate] [smalldatetime] NULL >) ON [PRIMARY] > > >Table Data (comma delimited): >*Sorry I can't list in insert commands as there are too many. > >"Manufactu rer","ProductNa me","Price","Ef fectiveDate" >"AAA","Pro duct One (MXT234)",1000, 2003-08-30 00:00:00 >"BBB","Pro duct One (MXT234)",1000, 2003-07-29 00:00:00 >"BBB","Pro duct One (MXT234)",28.15 ,2003-07-30 00:00:00 >"BBB","Pro duct One (MXT234)",35.22 ,2003-08-04 00:00:00 >"BBB","Pro duct One (MXT234)",35.22 ,2003-08-04 00:00:00 >"BBB","Pro duct One (MXT234)",1000, 2003-08-16 00:00:00 >"BBB","Pro duct One (MXT234)",33.8, 2003-08-27 00:00:00 >"CCC","Pro duct One (MXT234)",33.25 ,2003-08-31 00:00:00 >"DDD","Pro duct One (MXT234)",46.25 ,2003-01-02 00:00:00 >"EEE","Pro duct One (MXT234)",1000, 2003-08-30 00:00:00 >"FFF","Pro duct One (MXT234)",1000, 2003-08-30 00:00:00 >"GGG","Pro duct One (MXT234)",1000, 2003-09-01 00:00:00 >"HHH","Pro duct One (MXT234)",33.8, 2003-08-04 00:00:00 >"III","Pro duct One (MXT234)",1000, 2003-01-13 00:00:00 >"JJJ","Pro duct One (MXT234)",34.35 ,2003-07-30 00:00:00 >"JJJ","Pro duct One (MXT234)",34.35 ,2003-09-01 00:00:00 >"KKK","Pro duct One (MXT234)",1000, 2003-08-30 00:00:00 >"ZZZ","Pro duct One (MXT205)",54,20 03-01-13 00:00:00 >"AAA","Pro duct One (MXT205)",1000, 2003-08-30 00:00:00 >"BBB","Pro duct One (MXT205)",33.95 ,2003-07-27 00:00:00 >"BBB","Pro duct One (MXT205)",33.95 ,2003-07-29 00:00:00 >"BBB","Pro duct One (MXT205)",35.22 ,2003-08-04 00:00:00 >"BBB","Pro duct One (MXT205)",33.8, 2003-08-16 00:00:00 >"BBB","Pro duct One (MXT205)",33.5, 2003-08-27 00:00:00 >"CCC","Pro duct One (MXT205)",1000, 2003-08-30 00:00:00 >"DDD","Pro duct One (MXT205)",46.25 ,2003-01-02 00:00:00 >"EEE","Pro duct One (MXT205)",1000, 2003-08-30 00:00:00 >"FFF","Pro duct One (MXT205)",1000, 2003-08-30 00:00:00 >"GGG","Pro duct One (MXT205)",1000, 2003-09-01 00:00:00 >"HHH","Pro duct One (MXT205)",33.95 ,2003-08-04 00:00:00 >"III","Pro duct One (MXT205)",1000, 2003-01-13 00:00:00 >"JJJ","Pro duct One (MXT205)",34.35 ,2003-07-30 00:00:00 >"JJJ","Pro duct One (MXT205)",34.35 ,2003-09-01 00:00:00 >"KKK","Pro duct One (MXT205)",1000, 2003-08-30 00:00:00 >"ZZZ","Pro duct One (MXT548)",54,20 03-01-13 00:00:00 >"AAA","Pro duct One (MXT548)",25.04 ,2003-08-31 00:00:00 >"BBB","Pro duct One (MXT548)",33.95 ,2003-07-22 00:00:00 >"BBB","Pro duct One (MXT548)",33.95 ,2003-07-27 00:00:00 >"BBB","Pro duct One (MXT548)",35.22 ,2003-08-04 00:00:00 >"BBB","Pro duct One (MXT548)",33.8, 2003-08-16 00:00:00 >"BBB","Pro duct One (MXT548)",33.8, 2003-08-27 00:00:00 >"CCC","Pro duct One (MXT548)",1000, 2003-08-30 00:00:00 >"DDD","Pro duct One (MXT548)",46.25 ,2003-01-02 00:00:00 >"EEE","Pro duct One (MXT548)",1000, 2003-08-30 00:00:00 >"FFF","Pro duct One (MXT548)",1000, 2003-08-30 00:00:00 >"GGG","Pro duct One (MXT548)",1000, 2003-09-01 00:00:00 >"HHH","Pro duct One (MXT548)",33.8, 2003-08-04 00:00:00 >"III","Pro duct One (MXT548)",1000, 2003-01-13 00:00:00 >"JJJ","Pro duct One (MXT548)",34.35 ,2003-07-30 00:00:00 >"JJJ","Pro duct One (MXT548)",34.35 ,2003-09-01 00:00:00 >"KKK","Pro duct One (MXT548)",1000, 2003-08-30 00:00:00 >"ZZZ","Pro duct Two (DGT6789)",54,2 003-01-13 00:00:00 >"AAA","Pro duct Two (DGT6789)",1000 ,2003-08-30 00:00:00 >"BBB","Pro duct Two (DGT6789)",1000 ,2003-07-22 00:00:00 >"BBB","Pro duct Two (DGT6789)",44.7 5,2003-07-27 00:00:00 >"BBB","Pro duct Two (DGT6789)",44.7 5,2003-07-29 00:00:00 >"BBB","Pro duct Two (DGT6789)",44.3 5,2003-07-30 00:00:00 >"BBB","Pro duct Two (DGT6789)",44.7 5,2003-08-04 00:00:00 >"BBB","Pro duct Two (DGT6789)",44.7 5,2003-08-04 00:00:00 >"BBB","Pro duct Two (DGT6789)",1000 ,2003-08-23 00:00:00 >"BBB","Pro duct Two (DGT6789)",44.7 5,2003-08-27 00:00:00 >"CCC","Pro duct Two (DGT6789)",1000 ,2003-08-30 00:00:00 >"DDD","Pro duct Two (DGT6789)",46.2 5,2003-05-08 00:00:00 >"EEE","Pro duct Two (DGT6789)",1000 ,2003-08-30 00:00:00 >"FFF","Pro duct Two (DGT6789)",1000 ,2003-08-30 00:00:00 >"GGG","Pro duct Two (DGT6789)",1000 ,2003-09-01 00:00:00 >"HHH","Pro duct Two (DGT6789)",46.1 ,2003-08-04 00:00:00 >"III","Pro duct Two (DGT6789)",1000 ,2003-01-13 00:00:00 >"JJJ","Pro duct Two (DGT6789)",47.4 5,2003-08-04 00:00:00 >"JJJ","Pro duct Two (DGT6789)",47.4 5,2003-09-01 00:00:00 >"KKK","Pro duct Two (DGT6789)",44.7 5,2003-07-14 00:00:00 >"ZZZ","Pro duct Two (DGT6704)",54,2 003-01-13 00:00:00 >"AAA","Pro duct Two (DGT6704)",1000 ,2003-08-30 00:00:00 >"BBB","Pro duct Two (DGT6704)",45.5 ,2003-07-22 00:00:00 >"BBB","Pro duct Two (DGT6704)",46.1 ,2003-08-04 00:00:00 >"BBB","Pro duct Two (DGT6704)",46.1 ,2003-08-16 00:00:00 >"BBB","Pro duct Two (DGT6704)",44.7 5,2003-08-27 00:00:00 >"CCC","Pro duct Two (DGT6704)",1000 ,2003-08-30 00:00:00 >"DDD","Pro duct Two (DGT6704)",46.2 5,2003-05-08 00:00:00 >"EEE","Pro duct Two (DGT6704)",1000 ,2003-08-30 00:00:00 >"FFF","Pro duct Two (DGT6704)",1000 ,2003-08-30 00:00:00 >"GGG","Pro duct Two (DGT6704)",1000 ,2003-09-01 00:00:00 >"HHH","Pro duct Two (DGT6704)",46.1 ,2003-08-04 00:00:00 >"III","Pro duct Two (DGT6704)",1000 ,2003-01-13 00:00:00 >"JJJ","Pro duct Two (DGT6704)",47.4 5,2003-08-04 00:00:00 >"JJJ","Pro duct Two (DGT6704)",47.4 5,2003-09-01 00:00:00 >"KKK","Pro duct Two (DGT6704)",44.7 5,2003-07-14 00:00:00 >"ZZZ","Pro duct Three (QAT6785)",52,2 003-01-13 00:00:00 >"AAA","Pro duct Three (QAT6785)",1000 ,2003-08-30 00:00:00 >"BBB","Pro duct Three (QAT6785)",45.5 ,2003-07-22 00:00:00 >"BBB","Pro duct Three (QAT6785)",532. 25,2003-07-29 00:00:00 >"BBB","Pro duct Three (QAT6785)",1000 ,2003-07-30 00:00:00 >"BBB","Pro duct Three (QAT6785)",1000 ,2003-08-04 00:00:00 >"BBB","Pro duct Three (QAT6785)",46.1 ,2003-08-16 00:00:00 >"BBB","Pro duct Three (QAT6785)",44.7 5,2003-08-27 00:00:00 >"CCC","Pro duct Three (QAT6785)",42.6 5,2003-08-31 00:00:00 >"DDD","Pro duct Three (QAT6785)",46.2 5,2003-05-08 00:00:00 >"EEE","Pro duct Three (QAT6785)",1000 ,2003-08-30 00:00:00 >"FFF","Pro duct Three (QAT6785)",1000 ,2003-08-30 00:00:00 >"GGG","Pro duct Three (QAT6785)",1000 ,2003-09-01 00:00:00 >"HHH","Pro duct Three (QAT6785)",46.1 ,2003-08-04 00:00:00 >"III","Pro duct Three (QAT6785)",1000 ,2003-01-13 00:00:00 >"JJJ","Pro duct Three (QAT6785)",40.4 5,2003-08-04 00:00:00 >"JJJ","Pro duct Three (QAT6785)",40.4 5,2003-09-01 00:00:00 >"KKK","Pro duct Three (QAT6785)",44.7 5,2003-07-14 00:00:00 >"ZZZ","Pro duct Three (QAT556)",50.23 ,2003-01-13 00:00:00 >"AAA","Pro duct Three (QAT556)",1000, 2003-08-30 00:00:00 >"BBB","Pro duct Three (QAT556)",44.75 ,2003-07-22 00:00:00 >"BBB","Pro duct Three (QAT556)",532.2 5,2003-07-29 00:00:00 >"BBB","Pro duct Three (QAT556)",1000, 2003-07-30 00:00:00 >"BBB","Pro duct Three (QAT556)",1000, 2003-08-03 00:00:00 >"BBB","Pro duct Three (QAT556)",1000, 2003-08-04 00:00:00 >"BBB","Pro duct Three (QAT556)",44.75 ,2003-08-16 00:00:00 >"BBB","Pro duct Three (QAT556)",44.75 ,2003-08-27 00:00:00 >"CCC","Pro duct Three (QAT556)",1000, 2003-08-30 00:00:00 >"DDD","Pro duct Three (QAT556)",46.25 ,2003-05-08 00:00:00 >"EEE","Pro duct Three (QAT556)",1000, 2003-08-30 00:00:00 >"FFF","Pro duct Three (QAT556)",1000, 2003-08-30 00:00:00 >"GGG","Pro duct Three (QAT556)",1000, 2003-09-01 00:00:00 >"HHH","Pro duct Three (QAT556)",46.1, 2003-08-04 00:00:00 >"III","Pro duct Three (QAT556)",1000, 2003-01-13 00:00:00 >"JJJ","Pro duct Three (QAT556)",40.45 ,2003-08-04 00:00:00 >"JJJ","Pro duct Three (QAT556)",40.45 ,2003-09-01 00:00:00 >"KKK","Pro duct Three (QAT556)",44.75 ,2003-07-14 00:00:00 >"JJJ","Pro duct Three (QAT556)",40.15 ,2003-08-01 00:00:00
Hi Steve,
Thanks for your response. When I copied John's query, it works fine
and gives me the correct results. I did run both your query and his
against 330K of records, and it seems that his query runs much faster
(more than 10 times faster). I ran your query against clean products
names without the codes.
I am too novice to even know why the big difference. Both query
yields the same results. Can you tell why his statement is much more
efficient? LEFT OUTER JOIN vs EXISTS? I do want to learn from this.
Also, thanks for showing me the command to import the excel file to
db. Good to know :)
Thanks,
Netpurpose
Steve Kass <sk***@drew.edu > wrote in message news:<xW******* **********@news read2.news.atl. earthlink.net>. .. Did I miscopy John's query? It gives me the future price of $25.04 for Product One, but is easy to fix by adding
AND P1.EffectiveDat e <= CURRENT_TIMESTA MP
to the where clause of the derived table LatestPrices
John's initial idea of NOT EXISTS still works, too, in part. (Note that I blew off the ProductName parsing by taking a hatchet to that column with an update...
-- Be lazy UPDATE ProductList SET ProductName = SUBSTRING(Produ ctName, 1,CHARINDEX('(' , ProductName) - 2) GO
-- For each product, select the lowest non-future -- price that has not been superseded. SELECT ProductName, MIN(Price) as Price -- get the lowest price FROM ( SELECT ProductName, Price -- from all non-future prices FROM ProductList P1 WHERE P1.EffectiveDat e <= CURRENT_TIMESTA MP AND NOT EXISTS ( -- that have not been superseded SELECT * FROM ProductList P2 WHERE P2.Distributor = P1.Distributor AND P2.ProductName = P1.ProductName AND P2.EffectiveDat e <= CURRENT_TIMESTA MP AND P2.EffectiveDat e > P1.EffectiveDat e ) ) T GROUP BY ProductName -- for each product go
By the way, the delimited data was no trouble. I pasted it into Excel, did Text To Columns, named the data region, and inserted it with:
insert into [ProductList] select Manufacturer, ProductName, Price, EffectiveDate from OpenRowset( 'Microsoft.Jet. OLEDB.4.0', 'Excel 8.0;Database=e: \excel\ProductL ist.xls', 'select * from ProductList')
Be sure to close the Excel file before inserting.
-- Steve Kass -- Drew University -- Ref: 2498EA13-1756-4C81-9307-25AFD5BB451D netpurpose wrote: Hi Jag,
Thanks a lot!! It works like a charm! Your query saves me a lot of headache.
I have struggled with this for quite a while now and I am glad I find help here.
Again, thank you very much! I really appreciate your help in such a short time!
Regards, Netpurpose
"John Gilson" <ja*@acm.org> wrote in message news:<am******* **************@ twister.nyc.rr. com>...
"netpurpose " <ne********@hot mail.com> wrote in message news:31****** *************** *****@posting.g oogle.com...
Hi Jag,
Thank you for your response. I tried the query, but it gives me a different pricing result:
ProductNam e Price ---------------------------------------- --------------------- Product One (MXT234) 28.1500 Product Two (DGT6789) 44.3500 Product Three (QAT556) 40.1500
The correct result should be:
ProductNam e Price ---------------------------------------- --------------------- Product One 33.5000 Product Two 44.7500 Product Three 40.4500
The reason is because for Product One, Manufacturer "BBB" has a price of 28.15 on 2003-07-30, this price is no longer valid because "BBB" has more recent price of 33.5 starting on 2003-08-27. Same case with Product Two and Three (manufacturer "JJJ" overrides the 40.15 on 2003-08-01 with 40.45 on 2003-08-04).
I have to take the most recent prices from each manufacturer for a particular product (price at the latest date as of today), and then take the minimum price from the result.
Again, thank you for your help. Sorry I might not explain it well in the beginning. Any further help is appreciated. Thank you very much in advance!
Regards, Netpurpose
I was a little too quick on the draw and misread your requirements.
SELECT ProductName, MIN(Price) FROM (SELECT P1.Distributor, SUBSTRING(P1.Pr oductName, 1, CHARINDEX('(', P1.ProductName) - 2) AS ProductName, P1.EffectiveDat e AS LatestDate, P1.price FROM ProductList AS P1 LEFT OUTER JOIN ProductList AS P2 ON P1.Distributor = P2.Distributor AND P1.ProductName = P2.ProductName AND P1.EffectiveDat e <= CURRENT_TIMESTA MP AND P2.EffectiveDat e <= CURRENT_TIMESTA MP AND P2.EffectiveDat e > P1.EffectiveDat e WHERE P2.EffectiveDat e IS NULL) AS LatestPrices GROUP BY ProductName
Regards, jag
"John Gilson" <ja*@acm.org> wrote in message news:<_u******* **************@ twister.nyc.rr. com>...
>"netpurpose " <ne********@hot mail.com> wrote in message >news:31**** *************** *******@posting .google.com... > >>I need to extract data from this table to find the lowest prices of >>each product as of today. The product will be listed/grouped by the >>name only, discarding the product code - I use SUBSTRING(Produ ctName, >>1, CHARINDEX('(', ProductName)-2). >> >>I can get this result, but I had to use several views (totally >>inefficien t). I think this can be done in one efficient/fast query, >>but I can't think of one. >> >>In the case that one query is not possible, is there other efficient >>way to get the results? There are about 300K records in the table. >> >>Any help is appreciated! Thanks a lot in advance! >> >>Thanks, >>Netpurpo se > >SELECT P1.ProductName, P1.Price >FROM ProductList AS P1 >WHERE EffectiveDate <= CURRENT_TIMESTA MP AND > NOT EXISTS > (SELECT * > FROM ProductList AS P2 > WHERE SUBSTRING(P1.Pr oductName, 1, > CHARINDEX('(', P1.ProductName) - 2) = > SUBSTRING(P2.Pr oductName, 1, > CHARINDEX('(', P2.ProductName) - 2) AND > P2.EffectiveDat e <= CURRENT_TIMESTA MP AND > P2.Price < P1.Price) > >Regards, >jag > >
When I ran John's query on your sample data, It
returned the price of $25.04 for Product One, a price
that was not effective on the day I ran the query.
Make sure your sample data has some examples where
the lowest price is a future price that has been
recorded but is not yet effective.
As far as efficiency goes, all I did was look at query
plans. What I found was that John's query and mine
had virtually identical plans.
What I did was this:
Removed the part of product name in parentheses. If that data
is important, it should be a separate column. I removed it,
since to make this efficient there needed (I think) to be an
index with the ProductName value you want in the output, without
the extra comment, as the first column.
Removed all cases where the same (ProductName, Distributor,
EffectiveDate) appeared in more than one row (these probably
appeared when I removed the extra part of the product name).
Declared (ProductName, Distributor, EffectiveDate) to be the
primary key. Maybe I missed something and this does not work
as a key, but it seemed like a good choice for an index to
make the query fast. If it's not a key, add price as
a fourth column.
I realized now that there was extra nesting in my query.
I don't think it should have slowed it down tenfold, but the
outer two selects can be combined (if the data has no duplicates
in my key columns).
-- Select the lowest price
SELECT ProductName, MIN(Price)
-- from the table
FROM ProductList P1
-- that is not a future price
WHERE P1.EffectiveDat e <= CURRENT_TIMESTA MP
-- and that has not been superseded
AND NOT EXISTS (
-- by another price
SELECT * FROM ProductList P2
-- from the same distributor
WHERE P2.Distributor = P1.Distributor
-- for the same product
AND P2.ProductName = P1.ProductName
-- which other price has gone into effect
AND P2.EffectiveDat e <= CURRENT_TIMESTA MP
-- more recently
AND P2.EffectiveDat e > P1.EffectiveDat e
)
-- for each product
GROUP BY ProductName
Steve
netpurpose wrote: Hi Steve,
Thanks for your response. When I copied John's query, it works fine and gives me the correct results. I did run both your query and his against 330K of records, and it seems that his query runs much faster (more than 10 times faster). I ran your query against clean products names without the codes.
I am too novice to even know why the big difference. Both query yields the same results. Can you tell why his statement is much more efficient? LEFT OUTER JOIN vs EXISTS? I do want to learn from this.
Also, thanks for showing me the command to import the excel file to db. Good to know :)
Thanks, Netpurpose
Steve Kass <sk***@drew.edu > wrote in message news:<xW******* **********@news read2.news.atl. earthlink.net>. ..
Did I miscopy John's query? It gives me the future price of $25.04 for Product One, but is easy to fix by adding
AND P1.EffectiveDat e <= CURRENT_TIMESTA MP
to the where clause of the derived table LatestPrices
John's initial idea of NOT EXISTS still works, too, in part. (Note that I blew off the ProductName parsing by taking a hatchet to that column with an update...
-- Be lazy UPDATE ProductList SET ProductName = SUBSTRING(Produ ctName, 1,CHARINDEX('(' , ProductName) - 2) GO
-- For each product, select the lowest non-future -- price that has not been superseded. SELECT ProductName, MIN(Price) as Price -- get the lowest price FROM ( SELECT ProductName, Price -- from all non-future prices FROM ProductList P1 WHERE P1.EffectiveDat e <= CURRENT_TIMESTA MP AND NOT EXISTS ( -- that have not been superseded SELECT * FROM ProductList P2 WHERE P2.Distributor = P1.Distributor AND P2.ProductName = P1.ProductName AND P2.EffectiveDat e <= CURRENT_TIMESTA MP AND P2.EffectiveDat e > P1.EffectiveDat e ) ) T GROUP BY ProductName -- for each product go
By the way, the delimited data was no trouble. I pasted it into Excel, did Text To Columns, named the data region, and inserted it with:
insert into [ProductList] select Manufacturer, ProductName, Price, EffectiveDate from OpenRowset( 'Microsoft.Jet. OLEDB.4.0', 'Excel 8.0;Database=e: \excel\ProductL ist.xls', 'select * from ProductList')
Be sure to close the Excel file before inserting.
-- Steve Kass -- Drew University -- Ref: 2498EA13-1756-4C81-9307-25AFD5BB451D netpurpose wrote:
Hi Jag,
Thanks a lot!! It works like a charm! Your query saves me a lot of headache.
I have struggled with this for quite a while now and I am glad I find help here.
Again, thank you very much! I really appreciate your help in such a short time!
Regards, Netpurpose
"John Gilson" <ja*@acm.org> wrote in message news:<am******* **************@ twister.nyc.rr. com>...
"netpurpose " <ne********@hot mail.com> wrote in message news:31**** *************** *******@posting .google.com...
>Hi Jag, > >Thank you for your response. I tried the query, but it gives me a >differen t pricing result: > >ProductNam e Price >---------------------------------------- --------------------- >Product One (MXT234) 28.1500 >Product Two (DGT6789) 44.3500 >Product Three (QAT556) 40.1500 > >The correct result should be: > >ProductNam e Price >---------------------------------------- --------------------- >Product One 33.5000 >Product Two 44.7500 >Product Three 40.4500 > >The reason is because for Product One, Manufacturer "BBB" has a price >of 28.15 on 2003-07-30, this price is no longer valid because "BBB" >has more recent price of 33.5 starting on 2003-08-27. Same case with >Product Two and Three (manufacturer "JJJ" overrides the 40.15 on >2003-08-01 with 40.45 on 2003-08-04). > >I have to take the most recent prices from each manufacturer for a >particul ar product (price at the latest date as of today), and then >take the minimum price from the result. > >Again, thank you for your help. Sorry I might not explain it well in >the beginning. Any further help is appreciated. Thank you very much >in advance! > >Regards, >Netpurpo se
I was a little too quick on the draw and misread your requirements.
SELECT ProductName, MIN(Price)
FROM (SELECT P1.Distributor,
SUBSTRING(P1.Pr oductName, 1, CHARINDEX('(', P1.ProductName) - 2) AS ProductName, P1.EffectiveDat e AS LatestDate, P1.price FROM ProductList AS P1 LEFT OUTER JOIN ProductList AS P2 ON P1.Distributor = P2.Distributor AND P1.ProductName = P2.ProductName AND P1.EffectiveDat e <= CURRENT_TIMESTA MP AND P2.EffectiveDat e <= CURRENT_TIMESTA MP AND P2.EffectiveDat e > P1.EffectiveDat e WHERE P2.EffectiveDat e IS NULL) AS LatestPrices GROUP BY ProductName
Regards, jag >"John Gilson" <ja*@acm.org> wrote in message news:<_u******* **************@ twister.nyc.rr. com>... > > >>"netpurpo se" <ne********@hot mail.com> wrote in message >>news:31** *************** *********@posti ng.google.com.. . >> >> >>>I need to extract data from this table to find the lowest prices of >>>each product as of today. The product will be listed/grouped by the >>>name only, discarding the product code - I use SUBSTRING(Produ ctName, >>>1, CHARINDEX('(', ProductName)-2). >>> >>>I can get this result, but I had to use several views (totally >>>ineffici ent). I think this can be done in one efficient/fast query, >>>but I can't think of one. >>> >>>In the case that one query is not possible, is there other efficient >>>way to get the results? There are about 300K records in the table. >>> >>>Any help is appreciated! Thanks a lot in advance! >>> >>>Thanks , >>>Netpurpo se >> >>SELECT P1.ProductName, P1.Price > >>FROM ProductList AS P1 > >>WHERE EffectiveDate <= CURRENT_TIMESTA MP AND >> NOT EXISTS >> (SELECT * >> FROM ProductList AS P2 >> WHERE SUBSTRING(P1.Pr oductName, 1, >> CHARINDEX('(', P1.ProductName) - 2) = >> SUBSTRING(P2.Pr oductName, 1, >> CHARINDEX('(', P2.ProductName) - 2) AND >> P2.EffectiveDat e <= CURRENT_TIMESTA MP AND >> P2.Price < P1.Price) >> >>Regards , >>jag >> >>
Hi John and Steve,
Thank you very much for your help and the quick responses! I really
appreciate them.
Steve, thanks for explaining and commenting your query. I get a much
better understanding now.
John, I think in this last query, P2.EffectiveDat e <=
CURRENT_TIMESTA MP is missing from the OUTER JOIN. But as you
mentioned, it works fine when I just take the previous query and move
out the P1.EffectiveDat e <= CURRENT_TIMESTA MP. I didn't realize there
was a problem before because the live data as of yesterday has no
future pricing. Sorry, I should have tested it better.
Again, thanks to you both. The system works great and fast now!
Sincerely,
netpurpose
"John Gilson" <ja*@acm.org> wrote in message news:<Q5******* **************@ twister.nyc.rr. com>... "Steve Kass" <sk***@drew.edu > wrote in message news:u5******** *********@newsr ead2.news.atl.e arthlink.net... When I ran John's query on your sample data, It returned the price of $25.04 for Product One, a price that was not effective on the day I ran the query.
Make sure your sample data has some examples where the lowest price is a future price that has been recorded but is not yet effective. Steve, you're absolutely right and stems from the (mis)placement of the selection criteria for the outer join. The constraint P1.EffectiveDat e <= CURRENT_TIMESTA MP should be applied *after* the outer join condition, that is, in the WHERE clause. So this should do the right thing:
SELECT ProductName, MIN(Price) FROM (SELECT P1.Distributor, SUBSTRING(P1.Pr oductName, 1, CHARINDEX('(', P1.ProductName) - 2) AS ProductName, P1.EffectiveDat e AS LatestDate, P1.price FROM ProductList AS P1 LEFT OUTER JOIN ProductList AS P2 ON P1.Distributor = P2.Distributor AND P1.ProductName = P2.ProductName AND
P2.EffectiveDat e > P1.EffectiveDat e WHERE P2.EffectiveDat e IS NULL AND P1.EffectiveDat e <= CURRENT_TIMESTA MP) AS LatestPrices GROUP BY ProductName
Regards, jag
As far as efficiency goes, all I did was look at query plans. What I found was that John's query and mine had virtually identical plans.
What I did was this:
Removed the part of product name in parentheses. If that data is important, it should be a separate column. I removed it, since to make this efficient there needed (I think) to be an index with the ProductName value you want in the output, without the extra comment, as the first column.
Removed all cases where the same (ProductName, Distributor, EffectiveDate) appeared in more than one row (these probably appeared when I removed the extra part of the product name).
Declared (ProductName, Distributor, EffectiveDate) to be the primary key. Maybe I missed something and this does not work as a key, but it seemed like a good choice for an index to make the query fast. If it's not a key, add price as a fourth column.
I realized now that there was extra nesting in my query. I don't think it should have slowed it down tenfold, but the outer two selects can be combined (if the data has no duplicates in my key columns).
-- Select the lowest price SELECT ProductName, MIN(Price)
-- from the table FROM ProductList P1
-- that is not a future price WHERE P1.EffectiveDat e <= CURRENT_TIMESTA MP
-- and that has not been superseded AND NOT EXISTS ( -- by another price SELECT * FROM ProductList P2 -- from the same distributor WHERE P2.Distributor = P1.Distributor -- for the same product AND P2.ProductName = P1.ProductName -- which other price has gone into effect AND P2.EffectiveDat e <= CURRENT_TIMESTA MP -- more recently AND P2.EffectiveDat e > P1.EffectiveDat e )
-- for each product GROUP BY ProductName
Steve
netpurpose wrote: Hi Steve,
Thanks for your response. When I copied John's query, it works fine and gives me the correct results. I did run both your query and his against 330K of records, and it seems that his query runs much faster (more than 10 times faster). I ran your query against clean products names without the codes.
I am too novice to even know why the big difference. Both query yields the same results. Can you tell why his statement is much more efficient? LEFT OUTER JOIN vs EXISTS? I do want to learn from this.
Also, thanks for showing me the command to import the excel file to db. Good to know :)
Thanks, Netpurpose
Steve Kass <sk***@drew.edu > wrote in message news:<xW******* **********@news read2.news.atl. earthlink.net>. .. >Did I miscopy John's query? It gives me the future price of $25.04 >for Product One, but is easy to fix by adding > > AND P1.EffectiveDat e <= CURRENT_TIMESTA MP > >to the where clause of the derived table LatestPrices > >John's initial idea of NOT EXISTS still works, too, in part. (Note >that I blew off the ProductName parsing by taking a hatchet to that >column with an update... > > >-- Be lazy >UPDATE ProductList SET > ProductName = > SUBSTRING(Produ ctName, 1,CHARINDEX('(' , ProductName) - 2) >GO > >-- For each product, select the lowest non-future >-- price that has not been superseded. >SELECT ProductName, MIN(Price) as Price -- get the lowest price >FROM ( > SELECT ProductName, Price -- from all non-future prices > FROM ProductList P1 > WHERE P1.EffectiveDat e <= CURRENT_TIMESTA MP > AND NOT EXISTS ( -- that have not been superseded > SELECT * FROM ProductList P2 > WHERE P2.Distributor = P1.Distributor > AND P2.ProductName = P1.ProductName > AND P2.EffectiveDat e <= CURRENT_TIMESTA MP > AND P2.EffectiveDat e > P1.EffectiveDat e > ) >) T >GROUP BY ProductName -- for each product >go > >By the way, the delimited data was no trouble. I pasted it into >Excel, did Text To Columns, named the data region, and inserted it with: > >insert into [ProductList] >select Manufacturer, ProductName, Price, EffectiveDate >from OpenRowset( > 'Microsoft.Jet. OLEDB.4.0', > 'Excel 8.0;Database=e: \excel\ProductL ist.xls', > 'select * from ProductList') > >Be sure to close the Excel file before inserting. > >-- Steve Kass >-- Drew University >-- Ref: 2498EA13-1756-4C81-9307-25AFD5BB451D > > > >netpurpose wrote: > >>Hi Jag, >> >>Thanks a lot!! It works like a charm! Your query saves me a lot of headache. >> >>I have struggled with this for quite a while now and I am glad I find help here. >> >>Again, thank you very much! I really appreciate your help in such a short time! >> >>Regards, >>Netpurpose >> >> >>"John Gilson" <ja*@acm.org> wrote in message news:<am******* **************@ twister.nyc.rr. com>...>> >> >>>"netpurpose " <ne********@hot mail.com> wrote in message >>>news:31**** *************** *******@posting .google.com... >>> >>> >>>>Hi Jag, >>>> >>>>Thank you for your response. I tried the query, but it gives me a >>>>differen t pricing result: >>>> >>>>ProductNam e Price >>>>---------------------------------------- --------------------- >>>>Product One (MXT234) 28.1500 >>>>Product Two (DGT6789) 44.3500 >>>>Product Three (QAT556) 40.1500 >>>> >>>>The correct result should be: >>>> >>>>ProductNam e Price >>>>---------------------------------------- --------------------- >>>>Product One 33.5000 >>>>Product Two 44.7500 >>>>Product Three 40.4500 >>>> >>>>The reason is because for Product One, Manufacturer "BBB" has a price >>>>of 28.15 on 2003-07-30, this price is no longer valid because "BBB" >>>>has more recent price of 33.5 starting on 2003-08-27. Same case with >>>>Product Two and Three (manufacturer "JJJ" overrides the 40.15 on >>>>2003-08-01 with 40.45 on 2003-08-04). >>>> >>>>I have to take the most recent prices from each manufacturer for a >>>>particul ar product (price at the latest date as of today), and then >>>>take the minimum price from the result. >>>> >>>>Again, thank you for your help. Sorry I might not explain it well in >>>>the beginning. Any further help is appreciated. Thank you very much >>>>in advance! >>>> >>>>Regards, >>>>Netpurpo se >>> >>>I was a little too quick on the draw and misread your requirements. >>> >>>SELECT ProductName, MIN(Price)>FROM (SELECT P1.Distributor,> SUBSTRING(P1.Pr oductName, 1, >>> CHARINDEX('(', P1.ProductName) - 2) AS >>> ProductName, >>> P1.EffectiveDat e AS LatestDate, >>> P1.price >>> FROM ProductList AS P1 >>> LEFT OUTER JOIN >>> ProductList AS P2 >>> ON P1.Distributor = P2.Distributor AND >>> P1.ProductName = P2.ProductName AND >>> P1.EffectiveDat e <= CURRENT_TIMESTA MP AND >>> P2.EffectiveDat e <= CURRENT_TIMESTA MP AND >>> P2.EffectiveDat e > P1.EffectiveDat e >>> WHERE P2.EffectiveDat e IS NULL) AS LatestPrices >>>GROUP BY ProductName >>> >>>Regards, >>>jag >>> >>> >>> >>>>"John Gilson" <ja*@acm.org> wrote in message news:<_u******* **************@ twister.nyc.rr. com>...>>>> >>>> >>>>>"netpurpo se" <ne********@hot mail.com> wrote in message >>>>>news:31** *************** *********@posti ng.google.com.. . >>>>> >>>>> >>>>>>I need to extract data from this table to find the lowest prices of >>>>>>each product as of today. The product will be listed/grouped by the >>>>>>name only, discarding the product code - I use SUBSTRING(Produ ctName, >>>>>>1, CHARINDEX('(', ProductName)-2). >>>>>> >>>>>>I can get this result, but I had to use several views (totally >>>>>>ineffici ent). I think this can be done in one efficient/fast query, >>>>>>but I can't think of one. >>>>>> >>>>>>In the case that one query is not possible, is there other efficient >>>>>>way to get the results? There are about 300K records in the table. >>>>>> >>>>>>Any help is appreciated! Thanks a lot in advance! >>>>>> >>>>>>Thanks , >>>>>>Netpurpo se >>>>> >>>>>SELECT P1.ProductName, P1.Price>>>FROM ProductList AS P1>>>WHERE EffectiveDate <= CURRENT_TIMESTA MP AND >>>>> NOT EXISTS >>>>> (SELECT * >>>>> FROM ProductList AS P2 >>>>> WHERE SUBSTRING(P1.Pr oductName, 1, >>>>> CHARINDEX('(', P1.ProductName) - 2) = >>>>> SUBSTRING(P2.Pr oductName, 1, >>>>> CHARINDEX('(', P2.ProductName) - 2) AND>>>>> P2.EffectiveDat e <= CURRENT_TIMESTA MP AND >>>>> P2.Price < P1.Price) >>>>> >>>>>Regards , >>>>>jag >>>>> >>>>>
John,
I hate to be a pest, but I believe you need the
date restriction in two places. The query here will
fail if the lowest price for a product comes from a
distributor that has already announced a price to become
effective in the future. If that is true, the future
price will come up on the right side of the join, since
there is no date restriction there. That makes the value
of P2.EffectiveDat e not null and in the future, and the
P1.Price in that row, despite being the lowest for the
product, is filtered out.
Ok, I lied - I don't hate being a pest, but I do
think your query doesn't quite work.
Steve
John Gilson wrote: "Steve Kass" <sk***@drew.edu > wrote in message news:u5******** *********@newsr ead2.news.atl.e arthlink.net...
When I ran John's query on your sample data, It returned the price of $25.04 for Product One, a price that was not effective on the day I ran the query.
Make sure your sample data has some examples where the lowest price is a future price that has been recorded but is not yet effective.
Steve, you're absolutely right and stems from the (mis)placement of the selection criteria for the outer join. The constraint P1.EffectiveDat e <= CURRENT_TIMESTA MP should be applied *after* the outer join condition, that is, in the WHERE clause. So this should do the right thing:
SELECT ProductName, MIN(Price) FROM (SELECT P1.Distributor, SUBSTRING(P1.Pr oductName, 1, CHARINDEX('(', P1.ProductName) - 2) AS ProductName, P1.EffectiveDat e AS LatestDate, P1.price FROM ProductList AS P1 LEFT OUTER JOIN ProductList AS P2 ON P1.Distributor = P2.Distributor AND P1.ProductName = P2.ProductName AND P2.EffectiveDat e > P1.EffectiveDat e WHERE P2.EffectiveDat e IS NULL AND P1.EffectiveDat e <= CURRENT_TIMESTA MP) AS LatestPrices GROUP BY ProductName
Regards, jag
As far as efficiency goes, all I did was look at query plans. What I found was that John's query and mine had virtually identical plans.
What I did was this:
Removed the part of product name in parentheses. If that data is important, it should be a separate column. I removed it, since to make this efficient there needed (I think) to be an index with the ProductName value you want in the output, without the extra comment, as the first column.
Removed all cases where the same (ProductName, Distributor, EffectiveDate ) appeared in more than one row (these probably appeared when I removed the extra part of the product name).
Declared (ProductName, Distributor, EffectiveDate) to be the primary key. Maybe I missed something and this does not work as a key, but it seemed like a good choice for an index to make the query fast. If it's not a key, add price as a fourth column.
I realized now that there was extra nesting in my query. I don't think it should have slowed it down tenfold, but the outer two selects can be combined (if the data has no duplicates in my key columns).
-- Select the lowest price SELECT ProductName, MIN(Price)
-- from the table FROM ProductList P1
-- that is not a future price WHERE P1.EffectiveDat e <= CURRENT_TIMESTA MP
-- and that has not been superseded AND NOT EXISTS ( -- by another price SELECT * FROM ProductList P2 -- from the same distributor WHERE P2.Distributor = P1.Distributor -- for the same product AND P2.ProductName = P1.ProductName -- which other price has gone into effect AND P2.EffectiveDat e <= CURRENT_TIMESTA MP -- more recently AND P2.EffectiveDat e > P1.EffectiveDat e )
-- for each product GROUP BY ProductName
Steve
netpurpose wrote:
Hi Steve,
Thanks for your response. When I copied John's query, it works fine and gives me the correct results. I did run both your query and his against 330K of records, and it seems that his query runs much faster (more than 10 times faster). I ran your query against clean products names without the codes.
I am too novice to even know why the big difference. Both query yields the same results. Can you tell why his statement is much more efficient? LEFT OUTER JOIN vs EXISTS? I do want to learn from this.
Also, thanks for showing me the command to import the excel file to db. Good to know :)
Thanks, Netpurpose
Steve Kass <sk***@drew.edu > wrote in message news:<xW******* **********@news read2.news.atl. earthlink.net>. .. Did I miscopy John's query? It gives me the future price of $25.04 for Product One, but is easy to fix by adding
AND P1.EffectiveDat e <= CURRENT_TIMESTA MP
to the where clause of the derived table LatestPrices
John's initial idea of NOT EXISTS still works, too, in part. (Note that I blew off the ProductName parsing by taking a hatchet to that column with an update...
-- Be lazy UPDATE ProductList SET ProductName = SUBSTRING(Produ ctName, 1,CHARINDEX('(' , ProductName) - 2) GO
-- For each product, select the lowest non-future -- price that has not been superseded. SELECT ProductName, MIN(Price) as Price -- get the lowest price
FROM (
SELECT ProductName, Price -- from all non-future prices FROM ProductList P1 WHERE P1.EffectiveDat e <= CURRENT_TIMESTA MP AND NOT EXISTS ( -- that have not been superseded SELECT * FROM ProductList P2 WHERE P2.Distributor = P1.Distributor AND P2.ProductName = P1.ProductName AND P2.EffectiveDat e <= CURRENT_TIMESTA MP AND P2.EffectiveDat e > P1.EffectiveDat e ) ) T GROUP BY ProductName -- for each product go
By the way, the delimited data was no trouble. I pasted it into Excel, did Text To Columns, named the data region, and inserted it with:
insert into [ProductList] select Manufacturer, ProductName, Price, EffectiveDate
from OpenRowset(
'Microsoft.Jet. OLEDB.4.0', 'Excel 8.0;Database=e: \excel\ProductL ist.xls', 'select * from ProductList')
Be sure to close the Excel file before inserting.
-- Steve Kass -- Drew University -- Ref: 2498EA13-1756-4C81-9307-25AFD5BB451D netpurpos e wrote:
>Hi Jag, > >Thanks a lot!! It works like a charm! Your query saves me a lot of headache. > >I have struggled with this for quite a while now and I am glad I find help here. > >Again, thank you very much! I really appreciate your help in such a short time! > >Regards, >Netpurpo se > > >"John Gilson" <ja*@acm.org> wrote in message news:<am******* **************@ twister.nyc.rr. com>...
>>"netpurpo se" <ne********@hot mail.com> wrote in message >>news:31** *************** *********@posti ng.google.com.. . >> >> >> >>>Hi Jag, >>> >>>Thank you for your response. I tried the query, but it gives me a >>>differen t pricing result: >>> >>>ProductN ame Price >>>---------------------------------------- --------------------- >>>Produc t One (MXT234) 28.1500 >>>Produc t Two (DGT6789) 44.3500 >>>Produc t Three (QAT556) 40.1500 >>> >>>The correct result should be: >>> >>>ProductN ame Price >>>---------------------------------------- --------------------- >>>Produc t One 33.5000 >>>Produc t Two 44.7500 >>>Produc t Three 40.4500 >>> >>>The reason is because for Product One, Manufacturer "BBB" has a price >>>of 28.15 on 2003-07-30, this price is no longer valid because "BBB" >>>has more recent price of 33.5 starting on 2003-08-27. Same case with >>>Produc t Two and Three (manufacturer "JJJ" overrides the 40.15 on >>>2003-08-01 with 40.45 on 2003-08-04). >>> >>>I have to take the most recent prices from each manufacturer for a >>>particul ar product (price at the latest date as of today), and then >>>take the minimum price from the result. >>> >>>Again, thank you for your help. Sorry I might not explain it well in >>>the beginning. Any further help is appreciated. Thank you very much >>>in advance! >>> >>>Regard s, >>>Netpurpo se >> >>I was a little too quick on the draw and misread your requirements. >> >>SELECT ProductName, MIN(Price) > >>FROM (SELECT P1.Distributor, > > >> SUBSTRING(P1.Pr oductName, 1, >> CHARINDEX('(', P1.ProductName) - 2) AS >> ProductName, >> P1.EffectiveDat e AS LatestDate, >> P1.price >> FROM ProductList AS P1 >> LEFT OUTER JOIN >> ProductList AS P2 >> ON P1.Distributor = P2.Distributor AND >> P1.ProductName = P2.ProductName AND >> P1.EffectiveDat e <= CURRENT_TIMESTA MP AND >> P2.EffectiveDat e <= CURRENT_TIMESTA MP AND >> P2.EffectiveDat e > P1.EffectiveDat e >> WHERE P2.EffectiveDat e IS NULL) AS LatestPrices >>GROUP BY ProductName >> >>Regards , >>jag >> >> >> >> >>>"John Gilson" <ja*@acm.org> wrote in message news:<_u******* **************@ twister.nyc.rr. com>... > >>>>"netpur pose" <ne********@hot mail.com> wrote in message >>>>news:31 *************** ***********@pos ting.google.com ... >>>> >>>> >>>> >>>>>I need to extract data from this table to find the lowest prices of >>>>>each product as of today. The product will be listed/grouped by the >>>>>name only, discarding the product code - I use SUBSTRING(Produ ctName, >>>>>1, CHARINDEX('(', ProductName)-2). >>>>> >>>>>I can get this result, but I had to use several views (totally >>>>>ineffi cient). I think this can be done in one efficient/fast query, >>>>>but I can't think of one. >>>>> >>>>>In the case that one query is not possible, is there other efficient >>>>>way to get the results? There are about 300K records in the table. >>>>> >>>>>Any help is appreciated! Thanks a lot in advance! >>>>> >>>>>Thanks , >>>>>Netpur pose >>>> >>>>SELEC T P1.ProductName, P1.Price >>> >>>>FROM ProductList AS P1 >>> >>> >>>>WHERE EffectiveDate <= CURRENT_TIMESTA MP AND >>>> NOT EXISTS >>>> (SELECT * >>>> FROM ProductList AS P2 >>>> WHERE SUBSTRING(P1.Pr oductName, 1, >>>> CHARINDEX('(', P1.ProductName) - 2) = >>>> SUBSTRING(P2.Pr oductName, 1, >>>> CHARINDEX('(', P2.ProductName) - 2) AND >> P2.EffectiveDat e <= CURRENT_TIMESTA MP AND >>>> P2.Price < P1.Price) >>>> >>>>Regards , >>>>jag >>>> >>>>
"Steve Kass" <sk***@drew.edu > wrote in message
news:dG******** **********@news read2.news.atl. earthlink.net.. . John,
I hate to be a pest, but I believe you need the date restriction in two places. The query here will fail if the lowest price for a product comes from a distributor that has already announced a price to become effective in the future. If that is true, the future price will come up on the right side of the join, since there is no date restriction there. That makes the value of P2.EffectiveDat e not null and in the future, and the P1.Price in that row, despite being the lowest for the product, is filtered out.
Ok, I lied - I don't hate being a pest, but I do think your query doesn't quite work.
Steve
Steve, I don't hate you for being a pest. Ok, I lied - I do hate you
for being a pest but, of course, you happen to also be correct (again!)
so, to a small extent, it assuages your pestiferousness . But seriously,
in my haste, I seemed to have dropped a condition that I had originally
specified in the outer join, that is,
P2.EffectiveDat e <= CURRENT_TIMESTA MP
Putting this back in leaves us with
SELECT ProductName, MIN(Price) AS Price
FROM (SELECT P1.Distributor,
SUBSTRING(P1.Pr oductName, 1,
CHARINDEX('(', P1.ProductName) - 2) AS
ProductName,
P1.EffectiveDat e AS LatestDate,
P1.price
FROM ProductList AS P1
LEFT OUTER JOIN
ProductList AS P2
ON P1.Distributor = P2.Distributor AND
P1.ProductName = P2.ProductName AND
P2.EffectiveDat e > P1.EffectiveDat e AND
P2.EffectiveDat e <= CURRENT_TIMESTA MP
WHERE P2.EffectiveDat e IS NULL AND
P1.EffectiveDat e <= CURRENT_TIMESTA MP)
AS LatestPrices
GROUP BY ProductName
And Steve, thanks for the sharp eye and sharper mind.
Regards,
jag
John Gilson wrote: "Steve Kass" <sk***@drew.edu > wrote in message news:u5******** *********@newsr ead2.news.atl.e arthlink.net...
When I ran John's query on your sample data, It returned the price of $25.04 for Product One, a price that was not effective on the day I ran the query.
Make sure your sample data has some examples where the lowest price is a future price that has been recorded but is not yet effective.
Steve, you're absolutely right and stems from the (mis)placement of the selection criteria for the outer join. The constraint P1.EffectiveDat e <= CURRENT_TIMESTA MP should be applied *after* the outer join condition, that is, in the WHERE clause. So this should do the right thing:
SELECT ProductName, MIN(Price) FROM (SELECT P1.Distributor, SUBSTRING(P1.Pr oductName, 1, CHARINDEX('(', P1.ProductName) - 2) AS ProductName, P1.EffectiveDat e AS LatestDate, P1.price FROM ProductList AS P1 LEFT OUTER JOIN ProductList AS P2 ON P1.Distributor = P2.Distributor AND P1.ProductName = P2.ProductName AND P2.EffectiveDat e > P1.EffectiveDat e WHERE P2.EffectiveDat e IS NULL AND P1.EffectiveDat e <= CURRENT_TIMESTA MP) AS LatestPrices GROUP BY ProductName
Regards, jag
As far as efficiency goes, all I did was look at query plans. What I found was that John's query and mine had virtually identical plans.
What I did was this:
Removed the part of product name in parentheses. If that data is important, it should be a separate column. I removed it, since to make this efficient there needed (I think) to be an index with the ProductName value you want in the output, without the extra comment, as the first column.
Removed all cases where the same (ProductName, Distributor, EffectiveDate ) appeared in more than one row (these probably appeared when I removed the extra part of the product name).
Declared (ProductName, Distributor, EffectiveDate) to be the primary key. Maybe I missed something and this does not work as a key, but it seemed like a good choice for an index to make the query fast. If it's not a key, add price as a fourth column.
I realized now that there was extra nesting in my query. I don't think it should have slowed it down tenfold, but the outer two selects can be combined (if the data has no duplicates in my key columns).
-- Select the lowest price SELECT ProductName, MIN(Price)
-- from the table FROM ProductList P1
-- that is not a future price WHERE P1.EffectiveDat e <= CURRENT_TIMESTA MP
-- and that has not been superseded AND NOT EXISTS ( -- by another price SELECT * FROM ProductList P2 -- from the same distributor WHERE P2.Distributor = P1.Distributor -- for the same product AND P2.ProductName = P1.ProductName -- which other price has gone into effect AND P2.EffectiveDat e <= CURRENT_TIMESTA MP -- more recently AND P2.EffectiveDat e > P1.EffectiveDat e )
-- for each product GROUP BY ProductName
Steve
netpurpose wrote:
Hi Steve,
Thanks for your response. When I copied John's query, it works fine and gives me the correct results. I did run both your query and his against 330K of records, and it seems that his query runs much faster (more than 10 times faster). I ran your query against clean products names without the codes.
I am too novice to even know why the big difference. Both query yields the same results. Can you tell why his statement is much more efficient? LEFT OUTER JOIN vs EXISTS? I do want to learn from this.
Also, thanks for showing me the command to import the excel file to db. Good to know :)
Thanks, Netpurpose
Steve Kass <sk***@drew.edu > wrote in message
news:<xW******* **********@news read2.news.atl. earthlink.net>. ..
>Did I miscopy John's query? It gives me the future price of $25.04 >for Product One, but is easy to fix by adding > > AND P1.EffectiveDat e <= CURRENT_TIMESTA MP > >to the where clause of the derived table LatestPrices > >John's initial idea of NOT EXISTS still works, too, in part. (Note >that I blew off the ProductName parsing by taking a hatchet to that >column with an update... > > >-- Be lazy >UPDATE ProductList SET > ProductName = > SUBSTRING(Produ ctName, 1,CHARINDEX('(' , ProductName) - 2) >GO > >-- For each product, select the lowest non-future >-- price that has not been superseded. >SELECT ProductName, MIN(Price) as Price -- get the lowest price
>FROM (
> SELECT ProductName, Price -- from all non-future prices > FROM ProductList P1 > WHERE P1.EffectiveDat e <= CURRENT_TIMESTA MP > AND NOT EXISTS ( -- that have not been superseded > SELECT * FROM ProductList P2 > WHERE P2.Distributor = P1.Distributor > AND P2.ProductName = P1.ProductName > AND P2.EffectiveDat e <= CURRENT_TIMESTA MP > AND P2.EffectiveDat e > P1.EffectiveDat e > ) >) T >GROUP BY ProductName -- for each product >go > >By the way, the delimited data was no trouble. I pasted it into >Excel, did Text To Columns, named the data region, and inserted it with: > >insert into [ProductList] >select Manufacturer, ProductName, Price, EffectiveDate
>from OpenRowset(
> 'Microsoft.Jet. OLEDB.4.0', > 'Excel 8.0;Database=e: \excel\ProductL ist.xls', > 'select * from ProductList') > >Be sure to close the Excel file before inserting. > >-- Steve Kass >-- Drew University >-- Ref: 2498EA13-1756-4C81-9307-25AFD5BB451D > > > >netpurpos e wrote: > > >>Hi Jag, >> >>Thanks a lot!! It works like a charm! Your query saves me a lot of headache. >> >>I have struggled with this for quite a while now and I am glad I find help here. >> >>Again, thank you very much! I really appreciate your help in such a short time! >> >>Regards, >>Netpurpo se >> >> >>"John Gilson" <ja*@acm.org> wrote in message
news:<am******* **************@ twister.nyc.rr. com>...
>> >>>"netpurpo se" <ne********@hot mail.com> wrote in message >>>news:31** *************** *********@posti ng.google.com.. . >>> >>> >>> >>>>Hi Jag, >>>> >>>>Thank you for your response. I tried the query, but it gives me a >>>>differen t pricing result: >>>> >>>>ProductN ame Price >>>>---------------------------------------- --------------------- >>>>Produc t One (MXT234) 28.1500 >>>>Produc t Two (DGT6789) 44.3500 >>>>Produc t Three (QAT556) 40.1500 >>>> >>>>The correct result should be: >>>> >>>>ProductN ame Price >>>>---------------------------------------- --------------------- >>>>Produc t One 33.5000 >>>>Produc t Two 44.7500 >>>>Produc t Three 40.4500 >>>> >>>>The reason is because for Product One, Manufacturer "BBB" has a price >>>>of 28.15 on 2003-07-30, this price is no longer valid because "BBB" >>>>has more recent price of 33.5 starting on 2003-08-27. Same case with >>>>Produc t Two and Three (manufacturer "JJJ" overrides the 40.15 on >>>>2003-08-01 with 40.45 on 2003-08-04). >>>> >>>>I have to take the most recent prices from each manufacturer for a >>>>particul ar product (price at the latest date as of today), and then >>>>take the minimum price from the result. >>>> >>>>Again, thank you for your help. Sorry I might not explain it well in >>>>the beginning. Any further help is appreciated. Thank you very much >>>>in advance! >>>> >>>>Regard s, >>>>Netpurpo se >>> >>>I was a little too quick on the draw and misread your requirements. >>> >>>SELECT ProductName, MIN(Price) >> >>>FROM (SELECT P1.Distributor, >> >> >>> SUBSTRING(P1.Pr oductName, 1, >>> CHARINDEX('(', P1.ProductName) - 2) AS >>> ProductName, >>> P1.EffectiveDat e AS LatestDate, >>> P1.price >>> FROM ProductList AS P1 >>> LEFT OUTER JOIN >>> ProductList AS P2 >>> ON P1.Distributor = P2.Distributor AND >>> P1.ProductName = P2.ProductName AND >>> P1.EffectiveDat e <= CURRENT_TIMESTA MP AND >>> P2.EffectiveDat e <= CURRENT_TIMESTA MP AND >>> P2.EffectiveDat e > P1.EffectiveDat e >>> WHERE P2.EffectiveDat e IS NULL) AS LatestPrices >>>GROUP BY ProductName >>> >>>Regards , >>>jag >>> >>> >>> >>> >>>>"John Gilson" <ja*@acm.org> wrote in message
news:<_u******* **************@ twister.nyc.rr. com>...
>>>> >>>>>"netpur pose" <ne********@hot mail.com> wrote in message >>>>>news:31 *************** ***********@pos ting.google.com ... >>>>> >>>>> >>>>> >>>>>>I need to extract data from this table to find the lowest prices of >>>>>>each product as of today. The product will be listed/grouped by the >>>>>>name only, discarding the product code - I use SUBSTRING(Produ ctName, >>>>>>1, CHARINDEX('(', ProductName)-2). >>>>>> >>>>>>I can get this result, but I had to use several views (totally >>>>>>ineffi cient). I think this can be done in one efficient/fast query, >>>>>>but I can't think of one. >>>>>> >>>>>>In the case that one query is not possible, is there other efficient >>>>>>way to get the results? There are about 300K records in the table. >>>>>> >>>>>>Any help is appreciated! Thanks a lot in advance! >>>>>> >>>>>>Thanks , >>>>>>Netpur pose >>>>> >>>>>SELEC T P1.ProductName, P1.Price >>>> >>>>>FROM ProductList AS P1 >>>> >>>> >>>>>WHERE EffectiveDate <= CURRENT_TIMESTA MP AND >>>>> NOT EXISTS >>>>> (SELECT * >>>>> FROM ProductList AS P2 >>>>> WHERE SUBSTRING(P1.Pr oductName, 1, >>>>> CHARINDEX('(', P1.ProductName) - 2) = >>>>> SUBSTRING(P2.Pr oductName, 1, >>>>> CHARINDEX('(', P2.ProductName) - 2)
AND
>>>>> P2.EffectiveDat e <= CURRENT_TIMESTA MP AND >>>>> P2.Price < P1.Price) >>>>> >>>>>Regards , >>>>>jag >>>>> >>>>>
John Gilson wrote: "Steve Kass" <sk***@drew.edu > wrote in message news:dG******** **********@news read2.news.atl. earthlink.net.. .
John,
I hate to be a pest, but I believe you need the date restriction in two places. The query here will fail if the lowest price for a product comes from a distributor that has already announced a price to become effective in the future. If that is true, the future price will come up on the right side of the join, since there is no date restriction there. That makes the value of P2.EffectiveDat e not null and in the future, and the P1.Price in that row, despite being the lowest for the product, is filtered out.
Ok, I lied - I don't hate being a pest, but I do think your query doesn't quite work.
Steve
Steve, I don't hate you for being a pest. Ok, I lied - I do hate you for being a pest but, of course, you happen to also be correct (again!) so, to a small extent, it assuages your pestiferousness . But seriously, in my haste, I seemed to have dropped a condition that I had originally specified in the outer join, that is, P2.EffectiveDat e <= CURRENT_TIMESTA MP Putting this back in leaves us with
SELECT ProductName, MIN(Price) AS Price FROM (SELECT P1.Distributor, SUBSTRING(P1.Pr oductName, 1, CHARINDEX('(', P1.ProductName) - 2) AS ProductName, P1.EffectiveDat e AS LatestDate, P1.price FROM ProductList AS P1 LEFT OUTER JOIN ProductList AS P2 ON P1.Distributor = P2.Distributor AND P1.ProductName = P2.ProductName AND P2.EffectiveDat e > P1.EffectiveDat e AND P2.EffectiveDat e <= CURRENT_TIMESTA MP WHERE P2.EffectiveDat e IS NULL AND P1.EffectiveDat e <= CURRENT_TIMESTA MP) AS LatestPrices GROUP BY ProductName
And Steve, thanks for the sharp eye and sharper mind.
Don't mention it. Now if I could only do something about the matching
tongue.
Steve Regards, jag
John Gilson wrote:
"Steve Kass" <sk***@drew.edu > wrote in message news:u5***** ************@ne wsread2.news.at l.earthlink.net ...
When I ran John's query on your sample data, It returned the price of $25.04 for Product One, a price that was not effective on the day I ran the query.
Make sure your sample data has some examples where the lowest price is a future price that has been recorded but is not yet effective.
Steve, you're absolutely right and stems from the (mis)placement of the selection criteria for the outer join. The constraint P1.Effective Date <= CURRENT_TIMESTA MP should be applied *after* the outer join condition, that is, in the WHERE clause. So this should do the right thing:
SELECT ProductName, MIN(Price) FROM (SELECT P1.Distributor, SUBSTRING(P1.Pr oductName, 1, CHARINDEX('(', P1.ProductName) - 2) AS ProductName, P1.EffectiveDat e AS LatestDate, P1.price FROM ProductList AS P1 LEFT OUTER JOIN ProductList AS P2 ON P1.Distributor = P2.Distributor AND P1.ProductName = P2.ProductName AND P2.EffectiveDat e > P1.EffectiveDat e WHERE P2.EffectiveDat e IS NULL AND P1.EffectiveDat e <= CURRENT_TIMESTA MP) AS LatestPrices GROUP BY ProductName
Regards, jag As far as efficiency goes, all I did was look at query plans. What I found was that John's query and mine had virtually identical plans.
What I did was this:
Removed the part of product name in parentheses. If that data is important, it should be a separate column. I removed it, since to make this efficient there needed (I think) to be an index with the ProductName value you want in the output, without the extra comment, as the first column.
Removed all cases where the same (ProductName, Distributor, EffectiveDa te) appeared in more than one row (these probably appeared when I removed the extra part of the product name).
Declared (ProductName, Distributor, EffectiveDate) to be the primary key. Maybe I missed something and this does not work as a key, but it seemed like a good choice for an index to make the query fast. If it's not a key, add price as a fourth column.
I realized now that there was extra nesting in my query. I don't think it should have slowed it down tenfold, but the outer two selects can be combined (if the data has no duplicates in my key columns).
-- Select the lowest price SELECT ProductName, MIN(Price)
-- from the table
FROM ProductList P1
-- that is not a future price WHERE P1.EffectiveDat e <= CURRENT_TIMESTA MP
-- and that has not been superseded AND NOT EXISTS ( -- by another price SELECT * FROM ProductList P2 -- from the same distributor WHERE P2.Distributor = P1.Distributor -- for the same product AND P2.ProductName = P1.ProductName -- which other price has gone into effect AND P2.EffectiveDat e <= CURRENT_TIMESTA MP -- more recently AND P2.EffectiveDat e > P1.EffectiveDat e )
-- for each product GROUP BY ProductName
Steve
netpurpos e wrote:
>Hi Steve, > >Thanks for your response. When I copied John's query, it works fine >and gives me the correct results. I did run both your query and his >against 330K of records, and it seems that his query runs much faster >(more than 10 times faster). I ran your query against clean products >names without the codes. > >I am too novice to even know why the big difference. Both query >yields the same results. Can you tell why his statement is much more >efficien t? LEFT OUTER JOIN vs EXISTS? I do want to learn from this. > >Also, thanks for showing me the command to import the excel file to >db. Good to know :) > >Thanks, >Netpurpo se > >Steve Kass <sk***@drew.edu > wrote in message
news:<xW**** *************@n ewsread2.news.a tl.earthlink.ne t>...
>>Did I miscopy John's query? It gives me the future price of $25.04 >>for Product One, but is easy to fix by adding >> >> AND P1.EffectiveDat e <= CURRENT_TIMESTA MP >> >>to the where clause of the derived table LatestPrices >> >>John's initial idea of NOT EXISTS still works, too, in part. (Note >>that I blew off the ProductName parsing by taking a hatchet to that >>column with an update... >> >> >>-- Be lazy >>UPDATE ProductList SET >> ProductName = >> SUBSTRING(Produ ctName, 1,CHARINDEX('(' , ProductName) - 2) >>GO >> >>-- For each product, select the lowest non-future >>-- price that has not been superseded. >>SELECT ProductName, MIN(Price) as Price -- get the lowest price > >>FROM ( > > >> SELECT ProductName, Price -- from all non-future prices >> FROM ProductList P1 >> WHERE P1.EffectiveDat e <= CURRENT_TIMESTA MP >> AND NOT EXISTS ( -- that have not been superseded >> SELECT * FROM ProductList P2 >> WHERE P2.Distributor = P1.Distributor >> AND P2.ProductName = P1.ProductName >> AND P2.EffectiveDat e <= CURRENT_TIMESTA MP >> AND P2.EffectiveDat e > P1.EffectiveDat e >> ) >>) T >>GROUP BY ProductName -- for each product >>go >> >>By the way, the delimited data was no trouble. I pasted it into >>Excel, did Text To Columns, named the data region, and inserted it with: >> >>insert into [ProductList] >>select Manufacturer, ProductName, Price, EffectiveDate > >>from OpenRowset( > > >> 'Microsoft.Jet. OLEDB.4.0', >> 'Excel 8.0;Database=e: \excel\ProductL ist.xls', >> 'select * from ProductList') >> >>Be sure to close the Excel file before inserting. >> >>-- Steve Kass >>-- Drew University >>-- Ref: 2498EA13-1756-4C81-9307-25AFD5BB451D >> >> >> >>netpurpos e wrote: >> >> >> >>>Hi Jag, >>> >>>Thanks a lot!! It works like a charm! Your query saves me a lot of headache. >>> >>>I have struggled with this for quite a while now and I am glad I find help here. >>> >>>Again, thank you very much! I really appreciate your help in such a short time! >>> >>>Regard s, >>>Netpurpo se >>> >>> >>>"John Gilson" <ja*@acm.org> wrote in message
news:<am**** *************** **@twister.nyc. rr.com>...
>>>>"netpur pose" <ne********@hot mail.com> wrote in message >>>>news:31 *************** ***********@pos ting.google.com ... >>>> >>>> >>>> >>>> >>>>>Hi Jag, >>>>> >>>>>Than k you for your response. I tried the query, but it gives me a >>>>>differ ent pricing result: >>>>> >>>>>Produc tName Price >>>>>---------------------------------------- --------------------- >>>>>Produc t One (MXT234) 28.1500 >>>>>Produc t Two (DGT6789) 44.3500 >>>>>Produc t Three (QAT556) 40.1500 >>>>> >>>>>The correct result should be: >>>>> >>>>>Produc tName Price >>>>>---------------------------------------- --------------------- >>>>>Produc t One 33.5000 >>>>>Produc t Two 44.7500 >>>>>Produc t Three 40.4500 >>>>> >>>>>The reason is because for Product One, Manufacturer "BBB" has a price >>>>>of 28.15 on 2003-07-30, this price is no longer valid because "BBB" >>>>>has more recent price of 33.5 starting on 2003-08-27. Same case with >>>>>Produc t Two and Three (manufacturer "JJJ" overrides the 40.15 on >>>>>2003-08-01 with 40.45 on 2003-08-04). >>>>> >>>>>I have to take the most recent prices from each manufacturer for a >>>>>partic ular product (price at the latest date as of today), and then >>>>>take the minimum price from the result. >>>>> >>>>>Agai n, thank you for your help. Sorry I might not explain it well in >>>>>the beginning. Any further help is appreciated. Thank you very much >>>>>in advance! >>>>> >>>>>Regard s, >>>>>Netpur pose >>>> >>>>I was a little too quick on the draw and misread your requirements. >>>> >>>>SELEC T ProductName, MIN(Price) >>> >>>>FROM (SELECT P1.Distributor, >>> >>> >>> >>>> SUBSTRING(P1.Pr oductName, 1, >>>> CHARINDEX('(', P1.ProductName) - 2) AS >>>> ProductName, >>>> P1.EffectiveDat e AS LatestDate, >>>> P1.price >>>> FROM ProductList AS P1 >>>> LEFT OUTER JOIN >>>> ProductList AS P2 >>>> ON P1.Distributor = P2.Distributor AND >>>> P1.ProductName = P2.ProductName AND >>>> P1.EffectiveDat e <= CURRENT_TIMESTA MP AND >>>> P2.EffectiveDat e <= CURRENT_TIMESTA MP AND >>>> P2.EffectiveDat e > P1.EffectiveDat e >>>> WHERE P2.EffectiveDat e IS NULL) AS LatestPrices >>>>GROUP BY ProductName >>>> >>>>Regards , >>>>jag >>>> >>>> >>>> >>>> >>>> >>>>>"Joh n Gilson" <ja*@acm.org> wrote in message
news:<_u**** *************** **@twister.nyc. rr.com>...
>>>>>>"netp urpose" <ne********@hot mail.com> wrote in message >>>>>>news: 31************* *************@p osting.google.c om... >>>>>> >>>>>> >>>>>> >>>>>> >>>>>>>I need to extract data from this table to find the lowest prices of >>>>>>>ea ch product as of today. The product will be listed/grouped by the >>>>>>>na me only, discarding the product code - I use SUBSTRING(Produ ctName, >>>>>>>1, CHARINDEX('(', ProductName)-2). >>>>>>> >>>>>>>I can get this result, but I had to use several views (totally >>>>>>>inef ficient). I think this can be done in one efficient/fast query, >>>>>>>bu t I can't think of one. >>>>>>> >>>>>>>In the case that one query is not possible, is there other efficient >>>>>>>wa y to get the results? There are about 300K records in the table. >>>>>>> >>>>>>>An y help is appreciated! Thanks a lot in advance! >>>>>>> >>>>>>>Than ks, >>>>>>>Netp urpose >>>>>> >>>>>>SELEC T P1.ProductName, P1.Price >>>>> >>>>>>FRO M ProductList AS P1 >>>>> >>>>> >>>>> >>>>>>WHE RE EffectiveDate <= CURRENT_TIMESTA MP AND >>>>>> NOT EXISTS >>>>>> (SELECT * >>>>>> FROM ProductList AS P2 >>>>>> WHERE SUBSTRING(P1.Pr oductName, 1, >>>>>> CHARINDEX('(', P1.ProductName) - 2) = >>>>>> SUBSTRING(P2.Pr oductName, 1, >>>>>> CHARINDEX('(', P2.ProductName) - 2)
AND
>>>>>> P2.EffectiveDat e <= CURRENT_TIMESTA MP AND >>>>>> P2.Price < P1.Price) >>>>>> >>>>>>Regar ds, >>>>>>jag >>>>>> >>>>>>
This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
by: lawrence |
last post by:
I've been bad about documentation so far but I'm going to try to be
better. I've mostly worked alone so I'm the only one, so far, who's
suffered from my bad habits. But I'd like other programmers to have an
easier time understanding what I do. Therefore this weekend I'm going
to spend 3 days just writing comments. Before I do it, I thought I'd
ask other programmers what information they find useful.
Below is a typical class I've...
|
by: paii |
last post by:
I have a table that stores job milestone dates. The 2 milestones I am
interested in are "Ship Date" TypeID 1 and "Revised Ship Date" TypeID 18.
All jobs have TypeID 1 only some jobs have TypeID 18. I need a query that
will return the c date for TypeID 18 if it exist else the date for TypeID 1,
for all jobs.
the table structure is the following
Job
TypeID
|
by: pw |
last post by:
Hi,
I am having a mental block trying to figure out how to code this.
Two tables:
"tblQuestions" (fields = quesnum, questype, question)
"tblAnswers" (fields = clientnum, quesnum, questype, answer)
They are related by quesnum and questype. There are records in
|
by: K. Crothers |
last post by:
I administer a mechanical engineering database. I need to build a
query which uses the results from a subquery as its input or
criterion.
I am attempting to find all of the component parts of which a part may
be composed. I have a table of parts and their subparts. The problem
is that each of those subparts may be composed of smaller component
parts. The subpart would then be listed in the Part field linked to
each of its subparts in...
|
by: google |
last post by:
I have a database with four table. In one of the tables, I use about
five lookup fields to get populate their dropdown list. I have read
that lookup fields are really bad and may cause problems that are hard
to find. The main problem I am having right now is that I have a report
that is sorted by one of these lookup fields and it only displays the
record's ID number. When I add the source table to the query it makes
several records...
| |
by: ward |
last post by:
Greetings.
Ok, I admit it, I bit off a bit more than I can chew.
I need to complete this "Generate Report" page for my employer and I'm
a little over my head. I could use some additional assistance. I say
additional because I've already had help which is greatly appreciated.
I do try to take the time and understand the provided script in hopes
on not having to trouble others on those. But here it goes...
|
by: L. R. Du Broff |
last post by:
I own a small business. Need to track a few hundred pieces of rental
equipment that can be in any of a few dozen locations. I'm an old-time C
language programmer (UNIX environment). If the only tool you know how to
use is a hammer, every problem tends to look like a nail. That said, I
could solve my problem in C, but it's not the right tool. I need to come
into the Windows world, and I need to get this done in Access or
something...
|
by: Rnykster |
last post by:
I know a little about Access and have made several single table
databases. Been struggling for about a month to do a multiple table
database with no success. Help!
There are two tables. First has about 30 fields. Every entry in this
table will be unique. Second table has about 7 fields and is for
reference - strictly a look up type table. I want to use one field,
say FAMILY in the first table to look up any one of the 400 items in
the...
|
by: pbd22 |
last post by:
Hi.
I need some help with structuring my query strings.
I have a form with a search bar and some links. Each
link is a search type (such as "community"). The HREF
for the link's anchor looks like the following:
<a href="?searchtype=2">Community</a>
|
by: marktang |
last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look !
Part I. Meaning of...
|
by: Oralloy |
last post by:
Hello folks,
I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>".
The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed.
This is as boiled down as I can make it.
Here is my compilation command:
g++-12 -std=c++20 -Wnarrowing bit_field.cpp
Here is the code in...
| |
by: jinu1996 |
last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth.
The Art of Business Website Design
Your website is...
|
by: Hystou |
last post by:
Overview:
Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
|
by: agi2029 |
last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own....
Now, this would greatly impact the work of software developers. The idea...
|
by: isladogs |
last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM).
In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules.
He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms.
Adolph will...
|
by: conductexam |
last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one.
At the time of converting from word file to html my equations which are in the word document file was convert into image.
Globals.ThisAddIn.Application.ActiveDocument.Select();...
|
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
| |
by: bsmnconsultancy |
last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...
| |