Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

getting the minum value of two select statements

Posted on 2014-03-07
4
Medium Priority
?
272 Views
Last Modified: 2014-03-07
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
Comment
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
  • Learn & ask questions
  • 3
4 Comments
 
LVL 11

Expert Comment

by:Simone B
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

Open in new window

0
 
LVL 11

Accepted Solution

by:
Simone B earned 2000 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

Open in new window

0
 

Author Closing Comment

by:Jeff_Kingston
ID: 39912774
Super thanks for the rapid reply.
0
 
LVL 11

Expert Comment

by:Simone B
ID: 39912776
You're welcome!
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

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…
One of the most important things in an application is the query performance. This article intends to give you good tips to improve the performance of your queries.
This video shows how to set up a shell script to accept a positional parameter when called, pass that to a SQL script, accept the output from the statement back and then manipulate it in the Shell.
Using examples as well as descriptions, and references to Books Online, show the different Recovery Models available in SQL Server and explain, as well as show how full, differential and transaction log backups are performed

636 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