dbTalk Databases Forums  

conditional query

comp.databases.ms-sqlserver comp.databases.ms-sqlserver


Discuss conditional query in the comp.databases.ms-sqlserver forum.



Reply
 
Thread Tools Display Modes
  #1  
Old   
Harry
 
Posts: n/a

Default conditional query - 08-23-2011 , 08:49 AM






I have a table with three columns:

Name Date Action
Fred Today this
Fred Today this
Fred Today that
Wilma Today this


I want to sort by name and by date, but only if 'this' was done more
than once on the same day, and not if 'that' was done on the same
day,They have to perform this more than once, and they cannot have
performed that on the same day.


Thanks,
Harry

Reply With Quote
  #2  
Old   
Henk van den Berg
 
Posts: n/a

Default Re: conditional query - 08-23-2011 , 12:41 PM






Now what possible actions might this/that be?

This: drive to bowling alley
That: Feed Dino

Looks very much like a homework assignment to me...

Harry schreef op 23-08-2011 15:49:
Quote:
I have a table with three columns:

Name Date Action
Fred Today this
Fred Today this
Fred Today that
Wilma Today this


I want to sort by name and by date, but only if 'this' was done more
than once on the same day, and not if 'that' was done on the same
day,They have to perform this more than once, and they cannot have
performed that on the same day.


Thanks,
Harry

Reply With Quote
  #3  
Old   
Harry
 
Posts: n/a

Default Re: conditional query - 08-23-2011 , 12:56 PM



On Aug 23, 1:41*pm, Henk van den Berg <m... (AT) myplace (DOT) net> wrote:
Quote:
Now what possible actions might this/that be?

This: drive to bowling alley
That: Feed Dino

Looks very much like a homework assignment to me...

Harry schreef op 23-08-2011 15:49:







I have a table with three columns:

Name * * * * Date * * * * * Action
Fred * * * * * Today * * * * this
Fred * * * * * Today * * * * this
Fred * * * * * *Today * * * *that
Wilma * * * * Today * * * * this

I want to sort by name and by date, but only if 'this' was done more
than once on the same day, and not if 'that' was done on the same
day,They have to perform this more than once, and they cannot have
performed that on the same day.

Thanks,
Harry
Not a homework question. Clinical research. A patient may have one or
more stents inserted during a procedure. He may also have a stent
taken out. I am looking for patients who had more than one stent
inserted during a procedure, and who did not have a stent taken out
during the same procedure.

harry

Reply With Quote
  #4  
Old   
Bob Barrows
 
Posts: n/a

Default Re: conditional query - 08-23-2011 , 01:11 PM



Harry wrote:
Quote:
I have a table with three columns:

Name Date Action
Fred Today this
Fred Today this
Fred Today that
Wilma Today this


I want to sort by name and by date, but only if 'this' was done more
than once on the same day, and not if 'that' was done on the same
day,They have to perform this more than once, and they cannot have
performed that on the same day.


So the desired result from this sample data would be an empty resultset?
What do you want to display when the data actually contains rows that meet
your requirement? All the rows for that name and date? Or a single row for
that name and date?
Why don't you try again. Show us some sample data that includes data that
meets your requirements and then show us what you want the desired query to
return. The key being "show" - don't just describe the desired result: show
it to us.

Reply With Quote
  #5  
Old   
Harry
 
Posts: n/a

Default Re: conditional query - 08-23-2011 , 02:06 PM



On Aug 23, 2:11*pm, "Bob Barrows" <reb01... (AT) NOyahooSPAM (DOT) com> wrote:
Quote:
Harry wrote:
I have a table with three columns:

Name * * * * Date * * * * * Action
Fred * * * * * Today * * * * this
Fred * * * * * Today * * * * this
Fred * * * * * *Today * * * *that
Wilma * * * * Today * * * * this

I want to sort by name and by date, but only if 'this' was done more
than once on the same day, and not if 'that' was done on the same
day,They have to perform this more than once, and they cannot have
performed that on the same day.

So the desired result from this sample data would be an empty resultset?
What do you want to display when the data actually contains rows that meet
your requirement? All the rows for that name and date? Or a single row for
that name and date?
Why don't you try again. Show us some sample data that includes data that
meets your requirements and then show us what you want the desired query to
return. The key being "show" - don't just describe the desired result: show
it to us.
The raw data would be:

Name Date Proc
Fred 1 stent
Fred 1 stent
Fred 1 unstent
Wilma 2 stent
Wilma 2 stent
Barney 2 stent
Barnet 2 stent
Barney 2 stent
Barney 2 unstent
....


The query would return

Wilma 2 stent
Wilma 2 stent


Because Fred and Barney also had a stent pulled on the same day they
had more than one stent inserted.

HTH
Harry

Reply With Quote
  #6  
Old   
Bob Barrows
 
Posts: n/a

Default Re: conditional query - 08-23-2011 , 02:35 PM



Harry wrote:
Quote:
On Aug 23, 2:11 pm, "Bob Barrows" <reb01... (AT) NOyahooSPAM (DOT) com> wrote:
Harry wrote:
I have a table with three columns:

Name Date Action
Fred Today this
Fred Today this
Fred Today that
Wilma Today this

I want to sort by name and by date, but only if 'this' was done more
than once on the same day, and not if 'that' was done on the same
day,They have to perform this more than once, and they cannot have
performed that on the same day.

So the desired result from this sample data would be an empty
resultset? What do you want to display when the data actually
contains rows that meet your requirement? All the rows for that name
and date? Or a single row for that name and date?
Why don't you try again. Show us some sample data that includes data
that meets your requirements and then show us what you want the
desired query to return. The key being "show" - don't just describe
the desired result: show it to us.

The raw data would be:

Name Date Proc
Fred 1 stent
Fred 1 stent
Fred 1 unstent
Wilma 2 stent
Wilma 2 stent
Barney 2 stent
Barnet 2 stent
Barney 2 stent
Barney 2 unstent
...


The query would return

Wilma 2 stent
Wilma 2 stent


Because Fred and Barney also had a stent pulled on the same day they
had more than one stent inserted.

Assuming SQL 2005 or greater:

;WITH q AS (
SELECT Name,Date
,SUM(CASE WHEN Proc='stent' THEN 1 ELSE 0 END) stents
,SUM(CASE WHEN Proc='unstent' THEN 1 ELSE 0 END) unstents
FROM tablename
GROUP BY Name, Date)
,q2 AS (
SELECT Name,Date FROM q WHERE stents>1 AND unstents=0)
SELECT t.Name,t.Date,t.Proc FROM tablename t
JOIN q2 ON t.Name=q2.Name and t.Date=q2.Date

This is untested air code (obviously), solely intended to provide the
general idea.
Hopefully, you actually haven't used those reserved keywords for your column
names ...

Reply With Quote
  #7  
Old   
Henk van den Berg
 
Posts: n/a

Default Re: conditional query - 08-23-2011 , 04:04 PM



Harry schreef op 23-08-2011 19:56:
Quote:
Not a homework question. Clinical research. A patient may have one or
more stents inserted during a procedure. He may also have a stent
taken out. I am looking for patients who had more than one stent
inserted during a procedure, and who did not have a stent taken out
during the same procedure.

harry

My bad, sorry.

Reply With Quote
  #8  
Old   
Yonghang Wang
 
Posts: n/a

Default Re: conditional query - 08-24-2011 , 03:53 PM



select * from t1
where
proc = 'stent'
and (name, date) in
(
select name,date from t1
group by name,date
having count(distinct proc) = 1
)

Reply With Quote
  #9  
Old   
Bob Barrows
 
Posts: n/a

Default Re: conditional query - 08-24-2011 , 04:34 PM



Yonghang Wang wrote:
Quote:
select * from t1
where
proc = 'stent'
and (name, date) in
(
select name,date from t1
group by name,date
having count(distinct proc) = 1
)
That will "work" with this set of data, but fail if the data contains a
single row for a patient for a date. The requirement is to only return rows
where a patient has multiple stents on a single day, and where he also has
an unstent on the same day.

Reply With Quote
  #10  
Old   
Yonghang Wang
 
Posts: n/a

Default Re: conditional query - 08-24-2011 , 11:21 PM



oh,yes, u'r right. I made a mistake when tried to find some shortcut. how about change the predicate to :

having count(distinct proc) = 1 and count(*) > 1

Reply With Quote
Reply




Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off



Powered by vBulletin Version 3.5.3
Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.