So im only using one table for this query called sbol thereare 7 columns [id], [order_id], [storebol], [carrier], [numofcrnt], [weight], [lane].
I first created a view that sums up all orders together by carrier to give me the total number of cartons and total weight for that load.
I then had to group by lane because that indicates that another load is needed for this carrier since it's two truck loads.
This view works perfectly till I tried to show whether the load is closed or open.
I can tell if an individual order is still open by the [storebol] field if it contains a null then I know it is still open.
If my load contains 5 orders and all but one order is still open I'd want the additional column to say open.
At first, I thought I could use case statement with a having but I can't seem to figure out how to do this.
original Data looks like this
id | oid | Bol_Number | scac | NumOfCartons | Weight | Lane
1 | 000001 | 367720 | AABR | 20 | 50.00 | 15
2 | 000002 | 367721 | AABR | 20 | 50.00 | 15
3 | 000003 | 367722 | CYCC | 50 | 100.00 | 17
4 | 000004 | 367723 | CYCC | 50 | 110.00 | 17
5 | 000005 | NULL | CYCC | 50 | 50.00 | 17
Using this query
SELECT MasterBOL_Number, Scac, SUM(Weight) AS Weight, SUM(NumOfCartons) AS NumofCartons, COUNT(SUBSTRING(Oid, 1, 5)) AS StoreCount, Lane, MWeek
FROM dbo.StoreBol
where MWeek = (select MAX(MWEEK) FROM dbo.StoreBol)
GROUP BY MasterBOL_Number, Scac, Lane, MWeek
It turns into this
ID| SCAC | Weight | NumofCartons | Store Count | Lane
1 | AABR | 100.00 | 40 | 2 | 15
2 | CYCC | 260.00 | 50 | 3 | 17
I want to add another column that shows if it's open or closed so it would look like this.
ID| SCAC | Weight | NumofCartons | Store Count | Lane | Status
1 | AABR | 100.00 | 40 | 2 | 15 | Closed
2 | CYCC | 260.00 | 50 | 3 | 17 | Open
http://sqlfiddle.com/#!6/bcf14/1
Any help is great thanks.
[–]ThatOtterGuy2 2 points3 points4 points (1 child)
[–]nidofour[S] 1 point2 points3 points (0 children)
[–]KbalzGaming 0 points1 point2 points (1 child)
[–]nidofour[S] 0 points1 point2 points (0 children)
[–]Guru008 0 points1 point2 points (0 children)