Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

SQL Syntax

Posted on 2009-05-13
9
Medium Priority
?
189 Views
Last Modified: 2012-05-06
I need to pull data out of a table which has many of the same accountIDs but I need to only get back a count of 1 for each account_id
Here is the syntax I have now and it is pulling back every instance of that accountID. So if accountID 123 is in the table 7 times I want to return 1

Select h.name, COUNT(*)
from document_requests dr
INNER JOIN document_request_accounts dra on dr.id = dra.document_request_id
INNER JOIN hospitals h on dr.hospital_id = h.id
GROUP BY h.name

Please help!!!
0
Comment
Question by:healthcheckinc
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 2
  • 2
  • +2
9 Comments
 
LVL 93

Expert Comment

by:Patrick Matthews
ID: 24380052
Hello healthcheckinc,

Select h.name, 1 AS [Count ]
from document_requests dr
INNER JOIN document_request_accounts dra on dr.id = dra.document_request_id
INNER JOIN hospitals h on dr.hospital_id = h.id
GROUP BY h.name

Regards,

Patrick
0
 

Author Comment

by:healthcheckinc
ID: 24380104
That just gives me a 1 in the column next to the hospital. I need a count of all the account_ids in there but do not need the account duplicates. Heres an example

acctID          hospital
123              ABC hospital
123              ABC hospital
234              ABC Hospital
567              ABC hospital

I would need the result set to look like the following
AcctIDCount    Hospital
3                      ABC Hospital


123 would only count as 1

Hope this helps
0
 
LVL 93

Accepted Solution

by:
Patrick Matthews earned 2000 total points
ID: 24380123
OK, *now* I get it :)

Select h.name, COUNT(DISTINCT acctID)
from document_requests dr
INNER JOIN document_request_accounts dra on dr.id = dra.document_request_id
INNER JOIN hospitals h on dr.hospital_id = h.id
GROUP BY h.name
0
Prepare for your VMware VCP6-DCV exam.

Josh Coen and Jason Langer have prepared the latest edition of VCP study guide. Both authors have been working in the IT field for more than a decade, and both hold VMware certifications. This 163-page guide covers all 10 of the exam blueprint sections.

 

Author Comment

by:healthcheckinc
ID: 24380179
I still am not sure this is working correctly. Could there possibly be another way to write this?
0
 
LVL 57

Expert Comment

by:Raja Jegan R
ID: 24380500
Can you try this one out:

Select COUNT(acctID) as AcctIDCount, h.name
from document_requests dr
INNER JOIN document_request_accounts dra on dr.id = dra.document_request_id
INNER JOIN hospitals h on dr.hospital_id = h.id
GROUP BY h.name

Hope this helps
0
 
LVL 75

Expert Comment

by:Anthony Perkins
ID: 24380656
>>I would need the result set to look like the following
AcctIDCount    Hospital
3                      ABC Hospital
<<
So what happened to 567 ABC Hospital?

rrjegan17,

Not sure if you realize but the solution you posted, except for the alias, is identical to the query in the original question.  COUNT(*) =  COUNT(acctID)
0
 
LVL 9

Expert Comment

by:tculler
ID: 24380665
Alright, first of all, you're grouping by the wrong field. What a GROUP BY clause does is merge ALL matching fields into a single record that share that same field value. However, what exactly do you want to count? Just the total number of entries in the tables, the individual numbers of duplicates of each AccountID or Name, what? I can get you started, but I need more information. Try to be more specific in your question--what EXACTLY do you want to see in your query? How many fields? What should the fields be? What is the schema of the relevant tables?

Instead of grouping by h.name, group by account ID; in other words, ... GROUP BY tableName.AccountID, sustituting names for your names.
0
 
LVL 75

Expert Comment

by:Anthony Perkins
ID: 24380681
Actually on second thoughts matthewspatrick has given you the solution.  Here is the code to prove it:

Declare @Accounts table (
                              acctID varchar(10),
                              name varchar(50))

Insert @Accounts (acctID, name) Values ('123', 'ABC hospital')
Insert @Accounts (acctID, name) Values ('123', 'ABC hospital')
Insert @Accounts (acctID, name) Values ('234', 'ABC hospital')
Insert @Accounts (acctID, name) Values ('567', 'ABC hospital')


Select name, COUNT(DISTINCT acctID)
from @Accounts
GROUP BY name

Output:

name                                              
-------------------------------------------------- -----------
ABC hospital                                       3
0
 

Author Closing Comment

by:healthcheckinc
ID: 31581225
After reviewing the data your post was right. I appreciate it...

tculler - Take a xanax
0

Featured Post

Nothing ever in the clear!

This technical paper will help you implement VMware’s VM encryption as well as implement Veeam encryption which together will achieve the nothing ever in the clear goal. If a bad guy steals VMs, backups or traffic they get nothing.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Ever needed a SQL 2008 Database replicated/mirrored/log shipped on another server but you can't take the downtime inflicted by initial snapshot or disconnect while T-logs are restored or mirror applied? You can use SQL Server Initialize from Backup…
One of the most important things in an application is the query performance. This article intends to give you good tips to improve the performance of your queries.
Using examples as well as descriptions, and references to Books Online, show the documentation available for datatypes, explain the available data types and show how data can be passed into and out of variables.
Viewers will learn how to use the SELECT statement in SQL and will be exposed to the many uses the SELECT statement has.

636 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question