how to calculate nested topN use clickhouse sql - clickhouse

how to get result like this picture by one SQL?
first, gets top2 services by count desc,
then in every service,
gets top3 levels by count desc,
finally in every level,
gets top2 envs by count desc.
Please help me to solve this problem.

Related

How would I get total balance after each transaction

Hei guys. I am desperately seeking help.
Please take a look at this image first.
Thanks. Now
Please look at the table below. The rightmost column REMAINING BALANCE is holding the SUM of credit_amount - debit_amount. But when I add a new amount of credit it changes in the entire column. For example, the REMAINING BALANCE column has 102500.0. If I add 500 of credit is supposed to give me 102500 + 500 = 103000 only in that specific row. 102500 will stay same in the previous row. But my problem is, it changes in the entire column.
And I have used this repository to sum the total amount of credit and subtract total amount of debit.
// Find remaining balance
#Query("SELECT SUM(b.credit_amount - b.debit_amount) FROM Mosque b")
double remainingBalance();
Thanks for your attention.
Well, it seems that you need to specify a criteria in your query, otherwise, the SQL query (or JPQL in your case) will be applied to the entire table...
I'd do something like:
#Query("SELECT SUM(b.credit_amount - b.debit_amount) FROM Mosque b where b.id =?")
double remainingBalance(int id);
Otherwise, without criteria, for each Domain Object you have, you will query the entire table. Hence, you will always get the same result. If you run this query in your Database, you will see that you will have only one result, as SUM is "summing" all your rows together.

Oracle duplicate field but still correct

So i built a query for my leadership team that was correct, but i dont understand why oracle gave me the correct answer.
i have 3 tables that i needed to get data out of in order to get the total billed amount.
Here is my query (please forgive me, my 2nd post and im not sure how to properly format my querys)
select b.total_amount_billed as billed from t1.billing_information b
where b.billing_no in
(select h.billing_no
from t1.res_history h where h.res_seq_no in
(Select r.reservation_seq_no
from t1.res r where r.customer_order_no in ('THO40000') ))
so in the deepest select, i take the the sequence number where my customer order number was THO40000, this query returns 2 sequence numbers.
the second sub query returns the billing numbers for my order from the history table where the sequence number match, in this case for this order they both use the same billing number, 312000.
the final select, returns my total billed amount where it matched my billing numbers it found, in my case $110.
the query works, but what i dont understand is why is it not duplicated? why does it not return 110, for each time it found 312000, giving me 2 records of 110? the billing number is a PK in the billing_information table. im not sure why it worked without me using the distinct keyword on the query for the billing number.
anyway thanks for the help, ill do my best to explain if you have questions!
You are being saved because you used IN to get the billing_no values to use, rather than an INNER JOIN between the two tables using b.billing_no = h.billing_no. A join would have duplicated the records, but your IN query is essentially this:
select b.total_amount_billed as billed
from t1.billing_information b
where b.billing_no in (312000, 312000);
If there is a single row in billing_information having billing_no equal to 312000, it is in the list, so the WHERE condition is true and it is included in the results. The fact that it is in the list twice doesn't make the IN condition "more true".

Sum on Count in hql - error Not yet supported place for UDAF 'count'

I'm new here so please be gentle, my first question after using this website for a long time regards the below:
I'm trying to create a sum of count of events in the past 30 days:
select key, sum((COALESCE(count(*),0)))
from table
Where date>= '2016-08-13'
And date<= '2016-09-11'
group by key;
but the sum doesn't seem to work. i'm looking at the last 30 days, and i would like to count any row that exists for each key, and then sum the counts (i need to count on a daily basis and then sum all day's count).
If you can offer any other way to deal with this issue i'm open for suggestions!
Many thanks,
Shira
You can't nest aggregate functions in HQL (or SQL). However, if you just want a count of records falling within range for each key, then you can simply just use COUNT(*):
select key, count(*)
from table
where date >= '2016-08-13' and
date <= '2016-09-11'
group by key;
It looks like there was a couple things wrong with your code.
I've written this for you, haven't tested it but it passes the syntax test.
SELECT COUNT(key) AS Counting FROM tblname
WHERE date>= '2016-08-13'
AND date<= '2016-09-11'
GROUP BY key;
And this might help you. You should definitely be using COUNT for this query.
I'm not sure if it's related but there might be an issue with calling a field 'key' I kept receiving syntax errors for it.
Hope I was able to help!
-Krypton

OBIEE Merge two queries (join)

I need help.
I am new to obiee (recently moved from business objects and re-creating all reports in obiee).
Here is an example I need help with. I have created an analysis where I am listing all orders with their target delivery dates and number of products in each order.
Order Id......Target Delivery Date...No of products
Abc....1/1/2016.....5
I want to add to a column next to No of products called "No of prods delivered on time". I want to compare delivery date of each product within a order with the target delivery date and
Give count of products delivered within the target date.. So the output should be
Abc....1/1/2016....5.....3
Where 3 is number of products delivered on time.
I could do it in BO by running two queries and merging them however in obiee I am not able to add second query to my analysis. I did try at product level using case when target date >=delivery date then 1 else 0 and wrapped this with sum function to aggregate but it didn't work ..
Appreciate your help in this. Searching for this topics give me results for running queries from multiple subject area :(
You also have unions in OBIEE, you union the results of 2 queries which return the same structure, so you have query A with Order ID, Target Date, No Products and a Dummy column with a 0 and default agregation Sum, and a second query with Order ID, Target Date, Dummy column summing 0 and the number of products delivered.
You do all this in the criteria tab of the analysis. It's important the order in which you put your columns, because that's what OBIEE is using to do the union.
Regards

Oracle - SQL developer query using count

I have this table called, ACTIVITIES:
Name Activity Minutes
JOSE videogames 50
MARISSA videogames 50
TINA CHESS 90
JAKE CHESS 95
For each activity in the table, how do I make a count of how many persons did that activity? I would like to print the activity, the count, and the sum of the minutes in my query.
I tried working by parts & this was my attempt:
SELECT Distinct(Activity), count(Activity) as Total
FROM ACTIVITIES;
However, the query result gave me an error saying: "not a single-group group function"
You can use a group by clause to first group the table by activity then do the count and the sum. For example:
SELECT count(*), sum(NUMOFMINUTES), Activity
FROM activities
GROUP BY activity
Use group by to get the count/sum per activity like so:
select activity, count (activity) as totalcount, sum(minutes) as totalminutes
from activities
group by activity
If you want to get some aggregate value such as a count of a specific value, you first need to group the data into subsets based on the distinct values of that column. That is what that error message was referring to. Note that when you group by activity, the records will consider and display only the distinct values of activity, so you won't need to use the distinct keyword.
TRY this
SELECT DISTINCT(Activity),COUNT(Activity) AS Total FROM ACTIVITIES
GROUP BY Activity
Whenever you use any agreegate function and retrieves another field with it on which you are not performing agreegation then you have to use group by clause with non-agreegate field. so add 'group by Activity' at the end of your query.

Resources