how to order dates in ASC,
Dates are in the following format
01 January 2013
13 August 2013
27 June 2013
I've tried this query
SELECT * FROM table ORDER BY TIMESTAMP(date) ASC
and this query
SELECT * FROM property_rent ORDER BY UNIX_TIMESTAMP(date) ASC
Using the str_to_date function:
SELECT * FROM table ORDER BY str_to_date(date, 'your format here')
e.g.
SELECT * FROM table ORDER BY str_to_date(date, '%d %M %Y')
or similar.
Related
I have mysql database which contain date like this value
22 Jan 2019 11:03
I want to select all records that have values less than today , so I wrote this query
select username from radcheck
where DATE_FORMAT(STR_TO_DATE(value,'%d %M %Y') ,'%d-%m-%Y') < DATE_FORMAT(NOW(), '%d-%m-%Y')
but this query return all records even those not expired .
where is my problem
SELECT username
FROM radcheck
WHERE STR_TO_DATE(`value`,'%d %b %Y %H:%i') < CURRENT_DATE
I have been trying to do a running total "order by date". The problem is that if you have two entries with the same date, then it shows the same total on both rows.
Table structure is something like this
sipID|Date|Amount
1|2017-11-07|2
2|2017-11-09|27
3|2017-11-07|24
So I know how to do a running total by id like this.
SELECT `sipID`,DATE_FORMAT(`Date`,'%d %M %Y') as `DateFormat`,`Amount`,
(SELECT SUM(`Amount`) FROM `salesinvoice_payments` as `Lin` WHERE `Lin`.`sipID`<=`Lout`.`sipID` && `salesinvoice_id`=115) as `Balance`
FROM `salesinvoice_payments` as `Lout`
WHERE `salesinvoice_id`=115
ORDER BY `sipID`
But I wanted it to be ordered by date ascending so I did this
SELECT `sipID`,DATE_FORMAT(`Date`,'%d %M %Y') as `DateFormat`,`Amount`,
(SELECT SUM(`Amount`) FROM `salesinvoice_payments` as `Lin` WHERE `Lin`.`Date`<=`Lout`.`Date` && `salesinvoice_id`=115) as `Balance`
FROM `salesinvoice_payments` as `Lout`
WHERE `salesinvoice_id`=115
ORDER BY `Date` ASC
Now this does work. The problem happens when you have 2 rows with the same date. What happens then is that both rows have the same running total.
eg
07 November 2017 2.00 24.00
07 November 2017 22.00 24.00
09 November 2017 3.00 27.00
What I want is this
07 November 2017 2.00 2.00
07 November 2017 22.00 24.00
09 November 2017 3.00 27.00
Is there a workaround so that I can have a proper running total, and have it ordered by date without it getting the same total on the same date?
EDIT:
#Strawberry:
I have finally worked out how sqlfiddle works, and put this for my original question.
http://www.sqlfiddle.com/#!9/c6dc75/1
You need something to separate Ammount from same day.
SQL DEMO
SELECT `sipID`,
DATE_FORMAT(`Date`,'%d %M %Y') as `DateFormat`,
`Amount`,
(SELECT SUM(`Amount`)
FROM `salesinvoice_payments` as `Lin`
WHERE `Lin`.`Date` < `Lout`.`Date`
OR (`Lin`.`Date` = `Lout`.`Date`
AND `Lin`.`sipID` <= `Lout`.`sipID`)
) as `Balance`
FROM `salesinvoice_payments` as `Lout`
ORDER BY `Date` ASC
OUTPUT
Approach using user variables: DEMO with nod to Juan for setup of tables/data
SELECT `sipID`
, DATE_FORMAT(`Date`,'%d %M %Y') as `DateFormat`
,`Amount`
,#Bal:=#Bal+`amount` as `Balance`
FROM `salesinvoice_payments` as `Lout`
CROSS JOIN (SELECT #Bal:=0) z --initializes and declares variable in select
WHERE `salesinvoice_id`=115
ORDER BY `Date`, `sipID`
Or using pagination
SELECT *
FROM (SELECT `sipID`
, DATE_FORMAT(`Date`,'%d %M %Y') as `DateFormat`
,`Amount`
,#Bal:=#Bal+`amount` as `Balance`
FROM `salesinvoice_payments` as `Lout`
CROSS JOIN (SELECT #Bal:=0) z --initializes and declares variable in select
WHERE `salesinvoice_id`=115
ORDER BY `Date`, `sipID`) Z
ORDER BY `DateFormat`, `sipID`
LIMIT 5,10; -- Retrieve rows 6-15 Skip 5 retrieve next 10.
In this approach the variables have already been resolved in set {Z} thus the user variables are not impacted by the Limit/offset.
Example using your SQL Fiddle but we would need more sample data to really see the pagination impact.
I am trying to SELECT the date from my database in the date_format '%e %M %Y':
SELECT date_format(date, '%e %M %Y') AS date FROM table
The output of the date will be like: 1 january 2016.
When I add an ORDER BY to the query I the date will not sort on the date, but it sorts on alphabetical order.
Does someone know how I can fix this problem and sort column: date on the date order?
Here is my full statement:
SELECT date_format(date, '%e %M %Y') AS date FROM table ORDER BY date DESC
You are using a date as alias for column name. Just use another one:
SELECT date_format(date, '%e %M %Y') AS formatted_date
FROM table
ORDER BY date DESC;
I have a database where it is stored the date of creation of the record. The problem is that when I try to SELECT all records within 1 year, I get no rows returned.
I created a SQLFiddle to illustrate what I have and what I'm trying to do: http://sqlfiddle.com/#!9/33a32b/8
SELECT aux.description, DATE_FORMAT(aux.date_creation, '%d/%m/%Y às %H:%i') AS `formatted_date`
FROM aux
WHERE aux.date_creation BETWEEN 'CURDATE() - INTERVAL 1 YEAR' AND 'CURDATE()'
ORDER BY `formatted_date` DESC;
This is your query:
SELECT aux.description, DATE_FORMAT(aux.date_creation, '%d/%m/%Y às %H:%i') AS `formatted_date`
FROM aux
WHERE aux.date_creation BETWEEN 'CURDATE() - INTERVAL 1 YEAR' AND 'CURDATE()'
ORDER BY `formatted_date` DESC;
The single quotes are incorrect. You have a string constant, not a date expression. Presumably, date_creation is in the past, so all you need is:
SELECT aux.description,
DATE_FORMAT(aux.date_creation, '%d/%m/%Y às %H:%i') AS formatted_date
FROM aux
WHERE aux.date_creation >= CURDATE() - INTERVAL 1 YEAR
ORDER BY aux.date_creation DESC;
Note that I also changed the ORDER BY. Normally, one wants Jan 2nd to follow Jan 1st, not Feb 1st.
Here's the table structure and some sample data:
pID.....month.....year
27 .....3 .....2008
27 .....12 .....2012
31 .....6 .....2008
99 .....1 .....2006
42 .....1 .....2009
pID is the practiceID and month and year represent the date period they've entered data for. I need to grab the number of practices that have entered data for the first time in Oct 2012, Nov 2012, Dec 2012 and so on.
I tried the following query for Oct 2012:
SELECT *
FROM
IPIPKDIS
where
practiceID NOT IN (
SELECT practiceID
from
IPIPKDIS
where
year < 2012 and month < 10
)
and year = 2012
and month = 10
and measureCatRecID = 2
ORDER BY year, month;
but it's grabbing months and year less than 10/2012.
If I run the queries isolated (not as subquery) they both work fine.
Any ideas?
This summary query will yield the first (smallest) date in the table for each value of practiceID.
SELECT practiceID,
MIN(STR_TO_DATE( CONCAT(year, ' ', month), '%Y %m')) first_date
FROM IPIPKDIS
GROUP BY practiceID
If you want to retrieve then the whole row for the first reported month, you'd do a nested query like this:
SELECT *
FROM IPIPKDIS I
JOIN (
SELECT practiceID,
MIN(STR_TO_DATE( CONCAT(year, ' ', month), '%Y %m')) first_date
FROM IPIPKDIS
GROUP BY practiceID
) first ON ( first.practiceID = I.practiceID
AND STR_TO_DATE( CONCAT(I.year, ' ', I.month), '%Y %m') = first.first_date)
The trick to the second query is to use the JOIN to extract just the first-month rows from your table. We use date arithmetic to do the date comparisons.