0

I have a Mysql query that selects some columns from 2 tables with an inner join. it is supposed to count the "Y" values from one table column and rturn this count with a Group By.

This is my query:

SELECT (SELECT Count(tblinterview.Contacted) FROM tblinterview WHERE tblinterview.Contacted = "Y") 
as Contact_Y , tbldata.RegionID, tbldata.BranchCode, tbldata.BranchDesc, tbldata.CampaignID,      tblinterview.EndTime      
FROM tbldata INNER JOIN tblinterview 
WHERE tblinterview.DataID = tbldata.DataId 
AND tblinterview.Complete = "Y"
GROUP BY tbldata.BranchCode, tbldata.RegionID, tblinterview.EndTime

By my result set is: Where all the Contact_Y are the total count of all records instead of a grouping count.

Contact_Y      RegionID        BranchCode           BranchDesc     CampaignID   EndTime
1407            1           9725            Dada Motors        31           2012-01-26 12:51:12
1407            1           9725            Dada Motors        31           2012-01-31 10:25:18
1407            1           9725            Dada Motors        31           2012-02-01 11:30:37
1407            1           9725            Dada Motors        31           2012-02-01 14:48:57
1407            3           9732            Ballito Autohaus   37       2012-01-16 15:04:12
1407            3           9732            Ballito Autohaus   46           2012-01-18 11:59:53
1407            3           9732            Ballito Autohaus   31           2012-01-20 14:26:12
1407            3           9732            Ballito Autohaus   31           2012-01-20 16:05:48 

Could someone explain where my query is wrong?

4

2 に答える 2

4

Give this a try

SELECT   (SELECT Count(s.Contacted) 
        FROM tblinterview s
        WHERE s.Contacted = "Y" AND 
        tbldata .DataID = s.DataID) as Contact_Y , 
        tbldata.RegionID, 
        tbldata.BranchCode, 
        tbldata.BranchDesc, 
        tbldata.CampaignID,      
        tblinterview.EndTime      
FROM    tbldata 
        INNER JOIN tblinterview  ON tblinterview.DataID = tbldata.DataId 
WHERE   tblinterview.Complete = "Y"
GROUP BY tbldata.BranchCode, tbldata.RegionID, tblinterview.EndTime
于 2013-02-05T12:47:29.783 に答える
1
SELECT
  Count(tblinterview.Contacted) as Contact_Y,
  tbldata.RegionID,
  tbldata.BranchCode,
  tbldata.BranchDesc,
  tbldata.CampaignID,
  tblinterview.EndTime
FROM
  tbldata INNER JOIN tblinterview
  ON (tblinterview.DataID = tbldata.DataId
      AND tblinterview.Contacted = "Y")
GROUP BY
  tbldata.BranchCode,
  tbldata.RegionID,tblinterview.EndTime
于 2013-02-05T12:45:44.627 に答える