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
Related
My query is supposedly create new column based on the keyword in data using bigquery. For example if in data consists 'Mike' it will create Mike column, 'John' will create John column and the list goes on..
However, I want to create a 'other' column that is the substraction of overall name with the column I just created.
My code example (wrong at SUBSTRACT function):
SELECT
COUNT(Name) as n_name,
SUM(CASE WHEN Name LIKE '%MIKE%' THEN 1 ELSE 0 END) AS Mike,
SUM(CASE WHEN Name LIKE '%JOHN%' THEN 1 ELSE 0 END) AS John,
SUM(CASE WHEN Name LIKE '%MICHAEL%' THEN 1 ELSE 0 END) AS Michael,
.....
SUBSTRACT (n_name ,Mike and John) AS Others
FROM t
Is there any way to do some substraction by the column I just created ?
you can use below approach (BigQuery Standard SQL)
SELECT *,
n_name - Mike - John - Michael AS Other
FROM (
SELECT
COUNT(Name) AS n_name,
COUNTIF(Name LIKE '%MIKE%') AS Mike,
COUNTIF(Name LIKE '%JOHN%') AS John,
COUNTIF(Name LIKE '%MICHAEL%') AS Michael,
FROM t
)
SELECT
*
,n_name - Mike - John - Michael AS Other
FROM
(
SELECT
COUNT(Name) AS n_name
,COUNT(CASE WHEN Name like '%MIKE%' THEN 1 END) AS Mike
,COUNT(CASE WHEN Name LIKE '%JOHN%' THEN 1 END) AS John
,COUNT(CASE WHEN Name LIKE '%MICHAEL%' THEN 1 END) AS Michael
FROM
t
) aa
Hi all I am quite new to SQL. I have a table (TABLE1) with two columns as below
Name age
--------
jim 18
jim 21
dave 18
dave 18
john 23
john 41
I need to create a view in SSMS which lists distinct ages for each name in a separate column as below
Jim Dave John
---------------
18 18 23
21 41
I have tried a sub query like
SELECT DISTINCT AGE FROM TABLE1 WHERE NAME = JIM
But I encountered a sub query cannot return more than one value.
You can use row_number() & do aggregation :
select max(case when name = 'jim' then age end) as jim,
max(case when name = 'dave' then age end) as dave,
max(case when name = 'john' then age end) as john
from (select t.*, row_number() over (partition by name order by age) as seq
from table t
) t
group by seq;
I have a table population_table that contains columns with a user_id, provider_name, and city. I want to count the number of times a user appears in each city, per provider. So for instance, I would want the output to look something like this:
provider_name | Users | Atlanta | Chicago | New York
______________________________________________________
Alpha 100 50 25 25
Beta 200 100 75 25
Kappa 500 300 100 100
I tried using:
select provider_name, count (distinct user_id) AS Users, count(city) AS City
from population_table
group by provider_name
How can I write this query to get the breakdown of the users per provider per city?
I think you want conditional aggregation. It is not clear from your description that count(distinct) is necessary. So I would try this first:
select provider_name, count(*) AS Users,
sum(case when city = 'Atlanta' then 1 else 0 end) as Atlanta,
sum(case when city = 'Chicago' then 1 else 0 end) as Chicago,
sum(case when city = 'New York' then 1 else 0 end) as New_York
from population_table
group by provider_name;
If count(distinct) is necessary:
select provider_name, count(distinct user_id) AS Users,
count(distinct case when city = 'Atlanta' then user_id end) as Atlanta,
count(distinct case when city = 'Chicago' then user_id end) as Chicago,
count(distinct case when city = 'New York' then user_id end) as New_York
from population_table
group by provider_name
If you have a variable number of cities, I do not know how to supply the list in SparkSQL. But using pyspark, you could create output table from input like this:
counts = input.groupBy('provider_name', 'city').count().cache()
countsPerProvider = counts.groupBy('provider_name').count().withColumnRenamed("count", "users")
pivoted = counts.groupBy("provider_name").pivot("city").sum('count')
table = pivoted.join(countsPerProvider, pivoted["provider_name"] == countsPerProvider["provider_name"]).select(pivoted["*"], countsPerProvider["users"])
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?
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;