Google Query Language: How to select the min value? - json

I am doing some web scraping task, and I get prices from a website. The issue is that I would like to get the min between all options. For example: It will looks for one cellphone , which has 8GB and it is unlocked B, but I also need that it returns the min price between all options it finds. Google Query Language: How to select the min value?
This is my sheet. This is the google spreadsheet
=QUERY(ImportJSON(C2),"SELECT Col4,Col20 WHERE Col4 CONTAINS '8GB' AND Col4 CONTAINS 'Unlocked B' LIMIT 1 label Col4'',Col20''",1)
how can I modify that formula so it returns the min price? It's like a loop function, is that possible? regardless its color
For example, I want the function to look for the price, it already does, but I would like to get the lowest price it could finds, instead of the first price it finds and matches with the formula
Google-Query Language Reference

try:
=QUERY(IMPORTJSON(C2),
"select Col4,Col20
where Col4 contains '8GB'
and Col4 contains 'Unlocked B'
order by Col20
limit 1
label Col4'',Col20''", 1)
or:
=QUERY(IMPORTJSON(C2),
"select Col4,min(Col20)
where Col4 contains '8GB'
and Col4 contains 'Unlocked B'
group by Col4
order by min(Col20)
limit 1
label Col4'',min(Col20)''", 1)

Related

Is it possible to get the Next and Previous values in an SQL table when you know the Current value?

What I'm trying to do is get the Next and Previous values in SQL.
In this example I'd always know the current value.
This is how my table is laid out.
id
parentID
appID
Name
19410
18371
2da4
name600
19410
18371
4ac0
name24
19410
18371
348e
name441
So my goal is for example get the next/previous value from the current.
So if the current is test2, I'd get test3 back.
I have looked into using offset + limit but I don't think that allows you to select a current starting point.
I cannot store an array as I don't want it to be slow either.
This differs from other questions as I do not have a iterable value as for example name won't always be test1, test2, test3.
My version is 8.0.19 - MySQL Community Server - GPL
For efficiency, you will need INDEX(name) (or some index starting with name)
To get the next row based on "name":
SELECT ...
FROM ...
WHERE name > 'test2'
ORDER BY name ASC
LIMIT 1
For Previous, change 2 things:
SELECT ...
FROM ...
WHERE name < 'test2'
ORDER BY name DESC
LIMIT 1
To get both at the same time:
( SELECT ... FROM ... WHERE name > 'test2' ORDER BY name ASC LIMIT 1 )
UNION ALL
( SELECT ... FROM ... WHERE name < 'test2' ORDER BY name DESC LIMIT 1 )
This will be a lot faster than LEAD and LAG or Cursors.

I have a data in google spreadsheet dates are in horizantal form, I want to convert like vertical form with dates

my data is like this
enter image description here
Try this:
=arrayformula({A1:B1,"Date",C2:E2;{
regexreplace(query(split(flatten(split(flatten(query(transpose(query(if(C1:H1<>"",char(9998)&" "&substitute(A3:A," ",char(9999))&" "&substitute(B3:B," ",char(9999))&" "&C1:H1&" ",)&C3:H,"where Col2 !=''",0)),"",9^9)),char(9998)))," "),"select Col1,Col2 where Col1 !='' order by Col3",0),char(9999)," "),
query(split(flatten(split(flatten(query(transpose(query(if(C1:H1<>"",char(9998)&" "&substitute(A3:A," ",char(9999))&" "&substitute(B3:B," ",char(9999))&" "&C1:H1&" ",)&C3:H,"where Col2 !=''",0)),"",9^9)),char(9998)))," "),"select Col3,Col4,Col5,Col6 where Col1 !='' order by Col3",0)}
})
Within the formula, amend the ranges:
C1:H1 - change H to match your end range (keep #1)
C3:H - change H to match your end range
The formula is a little more complex than it needs to be to work with potential spaces in the Vendor and Client names whilst ensuring that numbers are formatted as numbers (not text).
A simpler formula to switch spaces to - would be:
=arrayformula({A1:B1,"Date",C2:E2;query(split(flatten(split(flatten(query(transpose(query(if(C1:H1<>"",char(9998)&" "&substitute(A3:A," ","-")&" "&substitute(B3:B," ","-")&" "&C1:H1&" ",)&C3:H,"where Col2 !=''",0)),"",9^9)),char(9998)))," "),"where Col1 !='' order by Col3",0)})
If you never have spaces in Vendor or Client name, then even simpler is:
=arrayformula({A1:B1,"Date",C2:E2;query(split(flatten(split(flatten(query(transpose(query(if(C1:H1<>"",char(9998)&" "&A3:A&" "&B3:B&" "&C1:H1&" ",)&C3:H,"where Col2 !=''",0)),"",9^9)),char(9998)))," "),"where Col1 !='' order by Col3",0)
})

How to insert SUM() function that sums rows with similar ID in a code part of witch is unchangeable?

I am trying to write a quarry in a module for Dolibarr ERP. But module hase a part of code that is predefined and can not be changed. And I need to insert a SUM() function in it that will combine rows with similar id. That i know how to do in a regular MySQL:
SELECT fk_product AS prod, SUM(value) AS qty
FROM llx_stock_mouvement
WHERE type_mouvement = 2 AND label LIKE 'SH%'
GROUP BY fk_product
ORDER BY 1 DESC
LIMIT 26
that gives me what I want :
prod qty
1 13
2 10
BUT module has a predefined unchangeable code :
this part is predefined module writes it himself based on values provider in it:
SELECT DISTINCT
c.fk_product AS com,
c.value AS qty
THIS PART I CAN WRITE IN A MODULES GUI:
FROM
llx_stock_mouvement AS c
WHERE
type_mouvement = 2
AND label LIKE 'SH%'
And this part is predefined:
ORDER BY 1 DESC
LIMIT 26
I would appreciate any help and advice on question is there any workaround that can be done to make my desired and result ampere ? As it would using the first code I posted ?
If you can only modify the bit in the middle box then you might need to use a subquery;
--fixed part
SELECT DISTINCT
c.fk_product AS com,
c.value AS qty
--begin your editable part
FROM
(
SELECT fk_product,
SUM(value) AS value
FROM llx_stock_mouvement
WHERE type_mouvement = 2 AND label LIKE 'SH%'
GROUP BY fk_product
) c
--end your editable part
--fixed part
ORDER BY 1
DESC
LIMIT 26

Mysql diversified results

I got table in db in following way:
id Name Type
1. Aero. Product
2. Ddd. Product
3. Sass. Image
4. Rrrrr. Image
This is just to understand and actual table in much bigger scale.
So the question is how to get diversfied results so the product type wont be like product,product, image, image
If i will do select * from table where 1 order by ‘id’
Results having grouped “type”
I want have results like
1 blabla product
3 blabla image
2 blabla product
4 blabla image
So the records with same type will be spreaded over results and
As much as possible wont stay together
As already suggested its better if you do this type of sorting in your application code, In Mysql you can do this by using user defined variables.
First break your data according to your types, get data for products and assign a row number and do the same with second data set for type image and then merge these sets using union operation and then sort them by row number
select *
from(
select d1.*, #row1:= #row1 + 1 as row
from demo d1,
(select #row1:=0) t1
where `Type` = 'Product'
union all
select d2.*, #row2:= #row2 + 1 as row
from demo d2,
(select #row2:=0) t2
where `Type` = 'Image'
) t
order by t.row, t.Type desc
demo

How do I make a google query that selects for row?

Example:
If my data set is
A B C
1 2 3
5 6 7
4 5 6
I could have "1", "5" and "4" show up by typing =query(A:C, select A).
I could have "1" and "4" show up by typing =query(A:C, select A where B < 6).
Lets say I wanted to query only entries that appeared after a certain row. In this case, row 3 is 4, 5, 6. So if I want only results that are row 3 or below, I could add a fourth column D somewhere, fill column D with =row(), and then have only **** show up by typing
=query(A:C, select A where D >= 3).
But I don't want to have to add a fourth column somewhere and fill it with the =row() formula. The query should be able to do this on its own.
Query parametres
try:
=QUERY(A:C,"select * offset 2",0)
offset parameter is zero base:
0 -- start from row 1
1 -- start from row 2
2 -- start from row 3
so on
You may find more usuful query tips here. Use special words: offset, limit, skipping. For example, to select only odd rows use:
=QUERY(A:C,"select * skipping 2",0)
Filter function
To have full control of rows you select, use this construction:
=filter(A:C,isodd(row(A:C))) -- only odd rows
=filter(A:C,row(A:C)=3) -- only 3-d row
=filter(A:C,row(A:C)>=3) -- all rows >= 3-d row
=query(filter(A:C,row(A:C)>=3),"select *") use filter + query
So, I had an issue like this, and here is what I did. I made a virtual column.
=query(A:C, select A where D >= 3) would then be something like
=query({A:C, ROW(A:C)}, "Select Col1 where Col4 >=3")
You have to make an array to query a column you do not want in your sheet. In doing so you can no longer use Column letters, so then A, B, C then becomes Col1, Col2, Col3 and so on.