search in json values from mysql tables - mysql

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;

Related

MySQL Query with Distinct on Two Different Columns

I'm trying to build a query that will give me results that are distinct in two separate columns, user_name and year_taken.
If I have results of my current query that look like this. These are basically all of the tests taken.
user_name
test_taken
score
year_taken
Bob Smith
101
85
2020
Jan Jones
101
99
2020
Mike Jackson
101
54
2021
Bob Smith
201
74
2020
Mike Jackson
201
70
2020
Jan Jones
300
75
2020
Mike Jackson
300
55
2021
Bob Smith
301
95
2021
Mike Jackson
301
97
2022
I need to narrow it down to just one test per year, per user. So, the results I'd like to get look like this:
user_name
test_taken
score
year_taken
Bob Smith
101
85
2020
Jan Jones
101
99
2020
Mike Jackson
101
54
2021
Mike Jackson
201
70
2020
Bob Smith
301
95
2021
Mike Jackson
301
97
2022
So, I need one test per one user per year.
My query draws from a few different tables, but it boils down to
select user_name, test_taken, score, year_taken from ....
I'm just not sure how to base a distinct on another column as well.
You can use ROW_NUMBER(), as in:
select *
from (
select *, row_number() over(partition by user_name, year_taken
order by score desc) as rn
from t
) x
where rn = 1

Problem with retrieving a table from Chinook dataset due to character

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")

Query Get Last Month Data (calculated from now) and parse Json to get item inside Json

I have two table that related like these:
master_ticket:
t_m_id t_open t_closed
==================================
111 2018-12-01 2018-12-05
222 2018-12-02 2018-12-06
333 2018-12-03 2018-12-07
444 2018-12-04 2018-12-08
master_data:
m_id m_reference
=====================================
111 {"id": "01","name": "Bahary"}
222 {"id": "02","name": "Mail"}
333 {"id": "03","name": "Ivan"}
444 {"id": "04","name": "Scheil"}
How im supposed to do, to make my table looks like this (with filter get last month data from t_open):
id name t_open t_closed
===============================
01 Bahary 2018-12-01 2018-12-05
02 Mail 2018-12-02 2018-12-06
03 Ivan 2018-12-03 2018-12-07
04 Scheil 2018-12-04 2018-12-08
Please help me guys .... Thanks,
I think this may give your expected output.
with cte as (
select 111 as t_m_id, '2018-12-01' as t_open, '2018-12-05' as t_closed
union all
select 222 as t_m_id, '2018-12-02' as t_open, '2018-12-06' as t_closed
union all
select 333 as t_m_id, '2018-12-03' as t_open, '2018-12-07' as t_closed
union all
select 444 as t_m_id, '2018-12-04' as t_open, '2018-12-08' as t_closed) ,
cte2 as (
select 111 as m_id, '{"id": "01","name": "Bahary"}' as m_reference union all
select 222 as m_id, '{"id": "02","name": "Mail"}' as m_reference union all
select 333 as m_id, '{"id": "03","name": "Ivan"}' as m_reference union all
select 444 as m_id, '{"id": "04","name": "Scheil"}' as m_reference)
select json_unquote(json_extract(m_reference, '$.id')) as ID, c.t_open, t_closed,
json_unquote(json_extract(m_reference, '$.name')) AS name from cte c
join cte2 c2 on c.t_m_id = c2.m_id;
Output:
ID, t_open, t_closed, name
01, 2018-12-01, 2018-12-05, Bahary
02, 2018-12-02, 2018-12-06, Mail
03, 2018-12-03, 2018-12-07, Ivan
04, 2018-12-04, 2018-12-08, Scheil

group multiple rows with the same value mysql

I have a certain result-set and I need to group them together on userId.
e.g.
userId 2019-01-15 2019-01-16
------------------------------
132 0 30_140
132 30_140 0
Required output:
userId 2019-01-15 2019-01-16
------------------------------
132 30_140 30_140
Since values contain non-numeric characters, SUM won't work.
If the empty values are all 0 or NULL you can just use MAX:
SELECT userID, MAX(`2019-01-15`) AS `2019-01-15`, MAX(`2019-01-16`) AS `2019-01-16`
FROM test
GROUP BY userID
Output:
userID 2019-01-15 2019-01-16
132 30_140 30_140
Demo on dbfiddle

How to Roll up recursive value in ssrs

I have a table with value for example
ID Name ParentID Value Date
1 ClassA 0 NULL NULL
2 ClassB 0 NULL NULL
7 Jason 1 50 2016-06-01
8 Jason 1 20 2016-06-02
9 Jason 1 30 2016-06-03
10 Kelly 1 20 2016-06-02
11 Kelly 1 30 2016-06-03
and i want to generate a report like this
2016-06-01 2016-06-02 2016-06-03
ClassA 50 40 60
Jason 50 20 30
Kelly 0 20 30
ClassB 0 0 0
as you can see, the classA has been rolled up and total up all the children
i tried few code, it either show me empty or show me recursive value where all the rows is the same value.
for example
=Sum(fields!QTY.Value)
it shows
Result1
Then second code i tried is
=IIF(ISNOTHING(Sum(Fields!QTY.Value, "Name", recursive)),0, Sum(Fields!QTY.Value, "Name", recursive))
, it shows
Recursive result
Anyone can help on this?..