dbTalk Databases Forums  

I Need help with a query...

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


Discuss I Need help with a query... in the comp.databases.ms-sqlserver forum.



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

Default I Need help with a query... - 04-05-2011 , 02:30 AM






hello.
I need help with a query please...
I've a table like this

Id | Contr | Type

the content of this table is like

Id | Contract | Type
1 | 1250 | W
2 | 1250 | T
3 | 5555 | W
4 | 6666 | T
5 | 7777 | W
6 | 6666 | W
7 | 6666 | W
8 | 6666 | T
9 | 7777 | W
10 | 1250 | W
11 | 1250 | T
12 | 1250 | W
13 | 1250 | T
14 | 1250 | T

the key is the ID

I need to extract all the "Contr" where type is not "T"
so i should have as result
3 | 5555 | W
5 | 7777 | W

whenever i found a type "T", I have to exclude that number of "contr".

I'm able to exclude all the records where type is not "T", but if I
found type ="T", that contr goes into results because of the ID
field...

any idea of how I can do this ?

thanks in advance

p.s.sorry for my english, I'm Italian. I really hope you can
understand me...

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

Default Re: I Need help with a query... - 04-05-2011 , 03:09 AM






On 05-04-2011 09:30, Alfaking wrote:
Quote:
hello.

I'm able to exclude all the records where type is not "T", but if I
found type ="T", that contr goes into results because of the ID
field...
Please post the query that you have used.

Quote:
any idea of how I can do this ?


thanks in advance

p.s.sorry for my english, I'm Italian. I really hope you can
understand me...

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

Default Re: I Need help with a query... - 04-05-2011 , 03:54 AM



On 5 Apr, 10:09, Henk van den Berg <hvandenb... (AT) xs4all (DOT) nl> wrote:

Quote:
Please post the query that you have used.
SELECT contr FROM table1
WHERE (contr NOT IN
(SELECT contr
FROM table1
WHERE (type = 'T')
GROUP BY contr)

In my idea this should be ok, but when I double-check the results,
something goes wrong...
no idea why

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

Default Re: I Need help with a query... - 04-05-2011 , 03:59 AM



Forgot to ask the following, which we need as well:
Can you provide us with a CREATE TABLE script and a INSERT INTO script?

On 05-04-2011 10:54, Alfaking wrote:
Quote:
On 5 Apr, 10:09, Henk van den Berg<hvandenb... (AT) xs4all (DOT) nl> wrote:

Please post the query that you have used.

SELECT contr FROM table1
WHERE (contr NOT IN
(SELECT contr
FROM table1
WHERE (type = 'T')
GROUP BY contr)

In my idea this should be ok, but when I double-check the results,
something goes wrong...
no idea why

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

Default Re: I Need help with a query... - 04-05-2011 , 04:14 AM



On 5 Apr, 10:59, Henk van den Berg <hvandenb... (AT) xs4all (DOT) nl> wrote:
Quote:
Forgot to ask the following, which we need as well:
Can you provide us with a CREATE TABLE script and a INSERT INTO script?
I'm sorry, I can't...
that table is created by a software, and I need to extract some data
from a table that is already in place ....

suppose that I have that table, with data very similar to those I
wrote...

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

Default Re: I Need help with a query... - 04-05-2011 , 04:29 AM



Okay, I'll work with that.

On 05-04-2011 11:14, Alfaking wrote:
Quote:
On 5 Apr, 10:59, Henk van den Berg<hvandenb... (AT) xs4all (DOT) nl> wrote:
Forgot to ask the following, which we need as well:
Can you provide us with a CREATE TABLE script and a INSERT INTO script?

I'm sorry, I can't...
that table is created by a software, and I need to extract some data
from a table that is already in place ....

suppose that I have that table, with data very similar to those I
wrote...

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

Default Re: I Need help with a query... - 04-05-2011 , 04:39 AM



On 05-04-2011 10:54, Alfaking wrote:
Quote:
On 5 Apr, 10:09, Henk van den Berg<hvandenb... (AT) xs4all (DOT) nl> wrote:

Please post the query that you have used.

SELECT contr FROM table1
WHERE (contr NOT IN
(SELECT contr
FROM table1
WHERE (type = 'T')
GROUP BY contr)

In my idea this should be ok, but when I double-check the results,
something goes wrong...
no idea why

SELECT DISTINCT contr FROM table1 WHERE type <> 'T'
should do the trick

SELECT contr FROM table1 WHERE type <> 'T' GROUP BY contr
as an alternative

Reply With Quote
  #8  
Old   
Lutz Uhlmann
 
Posts: n/a

Default Re: I Need help with a query... - 04-05-2011 , 04:50 AM



Am 05.04.2011 10:54, schrieb Alfaking:
Quote:
On 5 Apr, 10:09, Henk van den Berg<hvandenb... (AT) xs4all (DOT) nl> wrote:

Please post the query that you have used.

In my idea this should be ok, but when I double-check the results,
something goes wrong...
no idea why
When I run

SELECT *
FROM dbo.table1
WHERE contr NOT IN (SELECT contr FROM dbo.table1 WHERE (type = 'T')
GROUP BY contr)

i get this:
ID Contr Type
3 5555 W
5 7777 W
9 7777 W

I think this is the correct result!

Anyway there is also an alternative solution

SELECT t1.*
FROM dbo.table1 as t1 LEFT JOIN (SELECT contr FROM dbo.table1 WHERE
(type = 'T') GROUP BY contr) AS t2 ON t1.contr=t2.contr
WHERE t2.contr IS NULL

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

Default Re: I Need help with a query... - 04-05-2011 , 05:13 AM



On 05-04-2011 10:54, Alfaking wrote:

Quote:
SELECT contr FROM table1
WHERE (contr NOT IN
(SELECT contr
FROM table1
WHERE (type = 'T')
GROUP BY contr)

In my idea this should be ok, but when I double-check the results,
something goes wrong...
no idea why
It's one of those cases where there's a subtle difference between

"give me everything where condition <X> applies"

yields a different result from

"leave out everything where condition <X> does not apply"

In other words, all squares are geometric shapes, but not all geometric
shapes are squares.


If you

SELECT contr FROM table1
WHERE (contr IN
(SELECT contr
FROM table1
WHERE (type <> 'T')
GROUP BY contr)


you will get the expected results.

Note the difference from your version:
WHERE (contr NOT IN ... type = 'T' )

as opposed to
WHERE ( contr IN ... type <> 'T' )


Still, the simple version is preferable

SELECT DISTINCT contr FROM table1 WHERE type <> 'T'

Reply With Quote
  #10  
Old   
Alfaking
 
Posts: n/a

Default Re: I Need help with a query... - 04-05-2011 , 05:57 AM



On 5 Apr, 12:13, Henk van den Berg <hvandenb... (AT) xs4all (DOT) nl> wrote:

Quote:
If you

SELECT * * contr FROM * * * table1
* WHERE * * (contr *IN
* (SELECT * * contr
* FROM * * * * *table1
* WHERE * * *(type <> 'T')
* GROUP BY contr)

you will get the expected results.
Not sure about this...
if I

Quote:
SELECT contr
FROM table1
WHERE (type <> 'T')
i get also a number of "contr" where type='T', because of the
different ID (that's the key).
for example, in the results I have also

1250 because of the first line of the table...
but that's not correct !!!

Quote:
Still, the simple version is preferable
SELECT DISTINCT contr FROM table1 WHERE type <> 'T'
again, wrong results...

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.