Resend my activation email : Register : Log in 
BCF: Bike Chat Forums


simple SQL query is giving me a headache! (help ;) )

Reply to topic
Bike Chat Forums Index -> The Geek Zone
View previous topic : View next topic  
Author Message

feef
Energiser Bunny



Joined: 11 Feb 2002
Karma :

PostPosted: 16:00 - 23 Feb 2006    Post subject: simple SQL query is giving me a headache! (help ;) ) Reply with quote

this shoud be simple, but my brain's hurting.

I have a table with 3 fields...

IDnum,content,application

eg
123,stop,fb
123,start,fb
123,continue,fb
123,fault,fb
123,start,gt
123,stop,gt
123,continue,gt
123,start,ha
321,stop,fb
321,start,fb
321,continue,fb
321,fault,fb
321,start,gt


I need to know All those unique IDs that have "start" against them, but have not got "stop" against them, I'm not interested in the other content strings.

I need to query it based on a specific application ID.

there will be many records from each IDnum, but only one start for each. I need to know all those IDnums that have "started" and not "stopped"

this is on SQL Server.

tia

a
____________________
Mudskipper wrote: feef, that is such a beautiful post that it gave me a lady tingle Laughing
Windchill calculator - London Bike parking
Blog and stuff - PlentyMoreFish dating
 Back to top
View user's profile Send private message You must be logged in to rate posts

veeeffarr
Super Spammer



Joined: 22 Jul 2004
Karma :

PostPosted: 16:05 - 23 Feb 2006    Post subject: Reply with quote

Is IDNum Primary?

That list is an example of the values in your table?

So basically you want to know how many have started?
 Back to top
View user's profile Send private message You must be logged in to rate posts

Kickstart
The Oracle



Joined: 04 Feb 2002
Karma :

PostPosted: 16:06 - 23 Feb 2006    Post subject: Reply with quote

select distinct idnum
from blah
where content = "start"
and idnum not in (select idnum from blah where content = "stop")

Quick and nasty way of getting a list.

All the best

Keith
____________________
Traxpics, track day and racing photographs - Bimota Forum - Bike performance / thrust graphs for choosing gearing
 Back to top
View user's profile Send private message Send e-mail You must be logged in to rate posts

feef
Energiser Bunny



Joined: 11 Feb 2002
Karma :

PostPosted: 16:09 - 23 Feb 2006    Post subject: Reply with quote

Toby R wrote:
Is IDNum Primary?

That list is an example of the values in your table?

So basically you want to know how many have started?


how can idnum be primary with multiple duplicate entries Wink ? :p

a
____________________
Mudskipper wrote: feef, that is such a beautiful post that it gave me a lady tingle Laughing
Windchill calculator - London Bike parking
Blog and stuff - PlentyMoreFish dating
 Back to top
View user's profile Send private message You must be logged in to rate posts

feef
Energiser Bunny



Joined: 11 Feb 2002
Karma :

PostPosted: 16:12 - 23 Feb 2006    Post subject: Reply with quote

Kickstart wrote:
select distinct idnum
from blah
where content = "start"
and idnum not in (select idnum from blah where content = "stop")

Quick and nasty way of getting a list.

All the best

Keith


I'd rather avoid a sub-select in a table this size...

in this case a subselect is nasty, but not quick Wink

a
____________________
Mudskipper wrote: feef, that is such a beautiful post that it gave me a lady tingle Laughing
Windchill calculator - London Bike parking
Blog and stuff - PlentyMoreFish dating


Last edited by feef on 16:12 - 23 Feb 2006; edited 1 time in total
 Back to top
View user's profile Send private message You must be logged in to rate posts

veeeffarr
Super Spammer



Joined: 22 Jul 2004
Karma :

PostPosted: 16:12 - 23 Feb 2006    Post subject: Reply with quote

Sorry mate,

I didn't read properly, Keith's should work.
 Back to top
View user's profile Send private message You must be logged in to rate posts

Kickstart
The Oracle



Joined: 04 Feb 2002
Karma :

PostPosted: 16:18 - 23 Feb 2006    Post subject: Reply with quote

Hi

Not sure if you could play around with a left outer join, joining the table to itself on the idnum with one having start, the other stop, and then ignore the ones where it does find a match.

Failing that you could make the subselect more specific.

All the best

Keith
____________________
Traxpics, track day and racing photographs - Bimota Forum - Bike performance / thrust graphs for choosing gearing
 Back to top
View user's profile Send private message Send e-mail You must be logged in to rate posts

Kickstart
The Oracle



Joined: 04 Feb 2002
Karma :

PostPosted: 16:23 - 23 Feb 2006    Post subject: Reply with quote

Hi

Not tried this, but something like it might work (too many versions of SQL, think this works in some but not something I have used)

select *
from (select a.idnum subIdnum, a.content subContentA, b.content subContentB
from blah a left outer join blah b
on a.idnum = b.idnum
where a.content = "start"
and b.content = "stopped")
where subContentB is null

All the best

Keith
____________________
Traxpics, track day and racing photographs - Bimota Forum - Bike performance / thrust graphs for choosing gearing
 Back to top
View user's profile Send private message Send e-mail You must be logged in to rate posts

feef
Energiser Bunny



Joined: 11 Feb 2002
Karma :

PostPosted: 16:28 - 23 Feb 2006    Post subject: Reply with quote

Kickstart wrote:
Hi

Not sure if you could play around with a left outer join, joining the table to itself on the idnum with one having start, the other stop, and then ignore the ones where it does find a match.


that's when my head started hurting Wink

a
____________________
Mudskipper wrote: feef, that is such a beautiful post that it gave me a lady tingle Laughing
Windchill calculator - London Bike parking
Blog and stuff - PlentyMoreFish dating
 Back to top
View user's profile Send private message You must be logged in to rate posts

Kickstart
The Oracle



Joined: 04 Feb 2002
Karma :

PostPosted: 16:31 - 23 Feb 2006    Post subject: Reply with quote

Hi

Try the example. Be interested to know if it works (suspect it would in Oracle).

All the best

Keith
____________________
Traxpics, track day and racing photographs - Bimota Forum - Bike performance / thrust graphs for choosing gearing
 Back to top
View user's profile Send private message Send e-mail You must be logged in to rate posts

veeeffarr
Super Spammer



Joined: 22 Jul 2004
Karma :

PostPosted: 16:32 - 23 Feb 2006    Post subject: Reply with quote

I'd use the subselect unless speed really is an issue.

Unless you really want to be arsed with joins Smile
 Back to top
View user's profile Send private message You must be logged in to rate posts

feef
Energiser Bunny



Joined: 11 Feb 2002
Karma :

PostPosted: 16:47 - 23 Feb 2006    Post subject: Reply with quote

Toby R wrote:
I'd use the subselect unless speed really is an issue.

Unless you really want to be arsed with joins Smile


speed is in issue when the last subselect query I ran took a day and a half to run.

Very Happy

I think i've solved it tho, having found from the developer that there is another check I can do on the preceding data to narrow down the search further...

I'll go kick his ass later.

a
____________________
Mudskipper wrote: feef, that is such a beautiful post that it gave me a lady tingle Laughing
Windchill calculator - London Bike parking
Blog and stuff - PlentyMoreFish dating
 Back to top
View user's profile Send private message You must be logged in to rate posts

Kickstart
The Oracle



Joined: 04 Feb 2002
Karma :

PostPosted: 16:48 - 23 Feb 2006    Post subject: Reply with quote

Hi

Have you tried the join example?

Just interested in whether it would work.

All the best

Keith
____________________
Traxpics, track day and racing photographs - Bimota Forum - Bike performance / thrust graphs for choosing gearing
 Back to top
View user's profile Send private message Send e-mail You must be logged in to rate posts

feef
Energiser Bunny



Joined: 11 Feb 2002
Karma :

PostPosted: 17:08 - 23 Feb 2006    Post subject: Reply with quote

Kickstart wrote:
Hi

Try the example. Be interested to know if it works (suspect it would in Oracle).

All the best

Keith


the bit...

from (select a.idnum subIdnum, a.content subContentA, b.content subContentB
from blah a left outer join blah b
on a.idnum = b.idnum
where a.content = "start"
and b.content = "stopped")

works.. but you can't select from a select like you can in oracle.

at least it doesn't work in it's current guise.

Still.. by giving me botht he start and stop messages, that actually gives me more info.. it also shows that there have been multiple start messages sent in some case.. providing us with some useful info we hadn't thought about

thnks

a
____________________
Mudskipper wrote: feef, that is such a beautiful post that it gave me a lady tingle Laughing
Windchill calculator - London Bike parking
Blog and stuff - PlentyMoreFish dating
 Back to top
View user's profile Send private message You must be logged in to rate posts

Kickstart
The Oracle



Joined: 04 Feb 2002
Karma :

PostPosted: 17:27 - 23 Feb 2006    Post subject: Reply with quote

Hi

Interesting, something Oracle manages to do!

If the SQL is needed for a program then at least it is useable.

All the best

Keith
____________________
Traxpics, track day and racing photographs - Bimota Forum - Bike performance / thrust graphs for choosing gearing
 Back to top
View user's profile Send private message Send e-mail You must be logged in to rate posts

feef
Energiser Bunny



Joined: 11 Feb 2002
Karma :

PostPosted: 17:47 - 23 Feb 2006    Post subject: Reply with quote

Kickstart wrote:
Hi

Interesting, something Oracle manages to do!

If the SQL is needed for a program then at least it is useable.

All the best

Keith


it's a one off request for stats info..

thanks

a
____________________
Mudskipper wrote: feef, that is such a beautiful post that it gave me a lady tingle Laughing
Windchill calculator - London Bike parking
Blog and stuff - PlentyMoreFish dating
 Back to top
View user's profile Send private message You must be logged in to rate posts
Old Thread Alert!

The last post was made 18 years, 64 days ago. Instead of replying here, would creating a new thread be more useful?
  Display posts from previous:   
This page may contain affiliate links, which means we may earn a small commission if a visitor clicks through and makes a purchase. By clicking on an affiliate link, you accept that third-party cookies will be set.

Post new topic   Reply to topic    Bike Chat Forums Index -> The Geek Zone All times are GMT + 1 Hour
Page 1 of 1

 
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum
You cannot attach files in this forum
You cannot download files in this forum

Read the Terms of Use! - Powered by phpBB © phpBB Group
 

Debug Mode: ON - Server: birks (www) - Page Generation Time: 0.10 Sec - Server Load: 0.7 - MySQL Queries: 17 - Page Size: 104.51 Kb