Reputation: 175
I have 7 Tables with the following structures:
tbl_Trucks | tbl_Driver | tbl_Clients
-----------------------|-----------------------------|--------------------
tr_ID - int | dr_ID - int | cl_ID - int
tr_Name - varchar(50) | dr_LName - varchar(50) | cl_Name - varchar(50)
| dr_FName - varchar(50) |
| dr_MName - varchar(50) |
tbl_ExpenseHead | tbl_ExpenseDiesels
---------------------------------|---------------------------
eh_ID - int | dsl_ID - int
eh_DateAdded - date | dsl_amt - float
eh_RouteStart - varchar(50) | dsl_Ltrs - float
eh_RouteEnd - varchar(50) | eh_ID - int
cl_ID - int |
dr_ID - int |
tr_ID - int |
eh_Status - varchar(50) |
eh_ERnumber - varchar(50) |
eh_InvoiceNumber - varchar(50) |
tbl_ExpenseTotal | tbl_Helpers
---------------------------------|---------------------------
tot_ID - int | help_ID - int
tot_OverallExpense- date | help_FName - varchar(50)
eh_ID - int | help_MName - varchar(50)
| help_LName - varchar(50)
| eh_ID - int
currently have this query
SELECT
h.eh_DateAdded as [TRIP_DATE],
t.tr_Name as [TRUCK_NAME],
d.dr_LName + ', ' + d.dr_FName + ' ' + d.dr_MName as DRIVER,
c.cl_Name as CLIENT,
h.eh_RouteStart + ' to ' + h.eh_RouteEnd as TRIP,
h.eh_InvoiceNumber as [INVOICE_NUMBER],
h.eh_ERnumber as [ER_NUMBER],
SUM(dsl.dsl_amt) as [DIESEL_AMOUNT],
SUM(dsl.dsl_Ltrs) as [DIESEL_LITERS],
tot.tot_OverallExpense as EXPENSE
FROM tbl_ExpenseHead h INNER JOIN
tbl_Trucks t ON h.tr_ID = t.tr_ID INNER JOIN
tbl_Driver d ON h.dr_ID = d.dr_ID INNER JOIN
tbl_Clients c ON h.cl_ID = c.cl_ID INNER JOIN
tbl_ExpDiesels dsl ON h.eh_ID = dsl.eh_ID INNER JOIN
tbl_ExpenseTotal tot ON h.eh_ID = tot.eh_ID
WHERE h.eh_Status = 'APPROVED'
GROUP BY
h.eh_DateAdded,
t.tr_Name,
d.dr_LName,
d.dr_FName,
d.dr_MName,
c.cl_Name,
h.eh_RouteStart,
h.eh_RouteEnd,
h.eh_InvoiceNumber,
h.eh_ERnumber,
dsl.dsl_amt,
dsl.dsl_Ltrs,
tot.tot_OverallExpense
that outputs a table like this
as you can see, the helpers are not in the table. Given that i have tbl_Helpers populated with values like this
i want the first table to turn out something like this
i want to show the helpers for each line and if it happens to have less than 2 helpers then it would be just left blank.. or NULL. I'm trying some codes here and i'm wondering if there's quick way around this. Helpers won't exceed to 2 since i restrict the user from doing so.
I tried using DISTINCT in tbl_Helpers to get each different helpers and filter them with their eh_ID's but i don't know how to get them attached to make it turn out like the third table
Upvotes: 2
Views: 65
Reputation: 12621
Your first problem is you can't decide which helper should be #1 and which should be #2. So first let's assign them a number.
Next, let's join them to your ExpenseHead
:
WITH myHelpers AS (
SELECT eh_ID
, help_ID
, RANK() OVER (PARTITION BY eh_IDORDER BY help_ID) helperOrder
FROM tbl_Helpers
)
SELECT h.eh_ID
, hlp1.help_ID AS help1_ID
, hlp2.help_ID AS help2_ID
FROM tbl_ExpenseHead h LEFT JOIN
myHelpers hlp2 ON h.eh_ID = hlp2.eh_ID AND hlp2.helperOrder = 2 LEFT JOIN
myHelpers hlp1 ON h.eh_ID = hlp1.eh_ID AND hlp1.helperOrder = 1 AND hlp2.help_ID IS NOT NULL
Think of the WITH
-statement as a temporary table which exists for the duration of the query, it's called a CTE
.
Notice the use of LEFT JOIN
since we don't know whether there are 2 helpers.
Notice that I only select hlp1 in case hlp2.help_ID IS NOT NULL
, it's either 2 helpers or none.
Based on your example, I think you'll be able to apply this to your query. Good luck!
Upvotes: 1