Mysql 连接时不求和
我想做的是显示我这个月的当前表现,与预期的预定收益进行比较,然后按产品类型显示总预期金额 为了清楚起见,我有两个子产品,我以相同的名称分组 我的问题是,对于我的“收费”金额,它将两个子产品分开,而“预定”金额可以正常工作 该表应该如下所示:Mysql 连接时不求和,mysql,Mysql,我想做的是显示我这个月的当前表现,与预期的预定收益进行比较,然后按产品类型显示总预期金额 为了清楚起见,我有两个子产品,我以相同的名称分组 我的问题是,对于我的“收费”金额,它将两个子产品分开,而“预定”金额可以正常工作 该表应该如下所示: Type | Charged | Scheduled | Expected A 3 2 5 B 1 1 2 实际展示的是: Type | Charged | Sch
Type | Charged | Scheduled | Expected
A 3 2 5
B 1 1 2
实际展示的是:
Type | Charged | Scheduled | Expected
A 2 1 3
A 1 1 2
B 1 1 2
代码如下:
select
t2.product,
t1.Charged,
t2.Scheduled,
t1.charged + t2.scheduled as 'expected'
from(
select
case
when user_type = 'a1' then 'a'
when user_type = 'a2' then 'a'
else 'b'
end as 'Type',
SUM(charged) as 'Scheduled'
from
table
where
month(date) = month(now())
and
year(date) = year(now())
and status like 'scheduled'
group by 1
order by 2 desc) t2 join
(select
case
when user_type = 'a1' then 'a'
when user_type = 'a2' then 'a'
else 'b'
end as 'Type',
sum(charged) as 'Charged'
FROM table
WHERE (status = 'Complete'
AND str_to_date(concat(date_format(date, '%Y-%m'), '-01'), '%Y-%m-%d') = str_to_date(concat(date_format(now(), '%Y-%m'), '-01'), '%Y-%m-%d'))
GROUP BY user_type
ORDER BY user_type ASC) as t1 on t1.type = t2.type
我很感激我可能没有很好地解释这一点(而且我的代码可能相当笨拙-我还是相当新的!),所以任何帮助/指导都将不胜感激
谢谢 只是一些建议
在主选择中有一个列product,但在子查询中有type,而不是product
您不应该在列名周围使用单引号
广告您有按用户类型分组,但您需要按类型分组进行收费
select
t2.type,
t1.Charged,
t2.Scheduled,
t1.charged + t2.scheduled as 'expected'
from(
select
case
when user_type = 'a1' then 'a'
when user_type = 'a2' then 'a'
else 'b'
end as Type,
SUM(charged) as Scheduled
from
table
where
month(date) = month(now())
and
year(date) = year(now())
and status like 'scheduled'
group by 1
order by 2 desc) t2 join
(select
case
when user_type = 'a1' then 'a'
when user_type = 'a2' then 'a'
else 'b'
end as Type,
sum(charged) as Charged
FROM table
WHERE (status = 'Complete'
AND str_to_date(concat(date_format(date, '%Y-%m'), '-01'), '%Y-%m-%d') = str_to_date(concat(date_format(now(), '%Y-%m'), '-01'), '%Y-%m-%d'))
GROUP BY Type
ORDER BY Type ASC) as t1 on t1.type = t2.type
只是一些建议
在主选择中有一个列product,但在子查询中有type,而不是product
您不应该在列名周围使用单引号
广告您有按用户类型分组,但您需要按类型分组进行收费
select
t2.type,
t1.Charged,
t2.Scheduled,
t1.charged + t2.scheduled as 'expected'
from(
select
case
when user_type = 'a1' then 'a'
when user_type = 'a2' then 'a'
else 'b'
end as Type,
SUM(charged) as Scheduled
from
table
where
month(date) = month(now())
and
year(date) = year(now())
and status like 'scheduled'
group by 1
order by 2 desc) t2 join
(select
case
when user_type = 'a1' then 'a'
when user_type = 'a2' then 'a'
else 'b'
end as Type,
sum(charged) as Charged
FROM table
WHERE (status = 'Complete'
AND str_to_date(concat(date_format(date, '%Y-%m'), '-01'), '%Y-%m-%d') = str_to_date(concat(date_format(now(), '%Y-%m'), '-01'), '%Y-%m-%d'))
GROUP BY Type
ORDER BY Type ASC) as t1 on t1.type = t2.type
你的问题我不清楚。。你有错误吗?错误的结果?不清楚您的预期结果。因此,在计划中,a1和a2在A中正确相加,但在充电中,它将它们分开,但将它们都称为Ai已更新我的asnwer。。似乎你有错误的分组依据(用户类型而不是类型)…这很有效!非常感谢你!你的问题我不清楚。。你有错误吗?错误的结果?不清楚您的预期结果。因此,在计划中,a1和a2在A中正确相加,但在充电中,它将它们分开,但将它们都称为Ai已更新我的asnwer。。似乎你有错误的分组依据(用户类型而不是类型)…这很有效!非常感谢你!我不确定我是否完全理解你想说的话。你建议我用什么来代替单引号?在mysql中,引号用于文字字符串值,对于列名,如果不使用引号更好,当你需要复合名称时,例如
My column
或反向使用backtics,我不确定我是否完全理解你的意思。您建议我使用什么来代替单引号?在mysql中,引号用于文字字符串值,对于列名,如果不使用引号更好,并且当您需要复合名称时,例如My column
或反向单词使用backtics