I need to combine three tables and find the total count for fruit after combining the three tables using SQL. The name of the fruit columns in the three tables are pr16pnk.fruit, pr16puf.fruit, and pr16yag.fruit. I have successfully found the results when the tables are separated, but I am having trouble combining the results. Listed below is the code that I used. I also need help putting it in descending order.
SELECT pr16pnk.fruit, COUNT(*)
FROM pr16pnk
GROUP BY pr16pnk.fruit
UNION ALL
SELECT pr16puf.fruit, COUNT(*)
FROM pr16puf
GROUP BY pr16puf.fruit
UNION ALL
SELECT pr16yag.fruit, COUNT(*)
FROM pr16yag
GROUP BY pr16yag.fruit
Use aggregators and ORDER BY, query is as simple as below
SELECT sum(A.fruit ,B.fruit, C.fruit)
FROM pr16pnk A
pr16puf B'
pr16yag C
ORDER BY fruit desc;
Just use a subquery:
SELECT fruit, SUM(cnt)
FROM ((SELECT pr16pnk.fruit, COUNT(*) as cnt
FROM pr16pnk
GROUP BY pr16pnk.fruit
) UNION ALL
SELECT pr16puf.fruit, COUNT(*) as cnt
FROM pr16puf
GROUP BY pr16puf.fruit
) UNION ALL
(SELECT pr16yag.fruit, COUNT(*) as cnt
FROM pr16yag
GROUP BY pr16yag.fruit
)
) f
GROUP BY fruit
Try this,
select f.fruit, count(*) from
(
SELECT pr16pnk.fruit from pr16pnk
union
SELECT pr16puf.fruit from pr16puf
union
SELECT pr16yag.fruit from pr16yag
) f
group by f.fruit
order by f.fruit desc
I have data like below -
Year,winning_country
2001,IND
2002,IND
2003,IND
2004,AUS
2005,AUS
2006,SA
2007,SA
2008,SA
2009,IND
2010,IND
2011,IND
2012,IND
2013,AUS
2014,AUS
2015,SA
2016,NZ
2017,SL
2018,IND
The question here is to find out the longest streak of wins for each country and desired output will be like below -
Country,no_of_wins
IND,4
AUS,2
SA,3
SL,1
NZ,1
Can someone help here.
This is a gaps and islands problem, but the simplest method is to subtract a sequence from the year. So, to get all the sequences:
select country, count(*) as streak,
min(year) as from_year, max(year) as to_year
from (select year, country,
row_number() over (partition by country order by year) as seqnum
from t
) t
group by country, (year - seqnum);
To get the longest per country, aggregate again or use window functions:
select country, streak
from (select country, count(*) as streak,
min(year) as from_year, max(year) as to_year,
row_number() over (partition by country order by count(*) desc) as seqnum_2
from (select year, country,
row_number() over (partition by country order by year) as seqnum
from t
) t
group by country, (year - seqnum)
) cy
where seqnum_2 = 1;
I prefer using row_number() to get the longest streak because it allows you to also get the years when it occurred.
Looks like an gaps-and-islands problem.
The SQL below calculates some ranking based on 2 row_number.
Then it's just a matter of grouping.
SELECT q2.Country, MAX(q2.no_of_wins) AS no_of_wins
FROM
(
SELECT q1.winning_country as Country,
COUNT(*) AS no_of_wins
FROM
(
SELECT t.Year, t.winning_country,
(ROW_NUMBER() OVER (ORDER BY t.Year ASC) -
ROW_NUMBER() OVER (PARTITION BY t.winning_country ORDER BY t.Year)) AS rnk
FROM yourtable t
) q1
GROUP BY q1.winning_country, q1.rnk
) q2
GROUP BY q2.Country
ORDER BY MAX(q2.no_of_wins) DESC
If Redshift supports analytic function, below would be the query.
with t1 as
(
select 2001 as year,'IND' as cntry from dual union
select 2002,'IND' from dual union
select 2003,'IND' from dual union
select 2004,'AUS' from dual union
select 2005,'AUS' from dual union
select 2006,'SA' from dual union
select 2007,'SA' from dual union
select 2008,'SA' from dual union
select 2009,'IND' from dual union
select 2010,'IND' from dual union
select 2011,'IND' from dual union
select 2012,'IND' from dual union
select 2013,'AUS' from dual union
select 2014,'AUS' from dual union
select 2015,'SA' from dual union
select 2016,'NZ' from dual union
select 2017,'SL' from dual union
select 2018,'IND' from dual) ,
t2 as (select year, cntry, year - row_number() over (partition by cntry order by year) as grpBy from t1 order by cntry),
t3 as (select cntry, count(grpBy) as consWins from t2 group by cntry, grpBy),
res as (select cntry, consWins, row_number() over (partition by cntry order by consWins desc) as rnk from t3)
select cntry, consWins from res where rnk=1;
Hope this helps.
Here is a solution that leverages the use of Redshift Python UDF's
There may be simpler ways to achieve the same but this is a good example of how to create a simple UDF.
create table temp_c (competition_year int ,winning_country varchar(4));
insert into temp_c (competition_year, winning_country)
values
(2001,'IND'),
(2002,'IND'),
(2003,'IND'),
(2004,'AUS'),
(2005,'AUS'),
(2006,'SA'),
(2007,'SA'),
(2008,'SA'),
(2009,'IND'),
(2010,'IND'),
(2011,'IND'),
(2012,'IND'),
(2013,'AUS'),
(2014,'AUS'),
(2015,'SA'),
(2016,'NZ'),
(2017,'SL'),
(2018,'IND')
;
create or replace function find_longest_streak(InputStr varChar)
returns integer
stable
as $$
MaxStreak=0
ThisStreak=0
ThisYearStr=''
LastYear=0
for ThisYearStr in InputStr.split(','):
if int(ThisYearStr) == LastYear + 1:
ThisStreak+=1
else:
if ThisStreak > MaxStreak:
MaxStreak=ThisStreak
ThisStreak=1
LastYear=int(ThisYearStr)
return max(MaxStreak,1)
$$ language plpythonu;
select winning_country,
find_longest_streak(listagg(competition_year,',') within group (order by competition_year))
from temp_c
group by winning_country
order by 2 desc
;
How about something like...
SELECT
winning_country,
COUNT(*)
GROUP BY winning_country
HAVING MAX(year) - MIN(year) = COUNT(year) - 1
This assumes no duplicate entries.
Creating a session abstraction do the trick:
WITH winning_changes AS (
SELECT *,
CASE WHEN LAG(winning_country) OVER (ORDER BY year) <> winning_country THEN 1 ELSE 0 END AS same_winner
FROM winners
),
sequences AS (
SELECT *,
SUM(same_winner) OVER (ORDER BY year) AS winning_session
FROM winning_changes
),
streaks AS (
SELECT winning_country AS country,
winning_session,
COUNT(*) streak
FROM sequences
GROUP BY 1,2
)
SELECT country,
MAX(streak) AS no_of_wins
FROM streaks
GROUP BY 1;
Select rows with same ID/email but different value in other table
I have two tables: person and email, now there are mail addresses that have the same value, and persons/ID with different values.
Can anyone tell how to write an SQL query for this? I have tried but I can't figure it out. I have found some answers but then it is always finding the match in the same table
Like this
Table_person. Table_email
1 email#persoon1
2 email#persoon2
3 email#persoon3
4 email#persoon1
5 email#persoon5
6 email#persoon2
The output should be
Table_person Table_email
1 email#persoon1
4 email#persoon1
2 email#persoon2
6 email#persoon2
Using a common table expression with row_number()
;with cte as (
select *
, rn = row_number() over (partition by email order by person_id)
from email e
)
select *
from cte
where exists (
select 1
from cte i
where i.email = cte.email
and rn > 1
)
or using exists()
select *
from email e
where exists (
select 1
from email i
where i.email = e.email
and i.person_id <> e.person_id
)
rextester demo: http://rextester.com/JHFEF82373
Hope it will helps you
;with cte(Table_person,Table_email)
AS
(
SELECT 1,'email#persoon1' UNION ALL
SELECT 2,'email#persoon2' UNION ALL
SELECT 3,'email#persoon3' UNION ALL
SELECT 4,'email#persoon1' UNION ALL
SELECT 5,'email#persoon5' UNION ALL
SELECT 6,'email#persoon2'
)
,Cte2
AS
(
SELECT Table_person,Table_email From
(
Select Table_person,Table_email,ROW_NUMBER()OVER(Partition by Table_email Order By Table_person )Seq
from cte
)dt WHERE dt.Seq>1
)
,Final
AS
(
SELECT Table_person,Table_email From
(
Select Table_person,Table_email,ROW_NUMBER()OVER(Partition by Table_email Order By Table_email )Seq2
from cte
)dt
where dt.Seq2>1
Union ALL
SELECT Table_person,Table_email From cte2
)
SELECt Table_person,Table_email from Final
I am in a situation where I need to select rows that have the same content in two specific columns, AND distinct content in a third one. So far I got this for the two similar columns:
SELECT id, Title,
COUNT(*) AS NumOccurrences
FROM Table
GROUP BY id, Title
HAVING ( COUNT(*) > 1 )
I now need to specify a third distinct column in this query. Let's call it Ralph. This obviously does not work:
SELECT id, Title, DISTINCT Ralph,
COUNT(*) AS NumOccurrences
FROM Table
GROUP BY id, Title
HAVING ( COUNT(*) > 1 )
So what will?
select * from (
SELECT id, Title, COUNT(*) AS NumOccurrences
FROM Table t
GROUP BY id, Title
HAVING ( COUNT(*) > 1 )
) t
cross apply (
select distinct Ralph
from Table
where id = t.id and Title = t.Title
) t2
You can use COUNT(*) with OVER() clause
;WITH cte AS
(
SELECT id, Title, Ralph, COUNT(*) OVER (PARTITION BY id, Title) AS cnt
FROM dbo.test11
GROUP BY id, Title, Ralph
)
SELECT *
FROM cte
WHERE cnt > 1
Demo on SQLFiddle
I am trying to fetch the sum of several counts in one query:
SELECT(
SELECT COUNT( * )
FROM comments +
SELECT COUNT( * )
FROM tags +
SELECT COUNT( * )
FROM search
)
I am missing something here. I get syntax error.
SELECT ( SELECT COUNT(*) FROM comments )
+ ( SELECT COUNT(*) FROM tags )
+ ( SELECT COUNT(*) FROM search )
One more (not sure if supported with MySQL, though - works in SQL Server):
SELECT SUM(Counts) FROM
(SELECT COUNT(*) AS Counts FROM COMMENTS UNION ALL
SELECT COUNT(*) FROM Tags UNION ALL
SELECT COUNT(*) FROM Search) s
SELECT (
SELECT COUNT(*)
FROM comments
) +
(
SELECT COUNT(*)
FROM tags
) +
(
SELECT COUNT(*)
FROM search
)
SELECT SUM(ThisCount)
FROM (
SELECT COUNT(*) AS ThisCount
FROM comments
UNION ALL
SELECT COUNT(*) AS ThisCount
FROM tags
UNION ALL
SELECT COUNT(*) AS ThisCount
FROM search
)