Author |
Topic |
mjimenezh
Yak Posting Veteran
81 Posts |
Posted - 2015-03-18 : 10:28:49
|
Hi, I have problems with my sql 2008 following code, Why is Min Date not working? Thanks to all
SELECT TOP (100) PERCENT ItemCode, Description, LotNumber, ExpirationDate, PurchasePrice, PCur, XRate, QtyIn, QtyOut, Reserved, Available, MIN(DateStart) AS LotDate FROM dbo.VK_PCOMPRA_2 GROUP BY LotNumber, ItemCode, Description, ExpirationDate, PurchasePrice, PCur, XRate, QtyIn, QtyOut, Reserved, Available ORDER BY ItemCode |
|
sz1
Aged Yak Warrior
555 Posts |
Posted - 2015-03-18 : 10:42:51
|
Post some sample data.
We are the creators of our own reality! |
 |
|
mjimenezh
Yak Posting Veteran
81 Posts |
Posted - 2015-03-18 : 10:59:17
|
Example that I have :
ItemCode|Description|Exp.Date|P.Price|Xrate |LotDate item1 | item 1 | null |58.294 |13.054|2013-09-18 00:00:00.000 item1 | item 1 | null |57.250 |13.020|2013-08-20 00:00:00.000
And I need just the older lotdate like this :
ItemCode|Description|Exp.Date|P.Price|Xrate |LotDate item1 | item 1 | null |57.250 |13.020|2013-08-20 00:00:00.000
Thanks in advance
quote: Originally posted by sz1
Post some sample data.
We are the creators of our own reality!
|
 |
|
sz1
Aged Yak Warrior
555 Posts |
Posted - 2015-03-18 : 12:08:57
|
Its because of your grouping, if you use the MIN on its own it will be fine but you are adding groups from other fields, use row_number to get around this.
This logic will work for you:
with getmin as ( select item1, description, expdate, p_price, xrate, startdate Lotdate, row_number() over(partition by item1 order by startdate asc) rn from #mintest ) select item1, description, expdate, p_price, xrate, Lotdate from getmin where rn = 1
We are the creators of our own reality! |
 |
|
mjimenezh
Yak Posting Veteran
81 Posts |
Posted - 2015-03-18 : 13:03:07
|
Thak You So Much sz1, it works perfect.
Regards...
quote: Originally posted by sz1
Its because of your grouping, if you use the MIN on its own it will be fine but you are adding groups from other fields, use row_number to get around this.
This logic will work for you:
with getmin as ( select item1, description, expdate, p_price, xrate, startdate Lotdate, row_number() over(partition by item1 order by startdate asc) rn from #mintest ) select item1, description, expdate, p_price, xrate, Lotdate from getmin where rn = 1
We are the creators of our own reality!
|
 |
|
sz1
Aged Yak Warrior
555 Posts |
Posted - 2015-03-18 : 13:08:53
|
Neat :)
We are the creators of our own reality! |
 |
|
huangchen
Starting Member
37 Posts |
Posted - 2015-04-02 : 05:53:53
|
unspammed |
 |
|
|
|
|