Solved

Union query

Posted on 2014-11-25
6
116 Views
Last Modified: 2014-11-25
The following sql works without Union Select.

Question: Why it doesn't work with Union Select?

"SELECT '- all -' , 'n/a', 0 From tblCustomers cc Union SELECT" & _
                      " c.FirstName + ' ' + c.LastName AS Customer, o.OrderDate, o.OrderID" & _
                      " FROM tblCustomers c INNER JOIN tblOrders o ON c.CustomerID = o.CustomerID" & _
                      " Where o.CustomerID=IIf(" & custID & "=0, o.CustomerID, " & custID & ")")

Open in new window

0
Comment
Question by:Mike Eghtebas
  • 4
  • 2
6 Comments
 
LVL 65

Expert Comment

by:Jim Horn
ID: 40465013
UNION queries determine the column names from the first half of the UNION, and the above T-SQL doesn't have any.  

Try this...
"SELECT '- all -' AS Customer, 'n/a' as OrderDate, 0 as OrderID From tblCustomers cc Union SELECT" & _
" c.FirstName + ' ' + c.LastName , o.OrderDate, o.OrderID" & _
" FROM tblCustomers c INNER JOIN tblOrders o ON c.CustomerID = o.CustomerID" & _
" Where o.CustomerID=IIf(" & custID & "=0, o.CustomerID, " & custID & ")")

Open in new window


Also, if OrderDate is a date column, then 'n/a' isn't going to work as that's a varchar value, so you'll have to decide what you want here.
0
 
LVL 65

Expert Comment

by:Jim Horn
ID: 40465019
Easier to read
"SELECT '- all -' AS Customer, 'n/a' as OrderDate, 0 as OrderID " & _ 
"FROM tblCustomers cc " & _ 
"UNION SELECT c.FirstName + ' ' + c.LastName, o.OrderDate, o.OrderID " & _
"FROM tblCustomers c " & _
"   INNER JOIN tblOrders o ON c.CustomerID = o.CustomerID " & _
"WHERE o.CustomerID=IIf(" & custID & "=0, o.CustomerID, " & custID & ")")

Open in new window

btw IIF works in SQL 2012, if you're using a version before that you'll have to use CASE.
0
 
LVL 33

Author Comment

by:Mike Eghtebas
ID: 40465033
The problem was missing:   cast(o.OrderDate as varchar(20))

How can I prepare this cast to produce dates like 11/25/2014
0
DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

 
LVL 65

Accepted Solution

by:
Jim Horn earned 500 total points
ID: 40465058
>How can I prepare this cast to produce dates like 11/25/2014
SELECT CONVERT(varchar, o.OrderDate, 101)
See PortletPaul's handy-dandy article SQL Server Date Styles (formats) using CONVERT() as a reference.
0
 
LVL 33

Author Comment

by:Mike Eghtebas
ID: 40465104
Thank you.
0
 
LVL 65

Expert Comment

by:Jim Horn
ID: 40465182
Thanks for the grade.  Good luck with your project.  -Jim
0

Featured Post

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Oracle - Query link database loop 8 37
sql 2014,  lock limit 5 29
Find results from sql within a time span 11 29
optimize stored procedure 6 23
In this article I will describe the Backup & Restore method as one possible migration process and I will add the extra tasks needed for an upgrade when and where is applied so it will cover all.
Composite queries are used to retrieve the results from joining multiple queries after applying any filters. UNION, INTERSECT, MINUS, and UNION ALL are some of the operators used to get certain desired results.‚Äč
Via a live example, show how to shrink a transaction log file down to a reasonable size.
Viewers will learn how the fundamental information of how to create a table.

816 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now