SELECT COUNT(patient_id) AS idpateint,
patient_id
FROM patient
WHERE STR_TO_DATE(date_enter,'%d/%m/%Y' )
BETWEEN STR_TO_DATE( '$repeat','%d/%m/%Y' ) AND STR_TO_DATE('$to','%d/%m/%Y')
AND patient_type='opd'
AND patient_id ='$idpatient1'
ORDER BY STR_TO_DATE(date_enter,'%d/%m/%Y' )
Actually I saved date in dd/mm/yyyy format in db. Order by is not working. No SQL error but date is not coming in order.
Here is your query:
select count(patient_id) as numpatients, patient_id
from patient
where STR_TO_DATE(date_enter, '%d/%m/%Y' ) between STR_TO_DATE('$repeat', '%d/%m/%Y' ) and
STR_TO_DATE('$to', '%d/%m/%Y') and
patient_type = 'opd' and
patient_id = '$idpatient1'
order by STR_TO_DATE(date_enter, '%d/%m/%Y' )
You have a count() in the select. This turns the query into an aggregation query, so it only returns one row. In addition, you are selectxing only a single patient id. I could imagine that you want the counts by date, because you are so focused on date.
The following will give counts by date, regardless of the patient:
select STR_TO_DATE(date_enter, '%d/%m/%Y' ), count(patient_id) as numpatients
from patient
where STR_TO_DATE(date_enter, '%d/%m/%Y' ) between STR_TO_DATE('$repeat', '%d/%m/%Y' ) and
STR_TO_DATE('$to', '%d/%m/%Y') and
patient_type = 'opd' and
group by STR_TO_DATE(date_enter, '%d/%m/%Y' )
order by STR_TO_DATE(date_enter, '%d/%m/%Y' );
By the way. Don't you think the query looks ugly with all those calls to STR_TO_DATE(). They are ugly in addition to making the query less efficient. Store dates in the database using the database data types. That is what they are there for.
Related
I have working MySQL query:
SELECT date(timestamp), hour(timestamp), sum(numberDet), count(*)
FROM human_det_counter
GROUP BY hour( timestamp ) , day( timestamp )
ORDER BY date(timestamp)
I want select records, group by hour from each day and sum their number of detections.
I have tried this query:
SELECT date(h.timestamp), hour(h.timestamp), sum(h.numberDet), count(h)
FROM HumanDetCounter h
GROUP BY hour( h.timestamp ) , day(h.timestamp )
ORDER BY date(h.timestamp)
,but it's not working. I readed that jpa don't support Hour() function and that I should use TO_CHART function, but I don't know how.
Okey, I figured it out. There's my query if someone needs that:
createQuery("SELECT cast(Date(h.timestamp) as string), substring(h.timestamp, 12,2) , sum(h.numberDet), count(h) FROM HumanDetCounter h GROUP by substring(h.timestamp, 12,2), substring(h.timestamp, 9,2) ORDER BY h.timestamp")
It's probably not the best solution, but it's work :)
SO i have a task and i need to group my results by Date and by Provider_name but currently my code is listing out multiple dates and Providers. (need to have one provider per day (25 days in all) so my table shows how many messages the provider got that day and how much did they earn)
This needs to be my result. Result table
But this is what i'm currently getting
This is my code currently
SELECT date_format( time, '%Y-%m-%d' ) AS Date, provider_name, COUNT( message_id ) AS Messages_count, SUM( price ) AS Total_price
FROM mobile_log_messages_sms
INNER JOIN service_instances ON service_instances.service_instance_id = mobile_log_messages_sms.service_instance_id
INNER JOIN mobile_providers ON mobile_providers.network_code = mobile_log_messages_sms.network_code
WHERE time
BETWEEN '2017-02-26 00:00:00'
AND time
AND '2017-03-22 00:00:00'
AND price IS NOT NULL
AND price <> ''
AND service IS NOT NULL
AND service <> ''
AND enabled IS NOT NULL
AND enabled >=1
GROUP BY provider_name, time
ORDER BY time DESC
Can you tell me where i've messed up, i really can't figure out the answer.
Try like this:
....
GROUP BY provider_name, date_format( time, '%Y-%m-%d' )
ORDER BY time DESC
You are grouping time which will group the result by time including hour, minute and second so on ... that is why you getting different count from same day. Try grouping by day instead.
time column is datetime. So its grouped by date and time both rather than just date.
Change GROUP BY statement to
GROUP BY provider_name, date_format( time, '%Y-%m-%d' )
I get the error:
#1111 - Invalid use of group function.
I can't seem to understand what's wrong. My code is below:
Select * from (Select MAX(ticket_comment_date)
from ticket_movement_tran` group by ticket_id) AS T
where DATE(MAX(ticket_comment_date))
between DATE_FORMAT( STR_TO_DATE( '01-01-2014', '%d-%m-%Y' ) , '%y-%m-%d' )
and DATE_FORMAT( STR_TO_DATE( '01-11-2014', '%d-%m-%Y' ) , '%y-%m-%d' );
issue
ticket_id ( which is being grouped by in the inner query ) is not included in the selected fields. You can't group by a field which is not selected, hence the error message..
fix
add ticket_id to fields selected
no need for two layers of sql, can do this in one layer; adding the date filter logic in the having clause ( as it relates to the aggregated value )
adjusted query
select ticket_id, MAX(ticket_comment_date)
from ticket_movement_tran
group by ticket_id
having DATE(MAX(ticket_comment_date)) between
DATE_FORMAT( STR_TO_DATE( '01-01-2014', '%d-%m-%Y' ) , '%y-%m-%d' )
and DATE_FORMAT( STR_TO_DATE( '01-11-2014', '%d-%m-%Y' ) , '%y-%m-%d')
;
notes
I assume since you are using DATE(MAX(ticket_comment_date)) that ticket_comment_date is not properly defined as a date. Would recommend changing the storage type for this field to a DATE..
So if the ticket_comment_date is currently a character field, then MAX(ticket_comment_date) can give you a different results than you expect. in particular lexical ordering ( ordering of character fields ) can differ from numerical or date based ordering
On my mysql table I keep date as datetime. (eg: 2013-11-09 11:15:58), I want to write mysql Query to get number of dates to given date. can someone help me to write this..
eg:
Given date: 2014-01-19 10:15:15
On table 'date_added' has: 2013-11-09 11:15:58
is it possible to get number of dates using mysql query, like COUNT(), SUM()
I think your using TIMESTAMP to use this method
SELECT TIMESTAMPDIFF(DAY,'2003-02-01','2003-05-01') from tablename;
if you want to count following this code:
SELECT COUNT(*) FROM a1 WHERE '2014-03-01';
To find number of records on a specific date of given timestamp, use date_format or date functions on the field to filter by date.
select
date( date_added ) dt,
count( date_added ) dt_cnt
from my_table
group by
date_format( date_added, '%Y-%m-%d' );
Alternatively, you can also use date function to extract date part from a datetime column.
select
date( date_added ) dt,
count( date_added ) dt_cnt
from my_table
group by
date( date_added );
And if you want to count based on date part of an input datetime value then, use
select
date( '2014-01-19 10:15:15' ) dt,
count( date_added ) dt_cnt
from my_table
where
date( date_added ) = date( '2014-01-19 10:15:15' );
Refer to:
DATE(expr)
Extract the date part of a date or datetime expression
DATE_FORMAT(date,format)
Format date as specified
Im currently trying to run a SQL query to export data between a certain date, but it runs the query fine, just not the date selection and i can't figure out what's wrong.
SELECT
title AS Order_No,
FROM_UNIXTIME(entry_date, '%d-%m-%Y') AS Date,
status AS Status,
field_id_59 AS Transaction_ID,
field_id_32 AS Customer_Name,
field_id_26 AS Sub_Total,
field_id_28 AS VAT,
field_id_31 AS Discount,
field_id_27 AS Shipping_Cost,
(field_id_26+field_id_28+field_id_27-field_id_31) AS Total
FROM
exp_channel_data AS d NATURAL JOIN
exp_channel_titles AS t
WHERE
t.channel_id = 5 AND FROM_UNIXTIME(entry_date, '%d-%m-%Y') BETWEEN '01-05-2012' AND '31-05-2012' AND status = 'Shipped'
ORDER BY
entry_date DESC
As explained in the manual, date literals should be in YYYY-MM-DD format. Also, bearing in mind the point made by #ypercube in his answer, you want:
WHERE t.channel_id = 5
AND entry_date >= UNIX_TIMESTAMP('2012-05-01')
AND entry_date < UNIX_TIMESTAMP('2012-06-01')
AND status = 'Shipped'
Besides the date format there is another issue. To effectively use any index on entry_date, you should not apply functions to that column when you use it conditions in WHERE, GROUP BY or HAVING clauses (you can use the formatting in SELECT list, if you need a different than the default format to be shown). An effective way to write that part of the query would be:
( entry_date >= '2012-05-01'
AND entry_date < '2012-06-01'
)
It works with DATE, DATETIME and TIMESTAMP columns.