Solved

how to properly combine two queries

Posted on 2016-09-20
6
73 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 70

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 47

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 47

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

Simplifying Server Workload Migrations

This use case outlines the migration challenges that organizations face and how the Acronis AnyData Engine supports physical-to-physical (P2P), physical-to-virtual (P2V), virtual to physical (V2P), and cross-virtual (V2V) migration scenarios to address these challenges.

Question has a verified solution.

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

If you have heard of RFC822 date formats, they can be quite a challenge in SQL Server. RFC822 is an Internet standard format for email message headers, including all dates within those headers. The RFC822 protocols are available in detail at:   ht…
Load balancing is the method of dividing the total amount of work performed by one computer between two or more computers. Its aim is to get more work done in the same amount of time, ensuring that all the users get served faster.
Using examples as well as descriptions, and references to Books Online, show the different Recovery Models available in SQL Server and explain, as well as show how full, differential and transaction log backups are performed
Via a live example, show how to backup a database, simulate a failure backup the tail of the database transaction log and perform the restore.

777 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