1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24
| WITH Orders(ProductID, OrderDate, Quantity) AS
(
select 13, cast('1996-12-23' as datetime), 18 union all
select 13, cast('1996-12-25' as datetime), 10 union all
select 13, cast('1996-12-16' as datetime), 20 union all
select 14, cast('1996-12-06' as datetime), 15 union all
select 14, cast('1996-12-25' as datetime), 42 union all
select 18, cast('1996-12-13' as datetime), 9 union all
select 20, cast('1996-12-16' as datetime), 28
)
select
ProductID,
max(OrderDate) as OrderDate_m,
cast(substring(max(convert(VARCHAR(8),OrderDate, 112)+cast(Quantity as varchar(2))), 9, 2) as smallint) as Quantity_m
from Orders
group by ProductID;
ProductID OrderDate_m Quantity_m
--------------- ----------------------- ----------------
13 1996-12-25 00:00:00.000 10
14 1996-12-25 00:00:00.000 42
18 1996-12-13 00:00:00.000 9
20 1996-12-16 00:00:00.000 28 |
Partager