1

table1

person    | zipcode     | timestamp               | event  | device
--------------------------------------------------|--------|------
amar      | 11111       | 2016-09-28 20:05:03.001 | email  | phone
akbar     | 11111       | 2016-09-28 20:05:03.001 | email  | phone  
antony    | 11111       | 2016-09-28 20:07:03.001 | chat   | pc
amar      | 11111       | 2016-09-28 20:08:03.001 | email  | phone
amar      | 11111       | 2016-09-28 20:08:03.001 | chat   | phone
amar      | 22222       | 2016-09-28 20:09:03.001 | email  | phone
akbar     | 22222       | 2016-09-28 20:10:03.001 | email  | phone  
antony    | 22222       | 2016-09-28 20:10:03.001 | chat   | phone
amar      | 11111       | 2016-09-28 21:05:03.001 | email  | phone
akbar     | 11111       | 2016-09-28 21:05:03.001 | email  | phone  
antony    | 11111       | 2016-09-28 21:07:03.001 | chat   | phone

Output desired - records for all zipcodes for all hours where records with pc device is present

output table for above example

The only qualifying zipcode, timestamp_hour combination is 11111, 2016-09-28 20

person    | zipcode     | timestamp               | event  | device
--------------------------------------------------|--------|------
amar      | 11111       | 2016-09-28 20:05:03.001 | email  | phone
akbar     | 11111       | 2016-09-28 20:05:03.001 | email  | phone  
antony    | 11111       | 2016-09-28 20:07:03.001 | chat   | pc
amar      | 11111       | 2016-09-28 20:08:03.001 | email  | phone
amar      | 11111       | 2016-09-28 20:08:03.001 | chat   | phone

pseudocode

  1. create temp table of zipcode, timestamp_hour combination with filter device='pc' to get qualifying zipcode, timestamp_hour list
  2. apply #1 as a filter with table1

For #1 above, the code i've is as below , need help with #2

with zipcode_hour as (
  select zipcode,   date_trunc(`hour`, timestamp) as timestamp_hour
      from table1
      where device = 'pc'
      group by zipcode, timestamp_hour
)

2 Answers 2

2

You can use the below query

SELECT person
    ,zipcode
    ,timestamp1
    ,event
    ,device
FROM table1 a
WHERE EXISTS (
        SELECT 1
        FROM table1 b
        WHERE b.device = 'pc'
            AND date_trunc('hour', a.timestamp1) = date_trunc('hour', b.timestamp1)
            AND a.zipcode = b.zipcode
        );

Complete result SQLFiddle

Sign up to request clarification or add additional context in comments.

Comments

0

You can filter with a self join and where clause

select distinct t1.person, t1.zipcode, t1."timestamp", t1.event, t1.device
from table1 t1
join table1 t2 on t2.zipcode = t1.zipcode
               and date_trunc('hour', t2."timestamp") = date_trunc('hour', t1."timestamp")
where t2.device = 'pc'

This looks for all entries t1.*, t2.device having the same zipcode and hour, but keeps only the ones where t2.device is pc. The distinct keyword is needed to weed out duplicate entries.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.