October 1, 2008 at 10:04 am
Hi All
I am not a good SQL writer, i got a table as below
ProductPriceDate
HDD20001/10/2008
HDD10019/09/2008
HDD5021/08/2007
FPP001/10/2008
FPP10020/09/2008
SCR50015/09/2008
SCR10010/01/2008
HMR025/09/2008
I need the result as below
Product PriceDate
HDD20001/10/2008
FPP10020/09/2008
SCR10010/01/2008
HMR025/09/2008
Its like the records with max date where the price >0
Please help
Cheers
🙂
October 1, 2008 at 11:56 am
You did not mention the version of SQL Server you are using. Assuming that you are on SQL Server 2005, the following will work.
SET DATEFORMAT DMY;
SET NOCOUNT ON
DECLARE @t TABLE (Product CHAR(3), Price MONEY, Date DATETIME)
INSERT INTO @t (Product, Price, Date) SELECT 'HDD',200,'01/10/2008'
INSERT INTO @t (Product, Price, Date) SELECT 'HDD',100,'19/09/2008'
INSERT INTO @t (Product, Price, Date) SELECT 'HDD',50,'21/08/2007'
INSERT INTO @t (Product, Price, Date) SELECT 'FPP',0,'01/10/2008'
INSERT INTO @t (Product, Price, Date) SELECT 'FPP',100,'20/09/2008'
INSERT INTO @t (Product, Price, Date) SELECT 'SCR',500,'15/09/2008'
INSERT INTO @t (Product, Price, Date) SELECT 'SCR',100,'10/01/2008'
INSERT INTO @t (Product, Price, Date) SELECT 'HMR',0,'25/09/2008'
;WITH cte AS (
SELECT *, ROW_NUMBER() OVER(PARTITION BY Product ORDER BY Date DESC) AS Seq
FROM @t
WHERE Price > 0
)
SELECT Product, Price, Date
FROM cte
WHERE seq = 1
/*
Product Price Date
------- --------------------- -----------------------
FPP 100.00 2008-09-20 00:00:00.000
HDD 200.00 2008-10-01 00:00:00.000
SCR 500.00 2008-09-15 00:00:00.000
*/
.
October 2, 2008 at 3:46 am
Thanks Jacob, i am using 2005, i need to last rtow with 0 as well, i cant exclude that ones, what shall i do for that, please advise
Cheers
🙂
October 2, 2008 at 4:13 am
So what is the logic?
if there is only one row, take it even if it is ZERO and other wise take only non-zero values?
.
October 2, 2008 at 5:18 am
Hi Jacob
The logic is if there are more than one price, then get the latest price( Except 0), else get the price what ever it is ( including 0)
Cheers
🙂
October 2, 2008 at 5:39 am
May be this one?
SET DATEFORMAT DMY;
SET NOCOUNT ON
DECLARE @t TABLE (Product CHAR(3), Price MONEY, Date DATETIME)
INSERT INTO @t (Product, Price, Date) SELECT 'HDD',200,'01/10/2008'
INSERT INTO @t (Product, Price, Date) SELECT 'HDD',100,'19/09/2008'
INSERT INTO @t (Product, Price, Date) SELECT 'HDD',50,'21/08/2007'
INSERT INTO @t (Product, Price, Date) SELECT 'FPP',0,'01/10/2008'
INSERT INTO @t (Product, Price, Date) SELECT 'FPP',100,'20/09/2008'
INSERT INTO @t (Product, Price, Date) SELECT 'SCR',500,'15/09/2008'
INSERT INTO @t (Product, Price, Date) SELECT 'SCR',100,'10/01/2008'
INSERT INTO @t (Product, Price, Date) SELECT 'HMR',0,'25/09/2008'
;WITH cte AS (
SELECT
*,
COUNT(*) OVER(PARTITION BY Product) AS cnt
FROM @t
), cte2 AS (
SELECT *, ROW_NUMBER() OVER(PARTITION BY Product ORDER BY Date DESC) AS Seq
FROM cte
WHERE Price > 0 AND cnt > 0
)
SELECT Product, Price, Date
FROM cte2 WHERE seq = 1
UNION ALL
SELECT product, price, date
FROM cte WHERE cnt = 1
/*
Product Price Date
------- --------------------- -----------------------
FPP 100.00 2008-09-20 00:00:00.000
HDD 200.00 2008-10-01 00:00:00.000
HMR 0.00 2008-09-25 00:00:00.000
SCR 500.00 2008-09-15 00:00:00.000
*/
.
October 3, 2008 at 5:29 am
Thanks Jacob, this helped a lot 🙂
Viewing 7 posts - 1 through 6 (of 6 total)
You must be logged in to reply to this topic. Login to reply