Solved

how to properly combine two queries

Posted on 2016-09-20
6
59 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
6 Comments
 
LVL 69

Accepted Solution

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

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 45

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 45

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

Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

Join & Write a Comment

Introduction In my previous article (http://www.experts-exchange.com/Microsoft/Development/MS-SQL-Server/SSIS/A_9150-Loading-XML-Using-SSIS.html) I showed you how the XML Source component can be used to load XML files into a SQL Server database, us…
Ever needed a SQL 2008 Database replicated/mirrored/log shipped on another server but you can't take the downtime inflicted by initial snapshot or disconnect while T-logs are restored or mirror applied? You can use SQL Server Initialize from Backup…
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.
Viewers will learn how the fundamental information of how to create a table.

757 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

19 Experts available now in Live!

Get 1:1 Help Now