| Author |
Topic |
|
krishna_yess
Yak Posting Veteran
81 Posts |
Posted - 2009-04-15 : 06:05:30
|
i have table like thisProduct Date values of salesA 6-Jan-09 4A 13-Jan-09 345A 14-Jan-09 5A 24-Jan-09 -9B 1-Jan-09 51B 5-Jan-09 4B 7-Jan-09 4B 8-Jan-09 7B 9-Jan-09 5B 23-Jan-09 4B 25-Jan-09 4B 26-Jan-09 4B 27-Jan-09 4B 28-Jan-09 4C 15-Jan-09 4C 16-Jan-09 4D 21-Jan-09 4D 22-Jan-09 4 I need to write a query to list each product and value which has added recentlyfor example for product A, The result will beA 6-Jan-09 4 please help me..Thanks |
|
|
SwePeso
Patron Saint of Lost Yaks
30421 Posts |
Posted - 2009-04-15 : 06:47:33
|
What data type is Date column? DATETIME or VARCHAR? E 12°55'05.63"N 56°04'39.26" |
 |
|
|
krishna_yess
Yak Posting Veteran
81 Posts |
Posted - 2009-04-15 : 06:49:43
|
| is a DATETIME |
 |
|
|
bklr
Master Smack Fu Yak Hacker
1693 Posts |
Posted - 2009-04-15 : 06:53:01
|
| select product,date,valuesofsales from urtable where date in ( select min(date) from urtable group by product) and product = 'A' |
 |
|
|
SwePeso
Patron Saint of Lost Yaks
30421 Posts |
Posted - 2009-04-15 : 06:55:30
|
[code]SELECT Product, Date, ValueOfSalesFROM ( SELECT Product, Date, ValueOfSales, ROW_NUMBER() OVER (PARTITION BY Product ORDER BY Date DESC) AS recID FROM Table1 ) AS dWHERE recID = 1[/code] E 12°55'05.63"N 56°04'39.26" |
 |
|
|
aprichard
Yak Posting Veteran
62 Posts |
Posted - 2009-04-15 : 06:58:18
|
| Hi, Please check the given query that bring your result.declare @Table1 table(Product char(1), Date DateTime, Sales decimal(18,2))insert into @Table1 values('A','6-Jan-09',4)insert into @Table1 values('A', '13-Jan-09',345)insert into @Table1 values('A', '14-Jan-09',5)insert into @Table1 values('A', '24-Jan-09',-9)insert into @Table1 values('B', '1-Jan-09', 51)insert into @Table1 values('B', '5-Jan-09', 4)insert into @Table1 values('B', '7-Jan-09', 4)select * from @Table1 ainner join (select Product, Max(Date) Date from @Table1group by Product) b on a.Product=b.Product and a.Date=b.Date |
 |
|
|
krishna_yess
Yak Posting Veteran
81 Posts |
Posted - 2009-04-15 : 07:06:44
|
| Hi Peso,your solution worked perfectly..thanks a lot |
 |
|
|
madhivanan
Premature Yak Congratulator
22864 Posts |
|
|
krishna_yess
Yak Posting Veteran
81 Posts |
Posted - 2009-04-17 : 03:20:10
|
| ROW_NUMBER is not available in sql server v8.0, can you please suggest some alternatives |
 |
|
|
krishna_yess
Yak Posting Veteran
81 Posts |
Posted - 2009-07-02 : 03:36:25
|
| Row_number() function is not available in SQL server 2000. what is the alternative for this see this topic[url]http://www.sqlteam.com/forums/topic.asp?TOPIC_ID=123849[/url] |
 |
|
|
madhivanan
Premature Yak Congratulator
22864 Posts |
Posted - 2009-07-02 : 03:39:46
|
quote: Originally posted by krishna_yess Row_number() function is not available in SQL server 2000. what is the alternative for this see this topic[url]http://www.sqlteam.com/forums/topic.asp?TOPIC_ID=123849[/url]
It has circular reference now MadhivananFailing to plan is Planning to fail |
 |
|
|
krishna_yess
Yak Posting Veteran
81 Posts |
Posted - 2009-07-02 : 03:44:02
|
any alternatives? |
 |
|
|
madhivanan
Premature Yak Congratulator
22864 Posts |
Posted - 2009-07-02 : 04:12:52
|
quote: Originally posted by krishna_yess
any alternatives?
declare @Table1 table(Product char(1), Date DateTime, Sales decimal(18,2))insert into @Table1 values('A','6-Jan-09',4)insert into @Table1 values('A', '13-Jan-09',345)insert into @Table1 values('A', '14-Jan-09',5)insert into @Table1 values('A', '24-Jan-09',-9)insert into @Table1 values('B', '1-Jan-09', 51)insert into @Table1 values('B', '5-Jan-09', 4)insert into @Table1 values('B', '7-Jan-09', 4)select a.* from @Table1 ainner join (select Product, min(Date) Date from @Table1group by Product) b on a.Product=b.Product and a.Date=b.DateMadhivananFailing to plan is Planning to fail |
 |
|
|
bklr
Master Smack Fu Yak Hacker
1693 Posts |
Posted - 2009-07-02 : 04:35:04
|
| did u try the solution given by me on 04/15/2009 : 06:53:01 |
 |
|
|
krishna_yess
Yak Posting Veteran
81 Posts |
Posted - 2009-07-02 : 05:35:40
|
yes bklr, i tried that. it results duplicate product..i think this is because there are 2 date entries with same valueselect product,date,valuesofsales from urtable where date in ( select min(date) from urtable group by product) |
 |
|
|
krishna_yess
Yak Posting Veteran
81 Posts |
Posted - 2009-07-02 : 05:53:41
|
| [code]select product,date,valuesofsales from urtable where date in ( select min(date) from urtable group by product)[/code]if i use this, the valueOfSales field is getting summed ? |
 |
|
|
rajdaksha
Aged Yak Warrior
595 Posts |
Posted - 2009-07-02 : 08:50:09
|
| HiSELECT ROWID=IDENTITY(INT,1,1), NAME, ID, XTYPEINTO #tempsysobjects FROM SYSOBJECTSNOTEThe IDENTITY function can only be used when the SELECT statement has an INTO clause. |
 |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2009-07-02 : 11:18:30
|
quote: Originally posted by krishna_yess
select product,date,valuesofsales from urtable where date in ( select min(date) from urtable group by product) if i use this, the valueOfSales field is getting summed ?
thats wrong...you're either using it incorrectly or your full query contains something more in that case |
 |
|
|
|