0

我有 2 个表,其中包含需要加入的大量数据。问题是这 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 子句。

4

3 回答 3

0

像您已经作为子查询一样的查询之类的东西呢:

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
于 2011-02-15T23:27:53.897 回答
0

你能提供更多信息吗?您当前的查询是什么样的?表格的结构如何?你想加入钥匙吗?

您提到您正在“查看”检查 # 和 trx_number ......这些是结果吗?

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


我喜欢亚历克斯的回答,这似乎很可靠。

也许是一个愚蠢的评论,但如果你不在乎你得到什么 trx_number,并且在你的示例查询中你使用了一个左连接,这意味着你只想要来自 checksByNuglyPaycode 的“所有”数据,你为什么要加入这两个表?你需要两个表的所有数据吗?更像是一个完整的外部连接?

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

如果在 trxNumByNuglyPaycode 中有记录而不是在 checksByNuglyPaycode 中,你会需要它吗?


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

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
于 2011-02-15T22:33:41.273 回答
0

这是一个猜测,但你可以使用类似 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
于 2011-02-15T22:41:41.847 回答