I'm very confused about this one. can someone help? My issue:
I want to turn this query and table data
Query:
select A.DispatchNote, B.MStockCode, B.NComment
from MdnMaster A
left join MdnDetail B on A.DispatchNote = B.DispatchNote
A.DispatchNote | B.MStockCode | B.NComment |
---|---|---|
258579/0001 | RFK2-8520-150-25 | |
258579/0001 | FREIGHT | |
258579/0001 | 1 Parcel UPS Ground 2/28/2020 | |
258579/0001 | Trk#: 1Z8R9V900342021397 -- 0.9 lb | |
258585/0001 | CAW2-1832-25L | |
258585/0001 | FREIGHT | |
258585/0001 | 6 Parcels UPS Ground 2/28/2020 | |
258585/0001 | Trk#: 1Z8R9V900342126962 -- 15 lb | |
258585/0001 | Trk#: 1Z8R9V900342515176 -- 15 lb | |
258585/0001 | Trk#: 1Z8R9V900340703781 -- 15 lb | |
258585/0001 | Trk#: 1Z8R9V900340988792 -- 15 lb | |
258585/0001 | Trk#: 1Z8R9V900340706204 -- 15 lb | |
258585/0001 | Trk#: 1Z8R9V900342832014 -- 15 lb |
into something like this query and table
Query:
select
A.DispatchNote,
MAX(B.MStockCode) as StockCode,
MAX(case when B.NComment like 'Trk%' then B.NComment end) as NComment,
MAX(A.CustomerPoNumber) as CustomerPO
from MdnMaster A
left join MdnDetail B on A.DispatchNote = B.DispatchNote
and (B.NComment LIKE 'Trk%' OR B.MStockCode is not null)
and A.Customer = 'LAWSON'
and B.MLineShipDate =
case
when datepart(weekday, getdate() -1) = '7'
then DATEADD(hh,0,dateadd(DAY, datediff(day, 0, getdate()),-2)) -- if yesterday was Saturday, set to Friday
when datepart(weekday, getdate() -1) = '1'
then DATEADD(hh,0,dateadd(DAY, datediff(day, 0, getdate()),-3)) -- if yesterday was Sunday, set to Friday
else DATEADD(hh,0,dateadd(DAY, datediff(day, 0, getdate()),-1))
end
group by A.DispatchNote
The above will also select LAWSON only customers and will look for ship dates that are YESTERDAY.
A.DispatchNote | B.MStockCode | B.NComment |
---|---|---|
258579/0001 | RFK2-8520-150-25 | Trk#: 1Z8R9V900342021397 -- 0.9 lb |
258585/0001 | CAW2-1832-25L | Trk#: 1Z8R9V900342126962 -- 15 lb |
But I'm not sure how. When I run my query I still get rows that have random info in the NComment Column (like Blank NComments or things other than Trk#) and a whole lot of NULL in StockCode and NComment.. I think I'm closer, but I'm still missing something and haven't been able to find the query to match the above table.
Basically I want to clear the empty rows in MStockCode (or essentially condense them) and either pull ONE or ALL tracking numbers attached to a dispatchnote. (Whatever is easier)
Sample data from above query:
DispatchNote | StockCode | NComment | CustomerPO |
---|---|---|---|
165532/0006 | NULL | NULL | 340 |
165532/0007 | NULL | NULL | 340 |
165532/0008 | NULL | NULL | 340 |
169247/0003 | NULL | NULL | 50992-A |
170298/0010 | NULL | NULL | 51054 |
Am i able to correct my current query or should I take another approach like a CTE? I really just need to condense the information and grab either ONE tracking number or ALL of them for a specfic dispatchnote. It does not matter if I do one or the other, as long as I can clear out the blank rows and only have tracking info on the NComment column.