使用GROUP BY的SQL子查询中的列名错误无效

时间:2016-09-09 10:13:47

标签: sql group-by subquery

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)

错误:

Invalid column name 's1'.
Invalid column name 's2'.
Invalid column name 's3'.
Invalid column name 's4'.

6 个答案:

答案 0 :(得分:2)

您的查询非常尴尬。您有TOP 1,但没有ORDER BY,这意味着结果不确定。

但是,您可以使用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);

如果这样做但不能达到你想要的效果,那就问一下另一个问题,包括样本数据,想要的结果以及你想要计算的内容的解释。

答案 1 :(得分:1)

您已在组中使用了别名,因此会抛出错误:

group by CepTel , s1 , s2 , s3 , s4

取而代之的是替换原始列

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)

答案 2 :(得分:1)

您不能按别名分组。

使用实际表达式

group by (Select top 1 sa1.SirketAdi From SatisTum sa1 Where s.MusteriNo = sa1.MusteriNo ), 

答案 3 :(得分:0)

试试这个,

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)

答案 4 :(得分:0)

试试这个,

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)

答案 5 :(得分:0)

我认为您正在尝试从同一个表SatisTum获取数据。 您可以像下面一样简化查询。

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);