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

时间:2015-01-16 06:17:16

标签: sql-server-2008 tsql join inner-join

我有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

输出这样的表

enter image description here

如您所见,帮助程序不在表中。鉴于我有tbl_Helpers填充了像这样的值

enter image description here

我希望第一个表格变成这样的东西 enter image description here

我想显示每一行的助手,如果碰巧有少于2个助手,那么它就会留空......或者为空。我在这里尝试一些代码,我想知道是否有快速解决这个问题。由于我限制用户这样做,助手不会超过2个。

我尝试在tbl_Helpers中使用DISTINCT来获取每个不同的帮助器并使用它们的eh_ID过滤它们,但我不知道如何将它们连接起来使其变得像第三个表一样

1 个答案:

答案 0 :(得分:1)

你的第一个问题是你不能决定哪个助手应该是#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 - 语句看作是在查询期间存在的临时表,它被称为CTE

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

请注意,我只选择hlp2.help_ID IS NOT NULL中的hlp1,它是2个助手或者没有。

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