sqlite query need to tuned a bit

我只是一个虾纸丫 提交于 2020-01-17 04:21:09

问题


Hi current i have code which will display the sum of records for last one hour in 10min interval.

SELECT SUM(NO_OF_ADULT) AS ADULT
     , SUM(NO_OF_ADULT_F) AS ADULT_F
     , SUM(NO_OF_CHILDREN) AS CHILDREN
     , SUM(NO_OF_CHILDREN_F) AS CHILDREN_F
     , SUM(NO_OF_SENIOR_CITIZEN) AS SENIOR
     , SUM(NO_OF_SENIOR_CITIZEN_F) AS SENIOR_F
     , SUM(NO_OF_INFANT) AS INFANT
     , SUM(NO_OF_INFANT_F) AS INFANT_F
     , SUBSTR(END_TIME_OF_SERVICE, 1, 4) || '0 - ' || SUBSTR(END_TIME_OF_SERVICE, 1, 4) || '9' AS TIME_SLOT
  FROM CLNT_RESERVATION RES
      , CLNT_ORDER ORD
      , CLNT_ORDER_CHEQUE_RESERVATION_MAPPING RM
  WHERE RES.START_DATE_OF_SERVICE ="2012-09-24"
    AND ORD.ENT_CODE="****"
    AND ORD.BRANCH_CODE="******"
    AND ORD.STATION_CODE="*******"
    AND RES.STATION_CODE=ORD.STATION_CODE 
    AND ORD.ORDER_STATUS <> 111 
    AND ORD.ORDER_ID=RM.ORDER_ID 
    AND (ORD.CREATE_TIME LIKE "2012-09-24%" OR ORD.UPDATE_TIME LIKE "2012-09-24%")
    AND ORD.STATUS_FLG ="A"
    AND ORD.DELETE_FLG="N" 
    AND RM.RESERVATION_ID = RES.RESERVATION_ID 
    AND (strftime("%H:%M", RES.END_TIME_OF_SERVICE) BETWEEN strftime("%H:%M",TIME("now","localtime","-1 hour","-00 minutes")) 
    AND strftime ("%H:%M",TIME("now","localtime")) OR strftime("%H:%M", RES.END_TIME_OF_SERVICE) BETWEEN strftime("%H:%M",TIME("now","localtime","-1 hour","-00 minutes")) 
    AND strftime("%H:%M",TIME("now","localtime")) ) 
GROUP BY 9

which will give the below output like

ADULT  ADULT_F  CHILDREN CHILDREN_F SENIOR SENIOR_F INFANT INFANT_F  TIME_SLOT

 0       0         0       0          1        1      0      0     12:00 - 12:09
 1       0         0       0          0        1      0      0     12:30 - 12:39

now you can see that the values for 12:10 - 12:29 its not in the db hence its not display too.

what i am expecting is : - 1) Need the row to be created and simply fill with 0 in all the fields. (high) 2) time_slot should be calculated from the current time_stamp.(medium)

Help this is helpfull, kindly suggest me some solution thank a lot for time.


回答1:


  1. The database will not create records for data that isn't there.
    If you want to get records for all time slots (and cannot handle this in your application when reading the result), you have to add records filled with zeros to the table. (You can do this blindly for all desired time slots, as the sums won't be affected.)
  2. For strings in SQL, always use 'single quotes'. SQLite will accept double quotes for compatibility with some other SQL engines, but if you ever have some column with the same name, you will get hard-to-find errors.
  3. You do not need those TIME(...) calls; you can use strftime('%H:%M','now','localtime') directly.
  4. To get ten-minute time slots based on the current time, you must do calculations without splitting the time into h/m/s fields. This was already shown in the previous question:
SELECT DATETIME(strftime('%s', SERVICE) +
                ((strftime('%s', 'now') - strftime('%s', SERVICE))
                 % (60 * 10)),
                'unixepoch') ...


来源:https://stackoverflow.com/questions/12560439/sqlite-query-need-to-tuned-a-bit

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!