Solved

# getting the minum value of two select statements

Posted on 2014-03-07
268 Views
I have these two select statements joined with a union statement and they return the correct values.  I would like to select the minimum of the two values?

(select MIN(wou.use_date)
from web_OnlinePass_Use wou
inner join web_top_id wti on wti.ind_id = wou.id
inner join Name n on n.ID = wou.id
where wti.top_ID = '16040' and (USE_DATE >= '12/07/2012' and USE_DATE <= '12/06/2013'))
union
(select MIN(wolu.olu_date)
from web_Online_library_Use wolu
inner join web_top_id wti on wti.ind_id = wolu.id
inner join Name n on n.ID = wolu.id
where wti.top_ID = '16040' and (olu_DATE >= '12/07/2012' and olu_DATE <= '12/06/2013'))

need the figure how to nest this union as part of another select min() statement?
0
Question by:Jeff_Kingston
[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
• 3

LVL 11

Expert Comment

ID: 39912754
Try this:

``````SELECT MIN(X.DateValue) FROM (
(select MIN(wou.use_date) AS DateValue
from web_OnlinePass_Use wou
inner join web_top_id wti on wti.ind_id = wou.id
inner join Name n on n.ID = wou.id
where wti.top_ID = '16040' and (USE_DATE >= '12/07/2012' and USE_DATE <= '12/06/2013'))
union
(select MIN(wolu.olu_date) AS DateValue
from web_Online_library_Use wolu
inner join web_top_id wti on wti.ind_id = wolu.id
inner join Name n on n.ID = wolu.id
where wti.top_ID = '16040' and (olu_DATE >= '12/07/2012' and olu_DATE <= '12/06/2013'))
) AS X
``````
0

LVL 11

Accepted Solution

Simone B earned 500 total points
ID: 39912770
You can also use a Common Table Expression:

(Edit sorry, forgot the brackets)

``````WITH CTE (DateValue) AS
( (select MIN(wou.use_date) AS DateValue
from web_OnlinePass_Use wou
inner join web_top_id wti on wti.ind_id = wou.id
inner join Name n on n.ID = wou.id
where wti.top_ID = '16040' and (USE_DATE >= '12/07/2012' and USE_DATE <= '12/06/2013'))
union
(select MIN(wolu.olu_date) AS DateValue
from web_Online_library_Use wolu
inner join web_top_id wti on wti.ind_id = wolu.id
inner join Name n on n.ID = wolu.id
where wti.top_ID = '16040' and (olu_DATE >= '12/07/2012' and olu_DATE <= '12/06/2013')))
SELECT MIN( DateValue) FROM CTE
``````
0

Author Closing Comment

ID: 39912774
Super thanks for the rapid reply.
0

LVL 11

Expert Comment

ID: 39912776
You're welcome!
0

## Featured Post

Question has a verified solution.

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

Why is this different from all of the other step by step guides?  Because I make a living as a DBA and not as a writer and I lived through this experience. Defining the name: When I talk to people they say different names on this subject stuff l…
For both online and offline retail, the cross-channel business is the most recent pattern in the B2C trade space.
Via a live example, show how to extract insert data into a SQL Server database table using the Import/Export option and Bulk Insert.
Viewers will learn how to use the SELECT statement in SQL and will be exposed to the many uses the SELECT statement has.
###### Suggested Courses
Course of the Month6 days, 10 hours left to enroll