I am trying to calculate the difference between a certain sum of values and the same sum using specific roundup rules (columns 5 and 6):
select
A,
B,
C,
sum(D),
sum(D)/300,
case when sum(D)/300 < 1.5 then 0 else round(sum(D/300), 0) end
from table
group by grouping sets ((A,B,C), ())
The SQL works, but the final row is wrong. The totals in the final row seem correct for column 4 and 5, but in column 6 is doesn't add up the rounded up values of the column, but the rounded up value of the total of column 5...
What am I doing wrong? (Goal: compare the totals of column 5 and 6)
Any help is welcome!
EDIT:
the result right now is something like this (only column 5 and 6):
1,2 0
1,5 2
3,1 3
5,8 6
The total of the second column should say 5 in this example, but it says 6, using the unrounded values...
You are missing the outer SUM:
select
A,
B,
C,
sum(D),
sum(D)/300,
SUM(case when sum(D)/300 < 1.5 then 0 else round(sum(D/300), 0) end) as result
from table
group by grouping sets ((A,B,C), ())
Related
I have a table with multiple columns however I need to calculate a Total Percentage based off 2 columns.
Column 1 has unique identifier (number i.e. 15211, 36521, 45987 etc)
Column 2 has a "Y" or is blank (the criteria is built in to the DWH)
What i am wanting to do is get a Percentage of Column 2 of only the Y fields using Column 1 as the Denominator
Column 1
Column 2
25638
y
69857
n
78561
n
23149
y
based on the example above im expecting 2/4 = 0.50 or 50%
You can divide the result of a conditional aggregation on Column2 = 'Y', and the overall count.
SELECT COUNT(CASE WHEN Column2 = 'y' THEN 1 END) / COUNT(*) AS perc_y
FROM tab
Output:
perc_y
0.5000
If you want a percentage, multiply by 100, round up and concatenate with '%'.
Here's a demo in MySQL, although it should work on all the most common DBMS'.
Let's say I have a table with 2 columns - A & B.
Using plain SQL (No scripts/cursors etc.), how do I (window function?) calculate for EACH value in column A the number of values in column B that are bigger/smaller than it?
Thanks you.
You would use conditional aggregation:
select a,
sum(case when b < a then 1 else 0 end)
from t
group by a;
Window functions don't seem appropriate to this question.
I need some advice in google query language.
I want to count rows depending on date and a condition. But if the condition is not met, it should return 0.
What I'm trying to achieve:
Date Starts
05.09.2018 0
06.09.2018 3
07.09.2018 0
What I get:
Date Starts
06.09.2018 3
The query looks like =Query(Test!$A2:P; "select P, count(B) where (B contains 'starts') group by P label count(B) 'Starts'")
P contains ascending datevalues and B an event (like start in this case).
How can I force output a 0 for the dates with no entry containing "start"?
The main point is to get all needed data in one table in ascending order. But this is only working, if every day has an entry. If there is no entry for a day, the results for "start" do not match the datevalue in column A. 3 in column D would be in the first row of the table then.
I need it like this:
A B C D
Date Logins Sessions Starts
05.09.2018 1 2 0
06.09.2018 3 4 3
07.09.2018 4 5 0
Maybe this is easy to fix, but I don't see it.
Thanks in advance!
You can do some pre-processing before the query. Ex: check if column B contains 'start' with regexmatch and use a double unary (--) to force the boolean values into 1's and 0's. The use query to sum.
=Query(Arrayformula({--regexmatch(Test!$B2:B; "start")\ Test!$A2:P}); "select Col17, sum(Col1) where Col17 is not null group by Col17 label sum(Col1) 'Starts'")
Change ranges to suit.
just a simple question but somehow I can't find an answer here.
I have two columns (A and B). Both contains numbers with zeros and null. I would like to get a division one by the other to get information about the ratio between each single row but I am getting ORA-01476.
I know the divisior is equal to zero but I would like to get in this row a number and not an error for whole query
A B
1 5
2 Null
3 0
NULL 3
0 4
4
I am using sql developer.
If you divide a number by zero you get an error, because the answer to such division is undefined. SQL, however, has a value for undefined: NULL. So make the result NULLinstead:
select a, b, case when b = 0 then null else a / b end as ratio
from mytable;
or
select a, b, a / case when b = 0 then null else b end as ratio
from mytable;
This is standard SQL and works in Oracle as well as in about every other RDBMS. Oracle also provides the function NULLIF as a shorter way to write the expression in the second query.
You can use nullif to return null instead of raising an error:
select A / nullif(B, 0) as division
from YourTable
If your numbers are stored as varchar, cast them to numbers before using them:
select to_number(A) / nullif(to_number(B), 0) as division
from YourTable
I want to sum rows in select from value to value.
for example:
table T:
value: type:
1 5
2 10
2 8
3 10
3 2
I want to sum the values from the last type 10 (the last two "3" values)
do you have an idea how to do it?
thanks in advance!
Select value,sum(type) as SumofType
From T
Group by value
I think you want this SQL.
Select value,sum(type) as Sum_Type
From Ta where value=3
Group by value