Solved

how to properly combine two queries

Posted on 2016-09-20
6
66 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 46

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 46

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

This article describes how to use the timestamp of existing data in a database to allow Tableau to calculate the prior work day instead of relying on case statements or if statements to calculate the days of the week.
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…
This video shows, step by step, how to configure Oracle Heterogeneous Services via the Generic Gateway Agent in order to make a connection from an Oracle session and access a remote SQL Server database table.
Via a live example combined with referencing Books Online, show some of the information that can be extracted from the Catalog Views in SQL Server.

932 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

13 Experts available now in Live!

Get 1:1 Help Now