IF Customer is NEW then 1 else 0 如果客户是新客户则为1,否则为0。

huangapple go评论71阅读模式
英文:

SQL QUERY: IF Customer is NEW then 1 else 0

问题

我想找到一个查询,让我找到新客户何时有发票,然后将该客户标记为'NEW'为1,然后按EOMONTH(date)分组。

数据库中我有以下列:

日期     
帐户ID   
帐户名称    
收入

最终输出可能如下所示:

日期            帐户ID    帐户名称    收入  状态
30/06/2023      123           123有限公司     £123     1
30/06/2023      1234          1234有限公司    £1000    0 

任何帮助都将不胜感激。

英文:

I would like to find the query that allows me to find when a new customer has an invoice then mark that client as 1 for 'NEW' and then group it by by EOMONTH(date)

The columns I have in the database

Date     
Account_Id   
Account_Name    
Revenue

Final output would look something like this

Date            Account Id    Account Name    Revenue  Status
30/06/2023      123           123 Limited     £123     1
30/06/2023      1234          1234 Limited    £1000    0 

Any help would be really appreciated

答案1

得分: 1

考虑到发票具有以下查询所示的单独表格

select eOmonth(date) Date, Account_Id, Account_Name, sum(Revenue) Revenue
    , (case when (select top 1 1 from Invoices where Invoices.DocumentNumber = customer.Account_Id) = 1 then 1 else 0 end) status
from customer
group by eOmonth(date), Account_Id, Account_Name

如果发票在表格内,按照以下查询

select eOmonth(date) Date, Account_Id, Account_Name, sum(Revenue) Revenue
    , (case when Invoice is not null then 1 else 0 end) status
from customer
group by eOmonth(date), Account_Id, Account_Name
    , (case when Invoice is not null then 1 else 0 end)

根据您的需求使用 CASE 表达式。

英文:

Considered that invoices has separate table follow below query

select eOmonth(date) Date, Account_Id, Account_Name, sum(Revenue) Revenue
    , (case when (select top 1 1 from Invoices where Invoices.DocumentNumber = customer.Account_Id) = 1 then 1 else 0 end) status
from customer
group by eOmonth(date), Account_Id, Account_Name

If invoice is with in the table follow below query

select eOmonth(date) Date, Account_Id, Account_Name, sum(Revenue) Revenue
    , (case when Invoice is not null then 1 else 0 end) status
from customer
group by eOmonth(date), Account_Id, Account_Name
    , (case when Invoice is not null then 1 else 0 end)

Use CASE expression based on your requirements.

huangapple
  • 本文由 发表于 2023年7月6日 14:09:47
  • 转载请务必保留本文链接:https://go.coder-hub.com/76625957.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定