I am using TSQL to select data from the database. I want to select all data in a table but if the id is a duplicate add (sum) all column 'a' with same ID and make it just 1 row.
There should be no duplicate IDs in the output.
SELECT DISTINCT id,a,b FROM dbo.test WHERE
id not in (select id from dbo.test) CASE a WHEN a + a??
example:
dbo.test
========
id a
1 4
1 5
2 3
3 2
output:
1 9 <-- two ids of 1 so column 'a' is added together.
2 3
3 2
SELECT ID, SUM(a) as a, SUM(b) as B
FROM MyTable
GROUP BY ID
This is what grouping and aggregation is designed for!
FYI, this is how aggregation works:
When you group and aggregate fields, you are combining records into a single row.
In your OP, you wanted to see a SUM of A for every value of ID. That's what the original query I posted does.
You also want to include B, which is a varchar field, according to your comments.
In that case, you need to decide what to do with B. Since you are grouping multiple rows together, there are (potentially) multiple values of B per value of ID. You need to either:
Group by B as well, which will add extra rows
Apply an aggregation such as MAX(), MIN(), etc. to B
Exclude B from the results list.
Related
I have a table, sample data like this. There are names associated with multiple ids
Id Name
-------
1 A
1 B
2 A
2 B
Result needed
ID Name
--------
1 A
1 B
How to get this? Order of ID doesn't matter
TL; DR
SELECT min(id) as id, name from mytable GROUP BY name
When you need to summarize over multiple records in SQL, each column you include should either be aggregated using a function like min(), max() or sum(); or included in the GROUP BY clause. Here you are looking to pick an arbitrary ID, so why not use the "min()" ID? Then we want each unique name, so we add it to the GROUP BY clause.
I have a table format like below:
Id Code
1 A
1 B
2 A
3 A
3 C
4 A
4 B
I am trying to get count of code combinations like below:
Code Count
A,B 2 -- Row 1,2 and Row 6,7
A 1 -- Row 3
A,C 1 -- Row 4
I am unable to get the combination result. All I can do is group by but I am not getting count of IDs based in combinations.
You need to aggregate the rows, somehow, and do that twice. The code looks something like this:
select codes, count(*) as num_ids
from (select id, group_concat(code order by code) as codes
from t
group by id
) id
group by code;
group_concat() might be spelled listagg() or string_agg() depending on the database.
In SQL Server, use string_agg():
select codes, count(*) as num_ids
from (select id, string_agg(code, ',') within group (order by code) as codes
from t
group by id
) id
group by code;
Hi and thanks in advance.
Trying to simplify two SQL statements without much success.
Count unique records in a table (by unique, I mean the whole record is found only once in the table)
Select unique records in a table (by unique, I mean the whole record is found only once in the table)
Since the table has 30 columns, is there some way to simply specify ALL columns in the table rather than having to include all individually in the SQL?
I got this working where you spell out every column name (where 'col n name' refers to the last column) but it is not ideal since there are just so many columns …
SELECT col 1 name, col 2 name, col 3 name, …, col n name FROM table name
GROUP BY col 1 name, col 2 name, col 3 name, …, col n name
Having Count(*)=1
Thanks
deutz
create view tab_view1 as select DISTINCT * from tab;
select COUNT(*) from tab_view1; -- first desired result
select * from tab_view1; -- second desired result
First occurence is unique, second is not.
If you want to exclude any record that has duplicate:
create view tab_view2 as select tab.*, COUNT(*) AS occurs
from tab group by tab.*
having COUNT(*) = 1;
select COUNT(*) from tab_view2; -- first desired result
select * from tab_view2; -- second desired result
Let's say I have a table that looks like,
id
2
2
3
4
5
5
5
How do I get something like,
id count
2 2
3 1
4 1
5 3
where the count column is just the count of each id in the id column?
You want to use the GROUP BY operation
SELECT id, COUNT(id)
FROM table
GROUP BY id
select id, count(id) from table_name group by id
or
select id, count(*) from table_name group by id
This is your query:
SELECT id, COUNT(id)
FROM table
GROUP BY id
What GROUP BY clause does is this:
It will split your table based on ids i.e all your 1's are separated, then the 2's , 3's and so on. You can assume it like new tables are created where in one table all the 1's are stored, 2's in another , 3's in yet another and so on.
Then after that the SELECT query is applied on each of these separate tables and the result is returned for each of these "groups".
Good luck!
Kudos! :)
The confusing question is best asked through an example. Say we have the following result set:
What I want to do is count how many times one number appears from both columns.
So the returning data set might look like:
ID Counted
0 4
1 2
9 1
13 1
My original thought was to do some sort of addition between the counts on both IDs, but I'm not exactly sure how to GROUP them in SQL in a way that is working.
You can do this with a subquery, GROUP BY, and a UNION ALL, like this:
SELECT ID, COUNT(*)
FROM(
SELECT ID1 AS ID FROM MyTable
UNION ALL
SELECT ID2 AS ID FROM MyTable
) source
GROUP BY ID
ORDER BY ID ASC