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 campaign
id
The result should be in the following format : campaign, netsells
count, netsells
total.
Results should be sorted descending by netsells
total.
sample input data
Campaigns
id name
Engineering
Legal
Engineering
Events
Camapign
id 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 netsells
count Netsells
total
Engineering
legal
Explanation:
Transactions are considered in the order they appear.A sequence is a number of consecutive transactions with the same campaign
id
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
Campaign
id 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'.
campaign
id type amount
sell
sell
The third sequence qualifies with two sells and one buy. The sell transactions sum to
and the name is 'Engineering'.
Campaign
id type amount
sell
sell
buy
The final sequence does not qualify since it does not have more sell than buy transactions.
campaign
id 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 netsells
count Netsells
total
Engineering
but i
m getting this:
Campaign netsells
count Netsells
total
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