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
Solved

SQL Union Query

Posted on 2015-02-19
3
127 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
  • 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 500 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

Microsoft Certification Exam 74-409

Veeam® is happy to provide the Microsoft community with a study guide prepared by MVP and MCT, Orin Thomas. This guide will take you through each of the exam objectives, helping you to prepare for and pass the examination.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Excel - SQL export question 3 50
I've got an interview this morning and I want to sound intelligent... 4 69
mysql query for sum() 3 28
Inserting data into database 10 36
I use MySQL for many of my development projects in a Windows environment. To manage my databases (and perform queries) for years I used a tool called MySQL administrator.  This tool has since been replaced by MySQL Workbench. So I decided to m…
Foreword This article was written many years ago, in the days when PHP supported the MySQL extension (http://php.net/manual/en/function.mysql-connect.php).  Today (http://php.net/manual/en/migration70.removed-exts-sapis.php) you would not use MySQL…
Although Jacob Bernoulli (1654-1705) has been credited as the creator of "Binomial Distribution Table", Gottfried Leibniz (1646-1716) did his dissertation on the subject in 1666; Leibniz you may recall is the co-inventor of "Calculus" and beat Isaac…
The Email Laundry PDF encryption service allows companies to send confidential encrypted  emails to anybody. The PDF document can also contain attachments that are embedded in the encrypted PDF. The password is randomly generated by The Email Laundr…

829 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