编写查询来计算平均收入但是按错误获取组?

问题描述 投票:1回答:2

表格1:

create table Program_T
    (AccountName varchar(150) not null unique,
    ProgramID int not null,
    Revenue int,
    Advocates int,
    Shares int,
    Conversions int,
    Impressions int,
    LaunchDate date,
    CSMID int not null,
    constraint Program_PK primary key (AccountName, CSMID),
    constraint Program_FK1 foreign key (AccountName) references Account_T(AccountName),
    constraint Program_FK2 foreign key (CSMID) references CSM_T(CSMID));

表#2:

create table Account_T
    (AccountName varchar(150) not null unique,
    Health varchar(10) not null,
    EcommercePlatform varchar(50),
    CSMID int not null,
    Industry varchar(50),
    Amount int not null,
    constraint Accounts_PK primary key (AccountName),
    constraint Accounts_FK foreign key (CSMID) references CSM_T(CSMID));

我正在尝试编写一个查询,它将为我们提供每个电子商务平台订购的平均收入。

到目前为止我有....

Select Revenue, EcommercePlatform
From Program_T, Account_T
Where Avg(Revenue)
Order by EcommercePlatform;

但我收到的错误是:

无效使用组功能“但我甚至没有使用组功能。

请指教

mysql sql subquery
2个回答
0
投票

为此,代码应如下所示:

Select Avg(Revenue), EcommercePlatform
From Program_T inner join Account_T on Program_T.AccountName=Account_T.AccountName
group by EcommercePlatform;

这应该工作得很好。


0
投票

我想你想要:

select a.EcommercePlatform, Avg(p.Revenue)
grom Program_T p join
     Account_T a
     using (accountName)
group by a.EcommercePlatform;

笔记:

  • 学习使用正确的,明确的,标准的JOIN语法。
  • 切勿在FROM条款中使用逗号。
  • 限定所有列引用,尤其是在查询中引用了多个表时。
  • 要选择的列位于select子句中,而不是where子句。
© www.soinside.com 2019 - 2024. All rights reserved.