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

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

Related

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

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 Min Max as out put from single column grouped by user in select query

I am not good in SQL raw queries. Please help to get Min Max value grouped by the user_id.
Scenario is:
Table has multiple values in column "B" against user_id and i want to out put the column "B" twice as Min/Max in single output. Required output will looks like:
User_id, MaxB, MinB
1 5 2
2 10 3
Consider the Table has 5 entries for both user(1, 2) but the out put prints it as a single row with max and min of B. Thanks in advance. Suggest the link if i am repeating this question as i was unable to find the solution.
Select Max(B) as MAXB, Min(B) as MinB, User_ID
from TableName
group by User_ID

In an SSRS report builder expression, I am trying to get the sum of a conditional count

I want the sum of a count IF the count is >=3. This gives me a sum of all the counts, regardless if they are <> 3:
=Sum(Iif(CountDistinct(Fields!ENCOUNTER.Value)>=3,1,0))
This produces th same result, the total number of distinct encounters:
=Sum(Iif(CountDistinct(Fields!ENCOUNTER.Value)>=3,CountDistinct(Fields!ENCOUNTER.Value),Nothing))
I want the total number of distinct encounters if there are 3 or more per person. I am grouping on person first, then encounter id.
Ex:
Person Enc
John 1
Bob 4
Sue 2
Ann 3
Total Enc>=3: 2
Based on your requirement, if there are not details rows under ENCOUNTER, you should directly compare the Fields!ENCOUNTER.Value instead of using countdistinct()
Sum(IIF(Fields!ENCOUNTER.value>=3,1,0))
If you have multiple detail rows under ENCOUNTER group level, your requirement can't be achieved because we can't use an aggregation function within an aggregation function. Which means we can't get the distinct ENCOUNTER IDs first, then calculate the total.
I found a workaround. I created another query that selects only those people with 3 or more encounters and added it to the report as a subreport

Average Distinct Values in a single column in Power Pivot

I have a column in PowerPivot that basically goes:
1
1
2
3
4
3
5
4
If I =AVERAGE([Column]), it's going to average all 8 values in the sample column. I just need the average of the distinct values (i.e., in the example above I want the average of (1,2,3,4,5).
Any thoughts on how to go about doing this? I tried a combination of =(DISTINCT(AVERAGE)) but it gives a formula error.
Thanks!!
Kevin
There must be a cleaner way of doing this but here is one method which uses a measure to get the sum of the values divided by the number of times it appears (to basically give the original value) then uses an iterative function to do it for each unique value.
Apologies for the uninspired measure names:
[m1] = SUM(table1[theValue]) / COUNTROWS(Table1)
[m2] = AVERAGEX(VALUES(Tables1[theValue]), [m1])
Assuming your table is caled table1 and the column is called theValue