SQL Nested Aggregation - sql

I am working with SQL Language.
I have a table named parta. I want to count the fields 40b1 and 40b2 and find the sum of this. My query is here.
select
count(40b1) as 40b1,
count(40b2) as 40b2,
sum(count(40b1) + count(40b2) ) as sum,
code/100 as code
from parta
where 40b1=true and mandays>=1000
group by code/100 ;
Expected output
40b1 40b2 sum code verticalsum
5 5 10 20 7
2 2 4 21 7
How it done? Please help.
For getting this verticalsum column, what query can I use?

You don't need to SUM() the COUNT()'s. Just add them together.
select count(40b1) as 40b1,
count(40b2) as 40b2,
count(40b1) + count(40b2) as sum,
code/100 as code
from parta
where 40b1=true and mandays>=1000
group by code/100 ;

Related

How to calculate a progressive sum within an Access query

I have a query, let's call it qry_01, that produces a set of data similar to this:
ID N CN Sum
1 4 0 0
2 3 3 3
5 4 4 7
8 3 3 10
The values shown in this query actually come from a chain of queries and from a bunch of different tables.
The corrected value CN is calculated within the query, and counts N if the ID is not 1, and 0 if it is 1.
The Sum is the value I want to calculate by progressively summing up the CN values.
I tried to use DSUM, but I came out with nothing.
Can anyone please help me?
You could use a correlated subquery in the following way:
select t.id, t.n, t.cn, (select sum(u.cn) from qry_01 u where u.id <= t.id) as [sum]
from qry_01 t

How can i get incremental counter with sql?

Can you help me with sql query to get the desired result
Database used :- Redshift
requirement is
I have 3 columns as:- dish_id,cateogory_id,counter
So i want counter to increase +1 if the dish_id is repeated and if not it should remain 1
the query i need should be able to query the source table and get the results as
dish_id category_id counter
21 4 1
21 6 2
21 6 3
12 1 1
Unless I missunderstood your question, you can accomplish that using window functions:
SELECT *,row_number() OVER (PARTITION BY dish_id) FROM my_table;

Dividing summed field by another summed field in the same query

How to divide a summed field by another summed field in the same query.
Example: lets have the query "querySummary" which its field have been grouped already
SID SumOfCredits SumOfMarks
1 3 18
2 2 20
3 4 40
Group By Sum Sum
I want to add another field named "FAvg" to the same query that builds up of dividing "SumOfMarks" by SumOfCredits, so the the result should be as following
SID SumOfCredits SumOfMarks FAvg
1 3 18 6
2 2 20 10
3 2 40 20
Any help please ? many Thanks
Replace "Sum" in the "Total" row by "Expression" and in the "Field" row use the expression:
FAvg: Sum(Mark)/Sum(Credit)
You'll get something like this:
(The other Sum columns are not required for the FAvg expression)
The SQL looks like this:
SELECT
Table1.SID,
Sum(Table1.Credit) AS SumOfCredit,
Sum(Table1.Mark) AS SumOfMark,
Sum([Mark])/Sum([Credit]) AS FAvg
FROM
Table1
GROUP BY
Table1.SID;

How to write query using self join and group by?

I have sql server 2008 db table FILE_DETAILS in following format.
ID FileName Filesize_in_MB
--------------------------------
1 a.txt 5
2 b.txt 2
3 c.txt 2
3 d.txt 4
4 e.txt 6
4 f.txt 1
4 g.txt 2
5 h.txt 8
6 i.txt 7
now what i want to fetch is as bellow
ID FileName Filesize_in_MB
--------------------------------
1 a.txt 5
2 b.txt 2
3 c.txt;d.txt 6
4 e.txt;f.txt;g.txt 9
5 h.txt 8
6 i.txt 7
In above results what happens ID became unique key and FILENAME has get attached and separated by ; and also FILESIZE_IN_MB field in sum of group by ID
I tried with various combination like groupby + self join, also sub queries and all that
but i think i missing something.
is it possible to handle this in SQL query?
Thanks in advance
Try this:
SELECT ID,
STUFF(( SELECT ';' + [FileName]
FROM FILE_DETAILS
WHERE ID = f.ID FOR XML PATH('')), 1, 1, ''),
SUM(Filesize_in_MB)
FROM FILE_DETAILS f
GROUP BY ID
Here's some more information:
Concatenate many rows into a single text string?
You should be able to do this using a group by. Aggregating Filesize_IN_MB can be done using sum as aggregator. However, to aggregate FileName you may need to create an AGGREGATE in SQL SERVER 2008R2. This will allow you to use Concatenate as an aggregation function.
select Concatenate(FileName), sum(Filesize_IN_MB) FROM FILE_DETAILS group by ID
There is another way of aggregate concatenation which seems fairly simple but I haven't tried.

How to find percentage in SQL from list of zeros and ones?

I have result set like -
id achieved
1 0
2 1
3 1
4 0
5 0
The Percentage should be 2/5 i.e. 40 %. How can I write a SQL Query to achieve something like this ? I would prefer not to use and nested select as the actual query is already doing quite a bit. Thanks !
select avg(achieved) from ...
Note that you will have to use a group by function to include categories:
select gender, avg(achieved) from ... group by gender