【问题标题】:SQL Server 2008 two tables with common dates field how to select by datewise from both tablesSQL Server 2008 两个具有公共日期字段的表如何从两个表中按日期选择
【发布时间】:2015-06-11 11:16:00
【问题描述】:

两个带有日期的公共字段的表 在这里我提到了我如何需要输出 请帮助我。

table1
table1.col1 table1.col2  table1.col2
a           ab           2015-05-01 00:00:00.000
as          as           2015-05-01 00:00:00.000
as          asasd        2015-05-01 00:00:00.000
asd         aa           2015-05-02 00:00:00.000
asd         asd          2015-05-04 00:00:00.000


table2
table2.col1   table2.col2  table2.col3 
asd           aasd         2015-05-01 00:00:00.000
asasd         asd          2015-05-01 00:00:00.000  
asd           asd          2015-05-04 00:00:00.000
asd           asd          2015-05-05 00:00:00.000
asd           asd          2015-05-31 00:00:00.000

我想按日期选择

Date                      table1.col1 table2.col2  table2.col1   table2.col2
2015-05-01 00:00:00.000   a           ab           asd           aasd
                          as          as           asasd         asd
                          as          asasd        Null          Null 
2015-05-02 00:00:00.000   asd         aa           Null          NUll 
2015-05-04 00:00:00.000   asd         asd          Null          Null 
                          asd         asdas        Null          Null
2015-05-05 00:00:00.000   Null        Null         sdas          asds
                          Null        Null         adad          asda

【问题讨论】:

  • 可能在这种情况下使用 FULL OUTER JOIN
  • 我试过重复..

标签: sql-server sql-server-2008 date datetime


【解决方案1】:

FULL JOIN:

DECLARE @t1 TABLE
    (
      col1 VARCHAR(20) ,
      col2 VARCHAR(20) ,
      col3 DATE
    )
INSERT  INTO @t1
VALUES  ( 'a', 'ab', '2015-05-01 00:00:00.000' ),
        ( 'as', 'as', '2015-05-01 00:00:00.000' ),
        ( 'as', 'asasd', '2015-05-01 00:00:00.000' ),
        ( 'asd', 'aa', '2015-05-02 00:00:00.000' ),
        ( 'asd', 'asd', '2015-05-04 00:00:00.000' )


DECLARE @t2 TABLE
    (
      col1 VARCHAR(20) ,
      col2 VARCHAR(20) ,
      col3 DATE
    )
INSERT  INTO @t2
VALUES  ( 'asd', 'aasd', '2015-05-01 00:00:00.000' ),
        ( 'asasd', 'asd', '2015-05-01 00:00:00.000' ),
        ( 'asd', 'asd', '2015-05-04 00:00:00.000' ),
        ( 'asd', 'asd', '2015-05-05 00:00:00.000' ),
        ( 'asd', 'asd', '2015-05-31 00:00:00.000' )


SELECT  ISNULL(t1.col3, t2.col3) as date,
        t1.col1 ,
        t1.col2 ,
        t2.col1 ,
        t2.col2
FROM    ( SELECT    * ,
                    ROW_NUMBER() OVER ( PARTITION BY col3 ORDER BY ( SELECT NULL) ) rn
          FROM      @t1) t1
        FULL JOIN 
        ( SELECT  * ,
                    ROW_NUMBER() OVER ( PARTITION BY col3 ORDER BY ( SELECT NULL) ) rn
            FROM    @t2) t2 ON t1.col3 = t2.col3 AND t1.rn = t2.rn

输出:

date        col1    col2    col1    col2
2015-05-01  a       ab      asd     aasd
2015-05-01  as      as      asasd   asd
2015-05-01  as      asasd   NULL    NULL
2015-05-02  asd     aa      NULL    NULL
2015-05-04  asd     asd     asd     asd
2015-05-05  NULL    NULL    asd     asd
2015-05-31  NULL    NULL    asd     asd

【讨论】:

  • 我厌倦了按照你的完整加入 Msg 257, Level 16, State 3, Line 15 不允许从数据类型 smalldatetime 到 int 的隐式转换。使用 CONVERT 函数运行此查询。
  • @ChellaMani,我不知道你在做什么,但如果你运行我的查询,它就可以工作。尝试调整我的解决方案以适应您的数据库结构时,您做错了事
  • 非常感谢,我以你的方式使用了我所期望的。
【解决方案2】:
select table1.col3,
       table1.col1,
       table2.col1,
       table1.col2,
       table2.col2
from table1,table2
where table1.col3=table2.col3

为了获得相同的结果:

select table1.col3,
       table1.col1,
       table2.col1,
       table1.col2,
       table2.col2
from table1,table2
where table1.col3=table2.col3

union
select table1.col3,
       table1.col1,'NULL' as [table2.col1], table1.col2,'NULL' as [table2.col2]
       from table1 where table1.col3 not in (select table1.col3 from table1,table2 where table1.col3=table2.col3)
union
select table2.col3,
      'NULL' as     [table1.col1],table2.col1,'NULL' as [table1.col2],table2.col2
       from table2 where table2.col3 not in (select table1.col3 from table1,table2 where table1.col3=table2.col3)

【讨论】:

  • 结果不是 OP 所期望的。
  • 如果我使用这意味着我正在进入一张桌子
  • 谢谢我终于明白了
猜你喜欢
  • 2017-06-21
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-11-12
  • 1970-01-01
  • 2011-11-26
相关资源
最近更新 更多