Copy/pasting query in Google Sheets between cells with automatic replacement of values - sorting

I have this query in my sheet:
=query(A2:A100, "Order by A Desc Limit 20")
I want to copy and paste it to more cells and expect it will update range and column from A2:A100 to B2:B100, C2:C100 etc.. and A in query to B, C etc.
=query(B2:B100, "Order by B Desc Limit 20")
=query(C2:C100, "Order by C Desc Limit 20")
But when I copy/paste, it only updates range, but not query.
How to implement it?

use:
=SORTN(A2:A100; 20; 1; 1; 0)

use this instead for copy-pasting w/no issues:
=query({A2:A100}, "Order by Col1 Desc Limit 20")

Related

List the customer ID of customer who have placed more than one order in DBMS

List the customer ID of customer who have placed more than one order in DBMS
Select count (custid) as "number of orders more than one"
from orders
where order date is less than 1;
You can use a GROUP BY in conjunction with a HAVING clause:
SELECT CustomerId, Count(*) AS NumberOfOrders
FROM Order
WHERE OrderDate < 1
GROUP BY CustomerId
HAVING COUNT(*) > 1
The WHERE clause is applied before grouping. The HAVING clause is applied after grouping and is used when you need to apply constraints on values returned by aggregate functions.
I am not sure what "order date is less than 1;" means, but this condition must be placed in the WHERE clause.
If by "number of orders more than one" you mean number of orders exceeding 1, you can write Count(*) - 1 AS NumberOfOrdersExceedingOne (as #Bohemian pointed out) in conjunction with HAVING COUNT(*) > 1.

Google query date column sort issue

When I use
=query('Sheet1'!B2:E, "select B,C,D,E Order By B DESC",-1 )
where D is a date column the query works. But it does not work when I try to sort "B ASC" , Why?
it works too but your output is at the end of spreadsheet (scroll all the way down). to "fix" this you can do:
=QUERY('Sheet1'!B2:E, "select B,C,D,E where B is not null order By B asc", -1)

Inverting column data through a Query

In order to create a specific chart, I need to look for the values of column H in reverse to which they are presented. I tried via Query:
=QUERY(H2:J, "select H order by J desc", 0)
But it returned in the wrong way as you can see:
I would like to know what would need to be adjusted. Because did not return in the exact inverse sequence of column H as it should in theory happen.
Link to Spreadsheet:
https://docs.google.com/spreadsheets/d/1ehtyuyiAiuQvQUWgMrYBreUMA94jnAliu-VcHfBFi5s/edit?usp=sharing
try:
=ARRAYFORMULA(QUERY({ROW(H2:H), H2:H},
"select Col2
where Col2 is not null
order by Col1 desc", 0))

Using TOP in ORACLE SQL 9

Hello I'am very new to writing SQL and I am trying to find the appropriate way to use TOP in Oracle SQl 9:
My example:
select * from example e, test t
where e.id = t.id
and country = 'USA'
order by state ASC;
What I am trying to do is take the bottom 20 % of my query but I know you cannot use TOP. After researching I still have not found an applicable answer. I know you have to first order them but am unsure of how to then take the bottom 20%(which would be TOP since the order is ASC)
In general (like if you want the top or bottom 17.2% of the rows) you can use row_number() and count() (analytic functions) to get the result.
20% is easier - you are looking for the top (or bottom) quintile. For this, you can use the ntile() function, like so:
select [column_names]
from (
select e.*, t.*, ntile(5) over (order by state) as nt
from ..... etc
)
where nt = 1;
The subquery is your query. The column_names in the outer query are whatever you actually need; you could also use select * but that will show the ntile too (which will be 1 in all rows).
If sorting something in ASCending order gives us the top set then surely sorting in DESCending order can give us the bottom set.
This solution uses the function NTILE() to divide the records into five buckets. The first bucket is the set we want (because sorted in descending order). Sorting in ascending order and taking the fifth quintile would have the same outcome.
select * from (
select e.*
, t.*
, ntile(5) over (order by state desc) nt
from example e, test t
where e.id = t.id
and country = 'USA'
)
where nt = 1
order by state desc
/
You don't say what your sort criteria are, so I've guessed.

Display only the largest count in a group by statment

I'm trying to display only the largest group in this group by statement;
SELECT COUNT(type) AS booking, type FROM booking b, room r WHERE r.rno = b.rno AND r.hno = b.hno GROUP BY type;
I modified it so we get this query response now you can see group double is larger then family.
BOOKING TYPE
5 double
2 family
I know there is a HAVING keyword you can add in order display only a count compared to a number so I could do COUNT(type) HAVING > 2 or similar but that's not very dynamic and that would only work in this instance because I know the two amounts.
ORDER BY COUNT(type) DESC LIMIT 1
There isn't a having statement that does this. But you can use rownum with a subquery:
select t.*
from (SELECT COUNT(type) AS booking, type
FROM booking b join
room r
on r.rno = b.rno AND r.hno = b.hno
GROUP BY type
order by count(type) desc
) t
where rownum = 1;
Just order your query..
order by booking desc
regards
TRY this
SELECT COUNT(type) AS booking, type FROM booking b, room r WHERE r.rno = b.rno AND r.hno = b.hno ORDER BY type DESC LIMIT 1

Resources