Select daily data group by weekly in sql server 2008 - sql-server

hi friend i have following my table structure and data
CREATE TABLE [dbo].[Pairs_Details](
[sno] [int] IDENTITY(1,1) NOT NULL,
[userid] [nvarchar](50) NULL,
[date] [datetime] NULL,
[ljoin] [int] NULL,
[rjoin] [int] NULL
) ON [PRIMARY]
sno userid date ljoin rjoin
1 LDS 2014-02-17 00:00:00.000 1 NULL
2 LDS 2014-02-17 00:00:00.000 NULL 1
3 LDS1 2014-02-18 00:00:00.000 1 NULL
4 LDS 2014-02-18 00:00:00.000 1 NULL
5 LDS1 2014-02-18 00:00:00.000 NULL 1
6 LDS 2014-02-18 00:00:00.000 1 NULL
7 SUNIL1 2014-02-19 00:00:00.000 1 NULL
8 LDS1 2014-02-19 00:00:00.000 1 NULL
9 LDS 2014-02-19 00:00:00.000 1 NULL
10 SUNIL1 2014-02-19 00:00:00.000 NULL 1
11 LDS1 2014-02-19 00:00:00.000 1 NULL
12 LDS 2014-02-19 00:00:00.000 1 NULL
13 SUNIL2 2014-02-19 00:00:00.000 1 NULL
14 LDS1 2014-02-19 00:00:00.000 NULL 1
15 LDS 2014-02-19 00:00:00.000 1 NULL
16 rajesh123 2014-02-19 00:00:00.000 1 NULL
17 SUNIL1 2014-02-19 00:00:00.000 NULL 1
18 LDS1 2014-02-19 00:00:00.000 1 NULL
19 LDS 2014-02-19 00:00:00.000 1 NULL
20 SUNIL2 2014-02-19 00:00:00.000 NULL 1
21 LDS1 2014-02-19 00:00:00.000 NULL 1
22 LDS 2014-02-19 00:00:00.000 1 NULL
23 LDS2 2014-02-19 00:00:00.000 1 NULL
24 LDS 2014-02-19 00:00:00.000 NULL 1
25 SUNIL1 2014-02-20 00:00:00.000 NULL 1
26 LDS1 2014-02-20 00:00:00.000 1 NULL
27 LDS 2014-02-20 00:00:00.000 1 NULL
28 rajesh123 2014-02-20 00:00:00.000 NULL 1
29 SUNIL1 2014-02-20 00:00:00.000 NULL 1
30 LDS1 2014-02-20 00:00:00.000 1 NULL
31 LDS 2014-02-20 00:00:00.000 1 NULL
32 LDS 2014-02-24 00:00:00.000 NULL 1
33 Jitendra123 2014-02-27 00:00:00.000 1 NULL
34 LDS2 2014-02-27 00:00:00.000 1 NULL
35 LDS 2014-02-27 00:00:00.000 NULL 1
36 rajeev123 2014-02-27 00:00:00.000 1 NULL
37 Jitendra123 2014-02-27 00:00:00.000 1 NULL
40 jyoti123 2014-02-27 00:00:00.000 1 NULL
41 SUNIL1 2014-02-27 00:00:00.000 1 NULL
42 LDS1 2014-02-27 00:00:00.000 1 NULL
43 LDS 2014-02-27 00:00:00.000 1 NULL
44 meeta 2014-03-01 00:00:00.000 1 NULL
45 jyoti123 2014-03-01 00:00:00.000 1 NULL
46 SUNIL1 2014-03-01 00:00:00.000 1 NULL
47 LDS1 2014-03-01 00:00:00.000 1 NULL
48 LDS 2014-03-01 00:00:00.000 1 NULL
38 LDS2 2014-02-27 00:00:00.000 1 NULL
39 LDS 2014-02-27 00:00:00.000 NULL 1
and this is my stored procdure .
create proc [dbo].[pair_Scounting]
(
#userid nvarchar(50),
#start_date datetime,
#end_date datetime
)
as
begin
SET DATEFIRST 1;
SELECT userid,
Sum(ISNULL(ljoin,0)) AS ljoin,
Sum(ISNULL(rjoin,0)) AS rjoin, DATEPART(wk, Date) AS WeekNumber,
CASE
WHEN YEAR(DATEADD(DAY, 1-DATEPART(WEEKDAY, Min([date])), Min([date]))) < YEAR(Min([date]))
THEN CAST(DATEADD(YEAR, DATEDIFF(YEAR, 0,DATEADD(YEAR, 0 ,GETDATE())), 0) AS Varchar(50)) + ' TO ' + Cast(DATEADD(dd, 7-(DATEPART(dw, Min([date]))), Min([date])) AS Varchar(50))
ELSE
Cast(DATEADD(DAY, 1-DATEPART(WEEKDAY, Min([date])), Min([date])) AS Varchar(50)) + ' TO ' + Cast(DATEADD(dd, 7-(DATEPART(dw, Min([date]))), Min([date])) AS Varchar(50))
END DateRange,
Case
When Sum(ISNULL(ljoin,0)) > Sum(ISNULL(rjoin,0)) Then Sum(ISNULL(ljoin,0))-Sum(ISNULL(rjoin,0))
End LeftCary,
Case
When Sum(ISNULL(rjoin,0)) > Sum(ISNULL(ljoin,0)) Then Sum(ISNULL(rjoin,0))-Sum(ISNULL(ljoin,0))
End RightCary
FROM Pairs_Details where userid=#userid and date between #start_date and #end_date
Group By userid,DATEPART(wk, Date)
end
GO
if i execute my stored procedure as follows it return following result
exec pair_Scounting 'LDS','2014-02-17','2014-02-28'
userid ljoin rjoin WeekNumber DateRange LeftCary RightCary
LDS 10 2 8 17-02-2014 TO 23-02-2014 8 NULL
LDS 1 3 9 24-02-2014 TO 2-03-2014 NULL 2
so how we can add this LeftCary (8) with ljoin of next DateRange(24-02-2014 To 2-03-2014)and
RightCary(2)with rjoin of next DateRange(4-03-2014 TO 10-03-2014)
means here ljoin of DateRange(24-02-2014 TO 2-03-2014) is 1 and LeftCary of DateRange( 17-02-2014 TO 23-02-2014) is 8 so total ljoin of DateRange((24-02-2014 TO 2-03-2014)) is 8+1=9
My Desired result as follows
userid ljoin rjoin WeekNumber DateRange LeftCary RightCary
LDS 10 2 8 17-02-2014 TO 23-02-2014 8 NULL
LDS 9 3 9 24-02-2014 TO 2-03-2014 NULL 2
please any one can suggest us how this can be done
thanks

I would turn the stored procedure into an inline table valued function instead and join it on itself. The following presumes that you won't have duplicate WeekNumbers:
SELECT x.userid,
(x.ljoin + ISNULL((SELECT y.LeftCarry FROM dbo.InLine(#param1, #param2, #param3) AS y WHERE y.WeekNumber = (x.WeekNumber - 1)), 0)) AS ljoin_carry,
(x.rjoin + ISNULL((SELECT z.RightCarry FROM dbo.InLine(#param1, #param2, #param3) AS z WHERE z.WeekNumber = (x.WeekNumber - 1)), 0)) AS rjoin_carry,
x.WeekNumber,
x.DateRange,
x.LeftCarry,
x.RightCarry
FROM dbo.InLine(#param1, #param2, #param3) AS x
ORDER BY x.WeekNumber

Related

How to increment a column like 1, 1, 2, 2, 3, 3 in SQL Server

I have data like this in a table:
https://i.stack.imgur.com/JYZjz.png
I want to group first two events (1, 10) as F1, than second two events (1,10) as F2
Running this SQL query:
SELECT
Id, Date, Event,
CASE
WHEN Event = 1 AND LEAD(Event) OVER (ORDER BY ID) = 10
THEN 'F1'
WHEN Event = 10 AND LAG(Event) OVER (ORDER BY ID) = 1
THEN 'F1'
ELSE NULL
END AS Flow
FROM
lxfr.SystemEventsDiary
WHERE
Event IN (1, 10)
I get this result as output:
ID
Date
Event
Flow
4
2022-02-07 00:00:00.000
1
F1
44
2022-02-08 00:00:00.000
10
F1
47
2022-02-09 00:00:00.000
1
F1
72
2022-02-10 00:00:00.000
10
F1
75
2022-02-10 00:00:00.000
1
F1
97
2022-02-11 00:00:00.000
10
F1
100
2022-02-11 00:00:00.000
1
NULL
113
2022-02-28 00:00:00.000
1
F1
217
2022-03-04 00:00:00.000
10
F1
235
2022-03-22 00:00:00.000
10
NULL
241
2022-03-22 00:00:00.000
1
F1
270
2022-03-24 00:00:00.000
10
F1
273
2022-03-24 00:00:00.000
1
F1
308
2022-03-25 00:00:00.000
10
F1
But I need a result like this:
ID
Date
Event
Flow
4
2022-02-07 00:00:00.000
1
F1
44
2022-02-08 00:00:00.000
10
F1
47
2022-02-09 00:00:00.000
1
F2
72
2022-02-10 00:00:00.000
10
F2
75
2022-02-10 00:00:00.000
1
F3
97
2022-02-11 00:00:00.000
10
F3
100
2022-02-11 00:00:00.000
1
NULL
113
2022-02-28 00:00:00.000
1
F4
217
2022-03-04 00:00:00.000
10
F4
235
2022-03-22 00:00:00.000
10
NULL
241
2022-03-22 00:00:00.000
1
F5
270
2022-03-24 00:00:00.000
10
F5
273
2022-03-24 00:00:00.000
1
F6
308
2022-03-25 00:00:00.000
10
F6
I tried with a CTE, but I'm getting errors...
Thanks
This seems to work. I've copied your desired output as my input sample data just to show that my new column matches your Desired one, and also a couple of other renames for keywords:
declare #t table (ID int, DateOf datetime, EventNo int, DesiredFlow varchar(7))
insert into #t(ID,DateOf,EventNo,DesiredFlow) values
(4 ,'2022-02-07T00:00:00.000',1 ,'F1'),
(44 ,'2022-02-08T00:00:00.000',10 ,'F1'),
(47 ,'2022-02-09T00:00:00.000',1 ,'F2'),
(72 ,'2022-02-10T00:00:00.000',10 ,'F2'),
(75 ,'2022-02-10T00:00:00.000',1 ,'F3'),
(97 ,'2022-02-11T00:00:00.000',10 ,'F3'),
(100 ,'2022-02-11T00:00:00.000',1 ,NULL),
(113 ,'2022-02-28T00:00:00.000',1 ,'F4'),
(217 ,'2022-03-04T00:00:00.000',10 ,'F4'),
(235 ,'2022-03-22T00:00:00.000',10 ,NULL),
(241 ,'2022-03-22T00:00:00.000',1 ,'F5'),
(270 ,'2022-03-24T00:00:00.000',10 ,'F5'),
(273 ,'2022-03-24T00:00:00.000',1 ,'F6'),
(308 ,'2022-03-25T00:00:00.000',10 ,'F6')
;With Neighbours as (
select
*,
LEAD(EventNo,1,NULL) OVER (ORDER BY ID) as Successor,
LAG(EventNo,1,NULL) OVER (ORDER BY ID) as Predecessor
from
#t t
), NoStragglers as (
select
*
from
Neighbours
where
(EventNo = 1 and Successor = 10) or
(EventNo = 10 and Predecessor = 1)
), Ordered as (
select
*, ROW_NUMBER() OVER (PARTITION BY EventNo ORDER BY ID) as rn
from NoStragglers
)
select
t.*,'F' + (CONVERT(varchar(10),o.rn)) as ActualFlow
from
#t t
left join
Ordered o
on
t.Id = o.Id
Hopefully you can see how each CTE builds from the previous one (either by inspection or changing the final select to pull one one of the earlier CTEs instead).
Result:
ID DateOf EventNo DesiredFlow ActualFlow
----------- ----------------------- ----------- ----------- -----------
4 2022-02-07 00:00:00.000 1 F1 F1
44 2022-02-08 00:00:00.000 10 F1 F1
47 2022-02-09 00:00:00.000 1 F2 F2
72 2022-02-10 00:00:00.000 10 F2 F2
75 2022-02-10 00:00:00.000 1 F3 F3
97 2022-02-11 00:00:00.000 10 F3 F3
100 2022-02-11 00:00:00.000 1 NULL NULL
113 2022-02-28 00:00:00.000 1 F4 F4
217 2022-03-04 00:00:00.000 10 F4 F4
235 2022-03-22 00:00:00.000 10 NULL NULL
241 2022-03-22 00:00:00.000 1 F5 F5
270 2022-03-24 00:00:00.000 10 F5 F5
273 2022-03-24 00:00:00.000 1 F6 F6
308 2022-03-25 00:00:00.000 10 F6 F6
Which seems to match.
Check this,
create table #temp(id int,dates datetime,Events int)
insert into #temp(id,dates,events) values
(4 ,'2022-02-07 00:00:00.000', 1 )
,(44 ,'2022-02-08 00:00:00.000', 10 )
,(47 ,'2022-02-09 00:00:00.000', 1 )
,(72 ,'2022-02-10 00:00:00.000', 10 )
,(75 ,'2022-02-10 00:00:00.000', 1 )
,(97 ,'2022-02-11 00:00:00.000', 10 )
,(100 ,'2022-02-11 00:00:00.000', 1 )
,(113 ,'2022-02-28 00:00:00.000', 1 )
,(217 ,'2022-03-04 00:00:00.000', 10 )
,(235 ,'2022-03-22 00:00:00.000', 10 )
,(241 ,'2022-03-22 00:00:00.000', 1 )
,(270 ,'2022-03-24 00:00:00.000', 10 )
,(273 ,'2022-03-24 00:00:00.000', 1 )
,(308 ,'2022-03-25 00:00:00.000', 10 )
WITH CTE
AS (SELECT Id,
Dates,
Events,
CASE
WHEN Events = 1
AND LEAD(Events) OVER(
ORDER BY ID) = 10
THEN id
WHEN Events = 10
AND LAG(Events) OVER(
ORDER BY ID) = 1
THEN LAG(id) OVER(
ORDER BY ID)
ELSE NULL
END AS Flow
FROM #temp),
CtE1
AS (SELECT *,
DENSE_RANK() OVER(
ORDER BY flow) rn
FROM CTe
WHERE flow IS NOT NULL
UNION ALL
SELECT *,
NULL
FROM CTE
WHERE flow IS NULL)
SELECT id,
dates,
events,
CASE
WHEN rn IS NOT NULL
THEN concat('F', rn)
ELSE NULL
END Flow
FROM cte1
ORDER BY id;
drop table #temp
You can use cascading subqueries to assign a ROW_NUMBER() based on Flow values that are NOT NULL:
SELECT d.Id,
d.Date,
d.Event,
CASE WHEN b.rn IS NULL THEN NULL
ELSE CONCAT('F', b.rn)
END AS Flow
FROM (SELECT
Id,
Date,
Event,
Flow,
ROW_NUMBER() OVER (PARTITION BY Event ORDER BY Id ASC) as rn
FROM (SELECT
Id,
Date,
Event,
CASE WHEN Event = 1 AND LEAD(Event) OVER (ORDER BY Id) = 10 THEN 1
WHEN Event = 10 AND LAG(Event) OVER (ORDER BY Id) = 1 THEN 1
ELSE 0
END as Flow
FROM SystemEventsDiary WHERE Event IN (1, 10)
) a WHERE Flow = 1) b
RIGHT JOIN SystemEventsDiary d ON b.id = d.Id
ORDER BY d.Id ASC
Result:
Id
Date
Event
Flow
4
2022-02-07 00:00:00.000
1
F1
44
2022-02-08 00:00:00.000
10
F1
47
2022-02-09 00:00:00.000
1
F2
72
2022-02-10 00:00:00.000
10
F2
75
2022-02-10 00:00:00.000
1
F3
97
2022-02-11 00:00:00.000
10
F3
100
2022-02-11 00:00:00.000
1
NULL
113
2022-02-28 00:00:00.000
1
F4
217
2022-03-04 00:00:00.000
10
F4
235
2022-03-22 00:00:00.000
10
NULL
241
2022-03-22 00:00:00.000
1
F5
270
2022-03-24 00:00:00.000
10
F5
273
2022-03-24 00:00:00.000
1
F6
308
2022-03-25 00:00:00.000
10
F6
db<>fiddle here.

Subtract depletion And Limit for SUM

First I'm using AdventureWork2019 as a reference
I have a query where I'm joining 5 Tables
USE [AdventureWorks2019]
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
Alter PROCEDURE dbo.TestLocation
#UseDate DateTime
AS
BEGIN
SET NOCOUNT ON;
SELECT prodID
,SUM(PurchQty) AS TotalPurchase
,SUM(SalesQty) AS TotalSell
,StartDate
from (
SELECT DISTINCT WO.ProductID AS prodID
, StartDate
,WO.OrderQty AS PurchQty
,SOD.OrderQty AS SalesQty
FROM Sales.SalesOrderDetail SOD
LEFT JOIN Production.WorkOrderRouting WOR ON WOR.ProductID = SOD.ProductID
--LEFT JOIN Production.Location PL ON PL.LocationID = WOR.LocationID
--The above Join is the one for the locationID and it's working Fine
LEFT JOIN Production.WorkOrder WO ON WO.ProductID = SOD.ProductID
FULL OUTER JOIN Purchasing.PurchaseOrderDetail POD ON POD.ProductID = SOD.ProductID
WHERE StartDate = #UseDate
-- AND PL.LocationID >= 10
) Test3
Group by prodID,StartDate
order by prodID ASC, StartDate
END
GO
EXEC TestLocation '2011-07-02 00:00:00.000'
Output(sample):
prodID TotalPurc TotalSell StartDate
717 8 36 2011-07-02 00:00:00.000
730 9 47 2011-07-02 00:00:00.000
744 2 3 2011-07-02 00:00:00.000
747 12 21 2011-07-02 00:00:00.000
749 5 15 2011-07-02 00:00:00.000
761 16 138 2011-07-02 00:00:00.000
775 26 91 2011-07-02 00:00:00.000
777 12 78 2011-07-02 00:00:00.000
802 6 21 2011-07-02 00:00:00.000
804 40 60 2011-07-02 00:00:00.000
806 16 138 2011-07-02 00:00:00.000
807 24 23 2011-07-02 00:00:00.000
810 21 28 2011-07-02 00:00:00.000
811 6 21 2011-07-02 00:00:00.000
813 8 37 2011-07-02 00:00:00.000
817 21 28 2011-07-02 00:00:00.000
And another Table For LocationID (as a warehouse)
SELECT LocationID,CostRate,Availability
FROM Production.Location
WHERE LocationID >= 10
order by CostRate ASC
LocationID CostRate Availability
50 12.25 120.00
60 12.25 120.00
30 14.50 120.00
40 15.75 120.00
45 18.00 80.00
10 22.50 96.00
20 25.00 108.00
What I want to do is to take each LoactionId and ProdID and take TotalPurc to the location and decrement the quantity in the Availability column, each TotalSell will increment the Availability column. The max Availability quantity is 130.
If all locations have no Available quantity that is the Available is 0 for all locations then it will stop.
the above will work with the date specified as you can check the query and run it if you have
AdventureWork2019
simple output to check how I want the data to be:
prodID TotalPurc TotalSell StartDate
717 8 36 2011-07-02 00:00:00.000
730 9 47 2011-07-02 00:00:00.000
744 2 3 2011-07-02 00:00:00.000
747 12 21 2011-07-02 00:00:00.000
749 5 15 2011-07-02 00:00:00.000
LocationID CostRate Availability
50 12.25 120.00
60 12.25 120.00
30 14.50 120.00
40 15.75 120.00
45 18.00 80.00
10 22.50 96.00
20 25.00 108.00
Output :
prodID TotalPurc TotalSell StartDate LocationID Availability Remaining
717 8 36 2011-07-02 00:00:00.000 50 130 18
717 8 36 2011-07-02 00:00:00.000 60 130 8
717 8 36 2011-07-02 00:00:00.000 30 128 0
--what happened above is that I took the (120-8) = 112 then 112+36 = 148 we only can use 130 then the remaining is 18 then we took the next `LocationID` with the least Cost (120+18 = 138 we can use 130 so we took the 8) and used it in the next `LocationID`
730 9 47 2011-07-02 00:00:00.000 30 130 36
730 9 47 2011-07-02 00:00:00.000 40 130 26
730 9 47 2011-07-02 00:00:00.000 45 106 0
744 2 3 2011-07-02 00:00:00.000 45 107 0
747 12 21 2011-07-02 00:00:00.000 45 116 0
749 5 15 2011-07-02 00:00:00.000 45 126 0
--the above is the same as the first 3 rows we subtract and add to the availability
The other condition is that if all locations reached 0 or 130 then stop
How can I do that in SQL Server? I tried using CTE but didn't work well with me and tried the cursor which I think is the best for this kind of thing but didn't achieve anything.
Thank you in advance
Edit :
ALTER FUNCTION GetStockMovment
(
-- Add the parameters for the function here
#ForDate Datetime
)
RETURNS #Sums TABLE (
RemoveQTY Numeric(24, 7),
ADDQTY Numeric(24, 7)
)
AS
BEGIN
Declare #WoSum Numeric(24, 7),
#SODSUM Numeric(24, 7),
#WORSum Numeric(24, 7),
#PODSum Numeric(24, 7)
select #SODSUM = SUM(SOD.OrderQty) from Sales.SalesOrderDetail SOD
INNER JOIN Sales.SalesOrderHeader SOH ON SOD.SalesOrderID = SOH.SalesOrderID
where SOH.OrderDate = #ForDate
select #WoSum = sum(orderQty) from Production.WorkOrder
where StartDate = #ForDate
select #PODSum = sum(POD.OrderQty) from Purchasing.PurchaseOrderDetail POD
INNER JOIN Purchasing.PurchaseOrderHeader POH ON POD.PurchaseOrderID = POH.PurchaseOrderID
where POH.OrderDate = #ForDate
select #WoSum = sum(WO.OrderQty) from Production.WorkOrder WO
where WO.DueDate = #ForDate
INSERT INTO #Sums (RemoveQTY,ADDQTY)
SELECT isnull(#SODSUM,0) + isnull(#WORSum,0) , isnull(#PODSum,0) + isnull(#WoSum,0)
RETURN;
END;
GO
select * from dbo.GetStockMovment ('2014-05-26 00:00:00.000')
Output:
RemoveQTY ADDQTY
189.0000000 5334.0000000
You should use LAG or LEAD function.
https://learn.microsoft.com/en-us/sql/t-sql/functions/lead-transact-sql?view=sql-server-ver15
https://learn.microsoft.com/en-us/sql/t-sql/functions/lag-transact-sql?view=sql-server-ver15

SQL Server : subquery to return the latest cost revision of a unit of inventory on hand

I have 2 tables that I am trying to marry together to end up with a result, which tells me the standard cost of the item that is still on hand (based on a FIFO costing method). The first table is inventory receipts , which tells me the parts left to consume and the transaction dates of those receipts. The second is a standard cost view which tells me the cost history of the item (rev = revision number which increases by 1 each time the standard cost of the part gets updated).
I currently have a solution which works using TOP 1 and ordering by DESC on effective date of cost, however, when I run this for the entire inventory list of the company , it takes over 16 minutes due to the TOP 1 sub-query inefficiency and cost.
Sample data (inventory receipts on hand):
partID warehouse transDate seqn orderID qtytoconsume
-------------------------------------------------------------
P0003 W01 2019-01-24 00:00:00.000 1 ORD0187 2
P0003 W01 2018-06-24 00:00:00.000 1 ORD0099 3
P0003 W01 2018-11-24 00:00:00.000 1 ORD0165 1
P0003 W04 2018-12-14 00:00:00.000 1 ORD0175 1
P0002 W02 2019-01-14 00:00:00.000 1 ORD0184 4
P0002 W02 2019-03-24 00:00:00.000 1 ORD0199 1
P0002 W03 2018-05-27 00:00:00.000 1 ORD0093 1
P0002 W03 2018-12-06 00:00:00.000 1 ORD0171 2
P0001 W04 2018-09-09 00:00:00.000 1 ORD0146 5
P0001 W02 2019-04-22 00:00:00.000 1 ORD0200 4
P0001 W03 2019-03-29 00:00:00.000 1 ORD0200 2
P0001 W02 2018-02-14 00:00:00.000 1 ORD0061 1
and standard cost view:
partID document effdate rev costamt
-----------------------------------------------------
P0001 IV0001 2018-01-28 00:00:00.000 1 1000.00
P0001 IV0023 2018-06-30 00:00:00.000 2 1200.00
P0001 IV0045 2019-01-01 00:00:00.000 3 1300.00
P0002 IV0001 2018-01-28 00:00:00.000 1 45.00
P0002 IV0013 2018-04-10 00:00:00.000 2 42.00
P0002 IV0045 2019-01-01 00:00:00.000 3 56.00
P0003 IV0001 2018-01-28 00:00:00.000 1 23400.00
P0003 IV0003 2018-02-20 00:00:00.000 2 11200.00
P0003 IV0045 2019-01-01 00:00:00.000 3 15000.00
P0003 IV0047 2019-02-27 00:00:00.000 4 13400.00
P0003 IV0078 2019-05-03 00:00:00.000 5 14670.00
And my result (which equals my expected result), but for large row sets is less than ideal.
partID warehouse transDate seqn orderID qty costamt
-------------------------------------------------------------
P0003 W01 2019-01-24 00:00:00.000 1 ORD0187 2 15000.00
P0003 W01 2018-06-24 00:00:00.000 1 ORD0099 3 11200.00
P0003 W01 2018-11-24 00:00:00.000 1 ORD0165 1 11200.00
P0003 W04 2018-12-14 00:00:00.000 1 ORD0175 1 11200.00
P0002 W02 2019-01-14 00:00:00.000 1 ORD0184 4 56.00
P0002 W02 2019-03-24 00:00:00.000 1 ORD0199 1 56.00
P0002 W03 2018-05-27 00:00:00.000 1 ORD0093 1 42.00
P0002 W03 2018-12-06 00:00:00.000 1 ORD0171 2 42.00
P0001 W04 2018-09-09 00:00:00.000 1 ORD0146 5 1200.00
P0001 W02 2019-04-22 00:00:00.000 1 ORD0200 4 1300.00
P0001 W03 2019-03-29 00:00:00.000 1 ORD0200 2 1300.00
P0001 W02 2018-02-14 00:00:00.000 1 ORD0061 1 1000.00
My query is:
SELECT
ioh.*, sc.costamt, sc.effdate
FROM
inventoryonHand ioh
LEFT JOIN
standardcosts sc ON sc.partID = ioh.partID
AND sc.effdate = (SELECT TOP 1 sc2.effDate
FROM standardcosts sc2
WHERE sc2.partID = sc.partID
AND sc2.effDate < ioh.transDate
ORDER BY sc2.partID ASC, sc2.effDate DESC);
Thanks so much guys!
You can try it (if your consider partID and transdate can be unique into your inventoryonHand table, otherwhise use partition by on his key) :
select * from (
select f1.*,
f2.effdate, f2.costamt, f2.rev,
row_number() over(partition by f1.partid, f1.transdate order by f2.effdate desc, f2.rev desc) as lasteffDaterank
from inventoryonHand f1
left outer join standardcosts f2 on f1.partid=f2.partid and f2.effDate < f1.transDate
) tmp
where lasteffDaterank=1
You could try to simplify the subquery using max().
(SELECT max(sc1.effdate)
FROM standardcosts sc2
WHERE sc2.partid = sc.partid
AND sc2.effdate < ioh.transdate)
For performance try an index on standardcosts (partid ASC, effdate DESC).
You can ty this too, not really sur its better ;)
select f1.*, f3.*
from inventoryonHand f1
outer apply
(
select top 1 f2.costamt from standardcosts f2
where f1.partid=f2.partid and f2.effDate < f1.transDate
order by f2.effdate desc, f2.rev desc
) f3

Date and Time field query

If Date and Time field is having date entry like '2017-12-04 21:30:00.000' and Duration is '1800' and '2017-12-04 21:30:22.000' has '22' then our query should show record as '2017-12-04 21:30:00.000' as '1800' but 22 seconds should come at '2017-12-04 21:30:00.000' but my query is showing at 2017-12-04 21:00:00.000 with Duration as '1822' (sum of 1800+22). I have this question is this link in different manner but didn't get a proper answer hence asking this question here as I'm not able to modify in proper way.
Query:
SELECT Interval=(CASE WHEN datepart(MINUTE,[DateTime]) = 0 and datepart(SECOND,DateTime)=0 THEN
CAST(CONVERT(VARCHAR(30),DATEADD(HOUR,-1,[DateTime]),101) + ' '+ cast(format(DATEPART(HOUR,DATEADD(HOUR,-1,[DateTime])),'0#') as varchar)+':30:00' as DateTime)
ELSE (CAST(CONVERT(VARCHAR(30),[DateTime],101) +' ' + (case when datepart(MINUTE,[DateTime])<=30 and
datepart(SECOND,[DateTime])<59
then cast(format(DATEPART(HOUR,[DateTime]),'0#') as varchar)+':00:00'
else cast(format(DATEPART(HOUR,[DateTime]),'0#') as varchar)+':30:00' end) as DateTime)) END),
ID,Code,Duration=SUM(Duration) FROM Table
WHERE [DateTime] >= '2017-12-04 00:00:00' and [DateTime] <= '2017-12-04 23:59:59'
GROUP BY (CASE WHEN datepart(MINUTE,[DateTime]) = 0 and datepart(SECOND,DateTime)=0 THEN
CAST(CONVERT(VARCHAR(30),DATEADD(HOUR,-1,[DateTime]),101) + ' '+ cast(format(DATEPART(HOUR,DATEADD(HOUR,-1,[DateTime])),'0#') as varchar)+':30:00' as DateTime)
ELSE (CAST(CONVERT(VARCHAR(30),[DateTime],101) +' ' + (case when datepart(MINUTE,[DateTime])<=30 and
datepart(SECOND,[DateTime])<59 then cast(format(DATEPART(HOUR,[DateTime]),'0#') as varchar)+':00:00'
else cast(format(DATEPART(HOUR,[DateTime]),'0#') as varchar)+':30:00' end) as DateTime)) END),
ID,Code
Order by Interval
Actual table data.
DateTime ID Code Duration
2017-12-12 00:30:00 1 12 1800
2017-12-12 00:30:37 1 12 37
2017-12-12 01:00:00 1 12 1793
2017-12-12 01:30:00 1 12 1800
2017-12-12 01:30:59 1 12 59
If I run the query then expected result is,
DateTime ID Code Duration
2017-12-12 00:00:00 1 12 1837
2017-12-12 00:30:00 1 12 1800
2017-12-12 01:00:00 1 12 1800
2017-12-12 01:30:00 1 12 59
DateTime ID Code Duration
2017-12-12 00:00:00 1 12 1800
2017-12-12 00:30:00 1 12 1800
2017-12-12 01:00:00 1 12 1800
2017-12-12 01:30:00 1 12 59
You can use date functions to round [DateTime] into 30 minute intervals, but the result does not match your expected result, which appears to be wrong
DateTime ID Code Duration
2017-12-12 00:30:00 1 12 1800 1800 + 337 = 1837 2017-12-12 00:30
2017-12-12 00:30:37 1 12 37
2017-12-12 01:00:00 1 12 1793 1793 2017-12-12 01:00
2017-12-12 01:30:00 1 12 1800 1800 + 59 = 1859 2017-12-12 01:30
2017-12-12 01:30:59 1 12 59
SQL Fiddle
MS SQL Server 2014 Schema Setup:
CREATE TABLE Table1
([DateTime] datetime, [ID] int, [Code] int, [Duration] int)
;
INSERT INTO Table1
([DateTime], [ID], [Code], [Duration])
VALUES
('2017-12-12 00:30:00', 1, 12, 1800),
('2017-12-12 00:30:37', 1, 12, 37),
('2017-12-12 01:00:00', 1, 12, 1793),
('2017-12-12 01:30:00', 1, 12, 1800),
('2017-12-12 01:30:59', 1, 12, 59)
;
Query 1:
select
dateadd(minute, (datediff(minute, [DateTime], 0) / 30) * 30, 0)
, sum(duration)
from table1
group by
dateadd(minute, (datediff(minute, [DateTime], 0) / 30) * 30, 0)
Results:
| | |
|----------------------|------|
| 1782-01-19T22:30:00Z | 1859 |
| 1782-01-19T23:00:00Z | 1793 |
| 1782-01-19T23:30:00Z | 1837 |

How to update record based on value of next record in SQL Server 2008

I like to update a column based on a value of the next record:
resid startdate enddate weekday hours
-----------------------------------------------
2980 2013-09-23 2014-12-31 1 6
2980 2013-09-23 2014-12-31 2 6
2980 2013-09-23 2014-12-31 3 6
2980 2013-09-23 2014-12-31 4 6
2980 2013-09-23 2014-12-31 5 6
2980 2015-01-01 NULL 1 6,8
2980 2015-01-01 NULL 2 6,8
2980 2015-01-01 NULL 3 6,8
2980 2015-01-01 NULL 4 6,8
2980 2015-01-01 NULL 5 6,8
2980 2015-07-01 NULL 1 6
2980 2015-07-01 NULL 2 6
2980 2015-07-01 NULL 3 6
2980 2015-07-01 NULL 4 6
2980 2015-07-01 NULL 5 6
I like to update the NULL value in column enddate. It needs to get a value of the next startdate - 1 day.
For instance, for all records with startdate 2015-01-01, the column enddate needs to be updated with 2015-07-01 - 1 day.
Is there someone who has a solution?
update TableName set enddate = dateadd(day,1,startdate)
One way to do it is with a subquery:
UPDATE t1
SET enddate = (SELECT TOP 1 DATEADD(DAY, -1, startdate)
FROM YourTable t2
WHERE t1.startdate < t2.startdate
ORDER BY t2.startdate)
FROM YourTable t1
WHERE t1.enddate is null

Resources