Answered step by step
Verified Expert Solution
Question
1 Approved Answer
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
but im getting this:
Campaign netsellscount Netsellstotal
Engineering
Legal
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started