[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

how to properly combine two queries

Posted on 2016-09-20
6
Medium Priority
?
114 Views
Last Modified: 2016-09-26
Need to combine these two queries:

SELECT     dbo.WO_DETAIL_UNION.wono, dbo.WO_DETAIL_UNION.item, dbo.WO_DETAIL_UNION.descrip, dbo.WO_DETAIL_UNION.task, 
                      dbo.WO_DETAIL_UNION.qtyreq, dbo.WO_DETAIL_UNION.qty, dbo.WO_DETAIL_UNION.cost, dbo.WO_DETAIL_UNION.[ext cost], 
                      dbo.WO_DETAIL_UNION.cond, dbo.WO_DETAIL_UNION.gen_by, dbo.WO_DETAIL_UNION.add_date, dbo.WO_DETAIL_UNION.issue_date, 
                      dbo.WO_DETAIL_UNION.[lineno]
FROM         dbo.WOMAST01 LEFT OUTER JOIN
                      dbo.WO_DETAIL_UNION ON dbo.WOMAST01.wono = dbo.WO_DETAIL_UNION.wono
WHERE     (dbo.WOMAST01.dept NOT IN ('SUB', 'TST', 'ADM', 'STK')) AND (dbo.WOMAST01.item <> '0')

Open in new window


and

SELECT     woytrn01.wono, woytrn01.item, woytrn01.descrip, woytrn01.task, woytrn01.qtyreq, woytrn01.qty, woytrn01.cost, 
                      woytrn01.cost * woytrn01.qty AS [ext cost], woytrn01.cond, woytrn01.gen_by, woytrn01.add_date, woytrn01.issue_date, woytrn01.[lineno]
FROM         dbo.WOYTRN01 AS woytrn01 CROSS JOIN
                      dbo.WOTRAN01
WHERE     (DATEDIFF(d, woytrn01.add_date, GETDATE()) < 365) AND (woytrn01.task NOT LIKE '%SCRA%')
UNION
SELECT     wotran01.wono, wotran01.item, wotran01.descrip, wotran01.task, wotran01.qtyreq, wotran01.qty, wotran01.cost, 
                      wotran01.cost * wotran01.qty AS [ext cost], wotran01.cond, wotran01.gen_by, wotran01.add_date, wotran01.issue_date, wotran01.[lineno]
FROM         dbo.WOYTRN01 AS woytrn01 CROSS JOIN
                      dbo.WOTRAN01
WHERE     (DATEDIFF(d, wotran01.add_date, GETDATE()) < 365) AND (wotran01.task NOT LIKE '%SCRA%')

Open in new window

0
Comment
Question by:maximus1974
5 Comments
 
LVL 70

Accepted Solution

by:
Éric Moreau earned 2000 total points
ID: 41807460
What is the issue? Can't you  just add "UNION ALL" between the 2 queries?
0
 
LVL 19

Expert Comment

by:John Tsioumpris
ID: 41807493
as long you return the same number of fields with the same name you can UNION them
0
 
LVL 53

Expert Comment

by:Vitor Montalvão
ID: 41808193
Please define "combine".
0
 

Author Comment

by:maximus1974
ID: 41814030
Yes, there are comments in this question that led me to a solution. Eric Moreau's UNION ALL helped me to combine or consolidate the View with the query running off the view. Don't know what else to tell you Vitor. Please do not reopen this question.
0
 
LVL 53

Expert Comment

by:Vitor Montalvão
ID: 41815461
Eric Moreau's UNION ALL helped me to combine or consolidate the View with the query running off the view.
Good. Only Eric's comment should be marked as solution (previously you marked my comment as well). This time looks better and I don't have any intention to reopen this question.
Cheers
0

Featured Post

Vote for the Most Valuable Expert

It’s time to recognize experts that go above and beyond with helpful solutions and engagement on site. Choose from the top experts in the Hall of Fame or on the right rail of your favorite topic page. Look for the blue “Nominate” button on their profile to vote.

Question has a verified solution.

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

This article shows gives you an overview on SQL Server 2016 row level security. You will also get to know the usages of row-level-security and how it works
MSSQL DB-maintenance also needs implementation of multiple activities. However, unprecedented errors can hamper the database management. In that case, deploying Stellar SQL Database Toolkit ensures fast and accurate database and backup repair as wel…
Using examples as well as descriptions, and references to Books Online, show the documentation available for datatypes, explain the available data types and show how data can be passed into and out of variables.
With just a little bit of  SQL and VBA, many doors open to cool things like synchronize a list box to display data relevant to other information on a form.  If you have never written code or looked at an SQL statement before, no problem! ...  give i…
Suggested Courses

873 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