0

How can I get the count of a GROUP BY SQL query?

SELECT     Voter.*, temp100.YadiNo, temp100.CountHouseNo
FROM       temp100 
INNER JOIN Voter ON (temp100.HouseNo = Voter.HouseNo) AND (temp100.YadiNo = Voter.YadiNo)
WHERE      (((temp100.YadiNo) Between " & txtpartno.Text.Trim & " And " & txtpartno1.Text.Trim & ") 
AND        ((temp100.CountHouseNo) Between " & txtfamilymeb.Text.Trim & " And " & txtfamilymeb1.Text.Trim & "))
ORDER BY   voter.houseno

This means I has displayed group by house number. I want the count of this group of house numbers.

4

2 回答 2

0

这只是一个猜测,这将计算每个众议院编号的选民人数,例如:

 SELECT     Voter.houseno, COUNT(*) as CountHouseNo
 FROM       temp100 
 INNER JOIN Voter ON (temp100.HouseNo = Voter.HouseNo) AND (temp100.YadiNo = Voter.YadiNo)
 WHERE      (((temp100.YadiNo) Between " & txtpartno.Text.Trim & " And " &   txtpartno1.Text.Trim & ") 
 AND        ((temp100.CountHouseNo) Between " & txtfamilymeb.Text.Trim & " And " &  txtfamilymeb1.Text.Trim & "))
 GROUP BY   Voter.houseno
 ORDER BY   Voter.houseno

如果你想包括YadiNo(无论这意味着什么),你可以这样做:

 SELECT     Voter.houseno, temp100.YadiNo, COUNT(*) as CountHouseNo
 FROM       temp100 
 INNER JOIN Voter ON (temp100.HouseNo = Voter.HouseNo) AND (temp100.YadiNo = Voter.YadiNo)
 WHERE      (((temp100.YadiNo) Between " & txtpartno.Text.Trim & " And " &   txtpartno1.Text.Trim & ") 
 AND        ((temp100.CountHouseNo) Between " & txtfamilymeb.Text.Trim & " And " &  txtfamilymeb1.Text.Trim & "))
 GROUP BY   Voter.houseno, temp100.YadiNo
 ORDER BY   Voter.houseno

COUNT()是一个聚合函数,通常与GROUP BY子句配对。

请参见此处的示例。

于 2013-11-06T06:01:33.510 回答
0
U can't use group by with order by,
so change order by to group by 
like this

***SELECT     Voter.*, temp100.YadiNo, temp100.CountHouseNo
FROM       temp100 
INNER JOIN Voter ON (temp100.HouseNo = Voter.HouseNo) AND (temp100.YadiNo = Voter.YadiNo)
WHERE      (((temp100.YadiNo) Between " & txtpartno.Text.Trim & " And " & txtpartno1.Text.Trim & ") 
AND        ((temp100.CountHouseNo) Between " & txtfamilymeb.Text.Trim & " And " & txtfamilymeb1.Text.Trim & "))
GROUP BY   voter.houseno***
于 2013-11-06T05:56:40.517 回答