Counting Results in SQL - sql

I'm having trouble using COUNT in SQL...The following query returns two rows, but then returns the raps column as 137. So I believe it's counting the total number of operation_id columns in the dataset instead of from the results.
Is there any way to make it count only the columns from the results, so that raps returns as 1 in each of the columns? I would then use PHP to add them together.
//Query
SELECT DISTINCT hrap_id,
operation_id,
COUNT (operation_id) AS raps,
operation_type
FROM view_rappels
WHERE year = '2013' AND crew_id = '4'
GROUP BY hrap_id, operation_type, operation_id
//Results
10.00 702020000.00 137.00 operational
1.00 702020000.00 137.00 operational

You need to put DISTINCT inside of the count function like so
COUNT(DISTINCT operation_id) AS raps

Related

How do I aggregate data in sql for multiple rows of data by column name?

hi im new to sql and trying to understand how to work with data structures. I have a table
fact.userinteraction
interactionuserkey visitdatecode
0 20220404
1 20220404
5 20220402
5 20220128
If the interaction userkey number repeats then, i want a column called number of visits. in this case, for interactionuserkey 5, there are 2 total visits since its repeated twice. for interactionuserkey 0, number of visits =1 and so on. Basically, sum duplicates in column 1 and give total count AS number of visits. How do i do this?
In sql, it's resolved using basic aggregation
select interactionuserkey, count(*)
from your_table
group by interactionuserkey

Can you sum rows from one query's result set and use that total in another query?

I have a query that will sum scores from the top users of a website:
SELECT SUM(score)
FROM hacker_news
GROUP BY user
HAVING score > 200;
This produces a result set with 4 rows with 4 integers.
I want to sum these 4 rows and divide it by the sum of scores from the entire table.
I have tried running it using a subquery and a temporary
any help in writing the simplest query possible would be much appreciated. Ideally I'd like to know if this doable without creating a temporary table and joining
If you want the proportion of totals scores based on those with more than 200, then you can use conditional aggregation:
SELECT SUM(CASE WHEN score > 200 THEN score ELSE 0 END) / SUM(score)
FROM hacker_news

Postgres Distinct or Group by

I have a table with field bhk, size, price. i am using distinct to get unique by following query
1 Query
select distinct(bhk,size,perprice),bhk,size,price from project_units;
and i and also querying by
2 Query
select bhk, array_agg(size) as size from project_units where project_id = '12' and bhk is not null and not bhk = '1bhk' group by bhk
as a result i get
[
{
bhk:"1bhk",
size:{123,121,231}
},
{
bhk:"2bhk",
size:{223,321,131}
}
]
By 2 Query i also want to retrieve price also or there is any other way to get distinct on bhk size and price by 2 Query
and what price would you want there? as in the queries you write need to fit all data, not just for a given set. So let's imagine you have a
bkh='1bhk'
size='123'
price='1'
and
bhk='1bhk'
size='321'
price=2
so for a distinct bhk there are 2 possible prices.
If you know which price you want (min, max, average, sum) then you can add it to the query - just it needs to be a group expression

Query for SUM of grouped COUNT in SQL Query table

Please take this as an example where my primary table is
ID Name
-- -------
1 Alpha
2 Beta
3 Beta
4 Beta
5 Charlie
6 Charlie
as there is duplication in Name column. Resultant Table after grouping them by name, with count column is -
Name Count
------- -----
Alpha 1
Beta 3
Charlie 2
SUM 6
Here SUM is taken out as separate row of all the resultant COUNT column, I am trying to get SUM of all the rows from the resultant Count function from primary table but as separate query for SUM unlike separate row
My table has 2 fields Roles and User_Id.
I have already tried Below query
select orl.role ,
SUM (orl.role) as "No of Users"
from org_user_roles orl
group by orl.role
i think this is a string column with numerical values. So need to cast it to int before performing the sum() operation
select orl.role,
sum(orl.user_id::int) as "No of Users"
from org_user_roles orl
group by orl.role
If you want to count rows (users) for each role, use the COUNT aggregate function - not SUM:
select "role", count(*) as "No of Users" from org_user_roles group by "role";
To get the sum of these grouped counts - which is just the overall row count - use:
select count(*) as "Sum" from org_user_roles;

How to get three count values from same column using SQL in Access?

I have a table that has an integer column from which I am trying to get a few counts from. Basically I need four separate counts from the same column. The first value I need returned is the count of how many records have an integer value stored in this column between two values such as 213 and 9999, including the min and max values. The other three count values I need returned are just the count of records between different values of this column. I've tried doing queries like...
SELECT (SELECT Count(ID) FROM view1 WHERE ((MyIntColumn BETWEEN 213 AND 9999));)
AS Value1, (SELECT Count(ID) FROM FROM view1 WHERE ((MyIntColumn BETWEEN 500 AND 600));) AS Value2 FROM view1;
So there are for example, ten records with this column value between 213 and 9999. The result returned from this query gives me 10, but it gives me the same value of 10, 618 times which is the number of total records in the table. How would it be possible for me to only have it return one record of 10 instead?
Use the Iif() function instead of CASE WHEN
select Condition1: iif( ), condition2: iif( ), etc
P.S. : What I used to do when working with Access was have the iif() resolve to 1 or 0 and then do a SUM() to get the counts. Roundabout but it worked better with aggregation since it avoided nulls.
SELECT
COUNT(CASE
WHEN MyIntColumn >= 213 AND MyIntColumn <= 9999
THEN MyIntColumn
ELSE NULL
END) AS FirstValue
, ??? AS SecondValue
, ??? AS ThirdValue
, ??? AS FourthValue
FROM Table
This doesn't need nesting or CTE or anything. Just define via CASE your condition within COUNTs argument.
I dont really understand what You want in the second, third an fourth column. Sounds to me, its very similar to the first one.
Reformatted, your query looks like:
SELECT (
SELECT Count(ID)
FROM view1
WHERE MyIntColumn BETWEEN 213 AND 9999
) AS Value1
FROM view1;
So you are selecting a subquery expression that is not related to the outer query. For each row in view1, you calculate the number of rows in view1.
Instead, try to do the calculation once. You just have to remove your outer query:
SELECT Count(ID)
FROM view1
WHERE MyIntColumn BETWEEN 213 AND 9999;
OLEDB Connection in MS Access does not support key words CASE and WHEN .
You can only use iif() function to count two, three.. values in same columns
SELECT Attendance.StudentName, Count(IIf([Attendance]![Yes_No]='Yes',1,Null)) AS Yes, Count(IIf([Attendance]![Yes_No]='No',1,Null)) AS [No], Count(IIf([Attendance]![Yes_No]='Not',1,Null)) AS [Not], Count(IIf([Attendance]![Yes_No],1,Null)) AS Total
FROM Attendance
GROUP BY Attendance.StudentName;