将 DISTINCT 值从一个 table 连接到另一个

Joining DISTINCT values from one table to Another

我有 7 个具有以下结构的表:

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

当前有此查询

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

输出这样的table

如您所见,助手不在 table 中。鉴于我 tbl_Helpers 填充了这样的值

我希望第一个 table 变成这样的东西

我想显示每一行的助手,如果它恰好少于 2 个助手,那么它将留空.. 或 NULL。我在这里尝试一些代码,我想知道是否有快速解决方法。助手不会超过 2,因为我限制用户这样做。

我尝试在 tbl_Helpers 中使用 DISTINCT 来获取每个不同的助手并使用它们的 eh_ID 过滤它们,但我不知道如何附加它们以使其像第三个table

您的第一个问题是您无法决定哪个助手应该是#1,哪个应该是#2。所以首先让我们给他们分配一个号码。 接下来,让我们将它们加入您的 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

WITH 语句视为在查询期间存在的临时 table,称为 CTE

注意 LEFT JOIN 的使用,因为我们不知道是否有 2 个助手。

请注意,在 hlp2.help_ID IS NOT NULL 的情况下,我只 select hlp1,它要么是 2 个助手,要么是 none。

根据您的示例,我认为您可以将其应用于您的查询。 祝你好运!