how to sum multiple rows with same id in SQL Server - sql

Lets say I have following table:
id | name | no
--------------
1 | A | 10
1 | A | 20
1 | A | 40
2 | B | 20
2 | B | 20
And I want to perform a select query in SQL server which sums the value of "no" field which have same id.
Result should look like this,
id | name | no
--------------
1 | A | 70
2 | B | 40

Simple GROUP BY and SUM should work.
SELECT ID, NAME, SUM([NO])
FROM Your_TableName
GROUP BY ID, NAME;

Use SUM and GROUP BY
SELECT ID,NAME, SUM(NO) AS TOTAL_NO FROM TBL_NAME GROUP BY ID, NAME

SELECT *, SUM(no) AS no From TABLE_NAME GROUP BY name
This will return the same table by summing up the no column of the same name column.

Related

SQL sum all rows by id

I am trying to learn SQL queries and have this scenario where I have this table:
Table1
ID | Name | Hour
----------------
1 | Mark | 2
2 | ken | 1.5
3 | jake | 3
1 | Mark | 1.8
2 | ken | 1
Expected result
ID | Name | Hour
----------------
1 | Mark | 3.8
2 | ken | 2.5
3 | jake | 3
I have tried to use the sum() function but I get an error.
My query:
Select ID, Name, Sum(Hour)
From Table1
Where ID = ID
Response:
Kindly use Group by clause whenever the Aggregate functions (min(),max(),sum(),count(),...etc.,) and columns are used together.
Non aggregated columns present in SELECT columns should be used in GROUP BY clause.
For using aggregate function you need to use Group By like this:
Select ID, Name , Sum(Hour) AS Hour From Table1
Group By ID, Name
Order By ID

How to count/increment the current number of occurances of a table column in a MS SQL select

I have a table which looks like this:
id | name| fk_something
----------------
0 | 25 | 3
1 | 25 | 2
2 | 23 | 1
and I want to add another column with a number which increments everytime row name occurs, e.g.:
id | name| fk_something| n
--------------------------
0 | 25 | 3 | 1
1 | 25 | 2 | 2
2 | 23 | 1 | 1
I'm not really sure how to achieve this. Using count() I will only get the total number of occurances of name but I want to increment n so that I have a distinct value for each row.
You want row_number() :
select t.*, row_number() over (partition by name order by id) as n
from table t;
You may try using COUNT as an analytic function:
SELECT
id,
name,
fk_something,
COUNT(*) OVER (PARTITION BY name ORDER BY id) n
FROM yourTable
ORDER BY
id;
Demo

Identify the counts of each distinct value in one column in sqlite3

I am trying to identify the count of each distinct value in one column (name) in a table called brgy.
---------------------
| ID | name |
---------------------
| 1 | Alfonso |
| 2 | Arakan |
| 3 | Poblacion |
| 4 | Ilaya |
| 5 | Poblacion |
----------------------
I tried using this code but it keeps giving the COUNT as 1 despite Poblacion appearing twice in the name column:
SELECT name,COUNT(name) AS distinct_name
FROM (
SELECT DISTINCT name
FROM brgy
GROUP BY name
)
GROUP BY name;
The intended output should eliminate duplicate names but sum up the number of times the distinct name appears in the name column:
Expected Output is as below,
-----------------------------
| name | distinct_name |
-----------------------------
| Alfonso | 1 |
| Arakan | 1 |
| Ilaya | 1 |
| Poblacion | 2 |
-----------------------------
A simple GROUP BY name:
SELECT name, COUNT(name) AS distinct_name
FROM brgy
GROUP BY name;
you don't need the subquery:
SELECT DISTINCT name FROM brgy GROUP BY name
because GROUP BY name takes care of it.
Just removed the subquery because it will give you unique entry :
select name, count(*) as distinct_name
from brgy
group by name
order by distinct_name, name;

Hive query selecting top 2 rows by percentage count and display as columns

I have a table something like below in my hadoop cluster
ID | CATEGORY | COUNT
101 | A | 40
101 | B | 40
101 | C | 20
102 | D | 10
102 | A | 20
102 | E | 30
102 | F | 40
I have to write a Hive query which will show IDs and top 2 categories by percentage count as columns. So my result table should look like
ID | CAT1 | % | CAT2 | %
101 | A | 40 | B | 40
102 | F | 40 | E | 30
Please keep in my mind that this is only a sample table which I have kept very simple for explaining purpose.
To get the top 2 per ID, you can use the rank() function, see example here.
To get percentage out of overall, you can join on ID with an aggregate table:
select ID,sum(count) as sum from input_table group by ID
And finally, if you want to turn the table from ID, Cat, % to one ID per row, you would need to use collect_list for Cat and % in a sub query and then create a column for the array elements
Select ID, categories[0], pcts[0],categories[1], pcts[1] from (
Select a.ID, collect_list(Cat) as categories , collect_list(Count/sum) as pcts from (
Select ID, Cat, Count, rank from (
SELECT ID, Cat, Count,
rank() over (PARTITION BY ID ORDER BY Count DESC) as rank
FROM input_table) inner where rank <= 2 ) a,
(select ID,sum(count) as sum from input_table group by ID) b where a.ID = b.ID
group by a.ID ) inner;

SQL : Getting duplicate rows along with other variables

I am working on Terradata SQL. I would like to get the duplicate fields with their count and other variables as well. I can only find ways to get the count, but not exactly the variables as well.
Available input
+---------+----------+----------------------+
| id | name | Date |
+---------+----------+----------------------+
| 1 | abc | 21.03.2015 |
| 1 | def | 22.04.2015 |
| 2 | ajk | 22.03.2015 |
| 3 | ghi | 23.03.2015 |
| 3 | ghi | 23.03.2015 |
Expected output :
+---------+----------+----------------------+
| id | name | count | // Other fields
+---------+----------+----------------------+
| 1 | abc | 2 |
| 1 | def | 2 |
| 2 | ajk | 1 |
| 3 | ghi | 2 |
| 3 | ghi | 2 |
What am I looking for :
I am looking for all duplicate rows, where duplication is decided by ID and to retrieve the duplicate rows as well.
All I have till now is :
SELECT
id, name, other-variables, COUNT(*)
FROM
Table_NAME
GROUP BY
id, name
HAVING
COUNT(*) > 1
This is not showing correct data. Thank you.
You could use a window aggregate function, like this:
SELECT *
FROM (
SELECT id, name, other-variables,
COUNT(*) OVER (PARTITION BY id) AS duplicates
FROM users
) AS sub
WHERE duplicates > 1
Using a teradata extension to ISO SQL syntax, you can simplify the above to:
SELECT id, name, other-variables,
COUNT(*) OVER (PARTITION BY id) AS duplicates
FROM users
QUALIFY duplicates > 1
As an alternative to the accepted and perfectly correct answer, you can use:
SELECT {all your required 'variables' (they are not variables, but attributes)}
, cnt.Count_Dups
FROM Table_NAME TN
INNER JOIN (
SELECT id
, COUNT(1) Count_Dups
GROUP BY id
HAVING COUNT(1) > 1 -- If you want only duplicates
) cnt
ON cnt.id = TN.id
edit: According to your edit, duplicates are on id only. Edited my query accordingly.
try this,
SELECT
id, COUNT(id)
FROM
Table_NAME
GROUP BY
id
HAVING
COUNT(id) > 1