SQL Server SQL QUERY: IF Customer is NEW then 1 else 0 [closed]

bf1o4zei  于 2023-08-02  发布在  其他
关注(0)|答案(1)|浏览(110)

Closed. This question needs details or clarity . It is not currently accepting answers.

Want to improve this question? Add details and clarify the problem by editing this post .

Closed 27 days ago.
Improve this question

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

e37o9pze

e37o9pze1#

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.

相关问题