SQL Server 2008 two tables with common dates field how to select by datewise from both tables

Chelliah Chellamani

two tables with common field with dates Here i mentioned how i need output kindly any help me.

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

i want select by dateswise

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
Giorgi Nakeuri

With 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

Output:

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

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

How to select columns from two tables sql server

From Dev

How to select from two SQL tables

From Dev

How to correctly select from two tables (SQL)

From Dev

How to correctly select from two tables (SQL)

From Dev

SQL Server 2008 how to get top record from multiple tables

From Dev

How to ignore the field that common in two tables

From Dev

SQL query from two tables ordering results by both tables columns

From Dev

sql server select from two tables as null or value

From Dev

sql server select from two tables as null or value

From Dev

how to select content from 2 tables based on id that are in both tables

From Dev

How to join two tables such that same column from both the tables are not repeated?

From Dev

how to display dates from two different tables?

From Dev

select all from both tables with a common column - same column name

From Dev

Select two tables from Mysql server

From Dev

(SQL) Select and return all from more than 2 tables that have a common field where a specific value is present in all tables

From Dev

How to find MAX value from two tables in SQL Server?

From Dev

SQL Access Select NOT IN from two tables

From Dev

SQL SELECT SUM from two tables and group by

From Dev

SQL SELECT FROM TWO TABLES INNER JOIN

From Dev

Select Count from Two Tables = Multiplication in SQL?

From Dev

SQL Server: select query from multiple tables

From Dev

SQL Server SELECT from multiple tables

From Dev

SQL server select from 3 tables

From Dev

SQL Server : values from two or more tables

From Dev

SQL Server : merge data from two tables

From Dev

SQL Server Create View from Two Tables

From Dev

mysql query to retrive data from two tables having a common field

From Dev

populate datalist from 2 sql server 2008 tables, with sqldatasource

From Dev

Retrieving query data from four tables using SQL Server 2008?

Related Related

  1. 1

    How to select columns from two tables sql server

  2. 2

    How to select from two SQL tables

  3. 3

    How to correctly select from two tables (SQL)

  4. 4

    How to correctly select from two tables (SQL)

  5. 5

    SQL Server 2008 how to get top record from multiple tables

  6. 6

    How to ignore the field that common in two tables

  7. 7

    SQL query from two tables ordering results by both tables columns

  8. 8

    sql server select from two tables as null or value

  9. 9

    sql server select from two tables as null or value

  10. 10

    how to select content from 2 tables based on id that are in both tables

  11. 11

    How to join two tables such that same column from both the tables are not repeated?

  12. 12

    how to display dates from two different tables?

  13. 13

    select all from both tables with a common column - same column name

  14. 14

    Select two tables from Mysql server

  15. 15

    (SQL) Select and return all from more than 2 tables that have a common field where a specific value is present in all tables

  16. 16

    How to find MAX value from two tables in SQL Server?

  17. 17

    SQL Access Select NOT IN from two tables

  18. 18

    SQL SELECT SUM from two tables and group by

  19. 19

    SQL SELECT FROM TWO TABLES INNER JOIN

  20. 20

    Select Count from Two Tables = Multiplication in SQL?

  21. 21

    SQL Server: select query from multiple tables

  22. 22

    SQL Server SELECT from multiple tables

  23. 23

    SQL server select from 3 tables

  24. 24

    SQL Server : values from two or more tables

  25. 25

    SQL Server : merge data from two tables

  26. 26

    SQL Server Create View from Two Tables

  27. 27

    mysql query to retrive data from two tables having a common field

  28. 28

    populate datalist from 2 sql server 2008 tables, with sqldatasource

  29. 29

    Retrieving query data from four tables using SQL Server 2008?

HotTag

Archive