Quick SQL group by question - sql

Let's say I have a table with 2 columns number and name.
number name
--------------
0 name1
3 name4
3 name2
5 name1
So the number column has repeats in it and one or more (distinct) names assigned to each number. But for example, number 3 could have the same name as number 5.
How do I show all the distinct names for each number using group by in such a way that I get as a result both number and name.
I am trying to do this but I can't figure it due to must having the columns in SELECT in GROUP BY also.

Is this what you want?
SELECT NUMBER, NAME
FROM TABLENAME
GROUP BY NUMBER, NAME
ORDER BY NUMBER

First Thing 'Number' is a reserved word in sql so don't use it as a column name
Suppose your 'Number' column name is 'Number1"
then try this
select name, number1 from tableName
GROUP BY name, number1

Related

SQL: Need help listing all the unique names and the number of times they appear in two columns

I've got 2 columns filled with names. The list is seemingly random and a person's name could show up in column A or column B (but never in both at the same instance). I want to write a query that will tell me all the names and the number of times each person is in either list. Here's an example of what I'm talking about
Col A
Col B
Mark
Cheryl
Andy
Tom
James
Mark
Tom
Ann
Cheryl
Jeff
Andy
Mark
I'm looking for a query/function that will give me the list of names and the number of times they were seen in either column.
Any help is always appreciated!
Assuming your table name is names and your columns are a,b then this should work:
select n.name, count(n.name)
from (
select a as name from names
union all
select b as name from names
) n
group by n.name;
SELECT
Name,
COUNT(1) as cnt
FROM (
SELECT ColA As Name FROM my_table UNION ALL
SELECT ColB As Name FROM my_table
) t
GROUP BY
Name
db fiddle

Order By clause in sql server

Suppose, there is a table and I need to sort one of its column (name) alphabetically and at the same time I want to sort it by using ID column in asc order based on the condition ( rows that have same name). So, I failed to understand how this will work. Once the records will be sorted by column (name) then will it sort all rows by using id column?
Can someone explain how actually order by clause works in this case
select name,
id
from hack h
order by name,
id
use order by name, id
select name,
id
from hack
order by name,
id
I just tried to understand what you want to know, you want to realize how it happens when the order by clause have two or more columns ,am I right? Let's go to an example,
the first column is id and the second is name,
2 A
5 B
6 A
3 A
1 B
the result of SQL "select name,id from hack order by name,id" will get the result as below
A 2
A 3
A 6
B 1
B 5
see, it will sort first by name column, and then sort id in the same name value group.
That's it ,did I make it clear?
This answers the original question.
In the code you posted:
substring(name, len(name) - 2, len(name))
returns the last 3 characters of the name.
So you are sorting by these last 3 characters and not by name.
When there are 2 names with the same last 3 characters these will be sorted by id.
If there are more than one column names after "order by" keyword, the system orders the records according to the first column just after order by.

Determine the number of times a null value occurs in column B for a distinct value in column A, SQL table

I have a SQL table with "name" as one column, date as another, and location as a third. The location column supports null values.
I am trying to write a query to determine the number of times a null value occurs in the location column for each distinct value in the name column.
Can someone please assist?
One method uses conditional aggregation:
select name, sum(case when location is null then 1 else 0 end)
from t
group by name;
Another method that involves slightly less typing is:
select name, count(*) - count(location)
from t
group by name;
use count along with filters, as you only requires Null occurrence
select name, count(*) occurances
from mytable
where location is null
group by name
From your question, you'll want to get a distinct list of all different 'name' rows, and then you would like a count of how many NULLs there are per each name.
The following will achieve this:
SELECT name, count(*) as null_counts
FROM table
WHERE location IS NULL
GROUP BY name
The WHERE clause will only retrieve records where the records have NULL as their location.
The GROUP BY will pivot the data based on NAME.
The SELECT will give you the name, and the COUNT(*) of the number of records, per name.

MySQL - Is it possible to achieve this using SQL?

Imagine you've got a table with 2 columns: ID and NAME. ID is simply a number, incrementing for each row (as you'd expect). NAME is some random varchar string. NAME can be same for different rows. Now, imagine you want to get the 3 latest occurences in this table, where NAME only may occur once.
For example, if you've got this data:
ID NAME
1 HELLO
2 TEST
3 HELLO
4 HELLO
5 QWERTY
6 HELLO
Then the result of the question should be:
6 HELLO
5 QWERTY
2 TEST
Is it possible achieve this on SQL level?
SELECT
MAX(ID),
Name
FROM
table
GROUP BY
Name
ORDER BY
MAX(ID) desc
LIMIT 3
SELECT MAX(ID), NAME
FROM THAT_TABLE
GROUP BY NAME
See: GROUP BY (Aggregate) Functions
I suppose, you need to use "DISTINCT" for the "name" column:
SELECT DISTINCT name, id FROM table_name ORDER BY id DESC LIMIT 3;
Another way to achieve this is to use "GROUP BY" for "name" (see another answer)

How can I write this SQL SELECT query for this table?

I have this situation in a certain table:
id | name
1 'Test'
2 'Test'
3 'Test'
How can I make a query to SELECT by distinct the name? I also need the ID column, even if I get the first occurrence of the element, e.g. "if the name column repeats, give me the first record with this repetition."
select name, MIN(ID)
from aCertainTable
group by name