如何合并来自两个不同来源的数据

时间:2018-09-20 09:55:37

标签: sql sql-server

为我的“愚蠢”问题表示歉意,但我一点都不熟悉SQL。

我有以下两个查询,我只想返回这两个查询之间的区别:

SELECT [TIME],Consumption, temperature, speed
FROM database1 
WHERE Rev = '1' AND genDate = '2018-09-20' AND runDate = '2018-09-19' 
ORDER BY [TIME]

SELECT [TIME],Consumption, temperature, speed
FROM database1 
WHERE Rev = '1' AND genDate = '2018-09-19' AND runDate = '2018-09-18' 
ORDER BY [TIME]

我正在尝试类似的事情:

SELECT [TIME],Consumption, temperatureDiff, speedDiff

我认为我应该使用INNER JOIN,但是这两个查询可能没有相同的行数。最后的目标是保留所有行,如果一个查询中没有行,则在减号中使用0代替。

该表的示例:

enter image description here

6 个答案:

答案 0 :(得分:2)

尽管OUTER JOINISNULL()COALESCE()的答案都可以,但是有一个选项稍微复杂一些,不需要JOIN

SELECT
  [TIME],
  SUM(CASE WHEN genDate = '2018-09-19' THEN -consumption ELSE consumption END)   AS ConsumptionDiff,
  SUM(CASE WHEN genDate = '2018-09-19' THEN -temperature ELSE temperature END)   AS TemperatureDiff,
  SUM(CASE WHEN genDate = '2018-09-19' THEN -speed       ELSE speed       END)   AS SpeedDiff
FROM
  database1
WHERE
     (Rev = '1' AND genDate = '2018-09-20' AND runDate = '2018-09-19')
  OR (Rev = '1' AND genDate = '2018-09-19' AND runDate = '2018-09-18')
GROUP BY
  [Time]

有人可能会觉得更整洁的另一种选择...

SELECT
  [TIME],
  SUM(relative_data.consumption)   AS ConsumptionDiff,
  SUM(relative_data.temperature)   AS TemperatureDiff,
  SUM(relative_data.speed      )   AS SpeedDiff
FROM
  database1
CROSS APPLY
(
  SELECT  consumption,  temperature,  speed WHERE genDate = '2018-09-20' AND runDate = '2018-09-19'
  UNION ALL
  SELECT -consumption, -temperature, -speed WHERE genDate = '2018-09-19' AND runDate = '2018-09-18'
)
  relative_data
WHERE
  Rev = '1'
GROUP BY
  [Time]

答案 1 :(得分:1)

一种方法是full join

SELECT COALESCE(t1.time, t2.time) as time,
       ( COALESCE(t1.Consumption, 0) - COALESCE(t2.Consumption, 0) ) as consumption_diff,
       ( COALESCE(t1.temperature, 0) - COALESCE(t2.temperature, 0) ) as temperature_diff,
       ( COALESCE(t1.speed, 0) - COALESCE(t2.speed, 0) ) as consumption_speed
FROM (SELECT [TIME],Consumption, temperature, speed
      FROM database1 
      WHERE Rev = '1' AND genDate = '2018-09-20' AND runDate = '2018-09-19' 
     ) t1 FULL OUTER JOIN
     (SELECT [TIME],Consumption, temperature, speed
      FROM database1 
      WHERE Rev = '1' AND genDate = '2018-09-19' AND runDate = '2018-09-18' 
     ) t2
     ON t1.time = t2.time;

答案 2 :(得分:0)

尝试一下:

    select COALESCE(a.[time],b.[time]),
    case when a.Consumption is null then -b.Consumption when b.Consumption is null then a.Consumption else  (a.Consumption - b.Consumption) end as consumptionDiff, 
    case when a.temperature is null then -b.temperature when b.temperature is null then a.temperature else  (a.temperature - b.temperature) end as temperatureDiff, 
    case when a.speed is null then -b.speed when b.speed is null then a.speed else  (a.speed - b.speed) end as speedDiff
    from
    (
    SELECT [TIME] as time,Consumption, temperature, speed
    FROM database1 
    WHERE Rev = '1' AND genDate = '2018-09-20' AND runDate = '2018-09-19' 
    )  a full outer join
    (
    SELECT [TIME] as time,Consumption, temperature, speed
    FROM database1 
    WHERE Rev = '1' AND genDate = '2018-09-19' AND runDate = '2018-09-18' 
    )  b
    ON a.time = b.time

答案 3 :(得分:0)

您可以通过使用List<Site> siteList = new List<Site>(); XmlDocument xml = new XmlDocument(); xml.Load(@"D:\REM\config.xml"); foreach (XmlElement ndSites in xml.SelectNodes("SITES/SITE")) { siteList.ErrorCounter = int.Parse(ndSites["ERROR_COUNTER"].innerText); // You should handle the potential parse error expression /** do it for all you need */ foreach (XmlElement ndTests in ndSites.SelectNodes("TESTS/TEST")) { Test currentTest = new Test(); /** Fill it **/ siteList.Tests.add(currentTest); } } FULL OUTER JOIN来实现这一点,这里我假设两个表中的TIME int不超过一行。

ISNULL

答案 4 :(得分:0)

如果您要进行合并(或使用ISNULL),则使用几个CTE可能会使您的查询更清晰:

;WITH 
    initialFilter AS
    (
        SELECT * FROM database1 WHERE Rev = '1'
    )
    ,t1 AS
    (
        SELECT * FROM initialFilter
        WHERE genDate = '2018-09-20' AND runDate = '2018-09-19'
    )
    ,t2 AS
    (
        SELECT * FROM initialFilter
        WHERE genDate = '2018-09-19' AND runDate = '2018-09-18'
    )
SELECT
    COALESCE(t1.[TIME], t2.[TIME]) AS [TIME],
    COALESCE(t1.Consumption, 0) - COALESCE(t2.Consumption, 0) AS ConsumptionDiff,
    COALESCE(t1.temperature, 0) - COALESCE(t2.temperature, 0) AS temperatureDiff,
    COALESCE(t1.speed, 0) - COALESCE(t2.speed, 0) AS speedDiff
FROM t1 FULL OUTER JOIN t2 ON
    t2.[TIME] = t1.[TIME]
ORDER BY COALESCE(t1.[TIME], t2.[TIME])

答案 5 :(得分:-1)

除运算符外使用

 SELECT [TIME],Consumption, temperature, speed
 FROM database1 
 WHERE Rev = '1' AND genDate = '2018-09-20' AND runDate = '2018-09-19' 
 ORDER BY [TIME]
 EXCEPT
 SELECT [TIME],Consumption, temperature, speed
 FROM database1 
 WHERE Rev = '1' AND genDate = '2018-09-19' AND runDate = '2018-09-18' 
 ORDER BY [TIME]