SQl query help in grouping - mysql

An SQL table schema,
time, country, activer_users
If I just want to show the total number of active users over time, Below simple slect wil do that
SELECT time, sum(active_users) as activer_users GROUP BY time ORDER BY time
returned data will be like,
[{
"time": 1585878969,
"active_users": 2300
},....]
If I want active_users over time by country, then
SELECT time, country, sum(active_users) as activer_users GROUP BY time ORDER BY time, country
returned data will be like,
[{
"time": 1585878969,
"active_users": 1300,
"country": "India"
}, {
"time": 1585878969,
"active_users": 1000,
"country": "China"
}....]
I want data in the below format,
[{
"time": 1585878969,
"India": 1300,
"China": "1000"
}....]
Is this possible, to create dynamic columns from the value of a field and its value based on another field..
if suchthing is possible, what should be the query for that

Other helpful users may correct me, but I think is not possible altering MySQL responses like this. MySQL always responds in a COLUMN-VALUE way, so you would have to create a column e.g. "China" and store this data in there to get a native response like this.

Related

Finding a count of a nested JSON Array in PostgresSQL version 9.6.23

Working with an insurance health data set on different providers with entries of many records which look like this more or less:
"record":{
"npi":"1548306996",
"name":{
"last":"NGUYEN",
"first":"TU UYEN",
"middle":""
},
"type":"INDIVIDUAL",
"plans":"\\"[
{
"years":"\\"[
2020,
2021\
],
"plan_id":"30751MT0550006",
"network_tier":"PREFERRED",
"plan_id_type":"HIOS-PLAN-ID"
},
{
"years":"\\"[
2020,
2021\
],
"plan_id":"30751MT0550009",
"network_tier":"PREFERRED",
"plan_id_type":"HIOS-PLAN-ID"
},
{
"years":"\\"[
2020,
2021\
],
"plan_id":"30751MT0550010",
"network_tier":"PREFERRED",
"plan_id_type":"HIOS-PLAN-ID"
},
{
"years":"\\"[
2021\
],
"plan_id":"30751MT0550011",
"network_tier":"PREFERRED",
"plan_id_type":"HIOS-PLAN-ID"
},
"accepting":"accepting",
"addresses":"\\"[
{
"zip":"92703",
"city":"SANTA ANA",
"phone":"7149732022",
"state":"CA",
"address":"2701 W 1ST ST",
"address2":""
},
{
"zip":"90221",
"city":"COMPTON",
"phone":"3106394300",
"state":"CA",
"address":"1821 N LONG BEACH BLVD",
"address2":""
}"\\"
],
"specialty":"\\"[
"General Dentist""\\"
],
"last_updated_on":"\"2020-09-25”}"
I am trying to filter the records for distinct NPI, which is the provider number in my dataset, and sort them based on the amount of unique addresses.address they have. To see which one has the largest increase from 2020 to 2021.
Currently my query looks like this:
WITH prepared AS (
SELECT (json_array_elements(record->'plans')->>'years')::json years, json_array_elements(record -> 'addresses') ->> 'address' as Address, npi, source_id, cycle_id
FROM public_use_file_data
)
SELECT npi, source_id, cycle_id, COUNT(DISTINCT(Address)) as count
FROM prepared
WHERE ARRAY(SELECT json_array_elements_text(years)) #> ARRAY['2020','2021']
GROUP BY source_id, cycle_id, npi
ORDER BY count DESC;
I thought using a window function would make dealing with this json format the easiest, since I can't change it. However my query is taking forever to load, so wondering if there is a better way to do this

query from MYSQL to mongodb

This is my MYSQL query:
SELECT country, vaccines, MAX(people_fully_vaccinated_per_hundred) as vaccinated_precentage
FROM country_vaccinations
WHERE people_fully_vaccinated_per_hundred > 60
GROUP BY country, vaccines
ORDER BY MAX(people_fully_vaccinated_per_hundred) DESC;
It basically lists all countries that have fully vaccinated more than 60% of its people, and the types of vaccine offered by that country.
I am trying to do the same on MongoDB:
db.country_vaccinations.aggregate([
{$project: {_id:0,
country: 1,
vaccines: 1,
people_fully_vaccinated_per_hundred: 1},
}
{$match: {"people_fully_vaccinated_per_hundred":{$gt:60}}}
])
However, I am not sure why it returns "No Records Found" when i add in the $match to retrieve rows that have "people_fully_vaccinated_per_hundred">60%.
Can someone advise me on what is my mistake? I would really appreciate it, as I am new to noSQL and am not sure why.
I am not sure it does the same, test it before using it, and if doesn't work, give us if you can some sample data and the expected output so we can test it.
country_vaccinations.aggregate(
[{"$match": {"people_fully_vaccinated_per_hundred": {"$gt": 60}}},
{"$group":
{"_id": {"country": "$country", "vaccines": "$vaccines"},
"vaccinated_precentage": {"$max": "$people_fully_vaccinated_per_hundred"}}},
{"$sort": {"vaccinated_precentage": -1}},
{"$project":
{"_id": 0,
"country": "$_id.country",
"vaccines": "$_id.vaccines",
"vaccinated_precentage": 1}}])

Postgresql - Count of elements in nested JSON blob

I have a Postgres statement that returns extracts/iterates over a json blob in the value column of a table. I am able to get a count one level deep using the query below but I can't count any deeper. I was using:
select jsonb_array_length(value -> 'team') as team_count
This returns the proper count but I cant seem to leverage this to count the names under each team.
In a perfect world I would my results to return 4 lines of results like this(title and a matching count of names):
Product Owner, 2
Technical Product Manager, 2
Data Modeler, 0
Engineer, 0
How would I go about amending this query to give me the count of names under team? I tried all sorts of stuff but nothing that got me close.
Sample Json is below.
"team":[
{
"title":"Product Owner",
"names":[
"John Smith",
"Jane Doe"
]
},
{
"title":"Technical Project Manager",
"names":[
"Fred Flintstone",
"Barney Rubble"
]
},
{
"title":"Data Modeler"
},
{
"title":"Engineer"
}
You seem to be looking for
SELECT
role -> 'title' AS team_role,
jsonb_array_length(role -> 'names') AS member_count
FROM jsonb_array_elements(value -> 'team') AS team(role)

Using N1QL with document keys

I'm fairly new to couchbase and have tried to find the answer to a particular query I'm trying to create with not much success so far.
I've debated between using a view or N1QL for this particular case and settled with N1QL but haven't managed to get it to work so maybe a view is better after all.
Basically I have the document key (Group_1) for the following document:
Group_1
{
"cbType": "group",
"ID": 1,
"Name": "Group Atlas 3",
"StoreList": [
2,
4,
6
]
}
I also have 'store' documents, their keys are listed in this document's storelist. (Store_2, Store_4, Store_6 and they have a storeID value that is 2, 4 and 6) I basically want to obtain all 3 documents listed.
What I do have that works is I obtain this document with its id by doing:
var result = CouchbaseManager.Bucket.Get<dynamic>(couchbaseKey);
mygroup = JsonConvert.DeserializeObject<Group> (result.ToString());
I can then loop through it's storelist and obtain all it's stores in the same manner, but i don't need anything else from the group, all i want are the stores and would have prefered to do this in a single operation.
Does anyone know how to do a N1QL directly unto a specified document value?
Something like (and this is total imaginary non working code I'm just trying to clearly illustrate what I'm trying to get at):
SELECT * FROM mycouchbase WHERE documentkey IN
Group_1.StoreList
Thanks
UPDATE:
So Nic's solution does not work;
This is the closest I get to what I need atm:
SELECT b from DataBoard c USE KEYS ["Group_X"] UNNEST c.StoreList b;
"results":[{"b":2},{"b":4},{"b":6}]
Which returns the list of IDs of the Stores I want for any given group (Group_X) - I haven't found a way to get the full Stores instead of just the ID in the same statement yet.
Once I have, I'll post the full solution as well as all the speed bumps I've encountered in the process.
I apologize if I have a misunderstanding of your question, but I'm going to give it my best shot. If I misunderstood, please let me know and we'll work from there.
Let's use the following scenario:
group_1
{
"cbType": "group",
"ID": 1,
"Name": "Group Atlas 3",
"StoreList": [
2,
4,
6
]
}
store_2
{
"cbType": "store",
"ID": 2,
"name": "some store name"
}
store_4
{
"cbType": "store",
"ID": 4,
"name": "another store name"
}
store_6
{
"cbType": "store",
"ID": 6,
"name": "last store name"
}
Now lets say you wan't to query the stores from a particular group (group_1), but include no other information about the group. You essentially want to use N1QL's UNNEST and JOIN operators.
This might leave you with a query like so:
SELECT
stores.name
FROM `bucket-name-here` AS groups
UNNEST groups.StoreList AS groupstore
JOIN `bucket-name-here` AS stores ON KEYS ("store_" || groupstore.ID)
WHERE
META(groups).id = 'group_1';
A few assumptions are made in this. Both your documents exist in the same bucket and you only want to select from group_1. Of course you could use a LIKE and switch the group id to a percent wildcard.
Let me know if something doesn't make sense.
Best,
Try this query:
select Name
from buketname a join bucketname b ON KEYS a.StoreList
where Name="Group Atlas 3"
Based on your update, you can do the following:
SELECT b, s
FROM DataBoard c USE KEYS ["Group_X"]
UNNEST c.StoreList b
JOIN store_bucket s ON KEYS "Store_" || TO_STRING(b);
I have a similar requirement and I got what I needed with a query like this:
SELECT store
FROM `bucket-name-here` group
JOIN `bucket-name-here` store ON KEYS group.StoreList
WHERE group.cbType = 'group'
AND group.ID = 1

Store a big array in the MySQL table

I want to create tablestopsfor all stops with these columns id, stop_name, stop_lat, stop_long, route, arrivaltime but I dont know how can I store the arrivaltime into the table since this column is a big array
Like this:
{
"id": 1
"stops_name": "Amersham ",
"arrival_time": {
"mon-fri": [ "05:38", "06:07","06:37",.....50 entries],
"sat": ["05:34","06:01","06:31",...........50 entries],
"son": ["06:02","06:34","07:04",...........50 entries]
},
"stops_lat": 83.837994,
"stops_long": 18.700423
}
Is that to manage with mysql?
Generally speaking you would split the "arrival times" out into a new table, referencing back to the table of stops. You would also generally store each time as a single row, and then select the entire collection of rows.
This works best because it lets you query on the 'time' column and search for time ranges, etc and only get the relevant rows.
For the "day", I would most likely use a Set to have a column that can be 1 or more values. Also consider that likely you may need to store info on public holidays or other special dates as well:
https://dev.mysql.com/doc/refman/5.6/en/set.html
Stops: id, stops_name, stops_lat, stops_long (1, "Amersham", 83.837994, 18.700423)
Stops_arrivals: id, stops_id, day, time (1, 1, "Mon", "05:38"), (2, 1, "Mon", "06:07"), etc