在1个表中加入2条记录,在另一个表中加入2条记录,生成2条记录

时间:2011-02-15 22:22:23

标签: sql join cross-join

我有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子句。

3 个答案:

答案 0 :(得分:0)

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

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

您是否有一个示例,说明您提供的行会返回什么内容?


我喜欢亚历克斯的答案,看起来很稳固。

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

那么如果checkByNuglyPaycode中有一条不在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

答案 1 :(得分: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

答案 2 :(得分: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