根据日期insqlserver重复输出结果

时间:2016-05-24 19:17:28

标签: sql-server

连接3个表的多列中的值并显示在一列中。日期字段是datetime.now我的问题是如果日期重复一个表然后amt2,amt3也在同一日期重复

表1:

date                amt1
1-1-2016 10:44:00   111
1-1-2016 10:44:02   222    repeat date
3-4-2016 11:44:00   111

表2:

date                amt2
1-1-2016 10:44:02   101
2-2-2016 10:44:02   333
2-3-2016 10:44:02   444

表3:

date                amt3
2-2-2016 10:44:04   001
2-3-2016 10:44:02   002
3-3-2016 10:44:02   003

查询

SELECT
COALESCE(t1.date, t2.date, t3.date)
, t1.amt1
, t2.amt2
, t3.amt3
FROM tb_amount t1
FULL JOIN tb_cashamt t2 ON t1.date =t2.date
FULL JOIN tb_grandtotal t3 ON t2.date = t3.date

输出:

date                amt1  amt2  amt3
1-1-2016 10:44:00   111   null  NULL 
1-1-2016 10:44:02   111   101   null    data repeat on amt1(111)
2-2-2016 10:44:02   NULL  333   null
2-2-2016 10:44:04   NULL  333   001     data repeat on amt2(111)
TOTAL               ---   ---   -----

期望的输出:

date        amt1          amt2          amt3
1-1-2016    111           101           NULL
2-2-2016    NULL          333           001
TOTAL       total(amt1)   total(amt1)   total(amt1)

我在datecolumn上遇到了smalldatetime数据类型的问题。我想在每个日期得到一个结果,amt1,amt2,amt3显示这个日期的总数据

3 个答案:

答案 0 :(得分:1)

使用FULL JOIN

SELECT 
    COALESCE(t1.date, t2.date, t3.date)
  , t1.amt1
  , t2.amt2
  , t3.amt3
FROM Table1 t1
FULL JOIN Table2 t2 ON t1.date =t2.date 
FULL JOIN Table3 t3 ON t2.date = t3.date

答案 1 :(得分:1)

您需要使用FULL JOIN,也称为FULL OUTER JOIN,如Using Outer Joins所述:

  

要通过在连接结果中包含不匹配的行来保留不匹配的信息,请使用完整的外连接。 SQL Server提供完整的外连接运算符FULL OUTER JOIN,它包括来自两个表的所有行,无论另一个表是否具有匹配值。

;
WITH    Table1      AS
(       SELECT      [Date], Amt1
        FROM    (   VALUES
                    ('2016-01-01', 111),
                    ('2016-02-02', 222),
                    ('2016-04-03', 111)
                )   AS Table1(Date, Amt1)
),      Table2      AS
(       SELECT      [Date], Amt2
        FROM    (   VALUES
                    ('2016-01-01', 101),
                    ('2016-02-02', 333),
                    ('2016-03-02', 444),
                    ('2016-03-03', 456),
                    ('2016-04-01', 101),
                    ('2016-04-03', 111)
                )   AS Table2(Date, Amt2)
),      Table3      AS
(       SELECT      [Date], Amt3
        FROM    (   VALUES
                    ('2016-02-02', 001),
                    ('2016-03-02', 002),
                    ('2016-03-03', 003),
                    ('2016-04-01', 555),
                    ('2016-04-02', 666),
                    ('2016-04-03', 777)
                )   AS Table3(Date, Amt3)
)
SELECT      ISNULL(Table1.Date, ISNULL(Table2.Date, Table3.Date)) Date, 
            Table1.Amt1, Table2.Amt2, Table3.Amt3
FROM        Table1
FULL JOIN   Table2  ON  Table1.Date = Table2.Date
FULL JOIN   Table3  ON  Table2.Date = Table3.Date
ORDER BY    ISNULL(Table1.Date, ISNULL(Table2.Date, Table3.Date))

答案 2 :(得分:0)

如果每个日期只需要一行,则需要在加入+之前首先按日期级别对每个表进行分组+总和:

select coalesce(T1.Date,T2.Date,T3.Date), sum(T1.Amt1),Sum(T2.Amt2),Sum(T3.Amt3)
from
(
  select convert(date, date) as Date, sum(Amt1) as Amt1 
  from Table1 
  group by convert(date, date)
) T1 
full outer join (
  select convert(date, date) as Date, sum(Amt2) as Amt2 
  from Table2
  group by convert(date, date)
) T2 on T1.Date = T2.Date
full outer join (
  select convert(date, date) as Date, sum(Amt3) as Amt3
  from Table3
  group by convert(date, date)
) T3 on isnull(T1.Date,T2.Date) = T3.Date
group by rollup(coalesce(T1.Date,T2.Date,T3.Date))