SQL pivot-like records arrangement - sql

I have following table which carries records.
ID header value
1 firstname James
1 lastname Tulan
2 firstname Berty
2 lastname O-Nelly
3 firstname Ana
3 lastname Santos
I need to display the records as follows
id firstname Lastname
1 James Tulan
2 Berty O-Nelly
3 Ana Santos
I tried to use SQL PIVOT function. It didn't work properly. Anyone encountered the same?

Assuming the ids in the first table do identify the pairs, you can just do aggregation:
select id,
max(case when header = 'firstname' then value end) as firstname,
max(case when header = 'lastname' then value end) as lastname
from t
group by id;

Related

PostgreSQL - How to extract multiple data from a column in postgresql

I have a table in postgres:
Description
Value
Name
Jane
Last name
Doe
Age
23
Country
USA
And I want it like this:
Name
Last_name
Age
Country
Jane
Doe
23
USA
Please help :)
You can use conditional aggregation. In normal situation, your table has more than 3 rows, and there is another column that identifies which rows belong to the same person - say person_id:
select person_id,
max(case when description = 'Name' then value end) as name,
max(case when description = 'Last name' then value end) as last_name,
max(case when description = 'Age' then value end) as age,
max(case when description = 'Country' then value end) as country
from mytable
group by person_id

I have data in an SQL table in one format now I wanted to insert it into another table but with different format

I Have sample data in a table with the format of
column names: Key, Value, Id
Key Value Id
FirstName Amir 11
LastName Imtaiz 11
Age 25 11
FirstName Zohaib 12
LastName Hassan 12
Age 26 12
FirstName Mahyu 13
LastName Sultan 13
Age 24 13
Now I want to insert it in another table with following format.
Column names Id, FirstName, LastName, Age
Id FirstName LastName Age
11 Amir Imtaiz 25
12 Zohaib Hassan 26
13 Mahyu Sultan 24
I am unsure how to do that.
You can conditional aggregation by id.
SELECT id,
max(CASE
WHEN key = 'FirstName' THEN
value
END) firstname,
max(CASE
WHEN key = 'LastName' THEN
value
END) lastname,
max(CASE
WHEN key = 'Age' THEN
value
END) age
FROM elbat
GROUP BY id;

SQL Server table first name last name

I have two tables
select * from elig
select * from elig1
in the same database.
Table 1
Firstname, Lastname, MemberNo
Table 2
Firstname, Lastname, MemeberNo
Table 1:
ash dassant 1
alex peters 2
john lex 3
kyle passant 4
file leters 5
zed luthr 6
Table 2:
a dassant 1
a peters 2
j lex 3
When I try to select the records from table 1 I get complete firstname and lastname
But when I try to select records from table 2 I get only the first letter of first name and complete lastname
Any help here would be appreciated if I can get the full first name for second table

Challenge with Not Equal to operator

I have a problem to solve. I would like to get the countries where the gender not equal to Female from the following table using only the where clause. I don't want to use the sub query like: select country from table where country not in (select country from table where gender='Female')
Any ideas ?
ID Name Gender Country
1 Jhon Male USA
2 Katie Female USA
3 Steave Male UK
4 Gerry Female UK
5 Brad Male AUS
Regards,
Chandra.
Use not exists
select t.*
from table t
where not exists (select 1
from table
where Country = t.Country and
Gender = 'Female'
);
You can also use group by like that :
select Country
from table t
group by Country
having sum(case when Gender = 'Female' then 1 else 0 end) = 0;
You could avoid subquery and get full rows by using:
SELECT TOP 1 WITH TIES *
FROM tab
ORDER BY SUM(CASE WHEN Gender='Female' THEN 1 ELSE 0 END)
OVER(PARTITION BY Country);
DBFiddle Demo - SQL Server
You can do:
select country
from t
except
select country
from t
where gender = 'Female';
As a set operator, except removes duplicates.
Maybe I got your question wrong but why you don't use:
SELECT country FROM table WHERE gender NOT IN('Female')
Or is it a sub query?

Count distinct same names

I have table where have over 100k information.
ID FirstName
1 Bob
2 Bob
3 Tom
4 John
5 John
6 John
.. ....
Want procedure which will be count how much names are same, For example it must be like :
FirstName Count
Bob 2
Tom 1
John 3
Please help me to write it
It's very basic SQL example, group by column + aggregating results
select
FirstName, count(*)
from Table1
group by FirstName
Try this
select FirstName,Count(FirstName) From TableA group by FirstName
Try this
SELECT FirstName, COUNT(*) As Count
FROM YourTable
GROUP BY FirstName
HAVING COUNT(*) > 1
ORDER BY COUNT(*) DESC
Create Procedure GetCount
as
BEGIN
Select FirstName,Count(*) from tablename group by FirstName
END