SQL Exam
SQL Exam
event_type value
2 -5
3 4
I can bring out the lowest and the highest using below queries . This is how far I could reach
..confused on the way further .
SELECT a.event_type,
(b.value - a.value) AS value
FROM (SELECT *
FROM events
GROUP BY event_type, time, value
ORDER BY time ASC
LIMIT 2 OFFSET 1) AS a ,
(SELECT *
FROM events
GROUP BY event_type, time, value
ORDER BY time DESC
LIMIT 2 OFFSET 0) AS b
WHERE a.event_type IN ( SELECT e.event_type
FROM events e
GROUP BY e.event_type
HAVING COUNT(e.event_type) >= 2 )
AND a.event_type = b.event_type
ORDER BY a.event_type ASC
2. I want to have a sql that gives recipient that received of amount >=
1024, with number of transfer <=3.
-
1d e.g.,
ow
n
vot
e
favorite
Servy william007
167k18207309 4,10773675
1 Your SQL means that the person has had 3 or less transactions, not that the amount of 3 or
less is more than 1024 – James Z Mar 5 at 13:40
add a comment
3 Answers
activeoldest votes
from (
select *
, row_number() over (
partition by recipient
order by amount desc
) as rn
from transfers
) as i
where rn < 4
group by recipient
having sum(amount)>=1024
returns:
+--------------+
| account_name |
+--------------+
| Johnson |
| Taylor |
+--------------+
rextester postgres demo: http://rextester.com/PFR74297
3.
create table events (
sensor_id integer not null,
event_type integer not null,
value integer not null,
time timestamp unique not null);
expected result
from events
SELECT
E1.SENSOR_ID,
COUNT(E1.EVENT_TYPE) AS "TYPES"
FROM
(
SELECT
DISTINCT
E.SENSOR_ID,
E.EVENT_TYPE
FROM
EVENTS E
) E1
GROUP BY E1.SENSOR_ID
ORDER BY E1.SENSOR_ID ASC