Substraction by the column just created in bigquery - sql

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

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

Take a list of names and randomly pair them off (SQL)

I'm trying to do something I thought would be fairly easy and am now on the verge of bashing my head off the desk!
I have a list of names as an array and I want to create random pairs from that list.
So I have
SELECT * FROM (
VALUES
('Angie'),
('Bob'),
('Meg'),
('Colin'),
('Debbie'),
('Eddie'),
('Fiona'),
('Gary'),
('Harriet'),
('Ian'),
('Julie'),
('Kevin'),
('Mary'),
('Noah'),
('Olivia')
) AS t (name)enter code here
and I want to get back something like
Name 1 Name2
1. Olivia Debbie
2. Gary Harriet
3. Bob Mary
4. Noah Colin
5. Ian Fiona
6. Kevin Mary
7. Julie Eddie
8. Angie NULL
Is there a way this can be achieved?
Use row_number() and aggregation:
select max(case when mod(seqnum, 2) = 1 then name end) as name1,
max(case when mod(seqnum, 2) = 0 then name end) as name2
from (select name, row_number() over (order by random()) - 1 as seqnum
from t
) t
group by floor(seqnum / 2);

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?

Check for changes in all other columns based on similarities one column

FirstName LastName SSN Phone EncounterID
Justin Kelley 555-55-5555 517-555-1212 123456789
Justin Kelly 555-55-5555 517-555-1212 123456789
James Smith 444-44-4444 312-555-3434 99944444
James Smith 444-44-4444 312-555-3434 99944444
I have a table like the one above with millions of EncounterIDs. I need to know the number of times there is a difference (defect) in EACH column. My example output would be:
First Name - 2/2
Last Name - 1/2
SSN - 2/2
Phone - 2/2
Any help here?
The data that you basically want is the number of entities that have more than one value in a column.
This is most easily calculated on a column basis:
select sum(case when NumFirstNames <> 1 then 1 else 0 end) as DifferentFirstNames,
sum(case when NumLastNames <> 1 then 1 else 0 end) as DifferentLastNames,
sum(case when NumSSN <> 1 then 1 else 0 end) as DifferentSSN,
sum(case when NumPhone <> 1 then 1 else 0 end) as DifferentPhone
from (select EncounterId, count(*) as Num,
count(distinct FirstName) as NumFirstNames,
count(distinct LastName) as NumLastNames,
count(distinct SSN) as NumSSN,
count(distinct Phone) as NumPhone
from table t
group by EncounterId
) e;
You can format the results however you like.

SQL Group By Help Required

I have a table named People in the following format:
Date | Name.
When I count the people by Grouping By Name with
Select Date, Name, count(*)
From People
Group By Date, Name;
Will give the following
Date Name count(*)
10 Peter 25
10 John 30
10 Mark 25
11 Peter 15
11 John 10
11 Mark 5
But I would like the following result:
Date Peter John Mark
10 25 30 25
11 15 10 5
Is this possible? This is a simple example of a more complicated database. If someone helps me in solving this problem I will use the concept to implement it in my table
Thanks!
Select Date
, count(case when Name = 'Peter' then 1 else null end)
, count(case when Name = 'John' then 1 else null end)
, count(case when Name = 'Mark' then 1 else null end)
From People
Group By Date;
another option different from turbanoff's if, for some reason, you find yourself in a situation that you cant apply a group by:
Select distinct(P.Date),
(select count(*) from People where date=p.date and name='Peter') as Peter,
(select count(*) from People where date=p.date and name='John') as John,
(select count(*) from People where date=p.date and name='Mark') as Mark
From People P