Escolar Documentos
Profissional Documentos
Cultura Documentos
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.
-
1do e.g.,
wn
vote
avorite
f
Johnson is listed since Johnson account is listed because it has received 1112
USD in the following three transfers: 512 USD + 100 USD + 500 USD, Taylor is
with 1 transfer of USD 1024. Williams is not there since he receives 1200 in four
transactions.
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
up using row_number() and a derived table to limit each recipient to their top 3
vote8down amounts received, then grouping by recipient returning those
vote having sum(amount)>=1024
select recipient as account_name
accepted
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
The question was edited that removed some pertinent information from
the 3rd revision of the question: what was tried already.
I try
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