MS SQL Development
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me

The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.

Go Back   Dev Shed ForumsDatabasesMS SQL Development

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
  #1  
Old June 18th, 2012, 05:03 PM
inou inou is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jun 2012
Posts: 2 inou User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 34 m 43 sec
Reputation Power: 0
Count query help!

Could some one help me with a query to do the below.
I have 2 tables
tblCustomer:
ID1 ID2 Fname Lname
A1 1 Simth M.
A1 2 Bob B.
A2 1 David B.
A3 2 Derek B.
A4 3 Tammy N.
A4 1 Ann J.

tblRequest:
ReID CustID1 CustID2
1 A1 1
2 A1 1
3 A2 1
4 A2 1
5 A3 2
6 A1 1
7 A2 1
8 A4 3
9 A1 2
10 A4 1

How can I get the result below:
CustID1 CustID2 Fname Lname TotalRequests
A1 1 Smith M. 3
A1 2 Bob B. 1
A2 1 David B. 3
A3 2 Derek B. 1
A4 3 Tammy N. 1
A4 1 Ann J. 1
I have tried:
SELECT
A.[Fname],
A.[Lname],
A.[ID1],
A.[ID2],
COUNT(B.[CustID1]) AS TotalRequest
FROM
[tblCustomer] AS A
INNER JOIN
[tblRequest] AS B
ON B.[CustID2] = A.[ID2]
WHERE A.[ID1] = B.[CustID1]
GROUP BY
A.[Fname],
A.[Lname],
A.[ID1],
A.[ID2]
ORDER BY
A.[ID2],
A.[ID1][/indent]But not getting the right results.
Please help!
Thanks in advanced.

Reply With Quote
  #2  
Old June 19th, 2012, 04:27 AM
swampBoogie swampBoogie is offline
Contributing User
Dev Shed Regular (2000 - 2499 posts)
 
Join Date: Jan 2003
Location: Paris Uppland
Posts: 2,350 swampBoogie User rank is Major (30000 - 40000 Reputation Level)swampBoogie User rank is Major (30000 - 40000 Reputation Level)swampBoogie User rank is Major (30000 - 40000 Reputation Level)swampBoogie User rank is Major (30000 - 40000 Reputation Level)swampBoogie User rank is Major (30000 - 40000 Reputation Level)swampBoogie User rank is Major (30000 - 40000 Reputation Level)swampBoogie User rank is Major (30000 - 40000 Reputation Level)swampBoogie User rank is Major (30000 - 40000 Reputation Level)swampBoogie User rank is Major (30000 - 40000 Reputation Level)swampBoogie User rank is Major (30000 - 40000 Reputation Level) 
Time spent in forums: 1 Month 4 Days 7 h 40 m 21 sec
Reputation Power: 390
The query looks alright. In what sense is the result incorrect?

Reply With Quote
  #3  
Old June 19th, 2012, 09:59 AM
inou inou is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jun 2012
Posts: 2 inou User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 34 m 43 sec
Reputation Power: 0
Quote:
Originally Posted by swampBoogie
The query looks alright. In what sense is the result incorrect?

My request tbl contents 900,000 records but it returns only 400K. Something is missing.

Reply With Quote
  #4  
Old June 20th, 2012, 04:43 AM
swampBoogie swampBoogie is offline
Contributing User
Dev Shed Regular (2000 - 2499 posts)
 
Join Date: Jan 2003
Location: Paris Uppland
Posts: 2,350 swampBoogie User rank is Major (30000 - 40000 Reputation Level)swampBoogie User rank is Major (30000 - 40000 Reputation Level)swampBoogie User rank is Major (30000 - 40000 Reputation Level)swampBoogie User rank is Major (30000 - 40000 Reputation Level)swampBoogie User rank is Major (30000 - 40000 Reputation Level)swampBoogie User rank is Major (30000 - 40000 Reputation Level)swampBoogie User rank is Major (30000 - 40000 Reputation Level)swampBoogie User rank is Major (30000 - 40000 Reputation Level)swampBoogie User rank is Major (30000 - 40000 Reputation Level)swampBoogie User rank is Major (30000 - 40000 Reputation Level) 
Time spent in forums: 1 Month 4 Days 7 h 40 m 21 sec
Reputation Power: 390
I don't understand what you mean by missing. Compare with your example, which have 10 rows in the request table but six rows in the result.

Since there are duplicate records, with regard to custid1 and custid2 in the request table these will be aggregated as one row in the result.

Or do you mean that there are 900,000 records in the customer table? If so, what should be shown for customers without requests?

Last edited by swampBoogie : June 20th, 2012 at 04:49 AM.

Reply With Quote
Reply

Viewing: Dev Shed ForumsDatabasesMS SQL Development > Count query help!

Developer Shed Advertisers and Affiliates



Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


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
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump

Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 


Powered by: vBulletin Version 3.0.5
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.

© 2003-2013 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap