I have a table with 3 colums. Here is an example with just a few entries to demo. The table represents the dates that an item's price changes. I need a query that will tell me the prices for all the items on a specific date. The specific date may be between price changes. The price changes at midnight, so the date of the change, that is the price from then until and on the day before the previous change:
itemCode datePriceEffective price
AB 2012-01-01 9.99
AB 2012-03-02 10.50
XY 2011-09-20 34.99
I wish to get all the items prices for a given date. There is not an entry for every date, just the dates that the price changes.
so 2012-03-05 would return
AB 10.50
XY 34.99
and 2012-02-27 would return:
AB 9.99
XY 34.99
The SQL query needed to get this escapes me. Answers appreciated.
Edited as answers are going in wrong direction see italics for edit.
This retrieves first record by descending order of datePriceEffective per itemCode.
select top 1 with ties *
from ATable
where datePriceEffective <= #givenDate
order by row_number() over (partition by itemCode
order by datePriceEffective desc)
SELECT itemCode,price FRom TableNameHere WHERE datePriceEffective <= '2012-03-05'
Edit after more info - a 'simple' answer to hopefully make it easy to follow.
You're getting thelatest date for the item that is valid in your required date range and then joining the table to itself to get the price at that date
SELECT t1.itemCode,t1.price FROM
(
SELECT itemCode,MAX(datePriceEffective) AS datePriceEffective
FROM TableNameHere
WHERE datePriceEffective <= '2012-03-05'
) a
INNER JOIN TableNameHere t1 ON t1.itemCode = a.itemCode AND t1.datePriceEffective = a.datePriceEffective
You will need to join back to the table as follows:
declare #effdate datetime
set #effdate = CONVERT(datetime,'2012-02-27')
;WITH CTE_DATA as (
select itemCode='AB', datePriceEffective = CONVERT(Datetime,'2012-01-01'), price = 9.99
union all select itemCode='AB', datePriceEffective = CONVERT(Datetime,'2012-03-02'), price = 10.50
union all select itemCode='XY', datePriceEffective = CONVERT(Datetime,'2011-09-20'), price = 34.99
)
select
d.itemcode,
price
from
CTE_DATA d
join (
select
itemcode,
effdate = MAX(datepriceeffective)
from CTE_DATA sub where sub.datepriceeffective <= #effdate
group by itemcode
) x
on x.itemCode = d.itemCode
and x.effdate = d.datePriceEffective
Note that the CTE is just for this example, you should swap it for your real table.
UPDATE: An alternative approach is to use ROW_NUMBER and PARTITION as follows:
SELECT
itemcode,
price
FROM
(
SELECT
itemcode,
price,
rowno = ROW_NUMBER() over (partition by itemcode order by datePriceEffective desc)
from
CTE_DATA
where datePriceEffective <= #effdate
) x
where
rowno = 1
(Substitute this select for the one in the previous query to try it out on the data)
declare #date datetime = '2012-03-05'
select distinct itemcode,
(
select top(1) itemprice
from mytable mt2
where
mt1.itemcode = mt2.itemcode and
mt2.datepriceeffective <= #date
order by datepriceeffective desc
) as itemprice
from
mytable mt1
where
datepriceeffective <= #date
SELECT b.itemCode,
(SELECT Price FROM dbo.tblProducts a WHERE datePriceEffective =
(SELECT MAX(a.datePriceEffective) FROM dbo.tblProducts a WHERE a.itemCode = b.itemCode)) AS Price
FROM dbo.tblProducts b
WHERE b.datePriceEffective <= '2012-03-05'
GROUP BY b.itemCode
I have tested it! It will give you the most updated price for each Item
Assuming that you have another column called 'GivenDate', below is the query.
SELECT itemCode, price
FROM tblName
WHERE GivenDate= '2012-03-05'
This works on sql2000
SELECT s.itemCode
, ( SELECT TOP 1 price
FROM #MyTable
WHERE itemCode = s.itemCode
AND datePriceEffective < #SearchDate ) AS price
FROM (
SELECT DISTINCT itemCode
FROM #MyTable
) s
Related
Trying to get the average price change for several items. This is my first time using SQL server, coming from some light experience in Oracle SQL and MySQL. The table is set up like this:
ProductID
StartDate
EndDate
StandardCost
707
5/31/11
5/29/12
12.2
707
5/30/12
5/29/13
13
707
5/30/13
null
13.5
708
5/31/11
5/29/12
10
708
5/30/12
5/29/13
11
708
5/30/13
null
12
I would like it to return this:
ProductID
Difference
707
1.3
708
2
with a as
(
select
productID, standardcost, startDate, ISNULL(endDate, GETDATE())
from
production.ProductCostHistory
where (productID, endDate) in (select
productID, max(endDate)
from
Production.ProductCostHistory
group by
productID
)
),
b as
(
select
productID, standardcost, startDate, startDate
from
production.ProductCostHistory
where (productID, startDate) in (select
productID, min(StartDate)
from
Production.ProductCostHistory
group by
productID
)
)
select
a.productID, (a.standardcost - b.standardcost) difference
from
a join b
on a.ProductID = b.ProductID and
a.startDate = b.startDate and
a.EndDate = b.EndDate
The idea is to first get the price with the most recent end date, then subtract the price from the oldest start date, to get the change in cost over that period of time. I understand that the where ... in does not work like in Oracle SQL, but I have not found a way to use exists the way that I need it to work. The averaging will be done in PowerBI, so I'm not worried about doing that in the Query. I would appreciate any help. Thanks.
You can try calculating the Standardcosts corresponding to the first startdate, and latest enddate (for each product) using an analytic function FIRST_VALUE.
select
productid,
max(fv) as value_at_first_startdate,
max(lv) as value_at_latest_enddate,
diff = max(lv) - max(fv)
from (
select
productid,
first_value(standardcost)
over (partition by productid
order by startdate) as fv,
first_value(standardcost)
over (partition by productid
order by case when enddate is null then 0 else 1 end
, enddate desc) as lv
from cte
) t
group by productid
Assuming your SQL is supposed to return correct result:
with minmax (productId, minStart, maxEnd) as
( select
productID, min(startDate),max(endDate)
from
Production.ProductCostHistory
group by productID
),
a as
(
select
p.productID, standardcost, startDate, ISNULL(endDate, GETDATE())
from production.ProductCostHistory p
inner join minmax m on m.productId = p.productId AND
m.maxEnd = p.endDate
),
b as
(
select
p.productID, standardcost, startdate, startdate
from production.ProductCostHistory p
inner join minmax m on m.productId = p.productId AND
m.minStart = p.startDate
)
select
a.productID, (a.standardcost - b.standardcost) difference
from
a join b
on a.ProductID = b.ProductID and
a.startDate = b.startDate and
a.EndDate = b.EndDate
I have some prices for the month of January.
Date,Price
1,100
2,100
3,115
4,120
5,120
6,100
7,100
8,120
9,120
10,120
Now, the o/p I need is a non-overlapping date range for each price.
price,from,To
100,1,2
115,3,3
120,4,5
100,6,7
120,8,10
I need to do this using SQL only.
For now, if I simply group by and take min and max dates, I get the below, which is an overlapping range:
price,from,to
100,1,7
115,3,3
120,4,10
This is a gaps-and-islands problem. The simplest solution is the difference of row numbers:
select price, min(date), max(date)
from (select t.*,
row_number() over (order by date) as seqnum,
row_number() over (partition by price, order by date) as seqnum2
from t
) t
group by price, (seqnum - seqnum2)
order by min(date);
Why this works is a little hard to explain. But if you look at the results of the subquery, you will see how the adjacent rows are identified by the difference in the two values.
SELECT Lag.price,Lag.[date] AS [From], MIN(Lead.[date]-Lag.[date])+Lag.[date] AS [to]
FROM
(
SELECT [date],[Price]
FROM
(
SELECT [date],[Price],LAG(Price) OVER (ORDER BY DATE,Price) AS LagID FROM #table1 A
)B
WHERE CASE WHEN Price <> ISNULL(LagID,1) THEN 1 ELSE 0 END = 1
)Lag
JOIN
(
SELECT [date],[Price]
FROM
(
SELECT [date],Price,LEAD(Price) OVER (ORDER BY DATE,Price) AS LeadID FROM [#table1] A
)B
WHERE CASE WHEN Price <> ISNULL(LeadID,1) THEN 1 ELSE 0 END = 1
)Lead
ON Lag.[Price] = Lead.[Price]
WHERE Lead.[date]-Lag.[date] >= 0
GROUP BY Lag.[date],Lag.[price]
ORDER BY Lag.[date]
Another method using ROWS UNBOUNDED PRECEDING
SELECT price, MIN([date]) AS [from], [end_date] AS [To]
FROM
(
SELECT *, MIN([abc]) OVER (ORDER BY DATE DESC ROWS UNBOUNDED PRECEDING ) end_date
FROM
(
SELECT *, CASE WHEN price = next_price THEN NULL ELSE DATE END AS abc
FROM
(
SELECT a.* , b.[date] AS next_date, b.price AS next_price
FROM #table1 a
LEFT JOIN #table1 b
ON a.[date] = b.[date]-1
)AA
)BB
)CC
GROUP BY price, end_date
I'm trying to find the average qty on hand of my inventory over a date range from parameter #StartDate by averaging the ending qty from each day. I have three tables: a part table, a part transaction table, and a warehouse table, mocked up below.
PartNum | PartNum TranDate TranQty | PartNum OnHandQty
---------- | ------------------------------------ | --------------------
P1 | P1 6/28/2016 5 | P1 30
P2 | P1 6/26/2016 3 | P2 2
| P1 6/26/2016 -1 |
| P1 6/15/2016 2 |
| P2 6/15/2016 1 |
If today is 6/30/2016 and #StartDate = 6/1/2016, I expect a result like:
PartNum AverageOnHand
------------------------
P1 22.9
P2 1.5
However, I don't know what function would best allow me to get to an appropriate weighted sum which I could divide by the difference in dates. Is there a SumProduct function or similar that I can use here? My code, so far, is below:
select
[Part].[PartNum] as [Part_PartNum],
(max(PartWhse.OnHandQty)*datediff(day,max(PartTran.TranDate),Constants.Today)) as [Calculated_WeightedSum],
(WeightedSum/DATEDIFF(day, #StartDate, Constants.Today)) as [Calculated_AverageOnHand]
from Erp.Part as Part
right outer join Erp.PartTran as PartTran on
Part.PartNum = PartTran.PartNum
inner join Erp.PartWhse as PartWhse on
Part.PartNum = PartWhse.PartNum
group by [Part].[PartNum]
Here is a sql-server 2012 + method that is interesting.
;WITH cte AS (
SELECT
p.PartNum
,CAST(t.TranDate AS DATE) AS TranDate
,i.OnHandQty
--,SUM(SUM(t.TranQty)) OVER (PARTITION BY p.PartNum ORDER BY CAST(t.TranDate AS DATE) DESC) AS InventoryChange
,i.OnHandQty - SUM(SUM(t.TranQty)) OVER (PARTITION BY p.PartNum ORDER BY CAST(t.TranDate AS DATE) DESC) AS InventoryOnDate
,DATEDIFF(day,
CAST(ISNULL(LAG(MAX(TranDate)) OVER (PARTITION BY p.PartNum ORDER BY CAST(t.TranDate AS DATE) ASC),#StartDate) AS DATE)
,CAST(t.TranDate AS DATE)
) AS DaysAtInventory
FROM
#Parts p
LEFT JOIN #Transact t
ON p.PartNum = t.PartNum
LEFT JOIN #Inventory i
ON p.PartNum = i.PartNum
GROUP BY
p.PartNum
,CAST(t.TranDate AS DATE)
,i.OnHandQty
)
SELECT
PartNum
,(SUM(ISNULL(DaysAtInventory,0) * ISNULL(InventoryOnDate,0))
+ ((DATEDIFF(day,MAX(TranDate),CAST(GETDATE() AS DATE)) + 1) * ISNULL(MAX(OnHandQty),0)))
/((DATEDIFF(day,CAST(#StartDate AS DATE),CAST(GETDATE() AS DATE)) + 1) * 1.00) AS AvgDailyInventory
FROM
cte
GROUP BY
PartNum
This one actually gave me the 22.9 but 1.53333 the 333 gets introduced because 1 day has to get put somewhere so I stuck it as the current inventory.
Here is a previous method I answered with and this one it is a little easier to conceptualize the data..... I would be curious about performance differences between the 2 methods.
Some of these steps can be combined to be a little more concise but this works (although I got 22.6 not .1 or .9....) I rounded everything to a whole date while doing this so that you don't have to worry about beginning and end of day.
DECLARE #StartDate DATETIME = '6/1/2016'
;WITH cteDates AS (
SELECT #StartDate AS d
UNION ALL
SELECT
d + 1 AS d
FROM
cteDates c
WHERE c.d + 1 <= CAST(CAST(GETDATE() AS DATE) AS DATETIME)
--get dates to today beginning of day
)
, ctePartsDaysCross AS (
SELECT
d.d
,p.PartNum
,ISNULL(i.OnHandQty,0) AS OnHandQty
FROM
cteDates d
CROSS JOIN #Parts p
LEFT JOIN #Inventory i
ON p.PartNum = i.PartNum
)
, cteTransactsQuantityByDate AS (
SELECT
CAST(t.TranDate AS DATE) as d
,t.PartNum
,TranQty = SUM(t.TranQty)
FROM
#Transact t
GROUP BY
CAST(t.TranDate AS DATE)
,t.PartNum
)
,cteDailyInventory AS (
SELECT
c.d
,c.PartNum
,c.OnHandQty - SUM(ISNULL(t.TranQty,0)) OVER (PARTITION BY c.PartNum ORDER BY c.d DESC) AS DailyOnHand
FROM
ctePartsDaysCross c
LEFT JOIN cteTransactsQuantityByDate t
ON c.d = t.d
AND c.PartNum = t.PartNum
)
SELECT
PartNum
,AVG(CAST(DailyOnHand AS DECIMAL(6,3)))
FROM
cteDailyInventory
GROUP BY
PartNum
Here is the test data:
IF OBJECT_ID('tempdb..#Parts') IS NOT NULL
BEGIN
DROP TABLE #Parts
END
IF OBJECT_ID('tempdb..#Transact') IS NOT NULL
BEGIN
DROP TABLE #Transact
END
IF OBJECT_ID('tempdb..#Inventory') IS NOT NULL
BEGIN
DROP TABLE #Inventory
END
CREATE TABLE #Parts (
PartNum CHAR(2)
)
CREATE TABLE #Transact (
AutoId INT IDENTITY(1,1) NOT NULL
,PartNum CHAR(2)
,TranDate DATETIME
,TranQty INT
)
CREATE TABLE #Inventory (
PartNum CHAR(2)
,OnHandQty INT
)
INSERT INTO #Parts (PartNum) VALUES ('P1'),('P2'),('P3')
INSERT INTO #Transact (PartNum, TranDate, TranQty)
VALUES ('P1','6/28/2016',5),('P1','6/26/2016',3),('P1','6/26/2016',-1)
,('P1','6/15/2016',2) ,('P2','6/15/2016',1)
INSERT INTO #Inventory (PartNum, OnHandQty) VALUES ('P1',30),('P2',2)
I am thinking 1 recursive cte might be simpler might post that as an update.
Reverse the transactions to compute daily quantities. Add in the missing dates and look backward to the most recent date to fill in the daily quantities. I think I'm going to try for a better solution than this one.
http://rextester.com/JLD19862
with trn as (
select PartNum, TranDate, TranQty from PartTran
union all
select PartNum, cast('20160601' as date), 0 from PartWhse
union all
select PartNum, cast('20160630' as date), 0 from PartWhse
), qty as (
select
t.PartNum, t.TranDate,
-- assumes that end date corresponds with OnHandQty
min(w.OnHandQty) + sum(t.TranQty)
- sum(sum(t.TranQty))
over (partition by t.PartNum order by t.TranDate desc) as DailyOnHand,
coalesce(
lead(t.TranDate) over (partition by t.PartNum order by t.TranDate),
dateadd(day, 1, t.TranDate)
) as NextTranDate
-- if lead() isn't available...
-- coalesce(
-- (
-- select min(t2.TranDate) from trn as t2
-- where t2.PartNum = t.PartNum and t2.TranDate > t.TranDate
-- ),
-- dateadd(day, 1, t.TranDate)
-- ) as NextTranDate
from PartWhse as w inner join trn as t on t.PartNum = w.PartNum
where t.TranDate between '20160601' and '20160630'
group by t.PartNum, t.TranDate
)
select
PartNum,
sum(datediff(day, TranDate, NextTranDate) * DailyOnHand) * 1.00
/ sum(datediff(day, TranDate, NextTranDate)) as DailyAvg
from qty
group by PartNum;
I was able to solve this with a sum. First, I multiplied the final quantity on hand by the number of days in the range. Next, I multiplied each change in inventory by the time from #StartDate until the TransDate.
select
[Part].[PartNum] as [Part_PartNum],
(max(PartWhse.OnHandQty)*datediff(day,#StartDate,Constants.Today)-
sum(PartTran.TranQty*datediff(day,#StartDate,PartTran.TranDate))) as [Calculated_WeightedSum],
(WeightedSum/DATEDIFF(day, #StartDate, Constants.Today)) as [Calculated_AverageOnHand]
from Erp.Part as Part
right outer join Erp.PartTran as PartTran on
Part.PartNum = PartTran.PartNum
inner join Erp.PartWhse as PartWhse on
Part.PartNum = PartWhse.PartNum
group by [Part].[PartNum]
Thanks for your help everyone! You really helped me think it through.
I’m trying to select the pair of product – distribution center attached with the most recent order (based on order date). For one order I can have multiple products, but the whole order will be shipped from one specific distribution center.
How do I select the specific product-distribution center attached with the latest order?
My structure is basically like this:
data.orderdetail table has ordernum, orderdate, distributioncenter
I tried to pull like this, but it doesn’t give me the desired result. I’m using sql server 2008:
SELECT DISTINCT y.OrderNum, y.Product, y.DistributionCenter
, CAST(y.OrderDate AS DATE) AS Orderdate
FROM (SELECT OrderNum, MAX(CAST(Orderdate AS date)) AS orderdate
FROM data.OrderDetail
GROUP BY OrderNum) AS x
INNER JOIN data.OrderDetail AS y
ON y.OrderNum = x.OrderNum
It looks as if you need one more clause in your join Condition
You've got
ON y.OrderNum = x.OrderNum
Which will return all the orders that match the Order number in the subquery
But you'll need
ON y.OrderNum = x.OrderNum
AND y.OrderDate = x.orderdate
Which will return all the orders that match the Order number in the subquery and the maximum date for that order number
SELECT DISTINCT
y.OrderNum,
y.Product,
y.DistributionCenter,
CAST(y.OrderDate AS DATE) AS Orderdate
FROM (
SELECT
OrderNum,
MAX(CAST(Orderdate AS date)) AS orderdate
FROM data.OrderDetail
GROUP BY OrderNum
) AS x
INNER JOIN
data.OrderDetail AS y
ON y.OrderNum = x.OrderNum
AND y.OrderDate = x.orderdate
I believe what you are looking for is row_number. This will partition your result set by OrderNum then rank the sets by the OrderDate. You can then filter off the extra rows in another where clause.
select result.*,
CAST(result.OrderDate as date) as Orderdate,
from (
select y.*,
row_number() over (
partition by y.OrderNum order by CAST(y.OrderDate as date) desc
) rank_
from (
select OrderNum,
MAX(CAST(Orderdate as date)) as orderdate
from data.OrderDetail
group by OrderNum
) as x
inner join data.OrderDetail as y on y.OrderNum = x.OrderNum
) result
where result.rank_ = 1;
select * from
(
SELECT OrderNum, Product, DistributionCenter, OrderDate
, ROW_NUMBER() over (partition by OrderNum order by OrderDate desc) as rownum
FROM OrderDetail
) as xxx
where xxx.rownum = 1
ROW_NUMBER (Transact-SQL)
Try this.
; WITH CTE1
AS (
SELECT
od.OrderNum
, od.Product
, od.DistributionCenter
, CAST(od.OrderDate AS DATE) AS OrderDate
, RowNumber = ROW_NUMBER() OVER (PARTITION BY od.Product, od.DistributionCenter ORDER BY CAST(od.OrderDate AS DATE) DESC)
FROM data.OrderDetail od
)
SELECT
OrderNum
, Product
, DistributionCenter
, OrderDate
FROM CTE1
WHERE RowNumber = 1
Is it possible to find the difference of days of different records in SQL Server 2008 R2?
SELECT OrderDate FROM OrdersTbl WHERE SKU='AA0000' ORDER BY ORDERDATE DESC
OrderDate
-----------------------
2009-12-03 00:00:00.000
2009-04-03 00:00:00.000
2008-02-22 00:00:00.000
2008-02-21 00:00:00.000
2007-02-18 00:00:00.000
2007-01-27 00:00:00.000
2006-10-13 00:00:00.000
I would like a way to get how many days in between there are for each order date so that I could find the average frequency. Thanks in advance.
You can do it with a common table expression and ROW_NUMBER:
WITH OrderDates AS (
SELECT
ROW_NUMBER() OVER (ORDER BY OrderDate DESC) AS RowNumber,
OrderDate
FROM OrdersTable
WHERE SKU = 'AA0000'
)
SELECT
AVG(DATEDIFF(DD, O2.OrderDate, O1.OrderDate)) AS AverageFrequency
FROM OrderDates O1
LEFT JOIN OrderDates O2
ON O2.RowNumber = O1.RowNumber + 1
Sucks there's no LEAD/LAG support in SQL Server:
SELECT z.orderdate,
z.prev_date,
DATEDIFF(dd, z.prev_date, z.orderdate)
FROM (SELECT OrderDate,
(SELECT MAX(y.orderdate)
FROM ORDERSTBL y
WHERE y.orderdate < x.orderdate
AND y.sku = x.sku) AS prev_date
FROM OrdersTbl x
WHERE x.sku ='AA0000') z
ORDER BY z.orderdate DESC
;With cteDifference as (
Select SKU, OrderDate, Row_Number() OVER (Partition by SKU Order by OrderDate) as RowNumber
from OrdersTbl
)
select cur.SKU,
cur.OrderDate as CurrentDate,
prev.OrderDate as PreviousDate,
DATEDIFF(DD,prev.OrderDate, cur.OrderDate) as DaysDifference
from cteDifference cur
left join cteDifference prev
on cur.SKU = prev.SKU
and cur.RowNumber = prev.RowNumber + 1
where cur.SKU = 'AA0000'
order by cur.OrderDate desc