Expiring Today—Celebrate National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

how to properly combine two queries

Posted on 2016-09-20
6
Medium Priority
?
102 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
6 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 18

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 52

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 52

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

Moving data to the cloud? Find out if you’re ready

Before moving to the cloud, it is important to carefully define your db needs, plan for the migration & understand prod. environment. This wp explains how to define what you need from a cloud provider, plan for the migration & what putting a cloud solution into practice entails.

Question has a verified solution.

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

A Stored Procedure in Microsoft SQL Server is a powerful feature that it can be used to execute the Data Manipulation Language (DML) or Data Definition Language (DDL). Depending on business requirements, a single Stored Procedure can return differe…
Recently we ran in to an issue while running some SQL jobs where we were trying to process the cubes.  We got an error saying failure stating 'NT SERVICE\SQLSERVERAGENT does not have access to Analysis Services. So this is a way to automate that wit…
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.
Viewers will learn how to use the INSERT statement to insert data into their tables. It will also introduce the NULL statement, to show them what happens when no value is giving for any given column.

719 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