SQL count in a table - sql

I'm looking to add some form of count function to my table, but am not quite sure how to do it. The table I have is:
First name
Surname
Tom
James
Mike
James
Tom
James
Mike
Hamilton
William
Morris
Mike
James
Mike
James
I would like it to have a count, of the full names that come up twice or more, like so:
First name
Surname
Count
Tom
James
1
Mike
James
1
Tom
James
2
Mike
Hamilton
1
William
Morris
1
Mike
James
2
Mike
James
3
What is the best way to go about this in SQL? Unfortunately I need the result as per the table above, rather than simply:
First name
Surname
Count
Tom
James
2
Mike
James
3
Mike
Hamilton
1
William
Morris
1

row_number function should work
select *,
row_number() over(partition by [first name],surname order by [first name]) as count
from table_name

I believe using group by on multiple columns would be a more appropriate approach to
select [first name], surname, COUNT(*) from Employee Group BY [first name], surname;
I believe using group by on multiple columns would be a more appropriate approach to
select [first name], surname, COUNT(*) from Employee Group BY [first name], surname;
Result:

Related

How to add primary id referring to 2 unique columns, and 1 unique column in table (multiple joins)

Client First Name Client Last Name Patient Name
John Wick Raffie
John Wick Coco
Rick Thomas Sussie
Peter Parker Hershey
Peter Parker Malt
Rick Thomas Poky
Lucas Desmond Timmy
Result should be:
Client ID Client First Name Client Last Name Patient Name Patient ID
1 John Wick Raffie 1
1 John Wick Coco 2
2 Rick Thomas Sussie 3
3 Peter Parker Hershey 4
3 Peter Parker Malt 5
4 Rick Thomas Poky 6
5 Lucas Desmond Timmy 7
What I did is create a stored procedure to select the contents of the client table with row_number() then join it with patient table with row_number() also, but I want to know if I can do it in one t-sql script only?
Thank you.
I think that DENSE_RANK() function can be helpful in your problem
Select DENSE_RANK() OVER( order by t.[Client First], t.[Client Last Name]) as [Client ID]
, t.[Client First]
, t.[Client Last Name]
, t.[Patient Name]
, DENSE_RANK() OVER( order by t.[Patient Name]) as [Patient ID]
From [Table] t;

Sorting Middle string

I have the following values in a column
Name
Smith
Marry
Tom
Robert
Albert
I have to display in the following order. I want MARRY on top. For the rest of the values ordering is not matter.
Name
Marry
Smith
Tom
Robert
Albert
How can I achieve this?
You can use case in order by:
order by (case when name = 'Marry' then 1 else 2 end)

Calculate number of rows as a column in a query

Sorry about the title, it's hard to summarize what I am asking for.
Given this:
SELECT [FirstName], [LastName], '-', [Thing] FROM.....
John Smith - Plow
James Jones - Plow
James Jones - Shovel
James Jones - Axe
Sarah Lee - Hammer
Sarah Lee - Pliers
I need to get this:
John Smith - Plow - 1
James Jones - Plow - 3
James Jones - Shovel - 3
James Jones - Axe - 3
Sarah Lee - Hammer - 2
Sarah Lee - Pliers - 2
I need to include as a column the sum number of times that same person has appeared in the rows.
This is sounding like a pivot solution, but pivots make my brain hurt. Is there another way? if not, can anyone explain the pivot, if that's what it has to come to?
Thanks!
SELECT *,
COUNT(*) OVER (PARTITION BY FirstName, LastName) cnt
FROM mytable
This may not be the most efficient way to write it but gets the job done.
select
firstname
,Surname
,count(firstname + Surname) over (partition by firstname + surname) as NameOccurences
from table
I actually found a way on my own. I like the "OVER/Partition by" solutions though. Much shorter, but not as obvious to me what they are doing.
Here's what I eventually came up with:
with cte as (
select last_name, first_name, count(1) as theCount
from MetalThings
group by last_name, first_name
)
select m.*,
cte.theCount
from MetalThings m
JOIN cte on (m.last_name = cte.last_name and m.first_name = cte.first_name)

TSQL, counting pairs of values in a table

Given a table in the format of
ID Forename Surname
1 John Doe
2 Jane Doe
3 Bob Smith
4 John Doe
How would you go about getting the output
Forename Surname Count
John Doe 2
Jane Doe 1
Bob Smith 1
For a single column I would just use count, but am unsure how to apply that for multiple ones.
SELECT Forename, Surname, COUNT(*) FROM YourTable GROUP BY Forename, Surname
I think this should work:
SELECT Forename, Surname, COUNT(1) AS Num
FROM T
GROUP BY Forename, Surname

SQL: Add counters in select

I have a table which contains names:
Name
----
John Smith
John Smith
Sam Wood
George Wright
John Smith
Sam Wood
I want to create a select statement which shows this:
Name
'John Smith 1'
'John Smith 2'
'Sam Wood 1'
'George Wright 1'
'John Smith 3'
'Sam Wood 2'
In other words, I want to add separate counters to each name. Is there a way to do it without using cursors?
Use ROW_NUMBER():
SELECT Name, ROW_NUMBER() OVER(Partition BY Name ORDER BY Name) as [Rank]
FROM MyTable
Doing:
select name, count(*) as total from table group by name;
will get you something that looks like this:
name | total
-------------+------------
John Smith | 2
-------------+------------
Sam Wood | 2
-------------+------------
George Wright| 1
This isn't what you really wanted though - ROW_NUMBER(), as ck pointed out, is what you want, but not all databases support it - mysql doesn't, for example. If you're using MySQL, this might help:
ROW_NUMBER() in MySQL