How to select distinct result set in SQL Server? - sql

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.

Related

Count items in column SQL query

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! :)

Number of times one row column equals another row's other column in SQL

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

Select distinct name with random id

I have a table with an id and a name (an a bunch of other stuff not relevant for this query). Now I need an SQL statement that returns one row per distinct name and in that row I need the name and one id (can be any id).
The table is looking something like this:
id | name
---+-----
1 | a2
2 | a2
3 | a4
4 | a4
5 | a2
6 | a3
btw. using Postgres 8.4
Tried various combinations of grouping or joining with self. Is this even possible without creating extra tables?
Arbitrarily choosing to return the minimum id per name.
SELECT name, MIN(id)
FROM YourTable
GROUP BY name
You may look at PostgreSQL wiki. It shows how to select random rows.
You may use random() function to select random rows using ORDER BY clause of SELECT. Example:
SELECT id FROM mytable ORDER BY random()
You can then use GROUP BY to select distinct names. You may need to limit results using LIMIT clause. So the query looks something like this:
SELECT id, name FROM table_name GROUP BY name ORDER BY random() LIMIT 1
select ID, name from table group by name;

select data and if duplicate id add (sum) the data

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.

How can I select an entire row without selecting rows where a certain column value is duplicated?

How can I select an entire row without selecting rows where a certain column value is duplicated?
EDIT: Sorry my question is a bit vague. Assuming i have a table with two columns: names and score. There are duplicate values in names. I want to select distinct names and also select their scores.
Based on the edited information given, this will use the GROUP_CONCAT function to produce the distinct names and a comma-delimited list of scores. If more appropriate, you could substitute another aggregate function (e.g., MIN, MAX, SUM) for the GROUP_CONCAT.
SELECT Name, GROUP_CONCAT(Score)
FROM YourTable
GROUP BY Name
Using the following example data...
name score
----- -----
James 10
James 12
Lisa 45
John 42
...the following queries should return the third and fourth row.
select name, score
from table
where name in(select name
from table
group by name having count(*) = 1);
...less clear, but probable more efficient on MySQL.
select t1.name, t1.score
from (select name
from table
group by name having count(*) = 1
) t1
join table t2 on(t1.name = t2.name)
Try using the DISTINCT clause on the columns you don't want duplicates of.
based on your latest edit, try this:
select
name, SUM(score) AS TotalScore
from YourTable
group by name