Answered step by step
Verified Expert Solution
Link Copied!

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
1
Engineering
2
Legal
3
Engineering
Events
Camapign
_
id Type amount
2
buy
5
4
.
7
0
1
sell
7
6
.
0
2
2
sell
2
9
.
4
7
1
buy
4
6
.
3
2
3
sell
2
.
9
6
2
sell
2
8
.
9
6
3
buy
6
6
.
3
1
2
buy
8
9
.
6
0
1
sell
2
8
.
4
7
1
buy
5
1
.
9
7
1
sell
4
8
.
8
9
3
sell
1
7
.
3
7
3
sell
6
.
6
5
2
buy
9
.
4
1
1
sell
7
.
1
3
1
sell
6
4
.
5
0
1
buy
3
1
.
0
1
2
sell
1
0
.
5
1
2
buy
9
.
8
9
3
sell
7
4
.
7
9
2
sell
3
.
4
5
2
sell
5
4
.
7
0
2
sell
7
6
.
0
2
output:
Campaign netsells
_
count Netsells
_
total
Engineering
2
1
7
3
.
0
1
legal
1
1
3
4
.
1
7
Explanation:
1
.
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
1
sell
2
8
.
4
7
1
buy
5
1
.
9
7
1
sell
4
8
.
8
9
There are two sell transactions and only one buy transactions, so this sequence is reported. The sell transactions add to
7
7
.
3
6
and the name is 'Engineering'.
2
.
The next sequence has only sell transactions, so it is reported. The sell transactions add to
2
4
.
0
2
.
The name is 'Engineering'.
campaign
_
id type amount
3
sell
1
1
7
.
3
7
3
sell
6
.
6
5
3
.
The third sequence qualifies with two sells and one buy. The sell transactions sum to
7
1
.
6
3
,
and the name is 'Engineering'.
Campaign
_
id type amount
1
sell
7
.
1
3
1
sell
6
4
.
5
0
1
buy
3
1
.
0
1
4
.
The final sequence does not qualify since it does not have more sell than buy transactions.
campaign
_
id type amount
2
sell
1
0
.
5
1
2
buy
9
.
8
5
.
The qualifying sequences are associated with
2
different campaigns id
1
and
3
Both campaigns are named Engineering. The total of sell amounts is
7
7
.
3
6
+
2
4
.
0
2
+
7
1
.
6
3
=
1
7
3
.
0
1
I need my output to be this:
Campaign netsells
_
count Netsells
_
total
Engineering
2
1
7
3
.
0
1
but i
'
m getting this:
Campaign netsells
_
count Netsells
_
total
Engineering
2
3
2
6
.
7
8
Legal
1
6
8
.
9
4
image text in transcribed

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

More Books

Students also viewed these Databases questions