Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations bkrike on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Grouping

Status
Not open for further replies.

fcoomermd

Programmer
Nov 20, 2002
218
CA
I am having difficulty with the following:
I am trying to records when certain conditions apply, for example:
The query is based on 1 table
has the following columns:
Include
Delete
Car
Company
SalesAssociate

I would like to return only the records where
Delete=false, Include=true but... I only want to return records that have multiple results for example:
if the following was the data:
Include Delete Car Company SalesAssociate
true false GM AT Joe
true false GM AT Joe
false true GM AT Joe
true false GM KK Joe
true false GM GG Jan
true false GM KK Mike
true false GM GG Dan


So, the query would only return when Delete=false, Include=true and multiple records when the car company and SalesAssociate all eqaul the same. Therefore the following would be returned:
Include Delete Car Company SalesAssociate
true false GM AT Joe
true false GM AT Joe


Needing help..
Thanks
 
What have you tried so far?

Ignorance of certain subjects is a great part of wisdom
 
How about

select car,compnay,salesassociate,count(*) as count
from table
where include='true' and delete='false'
group by car,company,salesassociate
having count(*) > 1

(Seems to me that the data is the same.. so why
output redundant data?

If that will not necessarily be the case
use

select * from table A inner join table2 B (this is table created above)
on a.car=b.car and a.company=b.company and a.salesassociate=b.salesassociate
where a.include='true' and a.delete='false'
 
Based on exactly what you have I'll modify sagn's query a bit to get this:

Code:
select [Include], [Delete], Car, Compnay, SalesAssociate from table
where [Include]='true' and [Delete]='false'
group by Company, Car, SalesAssociate, [Include], [Delete]
having count(*) > 1
order by Company, Car, SalesAssociate

[small]"Mom........MEATLOAF!!!! F***!!!!"[/small]
<.
 
Monksnake-

That's close, but it will not include the two separate rows that fcoomermd is seeking. You need to join from the original table to the query you have there, as indicated in the fine print at the bottom of sagn's post.

fcoomermd, why the need for the two rows?

Ignorance of certain subjects is a great part of wisdom
 
Ahh, you're right Alex, grouping by all will print all the rows as distinct.

I would do so much better at making these queries up if I had a way to test them first.

[sad]

[small]"Mom........MEATLOAF!!!! F***!!!!"[/small]
<.
 
For a nominal fee, I will show you how to create table variables ;-)

Ignorance of certain subjects is a great part of wisdom
 
I needed that today :)

Ignorance of certain subjects is a great part of wisdom
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top