Solved

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

Posted on 2010-09-07
5
339 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
[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
5 Comments
 
LVL 18

Accepted Solution

by:
Cluskitt earned 250 total points
ID: 33617396
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:Eugene Z
ID: 33617923
--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
ID: 33617983
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
ID: 33620567
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
ID: 33637120
excellent feedack, Thanks all
0

Featured Post

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Batch & SQLCMD & SQL: execute maintenance plan outside sql 13 51
SQL Pivot table 2 42
SQL syntax question 6 38
Delete old Sharepoint backups 2 12
In this article we will get to know that how can we recover deleted data if it happens accidently. We really can recover deleted rows if we know the time when data is deleted by using the transaction log.
Slowly Changing Dimension Transformation component in data task flow is very useful for us to manage and control how data changes in SSIS.
Via a live example, show how to set up a backup for SQL Server using a Maintenance Plan and how to schedule the job into SQL Server Agent.
Using examples as well as descriptions, and references to Books Online, show the documentation available for datatypes, explain the available data types and show how data can be passed into and out of variables.

740 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