开发者

Produce result table trom multiple tables

开发者 https://www.devze.com 2023-03-28 01:10 出处:网络
SQL Server 2008 R2 I have 3 tables contained data for 3 different types of events Type1, Type2, Type3 with two columns:

SQL Server 2008 R2

I have 3 tables contained data for 3 different types of events

Type1, Type2, Type3 with two columns:

DatePoint ValuePoint

I want to produce result table which would look like that:

DatePoint TotalType1 TotalType2 TotalType3

I've started from that

SELECT [DatePoint]
      ,SUM(ValuePoint) as TotalType1 
  FROM [dbo].[Type1]
  GROUP BY [DatePoint]
  ORDER BY [DatePoint]

SELECT [DatePoint]
开发者_运维知识库      ,SUM(ValuePoint) as TotalType2
  FROM [dbo].[Type2]
  GROUP BY [DatePoint]
  ORDER BY [DatePoint]

SELECT [DatePoint]
      ,SUM(ValuePoint) as TotalType3
  FROM [dbo].[Type3]
  GROUP BY [DatePoint]
  ORDER BY [DatePoint]

So I have three result but I need to produce one (Date TotalType1 TotalType2 TotalType3), what I need to do next achieve my goal?

UPDATE

Forgot to mention that DatePoint which is exists in one type may or may not exist in another


Here's my take. I assume that you don't have the same datetime values in every table (certainly, the stuff I get to work with is never so consistant). There should be an easier way to do this, but once you're past two outer joins things can get pretty tricky.

SELECT
   dp.DatePoint
  ,isnull(t1.TotalType1, 0) TotalType1
  ,isnull(t2.TotalType2, 0) TotalType2
  ,isnull(t3.TotalType3, 0) TotalType3
 from (--  Without "ALL", UNION will filter out duplicates
       select DatePoint
        from Type1
       union select DatePoint
        from Type2
       union select DatePoint
        from Type3) dp
  left outer join (select DatePoint, sum(ValuePoint) TotalType1
                    from Type1
                    group by DatePoint) t1
   on t1.DatePoint = db.DatePoint
  left outer join (select DatePoint, sum(ValuePoint) TotalType2
                    from Type2
                    group by DatePoint) t2
   on t2.DatePoint = db.DatePoint
  left outer join (select DatePoint, sum(ValuePoint) TotalType3
                    from Type3
                    group by DatePoint) t3
   on t3.DatePoint = db.DatePoint
 order by dp.DatePoint


Suppose some distinct could help, but the general idea should be the following:

SELECT
    t.[DatePoint],
    SUM(t1.ValuePoint) as TotalType1,
    SUM(t2.ValuePoint) as TotalType2,
    SUM(t3.ValuePoint) as TotalType3 
FROM
(
    SELECT [DatePoint] FROM [dbo].[Type1]
    UNION
    SELECT [DatePoint] FROM [dbo].[Type2]
    UNION
    SELECT [DatePoint] FROM [dbo].[Type3]
) as t

LEFT JOIN
    [dbo].[Type1] t1
ON
    t1.[DatePoint] = t.[DatePoint]
LEFT JOIN
    [dbo].[Type2] t2
ON
    t2.[DatePoint] = t.[DatePoint]
LEFT JOIN
    [dbo].[Type3] t3
ON
    t3.[DatePoint] = t.[DatePoint]
GROUP BY
    t.[DatePoint]
ORDER BY
    t.[DatePoint]


To avoid all of the JOINs:

SELECT
    SQ.DatePoint,
    SUM(CASE WHEN SQ.type = 1 THEN SQ.ValuePoint ELSE 0 END) AS TotalType1,
    SUM(CASE WHEN SQ.type = 2 THEN SQ.ValuePoint ELSE 0 END) AS TotalType2,
    SUM(CASE WHEN SQ.type = 3 THEN SQ.ValuePoint ELSE 0 END) AS TotalType3
FROM (
    SELECT
        1 AS type,
        DatePoint,
        ValuePoint
    FROM
        dbo.Type1
    UNION ALL
    SELECT
        2 AS type,
        DatePoint,
        ValuePoint
    FROM
        dbo.Type2
    UNION ALL
    SELECT
        3 AS type,
        DatePoint,
        ValuePoint
    FROM
        dbo.Type3
    ) AS SQ
GROUP BY
    DatePoint
ORDER BY
    DatePoint

From the little information provided though, it seems like there are some flaws in the database design, which is probably part of the reason that querying the data is so difficult.

0

精彩评论

暂无评论...
验证码 换一张
取 消