Please start any new threads on our new
site at https://forums.sqlteam.com. We've got lots of great SQL Server
experts to answer whatever question you can come up with.
| Author |
Topic |
|
dzabor
Posting Yak Master
138 Posts |
Posted - 2010-06-30 : 12:15:46
|
| I have a query below that returns exactly what I need it to, however I would like to add more fields to help the user make decisions abotu the data.Query (returns 55 rows):select na.ID,preferred_bill,count(na.preferred_bill) as CountOfPreferredBillfrom Name_address nagroup by na.preferred_bill,na.idhaving count(na.preferred_bill) > 1 and preferred_bill = 1Results: ID Preferred_Bill CountOfPreferredBill101 1 2102 1 3103 1 2I would like to pull a couple more fields from na. My attempt below returns 17478 rows. I need only the original 55 rows and their additional fields.SELECT ID, purposeFROM name_addressWHERE exists (select na.ID,preferred_bill,count(na.preferred_bill) as CountOfPreferredBillfrom Name_address nagroup by na.preferred_bill,na.idhaving count(na.preferred_bill) > 1 and preferred_bill = 1)Thanks,dz |
|
|
X002548
Not Just a Number
15586 Posts |
Posted - 2010-06-30 : 12:27:38
|
| [code]SELECT * FROM Name_address nao WHERE EXISTS ( SELECT nai.ID,nai.preferred_bill FROM Name_address nai WHERE preferred_bill = 1 AND nai.preferred_bill = nao.preferred_bill AND nai.id = nao.id GROUP BY nai.preferred_bill,nai.id HAVING COUNT(nai.preferred_bill) > 1)[/code]Brett8-)Hint: Want your questions answered fast? Follow the direction in this linkhttp://weblogs.sqlteam.com/brettk/archive/2005/05/25/5276.aspxAdd yourself!http://www.frappr.com/sqlteam |
 |
|
|
dzabor
Posting Yak Master
138 Posts |
Posted - 2010-06-30 : 12:37:01
|
| It worked!I always assumned the select statment had to be able to stand on its own meaning the nao part would break it since it is not part of the select. so I should link the sub query with the main query (Which makes sense when creating reports in Crystal and access) within the sub report. Thanks for the response and would approciate any addtional thoughts - this is awesome!dz |
 |
|
|
X002548
Not Just a Number
15586 Posts |
|
|
|
|
|
|
|