How can I select from many table to one table?

Anue

I want to count data in table 8 table by date and result it as one table

______|| table 1 || table 2 || table 3 || table 4 ||
date1 || 7,000   || 8,000   || 9,000   || 10,000  ||  
date1 || 7,000   || 8,000   || 9,000   || 10,000  ||  
Giannis Paraskevopoulos

If you are in SQL Server:

;With Table1CTE AS
(
    SELECT CONVERT(DATETIME,CONVERT(VARCHAR,Date1,112)) AS Date1,
           COUNT(*) AS Table1
    FROM   Table1
    GROUP BY CONVERT(DATETIME,CONVERT(VARCHAR,Date1,112))
)
,Table2CTE AS
(
    SELECT CONVERT(DATETIME,CONVERT(VARCHAR,Date1,112)) AS Date1,
           COUNT(*) AS Table2
    FROM   Table2
    GROUP BY CONVERT(DATETIME,CONVERT(VARCHAR,Date1,112))
)
,Table3CTE AS
(
    SELECT CONVERT(DATETIME,CONVERT(VARCHAR,Date1,112)) AS Date1,
           COUNT(*) AS Table3
    FROM   Table3
    GROUP BY CONVERT(DATETIME,CONVERT(VARCHAR,Date1,112))
)
,Table4CTE AS
(
    SELECT CONVERT(DATETIME,CONVERT(VARCHAR,Date1,112)) AS Date1,
           COUNT(*) AS Table4
    FROM   Table4
    GROUP BY CONVERT(DATETIME,CONVERT(VARCHAR,Date1,112))
)
SELECT Date1,
       Table1,
       Table2,
       Table3,
       Table4
FROM   Table1CTE T1
       FULL OUTER JOIN Table2CTE T2
            ON T1.date1 = T2.date1
       FULL OUTER JOIN Table3CTE T3
            ON T1.date1 = T3.date1
       FULL OUTER JOIN Table4CTE T4
            ON T1.date1 = T4.date1

I am using FULL OUTER JOIN as i do not know if all dates are present in all tables.

You could always try joining subqueries:

SELECT Date1,
       Table1,
       Table2,
       Table3,
       Table4
FROM   (
           SELECT CONVERT(DATETIME,CONVERT(VARCHAR,Date1,112)) AS Date1,
                  COUNT(*) AS Table1
           FROM   Table1
           GROUP BY CONVERT(DATETIME,CONVERT(VARCHAR,Date1,112))
       ) T1
       FULL OUTER JOIN 
       (
           SELECT CONVERT(DATETIME,CONVERT(VARCHAR,Date1,112)) AS Date1,
                  COUNT(*) AS Table2
           FROM   Table2
           GROUP BY CONVERT(DATETIME,CONVERT(VARCHAR,Date1,112))
       ) T2
            ON T1.date1 = T2.date1
       FULL OUTER JOIN 
       (
           SELECT CONVERT(DATETIME,CONVERT(VARCHAR,Date1,112)) AS Date1,
                  COUNT(*) AS Table3
           FROM   Table3
           GROUP BY CONVERT(DATETIME,CONVERT(VARCHAR,Date1,112))
       ) T3
            ON T1.date1 = T3.date1
       FULL OUTER JOIN 
       (
           SELECT CONVERT(DATETIME,CONVERT(VARCHAR,Date1,112)) AS Date1,
                  COUNT(*) AS Table4
           FROM   Table4
           GROUP BY CONVERT(DATETIME,CONVERT(VARCHAR,Date1,112))
       ) T4
            ON T1.date1 = T4.date1

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 one to one relations from many to many table

From Dev

How can I use a variable from one table as a SELECT parameter for another table in php

From Dev

If I want to insert many records in one record from other table, how can I do that?

From Dev

I can not get values from table. Relationship one to many

From Dev

Postgres select from one to many table to single table rows

From Dev

How can i use SQL Select Insert to copy rows from one table to another

From Dev

How can I select * from table, but cast one of the columns to a different datatype?

From Dev

how can i select one in table use watirwebdriver?

From Dev

How can I query a table with a one to many relationship with another table using multiple parameters?

From Dev

How do I select items from one table such that a query in another table yields exactly one row?

From Dev

How do I select items from one table such that a query in another table yields exactly one row?

From Dev

How can I select 2 different random rows from a table?

From Dev

How can I insert values from SELECT SUM into other table?

From Dev

How can I update a table based on results from a select query?

From Dev

How can I insert values from SELECT SUM into other table?

From Java

How can I add column from one table to another in Clickhouse?

From Dev

how can i Copy Into from more than one Table ?? Snowflake

From Dev

How can I get just one column from a table?

From Dev

how can i get data from table with relation many to many in yii2?

From Dev

How can I populate a form with results from one table which can be edited and saved to another table

From Dev

Can I select from a destination table in a subquery?

From Dev

How to return result of many select statements as one custom table

From Dev

There is a table with a pcode with many dates in different rows. How can I select only the latest date for a pcode

From Dev

How can I reference an intersection table of a many-to-many relationship?

From Dev

How to select all from table one based on a value, then select from same table with value from that table?

From Dev

How can I select data from a mysql table, and then throw it into a html table

From Dev

How can I select data from a mysql table, and then throw it into a html table

From Dev

Select from many-to-many table

From Dev

How can I get all records from one table, even if there are no corresponding records in the JOINed table?

Related Related

  1. 1

    How to select one to one relations from many to many table

  2. 2

    How can I use a variable from one table as a SELECT parameter for another table in php

  3. 3

    If I want to insert many records in one record from other table, how can I do that?

  4. 4

    I can not get values from table. Relationship one to many

  5. 5

    Postgres select from one to many table to single table rows

  6. 6

    How can i use SQL Select Insert to copy rows from one table to another

  7. 7

    How can I select * from table, but cast one of the columns to a different datatype?

  8. 8

    how can i select one in table use watirwebdriver?

  9. 9

    How can I query a table with a one to many relationship with another table using multiple parameters?

  10. 10

    How do I select items from one table such that a query in another table yields exactly one row?

  11. 11

    How do I select items from one table such that a query in another table yields exactly one row?

  12. 12

    How can I select 2 different random rows from a table?

  13. 13

    How can I insert values from SELECT SUM into other table?

  14. 14

    How can I update a table based on results from a select query?

  15. 15

    How can I insert values from SELECT SUM into other table?

  16. 16

    How can I add column from one table to another in Clickhouse?

  17. 17

    how can i Copy Into from more than one Table ?? Snowflake

  18. 18

    How can I get just one column from a table?

  19. 19

    how can i get data from table with relation many to many in yii2?

  20. 20

    How can I populate a form with results from one table which can be edited and saved to another table

  21. 21

    Can I select from a destination table in a subquery?

  22. 22

    How to return result of many select statements as one custom table

  23. 23

    There is a table with a pcode with many dates in different rows. How can I select only the latest date for a pcode

  24. 24

    How can I reference an intersection table of a many-to-many relationship?

  25. 25

    How to select all from table one based on a value, then select from same table with value from that table?

  26. 26

    How can I select data from a mysql table, and then throw it into a html table

  27. 27

    How can I select data from a mysql table, and then throw it into a html table

  28. 28

    Select from many-to-many table

  29. 29

    How can I get all records from one table, even if there are no corresponding records in the JOINed table?

HotTag

Archive