Question: write a sql query for list of campaigns that have a sequence of events with more sell transactions than buy transactions. Transactions are considered in
write a sql query for list of campaigns that have a sequence of events with more sell transactions than buy transactions. Transactions are considered in the order they appear in the table.
A sequence is a number of consecutive transactions with the same campaignid
The result should be in the following format : campaign, netsellscount, netsellstotal.
Results should be sorted descending by netsellstotal.
sample input data
Campaigns
id name
Engineering
Legal
Engineering
Events
Camapignid Type amount
buy
sell
sell
buy
sell
sell
buy
buy
sell
buy
sell
sell
sell
buy
sell
sell
buy
sell
buy
sell
sell
sell
sell
output:
Campaign netsellscount Netsellstotal
Engineering
legal
Explanation:
Transactions are considered in the order they appear.A sequence is a number of consecutive transactions with the same campaignid
A sequence should be reported if the number of sell transactions exceeds the number of buys transactions.
the first sequence in the sample tables is
Campaignid type amount
sell
buy
sell
There are two sell transactions and only one buy transactions, so this sequence is reported. The sell transactions add to and the name is 'Engineering'.
The next sequence has only sell transactions, so it is reported. The sell transactions add to The name is 'Engineering'.
campaignid type amount
sell
sell
The third sequence qualifies with two sells and one buy. The sell transactions sum to and the name is 'Engineering'.
Campaignid type amount
sell
sell
buy
The final sequence does not qualify since it does not have more sell than buy transactions.
campaignid type amount
sell
buy
The qualifying sequences are associated with different campaigns id and
Both campaigns are named Engineering. The total of sell amounts is
I need my output to be this:
Campaign netsellscount Netsellstotal
Engineering
Step by Step Solution
There are 3 Steps involved in it
1 Expert Approved Answer
Step: 1 Unlock
Question Has Been Solved by an Expert!
Get step-by-step solutions from verified subject matter experts
Step: 2 Unlock
Step: 3 Unlock
