Selecting distinct values from table using two columns - sql

I have following data in the table.
Id Name
1 Abc
2 Abc
3 Xyz
4 Xyz
5 def
6 def
I want following results from the query
Id Name
1 Abc
2 Xyz
3 def
I want to avoid duplicates in the name column.
Any help is greatly appreciated.
Select distinct
id, name
from table A
will not work as ids are having a different values.

Use a group by instead.
select
min(id), [name]
from
tableA
group by [name]
Note that in your example, the ids that corresponds with Xyz are 3 and 4, so getting a 2 next to Xyz is only possible if you break the integrity of the table. If you are just looking for an auto number next to the ids you can do this:
SELECT row_number() OVER (ORDER BY min(id)) id,
name
FROM tableA
group by name

You can get your specific result using:
select row_number() over (order by min(id)) as id, name
from table A
group by name;
Renumbering the rows seems strange, but row_number() will do that.

Related

Get Count Based on Combinations of Values from Second Column

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;

SQL Server : increase the value by one in a field where matching foreign key id

This is probably really easy for someone good at SQL, at that someone isn't me!
I have a database table with two fields in it. the id field is a fk id to another table, and the other field displayorder currently contains all 1's.
I want to, as below, update display order by one, starting at 1 each time the fk id changes. I am using SQL Server. This is what is should look like in the end:-
FKID displayorder
---------------------
1 1
2 1
2 2
3 1
3 2
3 3
4 1
4 2
5 1
5 2
5 3
etc
It looks like you need ROW_NUMBER():
SELECT FKID,
ROW_NUMBER() OVER(PARTITION BY FKID ORDER BY (SELECT 1)) AS displayorder
FROM table
ORDER BY FKID;
Update:
WITH cte AS (
SELECT FKID, displayorder,
ROW_NUMBER() OVER(PARTITION BY FKID ORDER BY (SELECT 1)) AS do
FROM table
)
UPDATE cte
SET displayorder = do;
Please keep in mind that to get stable sort you should ORDER BY some column like PK/timestamp.

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

SQL Separating Distinct Values using single column

Does anyone happen to know a way of basically taking the 'Distinct' command but only using it on a single column. For lack of example, something similar to this:
Select (Distinct ID), Name, Term from Table
So it would get rid of row with duplicate ID's but still use the other column information. I would use distinct on the full query but the rows are all different due to certain columns data set. And I would need to output only the top most term between the two duplicates:
ID Name Term
1 Suzy A
1 Suzy B
2 John A
2 John B
3 Pete A
4 Carl A
5 Sally B
Any suggestions would be helpful.
select t.Id, t.Name, t.Term
from (select distinct ID from Table order by id, term) t
You can use row number for this
Select ID, Name, Term from(
Select ID, Name, Term, ROW_NUMBER ( )
OVER ( PARTITION BY ID order by Name) as rn from Table
Where rn = 1)
as tbl
Order by determines the order from which the first row will be picked.

Distinct SQL Query

I have a SQL Server 2008 database with the following information in a table:
ID Name
-- ----
1 John
2 Jill
3 John
4 Phil
5 Matt
6 Jill
I want to display the unique names in a drop down list. Because of this, I need just one of the IDs associated with the unique name. I know it's dirty. I didn't create this mess. I just need the unique names with one of the ids. How do I write a query that will do that? I know that the following won't work because of the ID field.
SELECT DISTINCT
[ID], [Name]
FROM
MyTable
SELECT MIN(ID) AS ID, [Name]
FROM MyTable
GROUP BY [Name]
This will return the first (i.e. MINimum) ID for each distinct Name
You could also do it with rank over function
SELECT
Id,
Name
FROM
(
SELECT
Id,
[Name],
RANK() OVER (PARTITION BY [Name] Order By Id) As Idx
FROM Test
) A
WHERE Idx = 1
To get understanding about rank over function read this:
http://msdn.microsoft.com/en-us/library/ms176102.aspx