If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

 
Go Back  dBforums > Database Server Software > MySQL > Problem to combine distinct with count and join

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 03-26-06, 09:46
benanat benanat is offline
Registered User
 
Join Date: Mar 2006
Posts: 7
Problem to combine distinct with count and join

hi

i have two tables with shared ID, the shared id in TABLE A is primery key but in TABLE B it is not primery key, and in TABLE B there are more then one record with the shared key.

i need just the COUNT and SUM of tow of the fields in Table A but its have to be records that the key of them exist in Table B

with normal query i have no problem to use DISTINCT and solve this like this
**********************************************
SELECT
DISTINCT
TableA.ID
FROM
TableA,
TableB
WHERE
(TableA.ID = TableB.ID)
*********************************************
this return only one record even that there are two
*********************************************

but what i need is not the records, i need just the COUNT and SUM of tow of the fields in Table A but its have to be records that the key of them exist in Table B, and the problem is that the distinct doesn't effect the count and sum functions even that its only on fields from table A

the query of the count looks like this:
*********************************************

SELECT
DISTINCT
COUNT(TableA.ID) AS NumOfOrders,
SUM(TableA.TotalPrice) AS Total
FROM
TableA,
TableB
WHERE
(TableA.ID = TableB.ID)
***************************************
this one return counting and sum of the same record twice just because the id appear two times in table b, the distinct doesn't effect it
***************************************

does any one have an idea how to solve this with out changing the structure or the information of the tables???

thanks
jonathan
Reply With Quote
  #2 (permalink)  
Old 03-26-06, 17:24
r937 r937 is offline
SQL Consultant
 
Join Date: Apr 2002
Location: Toronto, Canada
Posts: 19,534
Code:
SELECT COUNT(TableA.ID) AS NumOfOrders
     , SUM(TableA.TotalPrice) AS Total
  FROM TableA
 where exists
       ( select 937
           from TableB
          WHERE ID = TableA.ID )
__________________
r937.com | rudy.ca
please visit Simply SQL and buy my book
Reply With Quote
  #3 (permalink)  
Old 03-27-06, 03:53
benanat benanat is offline
Registered User
 
Join Date: Mar 2006
Posts: 7
not working

it seems that any way i am trying to run it i am having an error.
is it possible that mysql ver is too low i am using 3.23.58????
Reply With Quote
  #4 (permalink)  
Old 03-27-06, 07:07
r937 r937 is offline
SQL Consultant
 
Join Date: Apr 2002
Location: Toronto, Canada
Posts: 19,534
it is more than just possible, it is certain
__________________
r937.com | rudy.ca
please visit Simply SQL and buy my book
Reply With Quote
  #5 (permalink)  
Old 03-27-06, 07:16
benanat benanat is offline
Registered User
 
Join Date: Mar 2006
Posts: 7
ok but...

but is there any way to get what i want with my mysql version???
Reply With Quote
  #6 (permalink)  
Old 03-27-06, 09:01
r937 r937 is offline
SQL Consultant
 
Join Date: Apr 2002
Location: Toronto, Canada
Posts: 19,534
not in one query, no
__________________
r937.com | rudy.ca
please visit Simply SQL and buy my book
Reply With Quote
  #7 (permalink)  
Old 03-27-06, 09:06
benanat benanat is offline
Registered User
 
Join Date: Mar 2006
Posts: 7
ok so....

thank you very much any way...


Reply With Quote
Reply

Thread Tools Search this Thread
Search this Thread:

Advanced Search
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

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On