Count total from multiple row results using SQL - sql

say I have a table like this
user, count
1 5
2 3
3 20
4 13
5 10
I want to get the total from all the count row, so basically 5+3+20+13+10=51, I want to get the 51
How to do that?
Thanks.

select sum(count) as Total from tablename;

select sum(count) from yourtablename

Related

sql average on count result

I got a table with the following content:
Order_item
Order-id
item-id
1
45
4
45
4
57
5
68
5
32
6
68
I would like to know how many items are contained in average per order.
I tried that sql query:
select count(item-id), order-id
from order_item
group by order-id
that got me the following result:
Order-id
count
1
1
4
2
5
2
6
1
And now I would divide the 6 items of the count through 4 orders which gets me to my result of average 1,5 items per order.
How would I write a SQL query to get the result 1,5?
Divide count of all rows by distinct IDs and multiply by 1.0 to implicitly cast for numeric division.
select Count(*) / (Count(distinct Order_Id) * 1.0)
from Order_item;

SQL How to SUM rows in second column if first column contain

View of a table
ID
kWh
1
3
1
10
1
8
1
11
2
12
2
4
2
7
2
8
3
3
3
4
3
5
I want to recive
ID
kWh
1
32
2
31
3
12
The table itself is more complex and larger. But the point is this. How can this be done? And I can't know in advance the ID numbers of the first column.
SELECT T.ID,SUM(T.KWH)SUM_KWH
FROM YOUR_TABLE T
GROUP BY T.ID
Do you need this one?
Let's assume your database name is 'testdb' and table name is 'table1'.
SELECT * FROM testdb.table1;
SELECT id, SUM(kwh) AS "kwh2"
FROM stack.table1
WHERE id = 1
keep running the query will all (ids). you will get output.
By following this query you will get desired output.
Hope this helps.

SQL: Grouping, Count & Average

Trying an SQL question here. I have a SQLite DB (table):
type_id value
1 26
1 24
2 30
3 5
3 15
I want to achieve the following. For each type_id, I would like to know the number of rows (count) with that type_id and the average value (average) of the group. In the example table I would end up with:
type_id count average
1 2 25
2 1 30
3 2 10
Any ideas? Thanks :)
Just GROUP BY type_id and take the COUNT and AVG:
SELECT type_id, COUNT(*) AS count, AVG(value) AS average
FROM test
GROUP BY type_id
Output:
type_id count average
1 2 25
2 1 30
3 2 10
Demo on dbfiddle

Grouping of values in sql

My dataset looks like this:
Id MaxSpeed Distance
1 112 33
1 89 56
2 100 34
3 125 10
For each Id, I need to set up the count as 1.
Output has to be
Id count
1 1
2 1
3 1
I tried with group by on Id, it does not fetch me this result.
Any help would be really appreciated !!
Perhaps you are overthinking it.
Select Distinct ID,1 as Count From YourTable
Does the count always need to be 1? If so, could this query work?
SELECT DISTINCT ID, 1
FROM your_table;
Try following sql code:
Select distinct Id,1 as count From Table_Name;

Select Distinct and Get Count

I need to get unique records from table and get their count of recurrence. I have query:
SELECT AwardReference.ownerID
FROM AwardReference
WHERE AwardReference.ownerType = 'song';
This returns something like:
ownerID
1
2
4
4
5
5
5
I need do get something like:
ownerID count
1 1
2 1
4 2
5 3
Does anyone knows how to do it?
Also I have to do this task with MS ACCESS...
SELECT AwardReference.ownerID ,
COUNT(*) AS count
FROM AwardReference
WHERE AwardReference.ownerType = 'song'
GROUP BY AwardReference.ownerID