每月、每年和货币的总折扣,不包括 2017 年的数据

用户11254434

我需要知道每月、每年和货币的总折扣,不包括该数据库 2017 年的数据:

Sales table:
PK sale_id : text
FK1 client_id : text
tax_code : text
currency : text
amount : integer
notes : text
created_at : timestamp

Sales_entries table:
PK sale_entry_id : text
FK1 sale_id : text
price : integer
discount : integer
FK2 journey_id : text

我创建了这个查询,但我不知道它是否正确:

Select
Datetrunc(‘month’, a.created_at) as month,
Datetrunc(‘year’, a.created_at) as year,
a.Currency as currency,
Sum(b.discount) as discount
From sales as a
Left join sales_entries as b 
On a.sales_id = b.sale_id
Where year <> 2017
Group by
Month,
Year,
currency
戈登·利诺夫

你的日期算术有点不对劲。我认为这就是你想要的:

select year(s.created_at) as yyyy,
       month(s.created_at) as mm,
       s.Currency as currency,
       sum(se.discount) as discount
from sales s left join
     sales_entries se
     on s.sales_id = se.sale_id
where s.created_at < '2017-01-01' or
      s.created_at >= '2018-01-01'
group by year(s.created_at), month(s.created_at),
         s.currency
order by year(s.created_at), month(s.created_at), s.currency;

请注意,我还用有意义的缩写而不是任意字母替换了表别名。

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章