Fix NULL Date Column? - sql

My query:
SELECT
a.GtrReference, a.SourceWarehouse, a.TargetWarehouse, /*a.DateCreated,*/
d.ExpectedDueDate, a.EntryType, a.ControlAccount, a.InitialValue,
a.ValRecToDate, a.Operator, b.Description, c.Description,
Convert(decimal(14,2),
(a.InitialValue - a.ValRecToDate)) as RemainingValue
FROM [GtrMaster] a WITH (NOLOCK)
LEFT JOIN [InvWhControl] b WITH (NOLOCK)
ON (a.SourceWarehouse = b.Warehouse)
LEFT JOIN [InvWhControl] c WITH (NOLOCK)
ON (a.TargetWarehouse = c.Warehouse)
LEFT JOIN [GtrDetail] d WITH (NOLOCK)
ON (a.GtrReference = d.GtrReference and a.InitialValue = d.InitialValue)
WHERE ( a.EntryType = 'W' OR a.EntryType = 'S' )
AND a.Complete <> 'Y' AND a.GtrReference >= ''
ORDER BY a.GtrReference
What I get:
GtrReference
SourceWarehouse
TargetWarehouse
ExpectedDueDate
EntryType
ControlAccount
InitialValue
ValRecToDate
Operator
Description
Description
RemainingValue
02022023
W
01
NULL
W
1610
3616.00
0.00
CWHITE
IN TRANSIT WAREHOUSE
MAIN WAREHOUSE
3616.00
202212-019
W
01
NULL
W
1610
25365.40
0.00
ahsiao
IN TRANSIT WAREHOUSE
MAIN WAREHOUSE
25365.40
202301-014
W
01
2023-03-08 00:00:00.000
W
1610
20680.00
0.00
ahsiao
IN TRANSIT WAREHOUSE
MAIN WAREHOUSE
20680.00
I'm trying to see ExpectedDueDate from the GtrDetail table, but it's NULL. It has to do with my join. How do I fix it?
What I want to see:
GtrReference
SourceWarehouse
TargetWarehouse
ExpectedDueDate
EntryType
ControlAccount
InitialValue
ValRecToDate
Operator
Description
Description
RemainingValue
02022023
W
01
2023-02-17 00:00:00.000
W
1610
3616.00
0.00
CWHITE
IN TRANSIT WAREHOUSE
MAIN WAREHOUSE
3616.00
202212-019
W
01
2023-02-15 00:00:00.000
W
1610
25365.40
0.00
ahsiao
IN TRANSIT WAREHOUSE
MAIN WAREHOUSE
25365.40
202301-014
W
01
2023-03-08 00:00:00.000
W
1610
20680.00
0.00
ahsiao
IN TRANSIT WAREHOUSE
MAIN WAREHOUSE
20680.00
The GtrDetail table picks up each GIT, and there are multiple. I need it to just pick ONE, or else I get a ton of duplicate entries instead of the 3 I need.
For example, the GtrDetail table has this for the expectedduedate entry. I only need one of them (any one of them per GtrReference).
GtrReference
ExpectedDueDate
02022023
2023-02-17 00:00:00.000
02022023
2023-02-17 00:00:00.000
202212-019
2023-02-15 00:00:00.000
202212-019
2023-02-15 00:00:00.000
202301-014
2023-03-08 00:00:00.000
Sample GtrMaster
GtrReference
SourceWarehouse
TargetWarehouse
EntryType
NextLine
ControlAccount
InitialValue
ValRecToDate
DateCreated
TimeCreatedHh
TimeCreatedMm
Operator
Complete
InterfaceFlag
TimeStamp
02022023
W
01
W
3
1610
3616.00
0.00
2023-02-02 00:00:00.000
9
13
CWHITE
0x00000000AC2AAC0E
202212-019
W
01
W
3
1610
25365.40
0.00
2022-12-15 00:00:00.000
11
37
ahsiao
0x00000000A8109E62
202301-014
W
01
W
2
1610
20680.00
0.00
2023-01-06 00:00:00.000
8
39
ahsiao
0x00000000A9B92FB6
Sample GtrDetail
GtrReference
SourceWarehouse
TargetWarehouse
Line
StockCode
TransfCompPeriod
TransfCompYear
GtrQuantity
QtyReceived
InitialValue
ValueReceivedSrc
UnitCost
CostUom
ValueReceivedTgt
ProductClass
Branch
Area
SalesOrder
SalesOrderLine
DeliveryNote
Invoice
TransfDocNum
TransactionDate
ExpectedDueDate
TransferComplete
LockFlag
NextEntry
Version
Release
TimeStamp
02022023
W
01
1
RNHT-M8 BODY
0
0
265.000000
0.000000
1696.00
0.00
6.40000
EA
0.00
FMAN
0
2023-02-02 00:00:00.000
2023-02-17 00:00:00.000
Y
2
0x00000000AC78C0D5
02022023
W
01
2
RNHT-M10 BODY
0
0
300.000000
0.000000
1920.00
0.00
6.40000
EA
0.00
FMAN
0
2023-02-02 00:00:00.000
2023-02-17 00:00:00.000
Y
2
0x00000000AC78C0D6
202212-019
W
01
1
SH-60-043 RM
0
0
18000.000000
0.000000
8069.40
0.00
0.44830
EA
0.00
BITA
0
2022-12-15 00:00:00.000
2023-02-15 00:00:00.000
Y
2
0x00000000AC78C0D7
202212-019
W
01
2
SH-60-081
0
0
92000.000000
0.000000
17296.00
0.00
0.18800
EA
0.00
BITA
0
2022-12-15 00:00:00.000
2023-02-15 00:00:00.000
Y
2
0x00000000AC78C0D8
202301-014
W
01
1
SH-60-081
0
0
110000.000000
0.000000
20680.00
0.00
0.18800
EA
0.00
BITA
0
2023-01-06 00:00:00.000
2023-03-08 00:00:00.000
Y
2
0x00000000AC78C0D9
Tinitialvalue in GtrMaster is a summation of the initialvalues in GtrDetail for each GtrReference.

So sorry for the wait. First step is just to remove that initialValue from your join condition and see if, given your data, that solves the issue:
SELECT
a.GtrReference, a.SourceWarehouse, a.TargetWarehouse, /*a.DateCreated,*/
d.ExpectedDueDate, a.EntryType, a.ControlAccount, a.InitialValue,
a.ValRecToDate, a.Operator, b.Description, c.Description,
Convert(decimal(14,2),
(a.InitialValue - a.ValRecToDate)) as RemainingValue
FROM [GtrMaster] a WITH (NOLOCK)
LEFT JOIN [InvWhControl] b WITH (NOLOCK)
ON (a.SourceWarehouse = b.Warehouse)
LEFT JOIN [InvWhControl] c WITH (NOLOCK)
ON (a.TargetWarehouse = c.Warehouse)
LEFT JOIN [GtrDetail] d WITH (NOLOCK)
ON a.GtrReference = d.GtrReference
WHERE ( a.EntryType = 'W' OR a.EntryType = 'S' )
AND a.Complete <> 'Y' AND a.GtrReference >= ''
ORDER BY a.GtrReference
If you are getting more than one record returns from GtrDetail from this join (likely) and that isn't what you are wanting in this result set, you can move GtrDetail into a subquery and aggregate or distinct to a 1:1 relationship:
SELECT
a.GtrReference, a.SourceWarehouse, a.TargetWarehouse, /*a.DateCreated,*/
d.ExpectedDueDate, a.EntryType, a.ControlAccount, a.InitialValue,
a.ValRecToDate, a.Operator, b.Description, c.Description,
Convert(decimal(14,2),
(a.InitialValue - a.ValRecToDate)) as RemainingValue
FROM [GtrMaster] a WITH (NOLOCK)
LEFT JOIN [InvWhControl] b WITH (NOLOCK)
ON (a.SourceWarehouse = b.Warehouse)
LEFT JOIN [InvWhControl] c WITH (NOLOCK)
ON (a.TargetWarehouse = c.Warehouse)
LEFT JOIN
(
SELECT DISTINCT GtrReference, ExpectedDueDate
FROM [GtrDetail]
) d WITH (NOLOCK)
ON a.GtrReference = d.GtrReference
WHERE ( a.EntryType = 'W' OR a.EntryType = 'S' )
AND a.Complete <> 'Y' AND a.GtrReference >= ''
ORDER BY a.GtrReference

Related

Row Number Subquery Not Iterating Properly [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 2 days ago.
Improve this question
I have this query which generates the below report
select
RN,
DateCompleted,
GtrReference,
SourceWarehouse,
TargetWarehouse,
DateCreated,
EntryType,
ControlAccount,
InitialValue,
Operator,
FirstDesc,
SecondDesc
from
(
select
row_number() over(partition by a.GtrReference order by a.GtrReference desc) as RN,
--concat(d.TransfCompYear, case when len(d.TransfCompPeriod) = 1 then '0' end, d.TransfCompPeriod) as DateCompleted,
a.Complete,
d.Line,
d.TransfCompPeriod as DateMonth,
d.TransfCompYear as DateYear,
a.GtrReference as GtrReference,
a.SourceWarehouse as SourceWarehouse,
max(a.TargetWarehouse) over (partition by a.GtrReference) as TargetWarehouse,
a.DateCreated as DateCreated,
COALESCE(d.ExpectedDueDate, d.ExpectedDueDate) as ExpectedDueDate,
a.EntryType as EntryType,
a.ControlAccount as ControlAccount,
max(a.InitialValue) over (partition by a.GtrReference) as InitialValue,
a.Operator as Operator,
b.Description as FirstDesc,
c.Description as SecondDesc,
e.TransactionTyp as Movement,
e.TransactionDate as DateCompleted
FROM
[GtrMaster] a
LEFT JOIN [InvWhControl] b
ON (a.SourceWarehouse = b.Warehouse)
LEFT JOIN [InvWhControl] c
ON (a.TargetWarehouse = c.Warehouse)
LEFT JOIN [GtrDetail] d
ON (a.GtrReference = d.GtrReference)
LEFT JOIN [GtrTransactions] e
ON (a.GtrReference = e.GtrReference)
) as i
WHERE ( i.EntryType = 'W' OR i.EntryType = 'S' )
AND i.Complete = 'Y' AND i.GtrReference <> ''
--and i.Line = '1'
and i.DateCompleted >= DATEADD(hh,0,dateadd(DAY, datediff(day, +90, getdate()),0))
--and InitialValue = '4732.53'
and Movement = 'I'
-- and RN = '1'
ORDER BY i.DateCompleted desc
DateCompleted
GtrReference
SourceWarehouse
TargetWarehouse
DateCreated
EntryType
ControlAccount
InitialValue
Operator
FirstDesc
SecondDesc
2
2023-02-17 00:00:00.000
PI2302027
W
01
2023-02-15 00:00:00.000
W
1610
1441.40
ahsiao
IN TRANSIT WAREHOUSE
723
2023-02-17 00:00:00.000
MDSITA-220506
W
01
2022-05-10 00:00:00.000
W
1610
22239.24
ahsiao
IN TRANSIT WAREHOUSE
724
2023-02-17 00:00:00.000
MDSITA-220506
W
01
2022-05-10 00:00:00.000
W
1610
22239.24
ahsiao
IN TRANSIT WAREHOUSE
725
2023-02-17 00:00:00.000
MDSITA-220506
W
01
2022-05-10 00:00:00.000
W
1610
22239.24
ahsiao
IN TRANSIT WAREHOUSE
726
2023-02-17 00:00:00.000
MDSITA-220506
W
01
2022-05-10 00:00:00.000
W
1610
22239.24
ahsiao
IN TRANSIT WAREHOUSE
For some reason, I'm getting 2, 723, 724, 725 etc for row number instead of iteration.. But when I run the query outside the subquery, i get the correct row numbers.. so
select
row_number() over(partition by a.GtrReference order by a.GtrReference) as RN,
--concat(d.TransfCompYear, case when len(d.TransfCompPeriod) = 1 then '0' end, d.TransfCompPeriod) as DateCompleted,
a.Complete,
d.Line,
d.TransfCompPeriod as DateMonth,
d.TransfCompYear as DateYear,
a.GtrReference as GtrReference,
a.SourceWarehouse as SourceWarehouse,
max(a.TargetWarehouse) over (partition by a.GtrReference) as TargetWarehouse,
a.DateCreated as DateCreated,
COALESCE(d.ExpectedDueDate, d.ExpectedDueDate) as ExpectedDueDate,
a.EntryType as EntryType,
a.ControlAccount as ControlAccount,
max(a.InitialValue) over (partition by a.GtrReference) as InitialValue,
a.Operator as Operator,
b.Description as FirstDesc,
c.Description as SecondDesc,
e.TransactionTyp as Movement,
e.TransactionDate as DateCompleted
FROM
[GtrMaster] a
LEFT JOIN [InvWhControl] b
ON (a.SourceWarehouse = b.Warehouse)
LEFT JOIN [InvWhControl] c
ON (a.TargetWarehouse = c.Warehouse)
LEFT JOIN [GtrDetail] d
ON (a.GtrReference = d.GtrReference)
LEFT JOIN [GtrTransactions] e
ON (a.GtrReference = e.GtrReference)
WHERE ( a.EntryType = 'W' OR a.EntryType = 'S' )
AND a.Complete = 'Y' AND a.GtrReference <> ''
--and i.Line = '1'
and e.TransactionDate >= DATEADD(hh,0,dateadd(DAY, datediff(day, +90, getdate()),0))
--and InitialValue = '4732.53'
and e.TransactionTyp = 'I'
ORDER BY e.TransactionDate desc
RN
Complete
Line
DateMonth
DateYear
GtrReference
SourceWarehouse
TargetWarehouse
DateCreated
ExpectedDueDate
EntryType
ControlAccount
InitialValue
Operator
FirstDesc
SecondDesc
Movement
DateCompleted
1
Y
1
2
2023
PI2302027
W
01
2023-02-15 00:00:00.000
2023-02-22 00:00:00.000
W
1610
1441.40
ahsiao
IN TRANSIT WAREHOUSE
MAIN WAREHOUSE
I
2023-02-17 00:00:00.000
1
Y
1
5
2022
MDSITA-220506
W
01
2022-05-10 00:00:00.000
2022-05-13 00:00:00.000
W
1610
22239.24
ahsiao
IN TRANSIT WAREHOUSE
MAIN WAREHOUSE
I
2023-02-17 00:00:00.000
2
Y
2
5
2022
MDSITA-220506
W
01
2022-05-10 00:00:00.000
2022-05-13 00:00:00.000
W
1610
22239.24
ahsiao
IN TRANSIT WAREHOUSE
MAIN WAREHOUSE
I
2023-02-17 00:00:00.000
3
Y
3
5
2022
MDSITA-220506
W
01
2022-05-10 00:00:00.000
2022-05-13 00:00:00.000
W
1610
22239.24
ahsiao
IN TRANSIT WAREHOUSE
MAIN WAREHOUSE
I
2023-02-17 00:00:00.000
4
Y
4
5
2022
MDSITA-220506
W
01
2022-05-10 00:00:00.000
2022-05-13 00:00:00.000
W
1610
22239.24
ahsiao
IN TRANSIT WAREHOUSE
MAIN WAREHOUSE
I
2023-02-17 00:00:00.000
5
Y
5
5
2022
MDSITA-220506
W
01
2022-05-10 00:00:00.000
2022-05-13 00:00:00.000
W
1610
22239.24
ahsiao
IN TRANSIT WAREHOUSE
MAIN WAREHOUSE
I
2023-02-17 00:00:00.000
6
Y
6
5
2022
MDSITA-220506
W
01
2022-05-10 00:00:00.000
2022-05-13 00:00:00.000
W
1610
22239.24
ahsiao
IN TRANSIT WAREHOUSE
MAIN WAREHOUSE
I
2023-02-17 00:00:00.000
7
Y
7
5
2022
MDSITA-220506
W
01
2022-05-10 00:00:00.000
2022-05-13 00:00:00.000
W
1610
22239.24
ahsiao
IN TRANSIT WAREHOUSE
MAIN WAREHOUSE
I
2023-02-17 00:00:00.000
8
Y
8
5
2022
MDSITA-220506
W
01
2022-05-10 00:00:00.000
2022-05-13 00:00:00.000
W
1610
22239.24
ahsiao
IN TRANSIT WAREHOUSE
MAIN WAREHOUSE
I
2023-02-17 00:00:00.000
9
Y
9
2
2023
MDSITA-220506
W
01
2022-05-10 00:00:00.000
2022-05-13 00:00:00.000
W
1610
22239.24
ahsiao
IN TRANSIT WAREHOUSE
MAIN WAREHOUSE
I
2023-02-17 00:00:00.000
What am i doing wrong in my main query?
row_number() creates the sequence for the inner query. But all of WHERE clause criteria are applied in the outer query. This filters the rows after the sequence was created, leaving the gaps as shown.

SQL Server Get Last 3 Months of Date Using Year and Month Columns?

select
d.TransfCompPeriod,
d.TransfCompYear,
a.GtrReference,
a.SourceWarehouse,
max(a.TargetWarehouse) over (partition by a.GtrReference) as TargetWarehouse, a.DateCreated,
COALESCE(d.ExpectedDueDate, d.ExpectedDueDate) as ExpectedDueDate,
a.EntryType,
a.ControlAccount,
max(a.InitialValue) over (partition by a.GtrReference) as InitialValue,
a.Operator,
b.Description,
c.Description
FROM
[GtrMaster] a
LEFT JOIN [InvWhControl] b
ON (a.SourceWarehouse = b.Warehouse)
LEFT JOIN [InvWhControl] c
ON (a.TargetWarehouse = c.Warehouse)
LEFT JOIN [GtrDetail] d
ON (a.GtrReference = d.GtrReference)
WHERE ( a.EntryType = 'W' OR a.EntryType = 'S' )
AND a.Complete = 'Y' AND a.GtrReference >= ''
and d.Line = '1'
ORDER BY a.GtrReference
I have a query that generates this table:
TransfCompPeriod
TransfCompYear
GtrReference
SourceWarehouse
TargetWarehouse
DateCreated
ExpectedDueDate
EntryType
ControlAccount
InitialValue
Operator
Description
Description
8
2021
000360
W
01
2021-08-11 00:00:00.000
2021-08-18 00:00:00.000
W
1610
792.88
CWHITE
IN TRANSIT WAREHOUSE
MAIN WAREHOUSE
2
2019
01/19
W
01
2019-02-01 00:00:00.000
2019-02-04 00:00:00.000
W
1610
18159.77
AWAINWRIGHT
IN TRANSIT WAREHOUSE
MAIN WAREHOUSE
2
2023
01062023
W
01
2023-01-06 00:00:00.000
2023-02-21 00:00:00.000
W
1610
6080.00
CWHITE
IN TRANSIT WAREHOUSE
MAIN WAREHOUSE
2
2023
01172023
W
01
2023-01-17 00:00:00.000
2023-01-31 00:00:00.000
W
1610
6210.00
CWHITE
IN TRANSIT WAREHOUSE
MAIN WAREHOUSE
2
2020
01232020-NYTOAK
01
02
2020-01-23 00:00:00.000
2020-01-24 00:00:00.000
W
1610
16412.35
CWHITE
MAIN WAREHOUSE
Akron Facility- Finished Goods
2
2021
01272021
W
01
2021-01-27 00:00:00.000
2021-02-05 00:00:00.000
W
1610
437.36
CWHITE
IN TRANSIT WAREHOUSE
MAIN WAREHOUSE
2
2021
02112021
W
01
2021-02-11 00:00:00.000
2021-02-19 00:00:00.000
W
1610
10140.95
CWHITE
IN TRANSIT WAREHOUSE
MAIN WAREHOUSE
2
2022
02112022
W
01
2022-02-11 00:00:00.000
2022-02-28 00:00:00.000
W
1610
960.00
CWHITE
IN TRANSIT WAREHOUSE
MAIN WAREHOUSE
3
2021
02232021
W
01
2021-02-23 00:00:00.000
2021-03-01 00:00:00.000
W
1610
2368.00
CWHITE
IN TRANSIT WAREHOUSE
MAIN WAREHOUSE
3
2022
02232022
W
01
2022-02-23 00:00:00.000
2022-03-04 00:00:00.000
W
1610
7443.20
CWHITE
IN TRANSIT WAREHOUSE
MAIN WAREHOUSE
WHat I'm trying to do is filter in an AND statement that TransfCompPeriod and TransfCompYear will only pull for the last 3 months, but I'm not sure how to do it with those two columns. Can someone help me put together the right sql for that in my AND statement?
So for example, I want to see the BELOW (today being 02 / 08 /2023 and only showing the last 3 months)
TransfCompPeriod
TransfCompYear
GtrReference
SourceWarehouse
TargetWarehouse
DateCreated
ExpectedDueDate
EntryType
ControlAccount
InitialValue
Operator
Description
Description
2
2023
01062023
W
01
2023-01-06 00:00:00.000
2023-02-21 00:00:00.000
W
1610
6080.00
CWHITE
IN TRANSIT WAREHOUSE
MAIN WAREHOUSE
2
2023
01172023
W
01
2023-01-17 00:00:00.000
2023-01-31 00:00:00.000
W
1610
6210.00
CWHITE
IN TRANSIT WAREHOUSE
MAIN WAREHOUSE
1
2023
10282022
W
01
2022-10-28 00:00:00.000
2022-11-04 00:00:00.000
W
1610
162.14
CWHITE
IN TRANSIT WAREHOUSE
MAIN WAREHOUSE
2
2023
202209-020
W
01
2022-09-16 00:00:00.000
2022-09-23 00:00:00.000
W
1610
4512.00
ahsiao
IN TRANSIT WAREHOUSE
MAIN WAREHOUSE
1
2023
202210-036
W
01
2022-10-27 00:00:00.000
2022-12-30 00:00:00.000
W
1610
24643.20
ahsiao
IN TRANSIT WAREHOUSE
MAIN WAREHOUSE
1
2023
202301-005
W
01
2023-01-03 00:00:00.000
2023-01-10 00:00:00.000
W
1610
1562.50
ahsiao
IN TRANSIT WAREHOUSE
MAIN WAREHOUSE
1
2023
22111796
W
01
2022-11-28 00:00:00.000
2023-01-09 00:00:00.000
W
1610
22090.88
ahsiao
IN TRANSIT WAREHOUSE
MAIN WAREHOUSE
1
2023
22120969
W
01
2022-12-22 00:00:00.000
2022-12-27 00:00:00.000
W
1610
23844.89
CWHITE
IN TRANSIT WAREHOUSE
MAIN WAREHOUSE
1
2023
22120981
W
01
2022-12-14 00:00:00.000
2023-01-25 00:00:00.000
W
1610
41430.27
ahsiao
IN TRANSIT WAREHOUSE
MAIN WAREHOUSE
1
2023
23010001
W
01
2023-01-03 00:00:00.000
2023-01-09 00:00:00.000
W
1610
6.27
ahsiao
IN TRANSIT WAREHOUSE
MAIN WAREHOUSE
1
2023
23010005
W
DS
2023-01-03 00:00:00.000
2023-01-11 00:00:00.000
W
1610
34648.83
ahsiao
IN TRANSIT WAREHOUSE
DROP SHIPMENT
1
2023
23010306
W
01
2023-01-06 00:00:00.000
2023-01-13 00:00:00.000
W
1610
105.18
ahsiao
IN TRANSIT WAREHOUSE
MAIN WAREHOUSE
1
2023
23010408
W
01
2023-01-09 00:00:00.000
2023-01-16 00:00:00.000
W
1610
94.49
ahsiao
IN TRANSIT WAREHOUSE
MAIN WAREHOUSE
1
2023
23010629
W
01
2023-01-11 00:00:00.000
2023-01-20 00:00:00.000
W
1610
37607.70
ahsiao
IN TRANSIT WAREHOUSE
MAIN WAREHOUSE
1
2023
23010629-01
W
01
2023-01-11 00:00:00.000
2023-01-20 00:00:00.000
W
1610
2400.30
ahsiao
IN TRANSIT WAREHOUSE
MAIN WAREHOUSE
1
2023
23010629-1
W
01
2023-01-11 00:00:00.000
2023-01-20 00:00:00.000
W
1610
69.65
ahsiao
IN TRANSIT WAREHOUSE
MAIN WAREHOUSE
1
2023
23010630
W
DS
2023-01-11 00:00:00.000
2023-01-18 00:00:00.000
W
1610
8560.26
ahsiao
IN TRANSIT WAREHOUSE
DROP SHIPMENT
1
2023
23011427
W
01
2023-01-23 00:00:00.000
2023-01-30 00:00:00.000
W
1610
238.18
ahsiao
IN TRANSIT WAREHOUSE
MAIN WAREHOUSE
2
2023
23011634
W
01
2023-01-25 00:00:00.000
2023-02-03 00:00:00.000
W
1610
27559.02
ahsiao
IN TRANSIT WAREHOUSE
MAIN WAREHOUSE
1
2023
7740
W
01
2022-12-30 00:00:00.000
2022-12-27 00:00:00.000
W
1610
348.00
CWHITE
IN TRANSIT WAREHOUSE
MAIN WAREHOUSE

Retrieve records from left table based on matching with right table and also non matching records from right table based on condition

We have the requirements as per following, please guide me on how to achieve this.
Study table
id startedDate practitioner reference laterality
-------------------------------------------------------------------------------
1s 2022-09-01 00:00:00 p1 1-reference R
2s 2022-10-01 00:00:00 p1 1-reference L
3s 2022-09-03 00:00:00 null 2-reference R
4s 2022-09-03 00:00:00 null 3-reference R
5s 2022-10-03 00:00:00 p1 4-reference L
Obs Table
id reference laterality effectiveDate length
-------------------------------------------------------------------------------
1o 1-reference R 2022-08-30 00:00:00 2.1
2o 1-reference R 2022-08-01 00:00:00 2.2
3o 1-reference R 2022-09-03 00:00:00 2.1
4o 1-reference L 2022-08-03 00:00:00 2.0
5o 2-reference R 2022-08-03 00:00:00 2.0
6o 2-reference R 2022-08-01 00:00:00 2.0
7o 3-reference R 2022-10-01 00:00:00 2.0
8o 5-reference L 2022-10-02 00:00:00 1.9
90 5-reference L 2022-10-03 00:00:00 2.0
Output:
get max effectiveDate record group by reference and laterality but less than study.startedDate if practitioner!=null
and latest effectiveDate record group by reference and laterality if that record not found in study table on practitioner!=null
result:
id reference laterality effectiveDate length
1o 1-reference R 2022-08-30 00:00:00 2.1
4o 1-reference L 2022-08-03 00:00:00 2.0
5o 2-reference R 2022-08-03 00:00:00 2.0
7o 3-reference R 2022-10-01 00:00:00 2.0
9o 5-reference L 2022-10-03 00:00:00 2.0
the below example query only get matched records or understanding purpose but final output will be as above
select Obs.*
from Obs
inner join study
on Obs.reference = study.reference and Obs.effectiveDate < study.startedDate
and study.practitioner != null and Obs.laterality = study.laterality
Can someone please help me to achieve this.
Thanks
From your description, it seems like this is what you wanted.
You can use row_number() to get the row with latest effectiveDate
select *
from
(
select Obs.*,
rn = row_number() over (partition by Obs.reference, Obs.laterality
order by Obs.effectiveDate desc)
from Obs
inner join study on Obs.reference = study.reference
and Obs.laterality = study.laterality
where (study.practitioner is not null and Obs.effectiveDate < study.startedDate)
or (study.practitioner is null)
) o
where o.rn = 1
order by Id

I want to update values of a null column using the values of other column in SELECT statement

I have a select statement with multiple joins I also have a blank column in SQL statement I want to update the value of this NULL column which I created with the values of another column which is also in the same select statement.
I tried update statement but it did not work.
SELECT
null as loan_group
,[Loan Number] as loan_number
,[DateofNote] as open_data
,[lnamount] as original_note_amount
,[EOM Balance] as current_principal_balance
,b.status
FROM
tblbordata2 B
INNER JOIN tblLOANDATA2 L ON B.[Student ID] = L.[Student ID]
INNER JOIN tblLoanStatus LS ON LS.[Status ID] = B.Status
INNER JOIN SCHOOLS S ON S.SchoolNumber = b.SchoolNumber
INNER JOIN [School Types] ST ON ST.[School Type ID] = S.SchoolID
loan_group loan_number open_data original_note_amount current_principal_balance status
NULL 1566 1997-10-14 00:00:00.000 6495.00 0.00 6
NULL 1564 1997-10-13 00:00:00.000 6495.00 0.00 4
NULL 1577 1997-10-17 00:00:00.000 2895.00 0.00 9
NULL 1557 1997-10-11 00:00:00.000 2875.00 0.00 6
NULL 1558 1997-10-14 00:00:00.000 3845.00 0.00 19
NULL 1561 1997-10-10 00:00:00.000 1995.00 0.00 19
NULL 1553 1997-10-13 00:00:00.000 3500.00 0.00 6
NULL 1548 1997-10-09 00:00:00.000 3645.00 0.00 19
NULL 1555 1997-10-13 00:00:00.000 2000.00 0.00 4
NULL 1582 1997-10-23 00:00:00.000 4500.00 0.00 19
NULL 1575 1997-10-18 00:00:00.000 3945.00 0.00 4
NULL 1574 1997-10-16 00:00:00.000 4600.00 0.00 19
NULL 1560 1997-10-15 00:00:00.000 3736.00 0.00 4
NULL 1594 1997-10-20 00:00:00.000 2500.00 0.00 6
NULL 1593 1997-10-28 00:00:00.000 3000.00 0.00 4
NULL 1591 1997-10-24 00:00:00.000 3862.00 0.00 6
NULL 1590 1997-10-23 00:00:00.000 6495.00 0.00 6
NULL 1586 1997-10-13 00:00:00.000 3395.00 0.00 6
NULL 1588 1997-10-18 00:00:00.000 3495.00 0.00 4
NULL 1587 1997-10-18 00:00:00.000 3495.00 0.00 6
I want to replace the first column loan group such that if the status is 6 or 4 "R" else "P"
Use CASE with IN clause
SELECT
case when status IN
('6' , '4' )
then 'R'
else 'P'
end as loan_group
,[Loan Number] as loan_number
,[DateofNote] as open_data
,[lnamount] as original_note_amount
,[EOM Balance] as current_principal_balance
,b.status
FROM
tblbordata2 B
INNER JOIN tblLOANDATA2 L ON B.
[Student ID] = L.[Student ID]
INNER JOIN tblLoanStatus LS ON LS.
[Status ID] = B.Status
INNER JOIN SCHOOLS S ON
S.SchoolNumber = b.SchoolNumber
INNER JOIN [School Types] ST ON ST.
[School Type ID] = S.SchoolID
Put this in the code for the column you are trying to insert:
SELECT
case when loan_group is null then
case when status = 6 or status = 4 then 'R'
else 'P'
end
end
,[Loan Number] as loan_number
,[DateofNote] as open_data
,[lnamount] as original_note_amount
,[EOM Balance] as current_principal_balance
,b.status

SQL - Use Value until End, then Use last value

I have written a query that gives me Growth of an Estimate from 2015 - 2016.
I am using a Variable Called EBIT, EBIT with Date_Year = 2015 has data for Date_Month 2013-12-31 to 2016-12-31 while EBIT With Date_Year = 2016 has data for Date_Month 2013-12-31 to 2017-06-30.
Is there a way to do the exakt same as I've done but after 2016-12-31 use the last value of EBIT (2015) instead and hence keep going?
In the Code below EBIT (2015) & EBIT (2016) is just to illustrate the numbers.
The table furthest down is the optimal table that I am trying to get but I can't understand how I can get there ...
As you can see the Current Output stops at 2016-12-31 while the Desired Output assumes that EBIT (2015) = 936808 after it has no more values and EBIT (2016) keeps going until data for it ends.
I am thinking of maybe incorporating something like the code below yet I can't have a Max function inside of a Sum(Case when) and I also think this might not pick the maximum n2.EBIT for the max(Date_Month_Id) with same Date_Year_ID etc.
(
SUM(case when n2.Date_Month_Id < (Select Max(Date_Month_Id) From EBIT where Date_Year_ID in (2015))
Then n2.EBIT else max(n2.EBIT) end
)
/
SUM(n1.EBIT) - 1) AS 'EBIT Growth 2015-2016'
Current Query:
SELECT
m1.date_Month,
SUM(n2.EBIT) As 'EBIT (2015)',
SUM(n1.EBIT) AS 'EBIT (2016)',
SUM(n2.EBIT) /
SUM(n1.EBIT) - 1 AS 'EBIT Growth 2015-2016'
FROM EBIT AS n1
INNER JOIN date_year AS y1 ON y1.date_year_id = n1.date_year_id
INNER JOIN date_month AS m1 ON m1.date_month_id = n1.date_month_id
INNER JOIN EBIT AS n2
INNER JOIN date_year AS y2 ON y2.date_year_id = n2.date_year_id
INNER JOIN date_month AS m2 ON m2.date_month_id = n2.date_month_id
ON n1.Company_Id = n2.company_Id AND m1.date_month = m2.date_month
WHERE n1.EBIT <> 0 AND n2.EBIT <> 0 and y1.date_year = 2015 AND y2.date_year = 2016
GROUP BY m1.date_month
ORDER BY m1.Date_Month ASC;
Current Output:
date_Month EBIT (2015) EBIT (2016) EBIT Growth 2015-2016
2013-12-31 2198051.670 2053514.548 0.070385
2014-01-31 1112047.668 1045523.283 0.063627
2014-02-28 1109221.007 1043085.024 0.063404
2014-03-31 1118112.429 1047602.842 0.067305
2014-04-30 1117082.270 1044455.198 0.069535
2014-05-31 1122019.824 1045228.639 0.073468
2014-06-30 1130536.274 1053516.159 0.073107
2014-07-31 1135838.665 1055882.605 0.075724
2014-08-31 1138886.938 1056331.236 0.078153
2014-09-30 1147685.937 1064254.322 0.078394
2014-10-31 1126437.959 1040798.042 0.082282
2014-11-30 1116292.468 1029127.634 0.084697
2014-12-31 1111509.880 1020233.204 0.089466
2015-01-31 1109692.378 1011895.933 0.096646
2015-02-28 1103494.875 1000990.365 0.102403
2015-03-31 1099507.246 996252.094 0.103643
2015-04-30 1094694.816 997653.412 0.097269
2015-05-31 1103352.777 1007836.533 0.094773
2015-06-30 1098103.559 1004344.223 0.093353
2015-07-31 1081689.371 994391.939 0.087789
2015-08-31 1064033.692 979809.245 0.085960
2015-09-30 1041604.341 971746.514 0.071888
2015-10-31 1044583.652 979410.989 0.066542
2015-11-30 1049158.666 989746.574 0.060027
2015-12-31 1022646.632 969556.360 0.054757
2016-01-31 990592.876 968797.454 0.022497
2016-02-29 961009.086 934777.852 0.028061
2016-03-31 942917.628 933858.404 0.009700
2016-04-30 937784.980 931500.111 0.006747
2016-05-31 941049.211 928974.727 0.012997
2016-06-30 930969.603 929102.681 0.002009
2016-07-31 926670.277 928979.675 -0.002486
2016-08-31 927442.570 929233.754 -0.001928
2016-09-30 924658.701 930248.793 -0.006010
2016-10-31 925569.857 930250.547 -0.005032
2016-11-30 938894.794 930156.559 0.009394
2016-12-31 936808.419 929592.092 0.007762
Desired Output (and the % Increase but I know how to implement that code):
date_month EBIT(2015) EBIT(2016)
31/12/2013 2198052 2053515
31/01/2014 1112048 1045523
28/02/2014 1109221 1043085
31/03/2014 1118112 1047603
30/04/2014 1117082 1044455
31/05/2014 1122020 1045229
30/06/2014 1130536 1053516
31/07/2014 1135839 1055883
31/08/2014 1138887 1056331
30/09/2014 1147686 1064254
31/10/2014 1126438 1040798
30/11/2014 1116292 1029128
31/12/2014 1111510 1020233
31/01/2015 1109692 1011896
28/02/2015 1103495 1000990
31/03/2015 1099507 996252
30/04/2015 1094695 997653
31/05/2015 1103353 1007837
30/06/2015 1098104 1004344
31/07/2015 1081689 994392
31/08/2015 1064034 979809
30/09/2015 1041604 971747
31/10/2015 1044584 979411
30/11/2015 1049159 989747
31/12/2015 1022647 969556
31/01/2016 990593 968797
29/02/2016 961009 934778
31/03/2016 942918 933858
30/04/2016 937785 931500
31/05/2016 941049 928975
30/06/2016 930970 929103
31/07/2016 926670 928980
31/08/2016 927443 929234
30/09/2016 924659 930249
31/10/2016 925570 930251
30/11/2016 938895 930157
31/12/2016 936808 929592
31/01/2017 936808 942461
28/02/2017 936808 936845
31/03/2017 936808 940401
30/04/2017 936808 933644
31/05/2017 936808 942218
Ps. I'm using Microsoft SQL Server Management Studio
---- EDIT ---------
Thanks to assistance I've been able to get the code below that yields almost exactly what I need. however if I keep the "AND y1.date_year = y2.date_Year + 1 condition I don't get values for n2.EBIT past 2017-01-31. If I remove it the query sums up way to many values for some reason ...
... do you know any work around?
Reworked Code:
SELECT
m1.date_Month,
isnull(sum(case when y2.date_year = 2015 then n2.EBIT end),Max(innern2.december_value)) as 'EBIT 2015',
sum(case when y1.date_year = 2016 then n1.EBIT end) AS 'EBIT (2016)',
sum(case when y1.date_year = 2016 then n1.EBIT end) /
isnull(sum(case when y2.date_year = 2015 then n2.EBIT end),SUM(innern2.december_value)) - 1 AS 'EBIT Growth 2015-2016'
FROM EBIT AS n1
INNER JOIN date_year AS y1 ON y1.date_year_id = n1.date_year_id
INNER JOIN date_month AS m1 ON m1.date_month_id = n1.date_month_id
LEFT JOIN EBIT AS n2
INNER JOIN date_year AS y2 ON y2.date_year_id = n2.date_year_id
INNER JOIN date_month AS m2 ON m2.date_month_id = n2.date_month_id
ON n1.Company_Id = n2.company_Id AND m1.date_month = m2.date_month
LEFT JOIN
(
SELECT maxn2.date_year_id, SUM(maxn2.EBIT) as december_value
FROM EBIT maxn2
Inner join Date_Month As M on M.Date_Month_Id = maxn2.Date_Month_Id
inner join Date_Year as Y on Y.Date_Year_Id = maxn2.Date_Year_Id
WHERE Month(Date_Month) = 12 and year(Date_Month) = 2016
GROUP BY maxn2.date_year_id
) as innern2 on innern2.date_year_id = n1.date_year_id - 1
WHERE n1.EBIT <> 0 AND n2.EBIT <> 0 AND y1.date_year = y2.date_year + 1
and n2.Date_Month_Id >= (Select Min(E.Date_Month_Id) from EBIT as E inner join Date_Year as Y on Y.Date_Year_Id = E.Date_Year_Id Where Y.Date_Year = 2016)
GROUP BY m1.date_month
ORDER BY m1.Date_Month asc;
OutPut with Constraint mentioned:
date_Month EBIT 2015 EBIT (2016)
2013-12-31 2053514.548 2198051.670
2014-01-31 1045523.283 1112047.668
2014-02-28 1043085.024 1109221.007
2014-03-31 1047602.842 1118112.429
2014-04-30 1044455.198 1117082.270
2014-05-31 1045228.639 1122019.824
2014-06-30 1053516.159 1130536.274
2014-07-31 1055882.605 1135838.665
2014-08-31 1056331.236 1138886.938
2014-09-30 1064254.322 1147685.937
2014-10-31 1040798.042 1126437.959
2014-11-30 1029127.634 1116292.468
2014-12-31 1020233.204 1111509.880
2015-01-31 1011895.933 1109692.378
2015-02-28 1000990.365 1103494.875
2015-03-31 996252.094 1099507.246
2015-04-30 997653.412 1094694.816
2015-05-31 1007836.533 1103352.777
2015-06-30 1004344.223 1098103.559
2015-07-31 994391.939 1081689.371
2015-08-31 979809.245 1064033.692
2015-09-30 971746.514 1041604.341
2015-10-31 979410.989 1044583.652
2015-11-30 989746.574 1049158.666
2015-12-31 969556.360 1022646.632
2016-01-31 968797.454 990592.876
2016-02-29 934777.852 961009.086
2016-03-31 933858.404 942917.628
2016-04-30 931500.111 937784.980
2016-05-31 928974.727 941049.211
2016-06-30 929102.681 930969.603
2016-07-31 928979.675 926670.277
2016-08-31 929233.754 927442.570
2016-09-30 930248.793 924658.701
2016-10-31 930250.547 925569.857
2016-11-30 930156.559 938894.794
2016-12-31 929592.092 936808.419
2017-01-31 942617.388 NULL
2017-02-28 942617.388 NULL
2017-03-31 942617.388 NULL
2017-04-30 942617.388 NULL
2017-05-31 942617.388 NULL
Output with suggested constraint "AND y1.date_Year = 2016":
date_Month EBIT 2015 EBIT (2016)
2013-12-31 2053514.548 8781104.520
2014-01-31 1045523.283 3330912.804
2014-02-28 1043085.024 3322433.491
2014-03-31 1047602.842 3349059.127
2014-04-30 1044455.198 3346078.340
2014-05-31 1045228.639 3360889.672
2014-06-30 1053516.159 3385242.192
2014-07-31 1055882.605 3401244.625
2014-08-31 1056331.236 3410616.294
2014-09-30 1064254.322 3436856.111
2014-10-31 1040798.042 3373162.267
2014-11-30 1029127.634 3342757.174
2014-12-31 1020233.204 4363552.086
2015-01-31 1011895.933 4384691.285
2015-02-28 1000990.365 4376807.747
2015-03-31 996252.094 4375725.598
2015-04-30 997653.412 4357711.561
2015-05-31 1007836.533 4392712.780
2015-06-30 1004344.223 4373955.988
2015-07-31 994391.939 4314820.563
2015-08-31 979809.245 4247412.535
2015-09-30 971746.514 4151358.468
2015-10-31 979410.989 4161422.299
2015-11-30 989746.574 4179141.525
2015-12-31 969556.360 4077629.132
2016-01-31 968797.454 2970088.034
2016-02-29 934777.852 2881375.640
2016-03-31 933858.404 2827941.356
2016-04-30 931500.111 2815130.846
2016-05-31 928974.727 2824951.723
2016-06-30 929102.681 2794527.987
2016-07-31 928979.675 2783688.491
2016-08-31 929233.754 2785958.962
2016-09-30 930248.793 2783949.407
2016-10-31 930250.547 2787387.285
2016-11-30 930156.559 2827684.912
2016-12-31 929592.092 2822043.195
2017-01-31 930177.105 1884922.522
2017-02-28 930177.105 1873690.578
2017-03-31 930177.105 1880802.312
2017-04-30 930177.105 1867287.280
2017-05-31 930177.105 1884436.230
Output without "and y1.date_year = y2.date_year - 1 constraint:
date_Month EBIT 2015 EBIT (2016)
2013-12-31 4255200.364 8781104.520
2014-01-31 3237430.013 3330912.804
2014-02-28 3201517.162 3322433.491
2014-03-31 3190328.126 3349059.127
2014-04-30 3176902.028 3346078.340
2014-05-31 3171643.267 3360889.672
2014-06-30 3172452.363 3385242.192
2014-07-31 3169541.043 3401244.625
2014-08-31 3170477.020 3410616.294
2014-09-30 3192395.112 3436856.111
2014-10-31 3120825.630 3373162.267
2014-11-30 3082544.372 3342757.174
2014-12-31 4002614.551 4363552.086
2015-01-31 3997360.080 4384691.285
2015-02-28 3968359.858 4376807.747
2015-03-31 3965754.869 4375725.598
2015-04-30 3971742.802 4357711.561
2015-05-31 4010919.518 4392712.780
2015-06-30 3997415.185 4373955.988
2015-07-31 3966592.901 4314820.563
2015-08-31 3911382.409 4247412.535
2015-09-30 3871093.275 4151358.468
2015-10-31 3900314.386 4161422.299
2015-11-30 3938511.747 4179141.525
2015-12-31 3862780.776 4077629.132
2016-01-31 2903039.552 2970088.034
2016-02-29 2801085.898 2881375.640
2016-03-31 2798222.402 2827941.356
2016-04-30 2794500.333 2815130.846
2016-05-31 2787967.181 2824951.723
2016-06-30 2788351.043 2794527.987
2016-07-31 2787982.025 2783688.491
2016-08-31 2788744.262 2785958.962
2016-09-30 2790746.379 2783949.407
2016-10-31 2790751.641 2787387.285
2016-11-30 2790469.677 2827684.912
2016-12-31 2789361.289 2822043.195
2017-01-31 942617.388 1884922.522
2017-02-28 942617.388 1873690.578
2017-03-31 942617.388 1880802.312
2017-04-30 942617.388 1867287.280
2017-05-31 942617.388 1884436.230
Have you considered a left join?
SELECT
m1.date_Month,
isnull(SUM(n2.EBIT),936808) As 'EBIT (2015)',
SUM(n1.EBIT) AS 'EBIT (2016)',
isnull(SUM(n2.EBIT),936808) /
SUM(n1.EBIT) - 1 AS 'EBIT Growth 2015-2016'
FROM EBIT AS n1
INNER JOIN date_year AS y1 ON y1.date_year_id = n1.date_year_id
INNER JOIN date_month AS m1 ON m1.date_month_id = n1.date_month_id
LEFT JOIN EBIT AS n2
INNER JOIN date_year AS y2 ON y2.date_year_id = n2.date_year_id
INNER JOIN date_month AS m2 ON m2.date_month_id = n2.date_month_id
ON n1.Company_Id = n2.company_Id AND m1.date_month = m2.date_month
WHERE n1.EBIT <> 0 AND n2.EBIT <> 0 and y1.date_year = 2015 AND y2.date_year = 2016
GROUP BY m1.date_month
ORDER BY m1.Date_Month ASC;
Also, comparing you output column names, I was thinking your where clause may have mixed the years, so should be:
WHERE n1.EBIT <> 0 AND n2.EBIT <> 0 and y1.date_year = 2016 AND y2.date_year = 2015
EDIT---------
Add in a inline table to get the december EBIT from previous year
SELECT
m1.date_Month,
isnull(SUM(n2.EBIT),MAX(innern2.december_value)) As 'EBIT (2015)',
SUM(n1.EBIT) AS 'EBIT (2016)',
isnull(SUM(n2.EBIT),MAX(innern2.december_value)) /
SUM(n1.EBIT) - 1 AS 'EBIT Growth 2015-2016'
FROM EBIT AS n1
INNER JOIN date_year AS y1 ON y1.date_year_id = n1.date_year_id
INNER JOIN date_month AS m1 ON m1.date_month_id = n1.date_month_id
LEFT JOIN EBIT AS n2
INNER JOIN date_year AS y2 ON y2.date_year_id = n2.date_year_id
INNER JOIN date_month AS m2 ON m2.date_month_id = n2.date_month_id
ON n1.Company_Id = n2.company_Id
AND m1.date_month = m2.date_month
AND year(y1.date_year) = year(y2.date_year) - 1
LEFT JOIN
(
SELECT year(Y.date_year) as [year], SUM(maxn2.EBIT) as december_value
FROM EBIT maxn2
Inner join Date_Month As M on M.Date_Month_Id = maxn2.Date_Month_Id
inner join Date_Year as Y on Y.Date_Year_Id = maxn2.Date_Year_Id
WHERE Month(M.Date_Month) = 12
GROUP BY year(Y.date_year)
) as innern2 on innern2.[year] = year(y1.date_year) - 1
WHERE n1.EBIT <> 0 AND n2.EBIT <> 0 and year(y1.date_year) >= 2016
GROUP BY m1.date_month
ORDER BY m1.Date_Month ASC;
To check this approach, if you run this part from the inner query, you should get a list of years and the value of the december EBIT for that year:
SELECT year(Y.date_year) as [year], SUM(maxn2.EBIT) as december_value
FROM EBIT maxn2
Inner join Date_Month As M on M.Date_Month_Id = maxn2.Date_Month_Id
inner join Date_Year as Y on Y.Date_Year_Id = maxn2.Date_Year_Id
WHERE Month(M.Date_Month) = 12
GROUP BY year(Y.date_year)