Hi,
I have a table like the following columns:
Warehouse INTEGER NOT NULL,
sku INTEGER NOT NULL,
qtyOnHand DECIMAL(9,4),
DateLastPurchase DATE,
SupplierLastPurchase INTEGER
Primary key is Warehouse,Sku
It actually has more columns, but these serve for the purpose of an example.
I want to select the LAST purchase date for each SKU. In other words, one
row for each SKU, selecting the MAX(DateLastPurchase)
So if the table has
Warehouse Sku DateLastPuchase SupplierLastPurchase
1 1 '2010-01-15' 5
2 1 '2010-01-12' 4
3 1 '2010-01-10' 5
1 2 '2010-01-10' 4
2 2 '2010-01-10' 4
3 2 '2010-01-12' 3
Would return
Warehouse Sku DateLastPuchase SupplierLastPurchase
1 1 '2010-01-15' 5
3 2 '2010-01-12' 3
In other words, what was the last purchase for each item irrespective of the
warehouse.
How would I do this on MSSQL 2000?
Thanks,
Edgard
|
|
0
|
|
|
|
Reply
|
Edgard
|
6/24/2010 4:11:11 PM |
|
"Edgard L. Riba" <elriba at rimith dot com> wrote in message
news:#RPKff7ELHA.5668@TK2MSFTNGP04.phx.gbl...
> Hi,
>
> I have a table like the following columns:
>
> Warehouse INTEGER NOT NULL,
> sku INTEGER NOT NULL,
> qtyOnHand DECIMAL(9,4),
> DateLastPurchase DATE,
> SupplierLastPurchase INTEGER
>
> Primary key is Warehouse,Sku
>
> It actually has more columns, but these serve for the purpose of an
> example.
>
> I want to select the LAST purchase date for each SKU. In other words,
> one row for each SKU, selecting the MAX(DateLastPurchase)
>
> So if the table has
>
> Warehouse Sku DateLastPuchase SupplierLastPurchase
> 1 1 '2010-01-15' 5
> 2 1 '2010-01-12' 4
> 3 1 '2010-01-10' 5
> 1 2 '2010-01-10' 4
> 2 2 '2010-01-10' 4
> 3 2 '2010-01-12' 3
>
>
> Would return
> Warehouse Sku DateLastPuchase SupplierLastPurchase
> 1 1 '2010-01-15' 5
> 3 2 '2010-01-12' 3
>
>
> In other words, what was the last purchase for each item irrespective of
> the
> warehouse.
>
> How would I do this on MSSQL 2000?
>
>
> Thanks,
> Edgard
>
>
>
And what happens if you have 2 different SupplierLastPurchase values on the
same latest date? Which one do you want to see? Highest? Lowest? Average?
Sum? Something else?
--
Dan
|
|
0
|
|
|
|
Reply
|
Dan
|
6/24/2010 4:33:27 PM
|
|
CREATE TABLE Inventory_Report
(warehouse_nbr INTEGER NOT NULL,
sku INTEGER NOT NULL,
PRIMARY KEY (warehouse_nbr, sku),
onhand_qty DECIMAL(9,4) NOT NULL,
last_purchase_date DATE DEFAULR CURRENT_DATE NOT NULL,
last_purchase_supplier_id INTEGER NOT NULL);
Since SKU is a "Stock Keeping Unit", how can you have decimal places
for the quantity? Can I assume that you do not want NULLs?
>> I want to select the LAST purchase date for each SKU. In other words, one row for each SKU, selecting the MAX(DateLastPurchase) <<
Here is one way:
WITH Max_SKU_Dates
AS
(SELECT sku, MAX(last_purchase_date)
FROM Inventory_Report
GROUP BY sku)
SELECT R.warehouse_nbr, R.sku, R.onhand_qty, R.last_purchase_date,
R.last_purchase_supplier_id
FROM Inventory_Report AS R, Max_SKU_Dates AS L
WHERE R.sku = L.sku
AND R.last_purchase_date = L.last_purchase_date;
This will preserve ties.
|
|
0
|
|
|
|
Reply
|
CELKO
|
6/24/2010 6:00:28 PM
|
|
Hi Dan,
Thanks for looking into this.
In this particular case it doesn't matter. I just need to select at least
one supplier on the last date.
Edgard
>
> "Edgard L. Riba" <elriba at rimith dot com> wrote in message
> news:#RPKff7ELHA.5668@TK2MSFTNGP04.phx.gbl...
>> Hi,
>>
>> I have a table like the following columns:
>>
>> Warehouse INTEGER NOT NULL,
>> sku INTEGER NOT NULL,
>> qtyOnHand DECIMAL(9,4),
>> DateLastPurchase DATE,
>> SupplierLastPurchase INTEGER
>>
>> Primary key is Warehouse,Sku
>>
>> It actually has more columns, but these serve for the purpose of an
>> example.
>>
>> I want to select the LAST purchase date for each SKU. In other words,
>> one row for each SKU, selecting the MAX(DateLastPurchase)
>>
>> So if the table has
>>
>> Warehouse Sku DateLastPuchase SupplierLastPurchase
>> 1 1 '2010-01-15' 5
>> 2 1 '2010-01-12' 4
>> 3 1 '2010-01-10' 5
>> 1 2 '2010-01-10' 4
>> 2 2 '2010-01-10' 4
>> 3 2 '2010-01-12' 3
>>
>>
>> Would return
>> Warehouse Sku DateLastPuchase SupplierLastPurchase
>> 1 1 '2010-01-15' 5
>> 3 2 '2010-01-12' 3
>>
>>
>> In other words, what was the last purchase for each item irrespective of
>> the
>> warehouse.
>>
>> How would I do this on MSSQL 2000?
>>
>>
>> Thanks,
>> Edgard
>>
>>
>>
>
> And what happens if you have 2 different SupplierLastPurchase values on
> the same latest date? Which one do you want to see? Highest? Lowest?
> Average? Sum? Something else?
>
> --
> Dan
|
|
0
|
|
|
|
Reply
|
Edgard
|
6/24/2010 6:02:34 PM
|
|
Edgard L. Riba (elriba at rimith dot com) writes:
> Thanks for looking into this.
>
> In this particular case it doesn't matter. I just need to select at
> least one supplier on the last date.
But why then bother at all? I always get an uneasy feeling when someone
says "one, but it does not matter which".
Anyway, this could do it.
SELECT Warehouse, Sku, LastDate,
(SELECT TOP 1 SupplierLastPurchase
FROM tbl b
WHERE b.Warehouse = a.Warehouse
AND b.Sku = b.Sku
AND b.Lastdate = b.Lastdate
ORDER BY newid()) AS SupplierLastPurchase
FROM (SELECT Warehouse, Sku, LastDate = MAX(DateLastPurcahse)
FROM tbl
GROUP BY Warehouse, Sku) AS d
On SQL 2005/2008 you can use the row_number function which makes this a lot
easier, but you are not there unfortunately.
--
Erland Sommarskog, SQL Server MVP, esquel@sommarskog.se
Links for SQL Server Books Online:
SQL 2008: http://msdn.microsoft.com/en-us/sqlserver/cc514207.aspx
SQL 2005: http://msdn.microsoft.com/en-us/sqlserver/bb895970.aspx
SQL 2000: http://www.microsoft.com/sql/prodinfo/previousversions/books.mspx
|
|
0
|
|
|
|
Reply
|
Erland
|
6/25/2010 12:19:40 PM
|
|
|
4 Replies
490 Views
(page loaded in 0.056 seconds)
Similiar Articles: How to retrieve a distinct row using one column as criteria ...Hi, I have a table like the following columns: Warehouse INTEGER NOT NULL, sku INTEGER NOT NULL, q... how to make a table with 10 million rows in one column ...How to retrieve a distinct row using one column as criteria ... how to make a table with 10 million rows in one column ... How to retrieve a distinct row using one column ... Partial match for query criteria - microsoft.public.access ...... distinct words or phrases enter the following in the 'field' row of a blank column ... criteria' row of the business > name column ... one of the cells is input in the criteria ... Sum a column is criteria in two in column and other column are met ...Sum cells using criteria from a row and a column - microsoft ... Sum a column is ... Sum Values in one Column using filter criteria on another column ... I need to sum ... Select Distinct subquery in a Report Control? - microsoft.public ...How to retrieve a distinct row using one column as criteria ... Select Distinct subquery in a Report Control? - microsoft.public ... How to retrieve a distinct row using ... LINQ: Select records but enforce one field to be unique ...... records by forcing one field to be unique, is it necessary to use the .Distinct ... the "purchaser" column (property) of those rows ... if you insist on using the Distinct ... select distinct id with other data - microsoft.public.access ...How to retrieve a distinct row using one column as criteria ... I want to select the LAST purchase date for each SKU. In other words, one row for ... date, R.last_purchase ... get a list of sequence number that meet 3 criteria - microsoft ...How to retrieve a distinct row using one column as criteria ... get a list of sequence number that meet 3 criteria - microsoft ... How to retrieve a distinct row using one ... how to select top ten values - microsoft.public.excelHow to retrieve a distinct row using one column as criteria ... how to select top ten values - microsoft.public.excel How to retrieve a distinct row using one column as ... can one note record online Webinars - microsoft.public.onenote ...How to retrieve a distinct row using one column as criteria ... can one note record online Webinars - microsoft.public.onenote ... How to retrieve a distinct row using one ... How to retrieve a distinct row using one column as criteria ...Hi, I have a table like the following columns: Warehouse INTEGER NOT NULL, sku INTEGER NOT NULL, q... Retrieving Rows with SELECTThe DISTINCT clause specifies a column (or expression) for which to retrieve only one row per unique ... in this criteria. USING ( column ... Alventis User's Guide - Alventis Home Page• Retrieve a single row, or part of a ... Each column in this list must meet the following criteria: • Be in one of the ... to retain non-distinct rows. Note. When using the ... hibernate - how to retrieve distinct root entity row count in ...How to retrieve row count of one-to-many relation while also ... How to Retrieve MySQL Date Column Data with ... Pagination with Hibernate Criteria and DISTINCT_ROOT_ENTITY NHibernate: Get distinct results based on a column, but retrieve ...Is there a way to do this using the Criteria ... How to retrieve unique entities ... SQL Query: Get rows (all columns) from table basied on one distinct column 7/26/2012 3:02:34 PM
|