?
Solved

SQL Union Query

Posted on 2015-02-19
3
Medium Priority
?
130 Views
Last Modified: 2015-02-19
I have this case.

I want to get all records from a table called workorder where a column called datepaid is within a range, then all rows from a table called wo_adjs where the column woid is equal to the corresponding column in the table workorder called wokey and the colun in wo_adjs = 'L'.

Lets assume the date range is 2015-02-01 to 2015-02-16.

Can someone show me a sql query to do this?

The structure of both tables is attached.
table-wo-adjs.jpg
table-workorder.jpg
0
Comment
Question by:Richard Korts
[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
  • 2
3 Comments
 
LVL 11

Expert Comment

by:Dany Balian
ID: 40620213
Select * from workorder where datepaid between 2015-02-01 and 2015-02-16
Union
Select workorder.* from workorder
inner join wo_adjs on wo_adjs.whoid=workorder.wokey
where wo_adjs.type='L'
0
 

Author Comment

by:Richard Korts
ID: 40620232
To Dany Balian,

Doesn't work, I modified it slightly to omit Workorders where wokey > 200000.

So it looks like this:

Select * from workorder where datepaid >= '2015-02-01'  and datepaid <= '2015-02-16' and wokey < 200000
Union
Select workorder.* from workorder
inner join wo_adjs on wo_adjs.whoid=workorder.wokey
where wo_adjs.type='L'

It produces 119 rows.

If I just look at the table wo_adjs for type = 'L' with woid < 200000 I get 6 rows.
0
 
LVL 11

Accepted Solution

by:
Dany Balian earned 2000 total points
ID: 40620244
So u need only 6 rows in total? If yes then u don't need union!

Select workorder.* from workorder
inner join wo_adjs on wo_adjs.whoid=workorder.wokey
where wo_adjs.type='L'
And datepaid >= '2015-02-01'  and datepaid <= '2015-02-16' and wokey < 200000

Can u give us a sample data and expected result?
0

Featured Post

On Demand Webinar - Networking for the Cloud Era

This webinar discusses:
-Common barriers companies experience when moving to the cloud
-How SD-WAN changes the way we look at networks
-Best practices customers should employ moving forward with cloud migration
-What happens behind the scenes of SteelConnect’s one-click button

Question has a verified solution.

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

This post looks at MongoDB and MySQL, and covers high-level MongoDB strengths, weaknesses, features, and uses from the perspective of an SQL user.
Backups and Disaster RecoveryIn this post, we’ll look at strategies for backups and disaster recovery.
In this video, Percona Solution Engineer Dimitri Vanoverbeke discusses why you want to use at least three nodes in a database cluster. To discuss how Percona Consulting can help with your design and architecture needs for your database and infras…
In this video, Percona Solutions Engineer Barrett Chambers discusses some of the basic syntax differences between MySQL and MongoDB. To learn more check out our webinar on MongoDB administration for MySQL DBA: https://www.percona.com/resources/we…

741 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