子查询没有按照我想要的方式工作

Subquery not working the way i want it to

这是我第一次 post 在 Whosebug 上。

我想了解每个 marital_status 每个教育水平的最低、平均和最高工资是多少。 我试过这段代码,但它只对所有不同教育水平的最小值、平均值和最大值显示相同的值:

Select distinct education, (select min(convert(int, income))
                            from [dbo].[marketing_campaign] where marital_status = 'Married' and year_birth > 1980 and income <> 0) Min_Married,
                            (select avg(convert(int, income))
                            from [dbo].[marketing_campaign] where marital_status = 'Married' and year_birth > 1980 and income <> 0) Avg_Married,
                            (select max(convert(int, income))
                            from [dbo].[marketing_campaign] where marital_status = 'Married' and year_birth > 1980 and income <> 0) Max_Married,
                            (select min(convert(int, income))
                            from [dbo].[marketing_campaign] where marital_status = 'Single' and year_birth > 1980 and income <> 0) Min_Single,
                            (select avg(convert(int, income))
                            from [dbo].[marketing_campaign] where marital_status = 'Single' and year_birth > 1980 and income <> 0) Avg_Single,
                            (select max(convert(int, income))
                            from [dbo].[marketing_campaign] where marital_status = 'Single' and year_birth > 1980 and income <> 0) Max_Single,
                            (select min(convert(int, income))
                            from [dbo].[marketing_campaign] where marital_status = 'together' and year_birth > 1980 and income <> 0) Min_together,
                            (select avg(convert(int, income))
                            from [dbo].[marketing_campaign] where marital_status = 'together' and year_birth > 1980 and income <> 0) Avg_together,
                            (select max(convert(int, income))
                            from [dbo].[marketing_campaign] where marital_status = 'together' and year_birth > 1980 and income <> 0) Max_together
from [dbo].[marketing_campaign]
where  education in ('master', 'Graduation', 'phd', 'basic')
group by education
order by education desc

提前致谢!

在你的子查询中与education没有关联,当我怀疑你需要的是以下内容时,也没有必要重复使用子查询:

with i as (
    select education, 
        Iif(marital_status = 'Married' , Convert(int, income),0) Income_Married,
        Iif(marital_status = 'Single'  , Convert(int, income),0) Income_Single,
        Iif(marital_status = 'together', Convert(int, income),0) Income_Together,
    from dbo.marketing_campaign
    where education in ('master', 'Graduation', 'phd', 'basic')
    and year_birth > 1980 and income != 0
)
select education, 
    Min(Income_Married) Min_Married,
    Avg(Income_Married) Avg_Married,
    Max(Income_Married) Max_Married,
    Min(Income_Single) Min_Single,
    Avg(Income_Single) Avg_Single,
    Max(Income_Single) Max_Single,
    Min(Income_Together) Min_Together,
    Avg(Income_Together) Avg_Together,
    Max(Income_Together) Max_Together
from i
group by education
order by education desc;

您可以简单地按照以下步骤进行操作。我尝试制作与您在我的数据库中描述的相同的环境,希望这对您有所帮助。

SELECT   DISTINCT C.education,
         MIN(income) OVER(PARTITION BY education) AS MIN_Married, 
         AVG(income) OVER(PARTITION BY education) AS AVG_Married, 
         MAX(income) OVER(PARTITION BY education) AS MAX_Married 
FROM     [dbo].[marketing_campaign] C
WHERE    education IN ('master', 'Graduation', 'phd', 'basic')
         AND marital_status = 'Married'
         AND year_birth > 1980
ORDER BY education DESC

使用条件聚合:

SELECT education, 
       MIN(CASE WHEN marital_status = 'Married' AND year_birth > 1980 AND income <> 0) THEN CONVERT(INT, income) END) Min_Married,
       AVG(CASE WHEN marital_status = 'Married' AND year_birth > 1980 AND income <> 0) THEN CONVERT(INT, income) END) Avg_Married,
       MAX(CASE WHEN marital_status = 'Married' AND year_birth > 1980 AND income <> 0) THEN CONVERT(INT, income) END)  Max_Married,
       MIN(CASE WHEN marital_status = 'Single' AND year_birth > 1980 AND income <> 0) THEN CONVERT(INT, income) END) Min_Single,
       AVG(CASE WHEN marital_status = 'Single' AND year_birth > 1980 AND income <> 0) THEN CONVERT(INT, income) END) Avg_Single,
       MAX(CASE WHEN marital_status = 'Single' AND year_birth > 1980 AND income <> 0) THEN CONVERT(INT, income) END) Max_Single,
       MIN(CASE WHEN marital_status = 'together' AND year_birth > 1980 AND income <> 0) THEN CONVERT(INT, income) END) Min_together,
       AVG(CASE WHEN marital_status = 'together' AND year_birth > 1980 AND income <> 0) THEN CONVERT(INT, income) END) Avg_together,
       MAX(CASE WHEN marital_status = 'together' AND year_birth > 1980 AND income <> 0) THEN CONVERT(INT, income) END) Max_together
FROM [dbo].[marketing_campaign]
WHERE education IN ('master', 'Graduation', 'phd', 'basic')
GROUP BY education
ORDER BY education DESC;

或者,将通用条件移动到WHERE子句中:

SELECT education, 
       MIN(CASE WHEN marital_status = 'Married' THEN CONVERT(INT, income) END) Min_Married,
       AVG(CASE WHEN marital_status = 'Married' THEN CONVERT(INT, income) END) Avg_Married,
       MAX(CASE WHEN marital_status = 'Married' THEN CONVERT(INT, income) END)  Max_Married,
       MIN(CASE WHEN marital_status = 'Single' THEN CONVERT(INT, income) END) Min_Single,
       AVG(CASE WHEN marital_status = 'Single' THEN CONVERT(INT, income) END) Avg_Single,
       MAX(CASE WHEN marital_status = 'Single' THEN CONVERT(INT, income) END) Max_Single,
       MIN(CASE WHEN marital_status = 'together' THEN CONVERT(INT, income) END) Min_together,
       AVG(CASE WHEN marital_status = 'together' THEN CONVERT(INT, income) END) Avg_together,
       MAX(CASE WHEN marital_status = 'together' THEN CONVERT(INT, income) END) Max_together
FROM [dbo].[marketing_campaign]
WHERE education IN ('master', 'Graduation', 'phd', 'basic')
  AND year_birth > 1980 AND income <> 0
GROUP BY education
ORDER BY education DESC;