Sum multiple columns based on criteria from other columns - SQL Teradata - sql

I would like to get a total of the scores from columns 3 & 4 for each of the counties in columns 1 & 2 from the table shown below:
County1 | County2 | Player1_Score | Player2_Score
Norfolk | Hampshire | 5 | 7
Suffolk | Norfolk | 10 | 6
Hampshire | Suffolk | 16 | 12
Norfolk | Suffolk | 78 | 50
Hampshire | Norfolk | 4 | 8
Suffolk | Hampshire | 9 | 19
So the results I would like to see would be as follows:
Norfolk | 97
Suffolk | 77
Hampshire | 32
Can anyone please help with this? I tried a SELECT/GROUP BY query but am fairly new to SQL and couldn't get the results I wanted.
Many thanks

Try this:
SELECT County, SUM(Score) AS TotalScore
FROM (
SELECT County1 AS County, Player1_Score AS Score
FROM mytable
UNION ALL
SELECT County2, Player2_Score
FROM mytable) AS t
GROUP BY County
ORDER BY TotalScore DESC
Demo here

SEL CNTRY,SUM(SUM1) FROM
(
SEL COUNTRY1 AS CNTRY, SUM(player_score_1) AS SUM1 FROM VT1
GROUP BY 1
UNION
SEL COUNTRY2 AS CNTRY, SUM(player_score_2) FROM VT1
GROUP BY 1
) A
GROUP BY 1

Related

Joining 2 unrelated tables together

I have just delved into PostgreSQL and am currently trying to practice an unorthodox query whereby I want to join 2 unrelated tables, each with the same number of rows, together such that every row carries the combined columns of both tables.
These are what I have:
technical table
position | height | technical_id
----------+--------+-------------
Striker | 172 | 3
CAM | 165 | 4
(2 rows)
footballers table
name | age | country | game_id
----------+-----+-----------+--------
Pele | 77 | Brazil | 1
Maradona | 65 | Argentina | 2
(2 rows)
What i have tried:
SELECT name, '' AS position, null AS height, age, country, game_id, null as technical_id
from footballers
UNION
SELECT '' as name, position, height, null AS age,'' AS country, null as game_id, technical_id
from technical;
Output:
name | position | height | age | country | game_id | technical_id
----------+----------+--------+-----+-----------+---------+-------------
| Striker | 172 | | | | 3
| CAM | 165 | | | | 4
Maradona | | | 65 | Argentina | 2 |
Pele | | | 77 | Brazil | 1 |
(4 rows)
What I'm looking for (ideally):
name | position | height | age | country | game_id | technical_id
----------+----------+--------+-----+-----------+---------+-------------
Pele | Striker | 172 | 77 | Brazil | 1 | 3
Maradona | CAM | 165 | 65 | Argentina | 2 | 4
(2 rows)
Please use below query. But its not the right way of designing the schema. You should have a foreign key.
select t1.position,t1.height,t1.technical_id,t2.name,t2.age,t2.country,t2.game_id
from
(select position,height,technical_id, row_number() over(partition by
position,height,technical_id) as rnk) t1
inner join
(select name,age,country,game_id, row_number() over(partition by
name,age,country,game_id) as rnk) t2
on t1.rnk = t2.rnk;
You don't have a column to join on, so you can generate one. What works is a sequential number generated by row_number(). So:
select *
from (select t.*, row_number() over () as sequm
from technical t
) t join
(select f.*, row_number() over () as sequm
from footballers f
) f
using (seqnum);
Note: Postgres has extended the syntax of row_number() so it does not require an order by clause. The ordering of the rows is arbitrary and might change on different runs of the query.

SQL: How do I get a filtered count for each distinct row in a table?

Note: this is different from questions that ask "I want a count for each distinct row in a table" which has been answered numerous times. This is a filtered count, so the counting part of the query needs a more complex WHERE clause. Consider this dataset:
customer_id | user_id | age
-----------------------------
1 | 932 | 20
1 | 21 | 3
1 | 2334 | 32
2 | 232 | 10
2 | 238 | 28
3 | 838 | 39
3 | 928 | 83
4 | 842 | 12
I want to query this table and know the number of users over the age of 13 for each distinct customer_id. So the result would be:
customer_id | over_13_count
-----------------------------
1 | 2
2 | 1
3 | 2
4 | 0
I've tried something like this but it just runs forever, so I think I'm doing it wrong:
SELECT DISTINCT customer_id,
(SELECT COUNT(*) FROM mytable AS m2 WHERE m2.customer_id = m1.customer_id AND age > 13) AS over_13_count
FROM mytable AS m1
ORDER BY customer_id
Just use conditional aggregation:
SELECT customer_id,
SUM(CASE WHEN age > 13 THEN 1 ELSE 0 END) asover_13_count
FROM mytable m1
GROUP BY customer_id

How to count all distinct rows?

If I have a table like below, how can I count and sum all distinct values?
student_name | section | score | class
-------------|---------|-------|-------
John | B | 32 | 8
Doe | B | 43 | 8
Jane | A | 33 | 8
Smith | A | 88 | 8
Pat | B | 99 | 9
The output I desire is following for each class. So for class 8 it would be:
section | num_records | score_total
---------|--------------|-------------
B | 2 | 75
A | 2 | 121
Total | 4 | 196
You could use GROUPING SETS:
SELECT COALESCE(section, 'Total') AS section,
COUNT(*) AS num_records,
SUM(score) AS score_total
FROM t
WHERE class = 8
GROUP BY GROUPING SETS (section, ())
ORDER BY section;
db<>fiddle demo
you could use union all and subquery
select section,count(*),sum(score)
from t
where class =8
group by section
union all
select 'Total',count(*),sum(score) from t
where class=8
demo
output
section count sum
A 2 121
B 2 75
Total 4 196

Join multiple select queries SQL

I have two queries with different where clauses,and I need to join both the query to get a single result table.
First query:
SELECT
players.id,player_name,count(matches.winner) as wintotal
FROM
matches, players
WHERE
matches.winner = players.id
GROUP BY
players.id;
It returns these results:
id | player_name | wintotal
45 | Vijay | 2
43 | Rahul | 1
46 | Shinoy | 1
48 | Sunil | 2
44 | Adarsh | 4
Second query :
SELECT
players.id, player_name, count(*) as totalgames
FROM
matches, players
WHERE
matches.winner = players.id or matches.loser = players.id
GROUP BY
players.id;
Returns:
id | player_name | Total Matches
45 | Vijay | 4
43 | Rahul | 2
46 | Shinoy | 4
48 | Sunil | 2
44 | Adarsh | 6
47 | Pranjal | 2
In these two queries, the where clause is different for both queries and the last column is different.
First query returns total wins by players
Second query returns total matches played by player
How can I join both queries to get both columns wins and total matches in single query?
Expected output:
id | player_name | Total Matches | wintotal
45 | Vijay | 4 | 2
43 | Rahul | 2 | 1
46 | Shinoy | 4 | 1
48 | Sunil | 2 | 2
44 | Adarsh | 6 | 4
47 | Pranjal | 2 | 0
Thanks
try:
select players.id,
player_name,
count(case when matches.winner=players.id then 1 end) as wintotal ,
count(*) as totalgames
from matches
join players
on matches.winner=players.id or matches.loser=players.id
group by players.id,
player_name;
Check This.
select id , player_name ,Total_Matches , wintotal
(
select players.id,player_name,count(matches.winner) as wintotal from matches,players where matches.winner=players.id
group by players.id
) A,
(
select players.id,player_name,count(*) as Total_Matches from matches,players where matches.winner=players.id or
matches.loser=players.id
group by players.id
) B
where A.id=B.ID

Get properly data - city and province count

Structure of the table:
ID | firm | city | province
1 | test1 | Warszawa | 1
2 | test2 | Gdańsk | 12
3 | test3 | Otwock | 1
3 | test4 | Kraków | 3
4 | test5 | Olkusz | 3
This is my SQL query:
SELECT COUNT(miasto) AS cntcity, wojewodztwo, miasto FROM tabela GROUP BY wojewodztwo, miasto ORDER BY wojewodztwo
The result is:
cnt_city | province | city
6 1 Warszawa
2 1 Otwock
3 5 Kraków
7 5 Olkusz
3 12 Gdańsk
I want count "cnt_city" too, or rather SUM "cnt_city" - 6+2 = 8, 3+7=10,3 I Know how to do this in PHP, but it is possible to do it in SQL? How modify above SQL query?
I want result like this:
cnt_city | province | city | cnt_firm
6 1 Warszawa | 8
2 1 Otwock | 8
3 5 Kraków | 10
7 5 Olkusz | 10
3 12 Gdańsk | 3
Use Window Function
SELECT cntcity,
province,
city,
Sum(cntcity)OVER(partition BY province) As cnt_firm
FROM (SELECT Count(miasto) AS cntcity,
province,
city to
FROM tabela
GROUP BY wojewodztwo,
miasto) a