Listing the output from two identical tables. : Union « Set Operations « SQL Server / T-SQL Tutorial

SQL Server / T-SQL Tutorial
1. Query
2. Insert Delete Update
3. Table
4. Table Join
5. Data Types
6. Set Operations
7. Constraints
8. Subquery
9. Aggregate Functions
10. Date Functions
11. Math Functions
12. String Functions
13. Data Convert Functions
14. Analytical Functions
15. Sequence Indentity
16. View
17. Index
18. Cursor
19. Database
20. Transact SQL
21. Procedure Function
22. Trigger
23. Transaction
24. XML
25. System Functions
26. System Settings
27. System Tables Views
28. User Role
29. CLR
Java
Java Tutorial
Java Source Code / Java Documentation
Java Open Source
Jar File Download
Java Articles
Java Products
Java by API
Photoshop Tutorials
Maya Tutorials
Flash Tutorials
3ds-Max Tutorials
Illustrator Tutorials
GIMP Tutorials
C# / C Sharp
C# / CSharp Tutorial
C# / CSharp Open Source
ASP.Net
ASP.NET Tutorial
JavaScript DHTML
JavaScript Tutorial
JavaScript Reference
HTML / CSS
HTML CSS Reference
C / ANSI-C
C Tutorial
C++
C++ Tutorial
Ruby
PHP
Python
Python Tutorial
Python Open Source
SQL Server / T-SQL
Oracle PL / SQL
Oracle PL/SQL Tutorial
PostgreSQL
SQL / MySQL
MySQL Tutorial
VB.Net
VB.Net Tutorial
Flash / Flex / ActionScript
VBA / Excel / Access / Word
XML
XML Tutorial
Microsoft Office PowerPoint 2007 Tutorial
Microsoft Office Excel 2007 Tutorial
Microsoft Office Word 2007 Tutorial
SQL Server / T-SQL Tutorial » Set Operations » Union 
6. 3. 2. Listing the output from two identical tables.
3CREATE TABLE SalesMw (
4>     StoreID            integer NOT NULL,
5>     CD_ID               integer  NOT NULL,
6>     QtySold            integer,
7>     SalesDate          datetime)
8> GO
1INSERT into SalesMw VALUES(1300,2001,10,"Oct 31,1997")
2INSERT into SalesMw VALUES(1300,2002,15,"Oct 31,1997")
3INSERT into SalesMw VALUES(1300,2001,5,"Nov 30,1997")
4INSERT into SalesMw VALUES(1300,2003,16,"Nov 30,1997")
5INSERT into SalesMw VALUES(1300,2017,9,"Nov 30,1997")
6INSERT into SalesMw VALUES(1330,2000,2,"Oct 31,1997")
7INSERT into SalesMw VALUES(1330,2000,109,"Nov 30,1997")
8INSERT into SalesMw VALUES(1330,2029,5,"Nov 30,1997")
9INSERT into SalesMw VALUES(1330,2030,5,"Nov 30,1997")
10INSERT into SalesMw VALUES(1330,2015,20,"Nov 30,1997")
11INSERT into SalesMw VALUES(1330,2016,66,"Nov 30,1997")
12INSERT into SalesMw VALUES(1310,2005,11,"Nov 30,1997")
13INSERT into SalesMw VALUES(1320,2022,14,"Nov 30,1997")
14> GO

(rows affected)

(rows affected)

(rows affected)

(rows affected)

(rows affected)

(rows affected)

(rows affected)

(rows affected)

(rows affected)

(rows affected)

(rows affected)

(rows affected)

(rows affected)
1>
2CREATE TABLE SalesNe (
3>      StoreID           integer NOT NULL,
4>      CD_ID              integer NOT NULL,
5>      QtySold           integer,
6>      SalesDate         datetime)
7>
8INSERT into SalesNe VALUES(1200,2006,10,"Oct 31,1997")
9INSERT into SalesNe VALUES(1200,2007,15,"Oct 31,1997")
10INSERT into SalesNe VALUES(1200,2007,8,"Nov 30,1997")
11INSERT into SalesNe VALUES(1200,2008,12,"Nov 30,1997")
12INSERT into SalesNe VALUES(1200,2009,19,"Nov 30,1997")
13INSERT into SalesNe VALUES(1210,2008,7,"Oct 31,1997")
14INSERT into SalesNe VALUES(1210,2009,55,"Nov 30,1997")
15INSERT into SalesNe VALUES(1210,2010,18,"Nov 30,1997")
16INSERT into SalesNe VALUES(1210,2022,11,"Nov 30,1997")
17INSERT into SalesNe VALUES(1210,2023,20,"Nov 30,1997")
18INSERT into SalesNe VALUES(1210,2020,15,"Nov 30,1997")
19INSERT into SalesNe VALUES(1220,2018,6,"Nov 30,1997")
20INSERT into SalesNe VALUES(1220,2005,3,"Nov 30,1997")
21>
22>
23>
24>
25>
26>
27>
28>
29>
30SELECT from SalesMw
31> UNION
32SELECT from SalesNe
33>
34> GO

(rows affected)

(rows affected)

(rows affected)

(rows affected)

(rows affected)

(rows affected)

(rows affected)

(rows affected)

(rows affected)

(rows affected)

(rows affected)

(rows affected)

(rows affected)
StoreID     CD_ID        QtySold     SalesDate
----------- ----------- ----------- -----------------------
       1200        2006          10 1997-10-31 00:00:00.000
       1200        2007           8 1997-11-30 00:00:00.000
       1200        2007          15 1997-10-31 00:00:00.000
       1200        2008          12 1997-11-30 00:00:00.000
       1200        2009          19 1997-11-30 00:00:00.000
       1210        2008           7 1997-10-31 00:00:00.000
       1210        2009          55 1997-11-30 00:00:00.000
       1210        2010          18 1997-11-30 00:00:00.000
       1210        2020          15 1997-11-30 00:00:00.000
       1210        2022          11 1997-11-30 00:00:00.000
       1210        2023          20 1997-11-30 00:00:00.000
       1220        2005           3 1997-11-30 00:00:00.000
       1220        2018           6 1997-11-30 00:00:00.000
       1300        2001           5 1997-11-30 00:00:00.000
       1300        2001          10 1997-10-31 00:00:00.000
       1300        2002          15 1997-10-31 00:00:00.000
       1300        2003          16 1997-11-30 00:00:00.000
       1300        2017           9 1997-11-30 00:00:00.000
       1310        2005          11 1997-11-30 00:00:00.000
       1320        2022          14 1997-11-30 00:00:00.000
       1330        2000           2 1997-10-31 00:00:00.000
       1330        2000         109 1997-11-30 00:00:00.000
       1330        2015          20 1997-11-30 00:00:00.000
       1330        2016          66 1997-11-30 00:00:00.000
       1330        2029           5 1997-11-30 00:00:00.000
       1330        2030           5 1997-11-30 00:00:00.000

(26 rows affected)
1>
2> drop table SalesNe;
3> GO
6. 3. Union
6. 3. 1. Using Unions to Display Data from Multiple Queries
6. 3. 2. Listing the output from two identical tables.
6. 3. 3. UNION same type of columns from different tables
6. 3. 4. A union that combines information from the Billings table
6. 3. 5. A union that combines payment data from the same joined tables
6. 3. 6. OR operator can be used instead of the UNION operator, as the two equivalent examples.
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.