我有兩個表:SQL服務器:集團通過選擇statment給錯誤
PersonTBL
表:包含一個存儲人 的IP地址
ip-to-country
表中的列HostAddress
:包括IP號碼與範圍國家
我試圖計算每個國家的人,但是我收到一個錯誤:
Invalid column name 'CountryName'.
查詢:
SELECT
Count(HostAddress) as TotalNo,
(select [ip-to-country].CountryName
from [ip-to-country]
where ((CAST(PARSENAME(HostAddress, 4) AS Bigint) * 256 * 256 * 256) +
(CAST(PARSENAME(HostAddress, 3) AS INT) * 256 * 256) +
(CAST(PARSENAME(HostAddress, 2) AS INT) * 256) +
CAST(PARSENAME(HostAddress, 1) AS INT))
BETWEEN [ip-to-country].BegingIP AND [ip-to-country].EndIP) AS CountryName
FROM PersonTBL
GROUP BY
CountryName
我也試過:
SELECT
Count(HostAddress) as TotalNo,
(SELECT [ip-to-country].CountryName
FROM [ip-to-country]
WHERE
((CAST(PARSENAME(HostAddress, 4) AS Bigint) * 256 * 256 * 256) +
(CAST(PARSENAME(HostAddress, 3) AS INT) * 256 * 256) +
(CAST(PARSENAME(HostAddress, 2) AS INT) * 256) +
CAST(PARSENAME(HostAddress, 1) AS INT)) BETWEEN [ip-to-country].BegingIP AND [ip-to-country].EndIP) as CountryName
FROM PersonTBL
GROUP BY
(SELECT [ip-to-country].CountryName
FROM [ip-to-country]
WHERE
((CAST(PARSENAME(HostAddress, 4) AS Bigint) * 256 * 256 * 256) +
(CAST(PARSENAME(HostAddress, 3) AS INT) * 256 * 256) +
(CAST(PARSENAME(HostAddress, 2) AS INT) * 256) +
CAST(PARSENAME(HostAddress, 1) AS INT)) BETWEEN [ip-to-country].BegingIP AND [ip-to-country].EndIP) as CountryName
但我收到另一個錯誤:
Cannot use an aggregate or a subquery in an expression used for the group by list of a GROUP BY clause.
任何一個可以通過國家幫助我組的用戶?
此次榮獲」噸工作,你不能像'GROUP BY'那樣使用列別名 – Lamak
哦,是的,對不起。把插入的)放在「as Country」之前。 –