Let's say I have a table order as following:
o_id | o_uid | o_date
1 | 5 | June 10, 2015
2 | 1 | June 10, 2015
3 | 8 | June 10, 2015
5 | 15 | June 11, 2015
6 | 11 | June 11, 2015
7 | 16 | June 12, 2015
8 | 19 | June 12, 2015
I tried running the following query:
SELECT o_id, o_uid FROM order GROUP BY o_date
I thought it will give me result as follows:
[
"June 10, 2015" => [
[
"o_id" => 1
"o_uid" => 5
],
[
"o_id" => 2
"o_uid" => 1
],
[
"o_id" => 3
"o_uid" => 8
]
],
"June 11, 2015" => [
[
"o_id" => 4
"o_uid" => 15
],
[
"o_id" => 5
"o_uid" => 11
]
],
...
]
The query does not provide results as expected. I can use PHP to get the required results but why not use MySQL if you can and finish things off in a line. The GROUP BY Clause is quite confusing. Is there any other clause which can group records by date?
The result I get:
SQL Fiddle
use
SELECT o_id, GROUP_CONCAT(o_id),GROUP_CONCAT(o_uid),o_date FROM `order`
GROUP BY o_date
And then separate comma separated list using php/do manipulation accordingly.
Output : Formatter output from sqlfiddle.
o_id GROUP_CONCAT(o_id) GROUP_CONCAT(o_uid) o_date
1 1,2,3 5,1,8 June 10, 2015
4 4,5 15,11 June 11, 2015
6 6,7 16,19 June 12, 2015
Fiddle
Related
I have created a database in MySQL with data from the Chinook dataset, which has fictitious information on customers that buy music.
One of the tables ("Invoice"), has the billing addresses, which has characters in diverse languages:
InvoiceId CustomerId InvoiceDate BillingAddress
1 2 2009-01-01 00:00:00 Theodor-Heuss-Straße 34
2 4 2009-01-02 00:00:00 Ullevålsveien 14
3 8 2009-01-03 00:00:00 Grétrystraat 63
4 14 2009-01-06 00:00:00 8210 111 ST NW
I tried to retrieve the data using R, with the following code:
library(DBI)
library(RMySQL)
library(dplyr)
library(magrittr)
library(lubridate)
library(stringi)
# Step 1 - Connect to the database ----------------------------------------
con <- DBI::dbConnect(MySQL(),
dbname = Sys.getenv("DB_CHINOOK"),
host = Sys.getenv("HST_CHINOOK"),
user = Sys.getenv("USR_CHINOOK"),
password = Sys.getenv("PASS_CHINOOK"),
port = XXXX)
invoices_tbl <- tbl(con, "Invoice") %>%
collect()
The connection is ok, but when trying to visualize the data, I can't see the special characters:
> head(invoices_tbl[,1:4])
# A tibble: 6 x 4
InvoiceId CustomerId InvoiceDate BillingAddress
<int> <int> <chr> <chr>
1 1 2 2009-01-01 00:00:00 "Theodor-Heuss-Stra\xdfe 34"
2 2 4 2009-01-02 00:00:00 "Ullev\xe5lsveien 14"
3 3 8 2009-01-03 00:00:00 "Gr\xe9trystraat 63"
4 4 14 2009-01-06 00:00:00 "8210 111 ST NW"
5 5 23 2009-01-11 00:00:00 "69 Salem Street"
6 6 37 2009-01-19 00:00:00 "Berger Stra\xdfe 10"
My question is, should I change something in the configuration inside MySQL? Or is it an issue with R? How can I see the special characters? What is the meaning of \xdfe?
Please, any help will be greatly appreciated.
The hexadecimal format can be converted with iconv
invoices_tbl$BillingAddress <- iconv(invoices_tbl$BillingAddress,
"latin1", "utf-8")
-output
invoices_tbl
InvoiceId CustomerId InvoiceDate BillingAddress
1 1 2 2009-01-01 00:00:00 Theodor-Heuss-Straße 34
2 2 4 2009-01-02 00:00:00 Ullevålsveien 14
3 3 8 2009-01-03 00:00:00 Grétrystraat 63
4 4 14 2009-01-06 00:00:00 8210 111 ST NW
5 5 23 2009-01-11 00:00:00 69 Salem Street
6 6 37 2009-01-19 00:00:00 Berger Straße 10
data
invoices_tbl <- structure(list(InvoiceId = 1:6, CustomerId = c(2L, 4L, 8L, 14L,
23L, 37L), InvoiceDate = c("2009-01-01 00:00:00", "2009-01-02 00:00:00",
"2009-01-03 00:00:00", "2009-01-06 00:00:00", "2009-01-11 00:00:00",
"2009-01-19 00:00:00"), BillingAddress = c("Theodor-Heuss-Stra\xdfe 34",
"Ullev\xe5lsveien 14", "Gr\xe9trystraat 63", "8210 111 ST NW",
"69 Salem Street", "Berger Stra\xdfe 10")), row.names = c("1",
"2", "3", "4", "5", "6"), class = "data.frame")
I am trying to find the last entry for the previous years quarter.
All I can access is year i.e 2021 and quarter i.e 1
Here is the data in my database:
id
name
start
end
16
April 2021
2021-04-01
2021-04-30
15
March 2021
2021-03-01
2021-03-31
14
February 2021
2021-02-01
2021-02-28
57
November 2020
2020-11-01
2020-11-30
55
October 2020
2020-10-01
2020-10-31
29
September 2020
2020-09-01
2020-09-30
27
July 2020
2020-07-01
2020-07-31
24
April 2020
2020-04-01
2020-04-30
23
March 2020
2020-03-01
2020-03-31
22
February 2020
2020-02-01
2020-02-29
21
January 2020
2020-01-01
2020-01-31
Using the MySQL quarter function I can get it to print out the quarter as an integer in another column:
SET #given_year = 2021;
SET #given_quarter = 1;
SELECT
id, name, start, end, QUARTER(end) as "Q"
FROM
submissions
id
name
start
end
Q
16
April 2021
2021-04-01
2021-04-30
2
15
March 2021
2021-03-01
2021-03-31
1
14
February 2021
2021-02-01
2021-02-28
1
57
November 2020
2020-11-01
2020-11-30
4
55
October 2020
2020-10-01
2020-10-31
4
29
September 2020
2020-09-01
2020-09-30
3
27
July 2020
2020-07-01
2020-07-31
3
24
April 2020
2020-04-01
2020-04-30
2
23
March 2020
2020-03-01
2020-03-31
1
22
February 2020
2020-02-01
2020-02-29
1
21
January 2020
2020-01-01
2020-01-31
1
I tried using WHERE and LIKE but it is returning 0 rows:
SELECT * FROM (
SELECT
id, name, start, end, QUARTER(end) as "Q"
FROM
submissions as s
) AS vs
WHERE
vs.end
LIKE
#given_year
AND
vs.Q < #given_quarter
I also need to account for the possibility that there may be no rows this year and I need to find the previous year.
For example with these two rows, if I was passed the year 2021 and quarter 1 I would need to return November of the previous year and a different quarter.
id
name
start
end
Q
14
February
2021
2021-02-01
2021-02-28
57
November
2020
2020-11-01
2020-11-30
If I understand correctly, you want all the rows from the quarter in the data before a given quarter. You can filter and use dense_rank():
select s.*
from (select s.*,
dense_rank() over (order by year(start) desc, quarter(start) desc) as seqnum
from submissions s
where year(start) < #given_year or
(year(start) = #given_year and quarter(start) < #given_quarter)
) s
where seqnum = 1;
The above returns all rows from the previous quarter (which is what I thought you wanted). If you want only one row:
select s.*
from submissions s
where year(start) < #given_year or
(year(start) = #given_year and quarter(start) < #given_quarter)
order by start desc
limit 1;
I have a mysql table with some entries. sample data
nid | news_date
1 | 16 July 2015, 2:31 pm
2 | 16 July 2015, 2:31 pm
3 | 17 July 2015, 12:31 pm
4 | 18 July 2015, 4:28 pm
5 | 20 July 2015, 12:31 pm
I want daily report, and i tried with this sql command
SELECT count(nid), DATE(news_date)
FROM tbl_news
GROUP BY DATE(tbl_news.news_date);
But i am getting output as
count(nid) | DATE(news_date)
5 | NULL
But i want daily record count report, Anybody help.
Please try this query:-
SELECT count(nid), news_date
FROM tbl_news
GROUP BY (STR_TO_DATE(tbl_news.news_date, '%d %M %Y')) ;
I have removed the date keyword.
I have some data like :
id name ccode json
1 john 231 {"age": 12,"score": 90}
2 danny 231 {"age": 22,"score": 87}
3 danniel 231 {"age": 18,"score": 48}
4 sara 431 {"age": 16,"score": 67}
now, i want get all fields of all users that they ages are between 15 to 24 and they ccode is 231.
result must be something like :
2 danny 231 {"age": 22,"score": 87}
3 danniel 231 {"age": 18,"score": 48}
you can use the following query ,
select id,name,ccode,json, CAST(SUBSTRING(SUBSTRING_INDEX(json, ',', 1) FROM 8) AS UNSIGNED) as val
from events
where ccode=231 having val>15 and val<24;
I have a table with dates in "Aug 23, 2009" format and 5 values, so that it looks like this
SELECT * FROM table;
Date | Total | V1 | V2 | V3 | V4
Aug 21, 2009 | 41 | 23 | 8 | 8 | 2
Aug 22, 2009 | 39 | 22 | 8 | 7 | 2
Aug 23, 2009 | 35 | 20 | 6 | 7 | 2
Aug 24, 2009 | 34 | 20 | 6 | 6 | 2
Aug 25, 2009 | 32 | 19 | 5 | 6 | 2
Aug 26, 2009 | 31 | 19 | 5 | 5 | 2
Aug 27, 2009 | 30 | 19 | 5 | 5 | 1
So I need a query that will give me only the most recent (bottom) 3 entries. Should I setup some query by the date or just set a limit to the last 3 rows? I tried doing a subquery with a limit, but my version of MySQL does not support LIMIT in subquery, and to my knowledge there is no way to do a negative limit to grab the last x number of rows.
select *
from table
order by Date desc
limit 0, 3
Can MySQl do TOP ? If so
Select Top 3 * From Table
Order By Date Desc
Just change the order by to do your LIMIT.
So, in other words, add
ORDER BY `date` DESC
to your select statement. You'll then be a ble to limit the return results to whatever row count you need.
You don't need a sub-query, just use a LIMIT clause in your SELECT statement, and add an ORDER BY 'date' DESC clause. Generally it is a bad idea to use column names such as 'DATE' (or 'DATETIME' for that matter) because different databases may use these as reserved words.