[英]Invalid column name error in SQL sub-query with GROUP BY
SELECT
CepTel,
(SELECT u
top 1 sa1.SirketAdi
FROM
SatisTum sa1
WHERE s.MusteriNo = sa1.MusteriNo) s1,
(SELECT
top 1 sa2.FaturaSahibi
FROM
SatisTum sa2
WHERE s.MusteriNo = sa2.MusteriNo) s2,
(SELECT
top 1 sa3.Ad
FROM
SatisTum sa3
WHERE s.MusteriNo = sa3.MusteriNo) s3,
(SELECT
top 1 sa4.Soyad
FROM
SatisTum sa4
WHERE s.MusteriNo = sa4.MusteriNo) s4,
COUNT(DISTINCT MusteriNo)
FROM
SatisTum s
GROUP BY CepTel,s1,s2,s3,s4
HAVING COUNT(DISTINCT MusteriNo) > 1
ORDER BY COUNT(DISTINCT MusteriNo)
Error: 错误:
Invalid column name 's1'.
Invalid column name 's2'.
Invalid column name 's3'.
Invalid column name 's4'.
Your query is quite awkward. 您的查询很尴尬。 You have
TOP 1
, but there is no ORDER BY
, meaning that the results are indeterminate. 您具有
TOP 1
,但没有ORDER BY
,这意味着结果是不确定的。
However, you can solve the problem using OUTER APPLY
: 但是,您可以使用
OUTER APPLY
解决问题:
Select CepTel, s1.SirketAdi, s2.FaturaSahibi, s3.Ad, s4.Soyad,
Count(Distinct MusteriNo)
From SatisTum s outer apply
(Select top 1 sa1.SirketAdi From SatisTum sa1 Where s.MusteriNo = sa1.MusteriNo
) s1 outer apply
(Select top 1 sa2.FaturaSahibi From SatisTum sa2 Where s.MusteriNo = sa2.MusteriNo
) s2 outer apply
(Select top 1 sa3.Ad From SatisTum sa3 where s.MusteriNo = sa3.MusteriNo
) s3 outer apply
(Select top 1 sa4.Soyad From SatisTum sa4 where s.MusteriNo = sa4.MusteriNo
) s4
group by CepTel, s1.SirketAdi, s2.FaturaSahibi, s3.Ad, s4.Soyad
having Count(Distinct MusteriNo) > 1
order by COUNT(distinct MusteriNo);
If this works but does not do what you want, then ask another question with sample data, desired results, and an explanation of what you are trying to calculate. 如果此方法有效,但不能满足您的要求,请询问另一个问题,并提供示例数据,所需结果以及要计算的内容的解释。
You have used alias in group by so it throw error: 您在group by中使用了别名,因此会引发错误:
group by CepTel , s1 , s2 , s3 , s4
Instead replace original column 而是替换原始列
Select CepTel ,
(Select top 1 sa1.SirketAdi From SatisTum sa1 Where s.MusteriNo = sa1.MusteriNo ) s1,
(Select top 1 sa2.FaturaSahibi From SatisTum sa2 Where s.MusteriNo = sa2.MusteriNo ) s2 ,
(Select top 1 sa3.Ad From SatisTum sa3 where s.MusteriNo = sa3.MusteriNo ) s3,
(Select top 1 sa4.Soyad From SatisTum sa4 where s.MusteriNo = sa4.MusteriNo) s4,
Count(Distinct MusteriNo)
From SatisTum s
group by CepTel ,
(Select top 1 sa1.SirketAdi From SatisTum sa1 Where s.MusteriNo = sa1.MusteriNo ) ,
(Select top 1 sa2.FaturaSahibi From SatisTum sa2 Where s.MusteriNo = sa2.MusteriNo ) ,
(Select top 1 sa3.Ad From SatisTum sa3 where s.MusteriNo = sa3.MusteriNo ),
(Select top 1 sa4.Soyad From SatisTum sa4 where s.MusteriNo = sa4.MusteriNo)
having Count(Distinct MusteriNo) > 1 order by COUNT(distinct MusteriNo)
You cannot group by an alias. 您不能按别名分组。
Use the actual expressions 使用实际表达
group by (Select top 1 sa1.SirketAdi From SatisTum sa1 Where s.MusteriNo = sa1.MusteriNo ),
etc 等等
Try like this, 这样尝试
SELECT T.CepTel
,T.s1
,T.s2
,T.s3
,T.s4
,Count(DISTINCT T.MusteriNo)
FROM (
SELECT CepTel
,(
SELECT TOP 1 sa1.SirketAdi
FROM SatisTum sa1
WHERE s.MusteriNo = sa1.MusteriNo
) s1
,(
SELECT TOP 1 sa2.FaturaSahibi
FROM SatisTum sa2
WHERE s.MusteriNo = sa2.MusteriNo
) s2
,(
SELECT TOP 1 sa3.Ad
FROM SatisTum sa3
WHERE s.MusteriNo = sa3.MusteriNo
) s3
,(
SELECT TOP 1 sa4.Soyad
FROM SatisTum sa4
WHERE s.MusteriNo = sa4.MusteriNo
) s4
,MusteriNo
FROM SatisTum s
) T
GROUP BY T.CepTel
,T.s1
,T.s2
,T.s3
,T.s4
HAVING Count(DISTINCT T.MusteriNo) > 1
ORDER BY COUNT(DISTINCT T.MusteriNo)
Try this, 尝试这个,
Select CepTel ,
MusteriNo,
MAX(s.SirketAdi) s1, -- you are getting random top 1, let it be the max or min!
MAX(s.FaturaSahibi) s2 ,
MAX(s.Ad) s3,
MAX(s.Soyad) s4,
Count(Distinct MusteriNo)
From SatisTum s
group by CepTel ,MusteriNo
having Count(Distinct MusteriNo) > 1
order by COUNT(distinct MusteriNo)
I think you are trying to get data from the same table SatisTum . 我认为您正在尝试从同一张表SatisTum中获取数据。 You can simplify your query like below.
您可以像下面那样简化查询。
SELECT a.CepTel,t.SirketAdi,t.FaturaSahibi,t.Ad,t.Soyad,Count(Distinct a.MusteriNo)
FROM SatisTum a
OUTER APPLY (SELECT top 1 b.SirketAdi,b.FaturaSahibi,b.Ad,b.Soyad FROM SatisTum b WHERE a.MusteriNo = b.MusteriNo )t
GROUP BY CepTel,t.SirketAdi,t.FaturaSahibi,t.Ad,t.Soyad
HAVING Count(DISTINCT a.MusteriNo) > 1
ORDER BY COUNT(DISTINCT a.MusteriNo);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.