Domanda


I am struggling to find a solution to a problem I have and will appreciate any help -
I am working on an android app and part of it needs to collect the user clicks
I store every click in a sqlite table with the following parameters -
click id, click day, click month, click year and click hour
(The date breakdown is for other queries)
My Task -
I need to display a time division of the daily user clicks (from the point he asked and 24
hours back) by the following time division
morning any hour between 8-11, noon any hour between 12-17, evening any hour between 17-22
night any hour between 22-8 (thats 8 in the morning of the day after)
My Problem -
As for now my query unites the time sections in between two days that is -
In case I am looking 24 hours back from 5pm and there is a record with an hour 6pm on
the day before - it counts as today (and it shouldn't be since I want to seperate)
between time slices between two different days UNLESS its the night
time slice.
My sql query

SELECT (CASE 
WHEN (click_history_hour > '07:59:59' AND click_history_hour < '12:00:00') THEN '8 - 11' 
WHEN (click_history_hour > '11:59:59' AND click_history_hour < '17:00:00') THEN '12 - 17' 
WHEN (click_history_hour > '16:59:59' AND click_history_hour < '22:00:00') THEN '17 - 22' 
WHEN ((click_history_hour > '21:59:59' AND click_history_hour < '23:59:59') OR (click_history_hour > '00:00:00' AND click_history_hour < '08:00:00')) THEN '22 - 8'
END) 
AS time, COUNT(*) AS quantity 
FROM click_history 
WHERE (click_history_day='25' AND click_history_month='04' AND click_history_year='2014' AND click_history_hour>'00:45:21') 
OR (click_history_day='26' AND click_history_month='04' AND click_history_year='2014') 
GROUP BY (CASE 
WHEN (click_history_hour > '07:59:59' AND click_history_hour < '12:00:00') THEN '8 - 11' 
WHEN (click_history_hour > '11:59:59' AND click_history_hour < '17:00:00') THEN '12 - 17' 
WHEN (click_history_hour > '16:59:59' AND click_history_hour < '22:00:00') THEN '17 - 22' 
WHEN ((click_history_hour > '21:59:59' AND click_history_hour < '23:59:59') OR (click_history_hour > '00:00:00' AND click_history_hour < '08:00:00')) THEN '22 - 8'
END) 
ORDER BY (click_history_month || click_history_day || click_history_year), click_history_hour ASC;

Please note - the dates are since this is an actual query from a debug session
any help?


Edit
The test data I am checking as for now - enter image description here

The query result -
enter image description here

È stato utile?

Soluzione

Try this :

select 
  CASE 
    WHEN click_hour < '09' THEN '23 -  8'
    WHEN click_hour < '12' THEN ' 8 - 11'
    WHEN click_hour < '18' THEN '12 - 17'
    WHEN click_hour < '23' THEN '18 - 22'
    ELSE                        '23 -  8'
  END AS time, 
  COUNT(*) AS quantity 
from 
  (
  SELECT 
    click_history_year||'-'||click_history_month||'-'||click_history_day||' '||click_history_hour  as click_time, 
    substr(click_history_hour,1,2) as click_hour
  FROM click_history
  ) as click_history2
WHERE datetime('now','-1 day') <= click_time
group by time
;

Note that this would be even cleaner if you just used a single column to store the timestamp (which I artificially created as click_time). You would also eliminate the 4 columns you have now. To get the hour from the timestamp, use strftime(click_time, '%H').

Also, I adjusted the time ranges and comparisons, because some seemed wrong. Perhaps I just misunderstand your requirement.

And note that you could move the WHERE clause up into the sub-query (and eliminate click_time), but it depends upon how are going to use the code. It would be like this:

select 
  CASE 
    WHEN click_hour < '09' THEN '23 -  8'
    WHEN click_hour < '12' THEN ' 8 - 11'
    WHEN click_hour < '18' THEN '12 - 17'
    WHEN click_hour < '23' THEN '18 - 22'
    ELSE                        '23 -  8'
  END AS time, 
  COUNT(*) AS quantity 
from 
  (
  SELECT 
    substr(click_history_hour,1,2) as click_hour
  FROM click_history
  WHERE datetime('now','-1 day') <= click_history_year||'-'||click_history_month||'-'||click_history_day||' '||click_history_hour
  ) as click_history2
group by time
;

Here is an SQLfiddle to check the syntax: http://sqlfiddle.com/#!7/992ca/2

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top