SQL Server 2008两个具有公共日期的表字段如何从两个表

时间:2015-06-11 11:16:01

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

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

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

2 个答案:

答案 0 :(得分:0)

使用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

输出:

Ext.each(records, function(record){
            newItem = Ext.create('Ext.Panel', {
                material: record.get('material'),
                cls: 'setitems-item',
                tpl: '<div class="setitems-item-material">{material}</div>' +
                '<div class="setitems-item-atpstatus {atpstatus}">{cquantity}</div>' +
                '<div class="setitems-item-cdd">{[Cicero.Helper.formatSAPdate2Str(values.cdd)]}</div>' +
                '<div class="setitems-item-netprice">{netprice}</div>' +
                '<div class="setitems-item-netvalue">{netvalue}</div>',
                items: [
                    {
                        xtype: 'button',
                        text: Cicero.Text.getText('SC_I_CONDITIONS_BTN'),
                        itemId: 'setItemConditions',
                        cls:'setConditions'
                    }
                ],
                data: record.getData()
            });
            newSetItems.push(newItem);
        }, this);

答案 1 :(得分:0)

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)

enter image description here