Solved

SQL Query to return 2 values of the same column with conditions

Posted on 2010-09-07
5
331 Views
Last Modified: 2012-06-27


Hello,

I am trying to find a way to properly edit the below 2 SQL queries into a single query. What I want to do is return all values where es.name = widget a where value is less than 10440 AND where es.name = widget b where value is less than 10.  Since I am looking to report 2 different values from the same column(s), I can't find a way to combine this into a single query.
Any help is greatly appreciated.


QUERY1
select md.timestamp Time_Of_Poll, i.name Computer, es.name Metric,
md.numvalue Metric_Value from vMonitorMetricData md,
vItem i, Evt_Monitor_Metric_Status es
where md.resourceguid = i.guid and es.name = 'widget a'
and md.numvalue < '10440'
order by md.timestamp, es.name

QUERY 2
select md.timestamp Time_Of_Poll, i.name Computer, es.name Metric,
md.numvalue Metric_Value from vMonitorMetricData md,
vItem i, Evt_Monitor_Metric_Status es
where md.resourceguid = i.guid and es.name = 'widget b'
and md.numvalue < '10'
order by md.timestamp, es.name
0
Comment
Question by:Charlie_Melega
5 Comments
 
LVL 18

Accepted Solution

by:
Cluskitt earned 250 total points
Comment Utility
select md.timestamp Time_Of_Poll, i.name Computer, es.name Metric,
md.numvalue Metric_Value from vMonitorMetricData md,
vItem i, Evt_Monitor_Metric_Status es
where md.resourceguid = i.guid and ((es.name = 'widget a'
and md.numvalue < '10440') or (es.name = 'widget b'
and md.numvalue < '10'))
order by md.timestamp, es.name
0
 
LVL 42

Expert Comment

by:EugeneZ
Comment Utility
--try
select md.timestamp Time_Of_Poll, i.name Computer, es.name Metric,
md.numvalue Metric_Value from vMonitorMetricData md,
vItem i, Evt_Monitor_Metric_Status es
where md.resourceguid = i.guid and es.name = 'widget a'
and md.numvalue < '10440'
UNION ALL
select md.timestamp Time_Of_Poll, i.name Computer, es.name Metric,
md.numvalue Metric_Value from vMonitorMetricData md,
vItem i, Evt_Monitor_Metric_Status es
where md.resourceguid = i.guid and es.name = 'widget b'
and md.numvalue < '10'
order by md.timestamp, es.name
 
0
 
LVL 18

Expert Comment

by:Cluskitt
Comment Utility
In this case, a UNION is much less effective (meaning, slower) than a single query. It really is simple, seeing as only the conditions change. It's a simple AND/OR clause on WHERE. If there were different tables or views, then UNION might be better. But in this case, I don't think it's necessary. :)
0
 
LVL 35

Assisted Solution

by:David Todd
David Todd earned 250 total points
Comment Utility
Hi,

I noticed that you aren't using the ANSI join syntax

you wrote something like
select columns
from table1, table2, table3
where
  table1.col1 = table2.col2
  and table3.col3 = somevalue

compared to
select columns
from table1 t1
inner join table2 t2 on t2.col2 = t1.col1
inner join table3 t3 on t3.col3 = somevalue

Just that using the ANSI join syntax makes it easier to read and see what is a join and what is where condition.

HTH
  David
0
 

Author Closing Comment

by:Charlie_Melega
Comment Utility
excellent feedack, Thanks all
0

Featured Post

What is SQL Server and how does it work?

The purpose of this paper is to provide you background on SQL Server. It’s your self-study guide for learning fundamentals. It includes both the history of SQL and its technical basics. Concepts and definitions will form the solid foundation of your future DBA expertise.

Join & Write a Comment

Nowadays, some of developer are too much worried about data. Who is using data, who is updating it etc. etc. Because, data is more costlier in term of money and information. So security of data is focusing concern in days. Lets' understand the Au…
In this article I will describe the Copy Database Wizard method as one possible migration process and I will add the extra tasks needed for an upgrade when and where is applied so it will cover all.
Viewers will learn how to use the SELECT statement in SQL and will be exposed to the many uses the SELECT statement has.
Viewers will learn how to use the UPDATE and DELETE statements to change or remove existing data from their tables. Make a table: Update a specific column given a specific row using the UPDATE statement: Remove a set of values using the DELETE s…

762 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

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now