I'm new at QKL and got stuck doing some complex aggregations.
I have a dataset with two columns - one column named key and one value.
I want to make list of all the numeric values with the same key , but keep non-numeric value as seperate .
For example if my dataset looks like this
key
value
location
"CA"
location
"LA"
code
"2"
code
"3"
IP
"192.143.204.19"
Meaning I want to get the following table
key
value
location
"CA"
location
"LA"
code
["2","3"]
IP
"192.143.204.19"
summarize vals=make_set(val) by key won't work because it will combine "LA" and "CA" and when trying to do make_set_if I got empty lists in the non-numeric values.
Has anyone have an idea how to solve it ?
Thank you!
you could try this (handling both cases separately, then unioning the results):
let T = datatable(key:string, value:string)
[
"location", "CA",
"location", "LA",
"code", "2",
"code", "3",
"IP", "192.143.204.19",
]
| extend long_value = tolong(value)
;
T
| where isnull(long_value)
| union (
T
| where isnotnull(long_value)
| summarize value = tostring(make_list(long_value)) by key
)
| project key, value
key
value
location
CA
location
LA
IP
192.143.204.19
code
[2,3]
Related
I have a FireStore database and I need to convert it to SQL. My SeminarsAndTraining document looks like this:
{
"st_name": "SOL Level 1",
"attendance": [
{"date": "01/29/2021", "present": ["9103", "1282"], "absent": ["8127"]},
{"date": "01/29/2021", "present": ["1203", "1224"], "absent": ["0927"]}
]
}
I have multiple of these SeminarsAndTraining documents inside a collection. The object inside the attendance array contains the date for the attendance and the students' id is stored in the present and absent array.
Problem 1
I know you can't have arrays in SQL, so what is the best approach to this?.
"attendance": [
{"date": "01/29/2021", "present": ["9103", "1282"], "absent": ["8127"]},
{"date": "01/29/2021", "present": ["1203", "1224"], "absent": ["0927"]}
]
In a relational database you'll typically have these tables
SeminarsAndTraining, which stores st_name
SeminarsAndTraining_attendance, which stores the date of each attendance, and the ID of the SeminarsAndTraining it belong to.
SeminarsAndTraining_attendance_present, which stores each ID from the present field, and the ID of the SeminarsAndTraining_attendance it belong too.
SeminarsAndTraining_attendance_absent, which stores each ID from the abssent field, and the ID of the SeminarsAndTraining_attendance it belong too.
You could probably merge the last two tables, and include a present_or_absent value for each.
I have a container within CosmosDB that houses items. I am needing to find out the count of how many records I have within my container with the conditions of: Source and Date
This is a sample JSON schema in which each of my records/items holds. Each record has a unique id and acts as a single count.
{
"id": "1111111111122222222233333333",
"feedback": {
"Source": "test"
"Date": "1980-10-15T00:04:34Z",
"Ser": "test",
"Count_Of_Comments": "1",
"Count_Of_Votes": "1"
}
The container within CosmosDB looks like something like this:
Goal:
**I wish to return, the numb*er of id records (or the count) based on the Source and the Date.
This is what I have tried (below), however this does not seem to work and I am wondering if I am missing something here. Any help or suggestions are appreciated.
SELECT VALUE COUNT(c.id), c.Source, c.Date
FROM C
Where Source == "test", AND Date == "1980-10-15T00:04:34Z"
As David comments,there are some syntax errors.Please try this sql:
SELECT value COUNT(c.id) FROM c Where c.feedback.Source = "test" AND c.feedback.Date = "1980-10-15T00:04:34Z"
If you need Source and Date,you can try this:
SELECT COUNT(c.id) AS Count,max(c.feedback.Source) as Source,max(c.feedback.Date) as Date
FROM c
Where c.feedback.Source = "test" AND c.feedback.Date = "1980-10-15T00:04:34Z"
By the way,both COUNT(c.id) AND COUNT(1) can achieve your goal in your situation.More detail about SQL Query,you can refer to this documentation.
Hope this can help you.
I want to retrieve the full name column in the user_detail table, by the user entering the selected id first in the Order table using Query Builder
in the order table it looks like this
+"id": 1
+"order_detail_id": 115
+"user_id": 3
+"status": "pending"
+"updated_at": "2019-11-22 17:53:56"
+"created_at": "2019-11-22 15:44:28"
in table user_detail it looks like this
+"id": 3
+"full_name": michael jackson
+"address": "bla bla bla"
+"updated_at": "2019-11-22 17:53:56"
+"created_at": "2019-11-22 15:44:28"
I want if the user enters id 1 it will output like
"id": 1,
"order_detail_id": 115,
"user_id": 3,
"full_name": "michael jackson",
"address" : "bla bla bla"
"status": "pending"
I tried with this code but an error still appears
$cekclient = DB::table('order')
->where('id',$request->id)
->join('user', 'user.id', '=', 'order.user_id')
->select('order.*','user.name','user.address',)
->get();
dd($cekclient);
Try this code. please give table alias and use them in select and join.
$cekclient = DB::table('order as or')
->select('or*','u.name','u.address') // here you can add column names which you need in output
->leftJoin('user as u', 'u.id', '=', 'or.user_id')
->where('or.id',$request->id) // here if you need data by id of user then use u.id in where insead of or.id
->get();
I'm looking to query a table for a distinct list of values in a given JSON column.
In the code snippet below, the Survey_Results table has 3 columns:
Name, Email, and Payload. Payload is the JSON object to I want to query.
Table Name: Survey_Results
Name Email Payload
Ying SmartStuff#gmail.com [
{"fieldName":"Product Name", "Value":"Calculator"},
{"fieldName":"Product Price", "Value":"$54.99"}
]
Kendrick MrTexas#gmail.com [
{"fieldName":"Food Name", "Value":"Texas Toast"},
{"fieldName":"Food Taste", "Value":"Delicious"}
]
Andy WhereTheBass#gmail.com [
{"fieldName":"Band Name", "Value":"MetalHeads"}
{"fieldName":"Valid Member", "Value":"TRUE"}
]
I am looking for a unique list of all fieldNames mentioned.
The ideal answer would be query giving me a list containing "Product Name", "Product Price", "Food Name", "Food Taste", "Band Name", and "Valid Member".
Is something like this possible in Postgres?
Use jsonb_array_elements() in a lateral join:
select distinct value->>'fieldName' as field_name
from survey_results
cross join json_array_elements(payload)
field_name
---------------
Product Name
Valid Member
Food Taste
Product Price
Food Name
Band Name
(6 rows)
How to find distinct Food Name values?
select distinct value->>'Value' as food_name
from survey_results
cross join json_array_elements(payload)
where value->>'fieldName' = 'Food Name'
food_name
-------------
Texas Toast
(1 row)
Db<>fiddle.
Important. Note that the json structure is illogical and thus unnecessarily large and complex. Instead of
[
{"fieldName":"Product Name", "Value":"Calculator"},
{"fieldName":"Product Price", "Value":"$54.99"}
]
use
{"Product Name": "Calculator", "Product Price": "$54.99"}
Open this db<>fiddle to see that proper json structure implies simpler and faster queries.
Is it possible to select or group by an item within an array using SoQL?
For example, here is an item returned by the SODA API:
{
"business_name" : "ACME CO",
"phone_number" : {
"phone_number" : "2145551234"
},
"zip_code" : "75235"
}
I would like to group by the phone number, but can't find a way to support it. The phone number is not available as a non-array object, and the site does not support adding it to the group by via its GUI.
Trying something like the following causes an error stating all GROUP BY arguments must be column names:
$select=hospital_name%2Cphone_number&$group=hospital_name%2Cphone_number.phone_numer
Thanks in advance!