Skip to main content
added 8 characters in body
Source Link
Evan Carroll
  • 64.6k
  • 49
  • 251
  • 496

I hope it will help you.

SELECT max(date) AS date, sku
FROM PurchaseOrders
GROUP BY sku;

I hope it will help you.

SELECT max(date), sku
FROM PurchaseOrders
GROUP BY sku;

I hope it will help you.

SELECT max(date) AS date, sku
FROM PurchaseOrders
GROUP BY sku;
Moved sample data into the question
Source Link
Evan Carroll
  • 64.6k
  • 49
  • 251
  • 496

I hope it will help you.

WITH data_test AS (
    select 1 as id, 'ABC123' as sku, '2017-12-23' as date
    UNION ALL
    select 2 as id, 'ABC123' as sku, '2016-11-11' as date
    UNION ALL 
    select 3 as id, 'DEF456' as sku, '2011-01-03' as date
    UNION ALL
    select 4 as id, 'DEF456' as sku, '2011-10-21' as date
    UNION ALL
    select 5 as id, 'GHI789' as sku, '2017-01-23' as date
    UNION ALL
    select 6 as id, 'GHI789' as sku, '2017-11-21' as date
)
select * from data_test 
where (date, sku) IN (selectSELECT max(date), sku from data_test
FROM groupPurchaseOrders
GROUP byBY sku);sku;

I hope it will help you.

WITH data_test AS (
    select 1 as id, 'ABC123' as sku, '2017-12-23' as date
    UNION ALL
    select 2 as id, 'ABC123' as sku, '2016-11-11' as date
    UNION ALL 
    select 3 as id, 'DEF456' as sku, '2011-01-03' as date
    UNION ALL
    select 4 as id, 'DEF456' as sku, '2011-10-21' as date
    UNION ALL
    select 5 as id, 'GHI789' as sku, '2017-01-23' as date
    UNION ALL
    select 6 as id, 'GHI789' as sku, '2017-11-21' as date
)
select * from data_test 
where (date, sku) IN (select max(date), sku from data_test group by sku);

I hope it will help you.

SELECT max(date), sku
FROM PurchaseOrders
GROUP BY sku;
added 2 characters in body
Source Link
jonggu
  • 144
  • 10

I hope it iswill help you.

WITH data_test AS (
    select 1 as id, 'ABC123' as sku, '2017-12-23' as date
    UNION ALL
    select 2 as id, 'ABC123' as sku, '2016-11-11' as date
    UNION ALL 
    select 3 as id, 'DEF456' as sku, '2011-01-03' as date
    UNION ALL
    select 4 as id, 'DEF456' as sku, '2011-10-21' as date
    UNION ALL
    select 5 as id, 'GHI789' as sku, '2017-01-23' as date
    UNION ALL
    select 6 as id, 'GHI789' as sku, '2017-11-21' as date
)
select * from data_test 
where (date, sku) IN (select max(date), sku from data_test group by sku);

I hope it is help you.

WITH data_test AS (
    select 1 as id, 'ABC123' as sku, '2017-12-23' as date
    UNION ALL
    select 2 as id, 'ABC123' as sku, '2016-11-11' as date
    UNION ALL 
    select 3 as id, 'DEF456' as sku, '2011-01-03' as date
    UNION ALL
    select 4 as id, 'DEF456' as sku, '2011-10-21' as date
    UNION ALL
    select 5 as id, 'GHI789' as sku, '2017-01-23' as date
    UNION ALL
    select 6 as id, 'GHI789' as sku, '2017-11-21' as date
)
select * from data_test 
where (date, sku) IN (select max(date), sku from data_test group by sku);

I hope it will help you.

WITH data_test AS (
    select 1 as id, 'ABC123' as sku, '2017-12-23' as date
    UNION ALL
    select 2 as id, 'ABC123' as sku, '2016-11-11' as date
    UNION ALL 
    select 3 as id, 'DEF456' as sku, '2011-01-03' as date
    UNION ALL
    select 4 as id, 'DEF456' as sku, '2011-10-21' as date
    UNION ALL
    select 5 as id, 'GHI789' as sku, '2017-01-23' as date
    UNION ALL
    select 6 as id, 'GHI789' as sku, '2017-11-21' as date
)
select * from data_test 
where (date, sku) IN (select max(date), sku from data_test group by sku);
Source Link
jonggu
  • 144
  • 10
Loading