将 1 个表中的 2 条记录与另一个表中的 2 条记录连接起来,生成 2 条记录

发布于 2024-10-17 11:21:17 字数 2261 浏览 2 评论 0原文

我有 2 个表,其中有大量数据需要连接。问题是这两个表保存的数据基本相同,并且连接有时会产生不需要的结果,尽管这并非意外。结果。这是一个示例:

week_end_date  nugly   payroll_code  rate      hours     check_number
--------------------------------------------------------------
2010-01-17     AU9T8K  HRLY-W        13.00000  40.00000  530957               
2010-01-17     AU9T8K  HRLY-W        13.00000  40.00000  DD00000105382 

week_end_date   nugly   trx_number  pay_code    hours   rate
2010-01-17  AU9T8K  ETS00000010771815   HRLY-W  40.00000    13.00000
2010-01-17  AU9T8K  ETS00000010771684   HRLY-W  40.00000    13.00000

我正在查看连接中组合的检查 # 和 trx_number,但最终得到了交叉连接,因为所有内容都与我要加入的相同。对于我遇到的每一个这样的情况,我真的不在乎 trx_number 以哪个检查 # 结尾。

有什么想法吗?

以下是当前结果:

week_end_date   nugly   payroll_code    rate    hours   check_number    trx_number
2010-01-17  AU9T8K  HRLY-W  13.00000    40.00000    DD00000105382           ETS00000010771815
2010-01-17  AU9T8K  HRLY-W  13.00000    40.00000    530957                  ETS00000010771815
2010-01-17  AU9T8K  HRLY-W  13.00000    40.00000    DD00000105382           ETS00000010771684
2010-01-17  AU9T8K  HRLY-W  13.00000    40.00000    530957                  ETS00000010771684

我想要的是:

week_end_date   nugly   payroll_code    rate    hours   check_number    trx_number
2010-01-17  AU9T8K  HRLY-W  13.00000    40.00000    DD00000105382           ETS00000010771815
2010-01-17  AU9T8K  HRLY-W  13.00000    40.00000    530957                  ETS00000010771684

我并不真正关心哪个 trx_number 与哪个 check_number 。

这是我当前的查询:

SELECT c.week_end_date, c.nugly, c.payroll_code, c.rate, c.hours, c.check_number, t.trx_number
    FROM checksByNuglyPaycode c 
    LEFT OUTER JOIN trxNumByNuglyPaycode t ON c.db_id = t.db_id AND c.fridate = t.fridate
        AND c.nugly = t.nugly AND c.trx_type = t.trx_type AND c.payroll_code = t.pay_code
        AND c.hours = t.hours AND c.rate = t.rate AND c.week_end_date = t.week_end_date
    WHERE t.db_id = 'lal' AND c.nugly = 'AU9T8K' AND c.payroll_code = 'HRLY-W' 
        AND c.fridate = '2010-01-22' AND c.week_end_date = '2010-01-17'
ORDER BY c.fridate, c.nugly, payroll_code

where 子句显然是专门针对这种情况的,在最终的查询中,不会有 where 子句。

I have 2 tables with lots of data that I need to join. The problem is that the 2 tables hold mostly the same data, and the join sometimes produces undesired, though not unexpected. results. Here is an example:

week_end_date  nugly   payroll_code  rate      hours     check_number
--------------------------------------------------------------
2010-01-17     AU9T8K  HRLY-W        13.00000  40.00000  530957               
2010-01-17     AU9T8K  HRLY-W        13.00000  40.00000  DD00000105382 

week_end_date   nugly   trx_number  pay_code    hours   rate
2010-01-17  AU9T8K  ETS00000010771815   HRLY-W  40.00000    13.00000
2010-01-17  AU9T8K  ETS00000010771684   HRLY-W  40.00000    13.00000

I'm looking to the the check # and the trx_number combined in the join, but I end up with a cross join because everything is the same that I'm joining on. For every case I have like this, I really don't care with trx_number ends up with which check #.

Any thoughts?

Here are the current results:

week_end_date   nugly   payroll_code    rate    hours   check_number    trx_number
2010-01-17  AU9T8K  HRLY-W  13.00000    40.00000    DD00000105382           ETS00000010771815
2010-01-17  AU9T8K  HRLY-W  13.00000    40.00000    530957                  ETS00000010771815
2010-01-17  AU9T8K  HRLY-W  13.00000    40.00000    DD00000105382           ETS00000010771684
2010-01-17  AU9T8K  HRLY-W  13.00000    40.00000    530957                  ETS00000010771684

What I'd like is:

week_end_date   nugly   payroll_code    rate    hours   check_number    trx_number
2010-01-17  AU9T8K  HRLY-W  13.00000    40.00000    DD00000105382           ETS00000010771815
2010-01-17  AU9T8K  HRLY-W  13.00000    40.00000    530957                  ETS00000010771684

Where I don't really care which trx_number is with which check_number.

Here is my current query:

SELECT c.week_end_date, c.nugly, c.payroll_code, c.rate, c.hours, c.check_number, t.trx_number
    FROM checksByNuglyPaycode c 
    LEFT OUTER JOIN trxNumByNuglyPaycode t ON c.db_id = t.db_id AND c.fridate = t.fridate
        AND c.nugly = t.nugly AND c.trx_type = t.trx_type AND c.payroll_code = t.pay_code
        AND c.hours = t.hours AND c.rate = t.rate AND c.week_end_date = t.week_end_date
    WHERE t.db_id = 'lal' AND c.nugly = 'AU9T8K' AND c.payroll_code = 'HRLY-W' 
        AND c.fridate = '2010-01-22' AND c.week_end_date = '2010-01-17'
ORDER BY c.fridate, c.nugly, payroll_code

The where clause is obviously specifically for this case, in the final query, there will not be a where clause.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

疯了 2024-10-24 11:21:17

这是一个猜测,但是您可以使用 ROW_NUMBER 之类的东西为每个表创建一种身份字段,然后连接它们吗?

像这样的东西

CREATE VIEW vwOrderedTable1
AS
SELECT ROW_NUMBER() OVER(ORDER BY week_end_date) AS 'RowNumber', 
    week_end_date, 
    nugly, 
    payroll_code...
FROM Table1
GO

CREATE VIEW vwOrderedTable2
AS
SELECT ROW_NUMBER() OVER(ORDER BY week_end_date) AS 'RowNumber', 
    week_end_date, 
    nugly, 
    'payroll_code' = pay_code...
FROM Table2
GO

SELECT *
FROM vwOrderedTable1 
INNER JOIN vwOrderedTable2 ON vwOrderedTable1.RowNumber = vwOrderedTable2.RowNumber

It's a guess, but could you use something like ROW_NUMBER to make a sort of identity field for each table and then join on them?

Something like

CREATE VIEW vwOrderedTable1
AS
SELECT ROW_NUMBER() OVER(ORDER BY week_end_date) AS 'RowNumber', 
    week_end_date, 
    nugly, 
    payroll_code...
FROM Table1
GO

CREATE VIEW vwOrderedTable2
AS
SELECT ROW_NUMBER() OVER(ORDER BY week_end_date) AS 'RowNumber', 
    week_end_date, 
    nugly, 
    'payroll_code' = pay_code...
FROM Table2
GO

SELECT *
FROM vwOrderedTable1 
INNER JOIN vwOrderedTable2 ON vwOrderedTable1.RowNumber = vwOrderedTable2.RowNumber
我不咬妳我踢妳 2024-10-24 11:21:17

怎么样像你已经得到的作为子查询的查询:

SELECT MAX(trx_number), week_end_date, nugly, payroll_code, rate, hours, check_number
FROM
(
    QUERY_YOU_ALREADY_HAVE
) tbl
GROUP BY week_end_date, nugly, payroll_code, rate, hours, check_number

What about something like the query you've already got as a sub-query wrapped up with:

SELECT MAX(trx_number), week_end_date, nugly, payroll_code, rate, hours, check_number
FROM
(
    QUERY_YOU_ALREADY_HAVE
) tbl
GROUP BY week_end_date, nugly, payroll_code, rate, hours, check_number
痴骨ら 2024-10-24 11:21:17

您能提供更多信息吗?您当前的查询是什么样的?表格的结构如何?您是否正在尝试通过按键加入?

您提到您正在“查找”支票 # 和 trx_number...这些是结果吗?

您是否有一个示例说明您提供的行将返回什么?


我喜欢亚历克斯的回答,它看起来很可靠。

也许是一个愚蠢的评论,但如果您不关心得到的 trx_number ,并且在示例查询中您使用的是左联接,这意味着您只需要 checkByNuglyPaycode 中的“所有”数据,那么为什么要联接这两个表?您需要两个表的所有数据吗?更像是完整的外部连接?

因此,如果checksByNuglyPaycode 中有一条记录不在trxNumByNuglyPaycode 中,您会需要它吗?

如果 trxNumByNuglyPaycode 中有一条记录,而不是在 ChecksByNuglyPaycode 中,您会需要它吗?


有趣的是,如果您确实想要两个表中的所有数据,您可以使用下面的选择。注意:它使用不同的,这可能会很慢,而且我认为完整的外连接也可能很慢......已经有一段时间了......

SELECT DISTINCT
    ISNULL(c.week_end_date, t.week_end_date), ISNULL(c.nugly,t.nugly), 
    ISNULL(c.payroll_code,t.payroll_code), ISNULL(c.rate,t.rate), 
    ISNULL(c.hours, t.hours), c.check_number, t.trx_number
FROM 
    checksByNuglyPaycode c 
FULL OUTER JOIN 
    trxNumByNuglyPaycode t ON c.db_id = t.db_id 
                           AND c.fridate = t.fridate
                           AND c.nugly = t.nugly 
                           AND c.trx_type = t.trx_type 
                           AND c.payroll_code = t.pay_code
                           AND c.hours = t.hours 
                           AND c.rate = t.rate 
                           AND c.week_end_date = t.week_end_date
ORDER BY 
    c.fridate, c.nugly, payroll_code

Could you provide a bit more information? What's your current query look like? How are the tables structured? Are you trying to join on keys?

You mention you're "looking" to the check # and trx_number... are these the results?

Do you have an example of what would be returned for the lines you've provided?


I like Alex's answer, it seems solid.

Perhaps a silly comment, but if you don't care what trx_number you get, and in your sample query you're using a left join that implies you only want "all" the data from checksByNuglyPaycode, why are you joining the two tables? do you need all the data for both tables? More like a full outer join?

So if there is a record in checksByNuglyPaycode that is not in trxNumByNuglyPaycode you would need it?

And if there is a record in trxNumByNuglyPaycode and not in checksByNuglyPaycode you would need that?


Interesting aside, if you do want all the data from both tables you could use the select below. Note: it uses distinct which would likely be slow, and I think full outer joins can also be slow... it's been quite a while...

SELECT DISTINCT
    ISNULL(c.week_end_date, t.week_end_date), ISNULL(c.nugly,t.nugly), 
    ISNULL(c.payroll_code,t.payroll_code), ISNULL(c.rate,t.rate), 
    ISNULL(c.hours, t.hours), c.check_number, t.trx_number
FROM 
    checksByNuglyPaycode c 
FULL OUTER JOIN 
    trxNumByNuglyPaycode t ON c.db_id = t.db_id 
                           AND c.fridate = t.fridate
                           AND c.nugly = t.nugly 
                           AND c.trx_type = t.trx_type 
                           AND c.payroll_code = t.pay_code
                           AND c.hours = t.hours 
                           AND c.rate = t.rate 
                           AND c.week_end_date = t.week_end_date
ORDER BY 
    c.fridate, c.nugly, payroll_code
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文