合并两个表

时间:2009-07-31 18:55:06

标签: sql sql-server sql-server-2005

我有两个包含以下列的表(两个表中的列相同):

  1. functionName,而
  2. FrequencyCount
  3. 我想将这两个表合并到一个包含列的最终表中:

    1. 功能名称
    2. 基本频率计数
    3. 比较频率计数
    4. 频次数增量
    5. 合并操作应按如下方式进行:

      • 如果[FunctionName]在表1中而不在表2中,

        [Base Frequency Count] = Table1.[FrequencyCount]
        [Compared Frequency Count] = 0
        [Delta of Frequency Count] = Table1.[FrequencyCount]
        
      • 如果[FunctionName]在Table2中而Not在Table1中,

        [Base Frequency Count] = 0         
        [Compared Frequency Count] = Table2.[FrequencyCount]
        [Delta of Frequency Count] = Table2.[FrequencyCount]
        
      • 如果[FunctionName]同时出现在Table1和Table2中,

        [Base Frequency Count] = Table1.[FrequencyCount]         
        [Compared Frequency Count] = Table2.[FrequencyCount]
        [Delta of Frequency Count] = Table1.[FrequencyCount]-Table2.[FrequencyCount]
        

      希望查询具有良好的性能,最小的no。连接 如果有人能给出好的指示,那就太好了。

3 个答案:

答案 0 :(得分:2)

SELECT ISNULL(fn.FunctionName, fc.FunctionName) AS FunctionName, 
       ISNULL(fn.FrequencyCount, 0) AS BaseFrequency,
       ISNULL(fc.FrequencyCount, 0) AS ComparedFrequencyCount,
       COALESCE((fn.FrequencyCount - fc.FrequencyCount), fn.FrequencyCount, fc.FrequencyCount) AS DeltaOfFrequencyCount
INTO FinalTable
FROM FunctionName fn FULL OUTER JOIN FrequencyCount fc ON fn.FunctionName = fc.FunctionName

请注意,如果fn.FrequencyCount或fc.FrequencyCount为null(在SQL中,value - null = null),COALESCE将在第一个表达式中导致null(继续到链中的下一个表达式)。

答案 1 :(得分:1)

SELECT *
  INTO new_table_name 
  FROM (SELECT t.frequencycount 'Base Frequency Count',
               0 'Compared Frequency Count',
               t.frequencycount 'Delta of Frequency Count'
          FROM TABLE1 t
          JOIN TABLE2 t2 ON t2.functionname != t.functionname
        UNION
        SELECT 0 'Base Frequency Count',
               t2.frequencycount 'Compared Frequency Count',
               t2.frequencycount 'Delta of Frequency Count'
          FROM TABLE2 t2
          JOIN TABLE1 t ON t.functionname != t2.functionname
        UNION
        SELECT t.frequencycount 'Base Frequency Count',
               t2.frequencycount 'Compared Frequency Count',
               t.frequencycount - t2.frequencycount 'Delta of Frequency Count'
          FROM TABLE1 t
          JOIN TABLE2 t2 ON t.functionname = t2.functionname)

答案 2 :(得分:0)

不确定这是否比rexem的解决方案慢(我没有对它进行基准测试)。但是这对你来说可能更容易阅读。通过选择基于主键而不是FunctionName,可以显着提高性能。

--TABLE 1
SELECT FunctionName, FrequencyCount AS Base, 0 AS Compared, FrequencyCount AS Delta
FROM TableOne
WHERE FunctionName NOT IN (SELECT FunctionName FROM TableTwo)
UNION

--TABLE 2
SELECT FunctionName, 0 AS Base, FrequencyCount AS Compared, FrequencyCount AS Delta
FROM TableTwo
WHERE FunctionName NOT IN (SELECT FunctionName FROM TableOne)
UNION

--DELTA
SELECT FunctionName, 
(SELECT FrequencyCount FROM TableOne WHERE FunctionName = DeltaTable.FunctionName) AS Base,
(SELECT FrequencyCount FROM TableTwo WHERE FunctionName = DeltaTable.FunctionName) AS Compared,
(SELECT FrequencyCount FROM TableOne WHERE FunctionName = DeltaTable.FunctionName) - (SELECT FrequencyCount FROM TableTwo WHERE FunctionName = DeltaTable.FunctionName) AS Delta
FROM TableOne DeltaTable
WHERE FunctionName IN (SELECT FunctionName FROM TableOne)
AND FunctionName IN (SELECT FunctionName FROM TableTwo)

修改了Delta

--DELTA
SELECT One.FunctionName, 
One.FrequencyCount AS Base,
Two.FrequencyCount AS Compared,
One.FrequencyCount - Two.FrequencyCount AS Delta
FROM TableOne One
INNER JOIN TableTwo Two
    ON One.FunctionName = Two.FunctionName