I have documents like that
{"name":"Name_29","date":"2017-04-26","id":"ID_0","version":1}
And I try to have the last version of a document before a given date. ( version restart at zero on each date ).
I'm trying to perfome this kind of request
SELECT *
FROM docs
WHERE date <= '2018-02-03'
AND id = 'x'
ORDER by date DESC, version DESC
LIMIT 1
But for and all a range of name.
SELECT RAW ARRAY_AGG({'name':name, 'id' : id ,'version' : version, 'date' : date})
FROM doc
WHERE date <= '2017-02-27'
AND name = 'NAME_02'
GROUP BY id
But I'm strugling to extract the greatest date version of the array.
Is there any good and efficient practice to do this?
SELECT MAX([date,version,doc])[2]
FROM doc
WHERE date <= '2017-02-27'
AND name = 'NAME_02'
GROUP BY id;
Check following link for explanation.
couchbase N1ql query select with non-group by fields
Related
I have a MySQL script to find out the newest record, but it fails to get the incorrect result. I am giving the SQL script below:
SELECT
adjust_after,
spu_code,
sku_code,
store_id,
create_time
FROM
goods_store_stock_update_record
WHERE
is_del = 0
AND create_time >= DATE '2021-05-26'
AND create_time <= DATE '2021-05-27'
AND store_id = '18cc1cc715774d0196d17420c5104c15'
AND sku_code = 'SKUH201900001202050004' HAVING MAX(create_time)
There is a result without Having max(create_time):
"adjust_after" "spu_code" "sku_code" "store_id" "create_time"
"8" "SPH201900001202050001" "SKUH201900001202050004" "18cc1cc715774d0196d17420c5104c15" "26/5/2021 11:29:20"
"7" "SPH201900001202050001" "SKUH201900001202050004" "18cc1cc715774d0196d17420c5104c15" "26/5/2021 11:35:14"
But, when I am trying to query the create_time that was the newest:2021-05-26 11:35:14 record using having max(create_time). Unfortunately, It returns the incorrect record below:
8 SPH201900001202050001 SKUH201900001202050004 18cc1cc715774d0196d17420c5104c15 2021-05-26 11:29:20
Therefore, in this situation, how can I correct the querying script without using order by create_time and limit one.
Best regards. I really appreciate it.
There are two main mistakes in your query.
First, HAVING wants a boolean expression, just like WHERE. MAX(create_time) is not a boolean, but a datetime. What MySQL does here is convert the datetime to a number (maybe the internal representation of that datetime) and the number to a boolean (where 0 = false and everything else = true). So, that expression will result in true for about every row.
Second, MAX(create_time) is an aggregation. Without a GROUP BYclause this results in a single row. adjust_after etc. however, are not aggregated. This should result in a syntax error, but MySQL applies ANY_VALUE instead. This means you are telling MySQL, to give you one of the adjust_after that it finds, one of the spu_code, etc., all arbitrarily picked.
The straight-forward solution would be ORDER BY create_time LIMIT 1, which you don't want for reasons unknown to us.
Another solution is
SELECT
adjust_after,
spu_code,
sku_code,
store_id,
create_time
FROM goods_store_stock_update_record
WHERE is_del = 0
AND store_id = '18cc1cc715774d0196d17420c5104c15'
AND sku_code = 'SKUH201900001202050004' HAVING MAX(create_time)
AND create_time =
(
SELECT MAX(create_time)
FROM goods_store_stock_update_record
WHERE is_del = 0
AND create_time >= DATE '2021-05-26'
AND create_time <= DATE '2021-05-27'
AND store_id = '18cc1cc715774d0196d17420c5104c15'
AND sku_code = 'SKUH201900001202050004'
);
I have a table with several rows of timestamp (unix epoch)
eg: 1620518277 , 1556748676 , 1547547076, 1602756807, 944971077 (field name -> date_stamp)
And by using
SELECT *
FROM table
ORDER BY date_stamp DESC
The result of this query is :
1. 944971077
2. 1620518277
3. 1602756807
4. 1556748676
5. 1547547076
Everything is sorted fine but how can 944971077 > 1620518277 ???
Anybody had this kind of strange SQL issues ?
Presumably, you are storing these timestamps as strings, not as numbers. A simple option forces a numeric conversion:
SELECT * FROM table ORDER BY date_stamp + 0 DESC
This would occur if timestamp were a string. A simple method is to convert to a number using implicit conversion:
SELECT *
FROM table
ORDER BY date_stamp + 0 DESC
I want to fetch data between two range but in my database Date field as Text.
How Now fetch data between two range
$startdate = 01-01-2020
$enddate = 31-12-2020
and my database field name DATE as text datatype(format 01-12-2019)
Below is the query I am using
SELECT m.id, m.centers, c.BUDGET_ANNUAL_AMOUNT
FROM Cost_centers m INNER JOIN ANNUAL_BUDGET_BUDGET_CENTER c
ON c.BUDGET_ID = 25
where (START_DATE BETWEEN '$startdate' AND '$enddate')
ORDER BY ID DESC
Please help how to get data with text datattype with range. How to convert text to data .
Please help me
have you tried using STR_TO_DATE() function available in mysql ?
I think something like below should work.
SELECT m.id, m.centers, c.BUDGET_ANNUAL_AMOUNT
FROM Cost_centers m INNER JOIN ANNUAL_BUDGET_BUDGET_CENTER c
ON c.BUDGET_ID = 25
where (STR_TO_DATE(START_DATE, "%d-%m-%Y") BETWEEN '$startdate' AND '$enddate')
ORDER BY ID DESC
According to
My SQL Reference - DATE, DATETIME, and TIMESTAMP Types
MySQL retrieves and displays DATE values in 'YYYY-MM-DD' format.
I would suggest trying this format.
There is also a worked example where variables are used in date queries on :
TutorialsPoint
I know how to search the LATEST date and the MOST value specifically:
Most Quantity Used:
SELECT * FROM tour_packages
WHERE active = 1
ORDER BY quantity_used DESC
Latest Date:
SELECT * FROM tour_packages
WHERE active = 1
ORDER BY start_date DESC
But how can I do both, by able to search the LATEST date WITH the MOST value in quantity_used? Is this practice even possible?
EDITED: I think my question is not clear enough.
I intend to find the data with the LATEST date first, then from that result FIND the highest VALUE from quantity_used.
I think you just want two order by keys:
SELECT tp.*
FROM tour_packages tp
WHERE tp.active = 1
ORDER BY tp.start_date DESC, tp.quantity_used DESC;
This returns the rows ordered by date and within each date, the ones with the largest quantity go first.
im using a query to get data between dates but for some reason it does not pull the data of the last date selected here is my query:
SELECT * FROM order WHERE status = "completed" AND orderdate >= ? AND orderdate <= ? ORDER BY orderid DESC
Im using is equal to or less then... but still?
what am i doing wrong ?
SELECT * FROM order WHERE status = "completed" AND date(orderdate) >= date(?) AND date(orderdate) <= date(?) ORDER BY orderid DESC
It happened with me also, but in my case instead of passing a date I was querying using a datetime variable, Please make sure you are querying with date variable only.
Make sure that orderdate is date as well as your query parameter is also date, or use appropriate function to convert them in date, than query.
Your dates are actually datetimes - so you are actually, in the case of the upperbound, saying "12 midnight" on whichever date you choose. Hence, if it tries to test a value at say 10am in the morning, it fails as being outside the range.
Either set the upperbound date one day forward, or explicitly only test the date part of the datetime...