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

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

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.

Fix NULL Date Column?

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

in SQL, how do I get min starting and max ending date for each consecutive position an employee works?

I have some problem, I want to get earliest work beginning, and latest work ending for each position.
Data in Table:
EMPLOYEE_ID PCN POSITION WorkBEG WorkEND
--16156 056-240-35 056FOOD706 2014-09-18 00:00:00.000 2015-06-04 00:00:00.000
--16156 056-240-35 056FOOD706 2015-08-20 00:00:00.000 2015-08-23 00:00:00.000
--16156 057-240-35 057FOOD706 2015-08-24 00:00:00.000 2016-06-02 00:00:00.000
--16156 057-240-35 057FOOD706 2016-08-18 00:00:00.000 2017-06-02 00:00:00.000
--16156 TEMP 715SUMREMB 2017-06-05 00:00:00.000 2017-08-13 00:00:00.000
--16156 057-240-35 057FOOD706 2017-08-14 00:00:00.000 2018-06-01 00:00:00.000
-- my query
-- 3 rows
select m.ID as EMPLOYEE_ID,
p.PCN,
p.POSITION,
min(p.CALC_BEG) as WorkBEG,
max(p.CALC_END) as WorkEND
from hr_empmstr m
inner join hr_emppay p on p.ID = m.ID
where (m.LASTDAYWRK is null
or m.LASTDAYWRK >= dateadd(y,-5,getdate()))
and p.REC_TYPE = 'PM'
and p.CALC_END <> p.CALC_BEG
and m.ID = '16156'
group by m.ID, p.PCN, p.POSITION
order by m.ID, WorkBEG;
results:
--EMPLOYEE_ID PCN POSITION WorkBEG WorkEND
--16156 056-240-35 056FOOD706 2014-09-18 00:00:00.000 2015-08-23 00:00:00.000
--16156 057-240-35 057FOOD706 2015-08-24 00:00:00.000 2018-06-01 00:00:00.000
--16156 TEMP 715SUMREMB 2017-06-05 00:00:00.000 2017-08-13 00:00:00.000
trying to get:
--EMPLOYEE_ID PCN POSITION POS_LONG WorkBEG WorkEND
--16156 056-240-35 056FOOD706 FOOD SERV/CAFE HELP 2014-09-18 00:00:00.000 2015-08-23 00:00:00.000
--16156 057-240-35 057FOOD706 FOOD SERV/CAFE HELP 2015-08-24 00:00:00.000 2017-06-02 00:00:00.000
--16156 TEMP 715SUMREMB SUMMER EMPLOYEE 2017-06-05 00:00:00.000 2017-08-13 00:00:00.000
--16156 057-240-35 057FOOD706 FOOD SERV/CAFE HELP 2017-08-14 00:00:00.000 2018-06-01 00:00:00.000

Combing temp tables in series

Say, I have 6 temp tables stored as the following (those 3 are samples) and I would to form them into 1 single table, to be in series (headers Date, Com, Price).
Com A
Date Price
2015-05-01 00:00:00.000 34.25
2015-05-02 00:00:00.000 35.20
2015-05-03 00:00:00.000 36.70
2015-05-04 00:00:00.000 32.37
2015-05-05 00:00:00.000 32.40
2015-05-06 00:00:00.000 32.20
Com B
Date Price
2015-05-07 00:00:00.000 54.29
2015-05-08 00:00:00.000 54.50
2015-05-09 00:00:00.000 56.21
2015-05-10 00:00:00.000 56.70
2015-05-11 00:00:00.000 58.20
Com C
Date Price
2015-05-12 00:00:00.000 34.29
2015-05-13 00:00:00.000 24.50
2015-05-14 00:00:00.000 76.21
2015-05-15 00:00:00.000 36.70
2015-05-16 00:00:00.000 48.20
The output to look like, and I would like to store it as another temp table for merging later:
Date Com Price
2015-05-01 00:00:00.000 A 34.25
2015-05-02 00:00:00.000 A 35.20
2015-05-03 00:00:00.000 A 36.70
2015-05-04 00:00:00.000 A 32.37
2015-05-05 00:00:00.000 A 32.40
2015-05-06 00:00:00.000 A 32.20
2015-05-07 00:00:00.000 B 54.29
2015-05-08 00:00:00.000 B 54.50
2015-05-09 00:00:00.000 B 56.21
2015-05-10 00:00:00.000 B 56.70
2015-05-11 00:00:00.000 B 58.20
2015-05-12 00:00:00.000 C 34.29
2015-05-13 00:00:00.000 C 24.50
2015-05-14 00:00:00.000 C 76.21
2015-05-15 00:00:00.000 C 36.70
2015-05-16 00:00:00.000 C 48.20
Seems like a simple union all to me:
SELECT [Date], 'A' as Com, Price
FROM [Com A]
UNION ALL
SELECT [Date], 'B' as Com, Price
FROM [Com B]
UNION ALL
SELECT [Date], 'C' as Com, Price
FROM [Com C]
Based on your sample data
Select Date,'A' AS Com,Price from [COM A]
UNION ALL
Select Date,'B' AS Com,Price from [COM B]
UNION ALL
Select Date,'C' AS Com,Price from [COM C]

joining monthly values with daily values in sql

I have daily values in one table and monthly values in another table. I need to use the values of the monthly table and calculate them on a daily basis.
basically, monthly factor * daily factor -- for each day
thanks!
I have a table like this:
2010-12-31 00:00:00.000 28.3
2010-09-30 00:00:00.000 64.1
2010-06-30 00:00:00.000 66.15
2010-03-31 00:00:00.000 12.54
and a table like this :
2010-12-31 00:00:00.000 98.1
2010-12-30 00:00:00.000 97.61
2010-12-29 00:00:00.000 99.03
2010-12-28 00:00:00.000 97.7
2010-12-27 00:00:00.000 96.87
2010-12-23 00:00:00.000 97.44
2010-12-22 00:00:00.000 97.76
2010-12-21 00:00:00.000 96.63
2010-12-20 00:00:00.000 95.47
2010-12-17 00:00:00.000 95.2
2010-12-16 00:00:00.000 94.84
2010-12-15 00:00:00.000 94.8
2010-12-14 00:00:00.000 94.1
2010-12-13 00:00:00.000 93.88
2010-12-10 00:00:00.000 93.04
2010-12-09 00:00:00.000 91.07
2010-12-08 00:00:00.000 90.89
2010-12-07 00:00:00.000 92.72
2010-12-06 00:00:00.000 93.05
2010-12-03 00:00:00.000 91.74
2010-12-02 00:00:00.000 90.74
2010-12-01 00:00:00.000 90.25
I need to take the value for the quarter and multiply it buy all the days in the quarter by the daily value
You could try:
SELECT dt.day, dt.factor*mt.factor AS daily_factor
FROM daily_table dt INNER JOIN month_table mt
ON YEAR(dt.day) = YEAR(mt.day)
AND FLOOR((MONTH(dt.day)-1)/3) = FLOOR((MONTH(mt.day)-1)/3)
ORDER BY dt.day
or (as suggested by #Andriy)
SELECT dt.day, dt.factor*mt.factor AS daily_factor
FROM daily_table dt INNER JOIN month_table mt
ON YEAR(dt.day) = YEAR(mt.day)
AND DATEPART(QUARTER, dt.day) = DATEPART(QUARTER, mt.day)
ORDER BY dt.day