I am having difficulty with writing an expression for counting the number of duplicate column pairs.
For example, I have a table with two columns:
BUYER
SELLER
ALEX
1
ALEX
1
ALEX
1
ALEX
2
ALEX
2
JOE
1
JOE
3
JOE
3
I want to count the number of matching pairs for buyer and seller, and create a new column (count) based on the total number of matching pairs, as such:
BUYER
SELLER
COUNT
ALEX
1
3
ALEX
2
2
JOE
1
1
JOE
3
2
I know that the COUNT function is required to solve this, but am not sure how to implement it.
I would appreciate any help!
Thanks.
You want to GROUP BY the buyer and seller columns and then aggregate using the COUNT function:
SELECT buyer, seller, COUNT(*)
FROM table_name
GROUP BY buyer, seller
Related
Consider this table:
id name department email
1 Alex IT blah#gmail.com
1 Alex IT blah#gmail.com
2 Jay HR jay#gmail.com
2 Jay Marketing zou#gmail.com
If I group byid,name and count I get:
id name count(*)
1 Alex 2
2 Jay 2
With this query:
select id,name,count(*) from tb group by id,name;
However I would like to count only records that diverge from department,email, so as to have:
id name count(*)
1 Alex 0
2 Jay 1
This time the count for the first group 1,Alex is 0 because department,email have the same values (duplicated) , on the other hand 2,Jay is one because department,email has one different value.
If you meant "two different values" for "Jay", you can use distinct:
select id,name,count(*) from (SELECT distinct * FROM tb) group by id,name;
You can use count(*) - 1 to get similar results in your question.
this is my table schema, total_hours column is the result of a sum function.
Id name client total_hours
1 John company 1 100
1 John company 2 200
2 Jack company 3 350
2 Jack company 2 150
I want to merge the rows with similar ID into one row, looking like this.
Id name client_a total_hours_a client_b total_hours_b
1 John company 1 100 company 2 200
2 Jack company 3 350 company 2 150
I tried to use pivot but this function does not seem to exist in Dbeaver. Here is my query
SELECT
client
,name
,sum(hours) AS total_hours
FROM pojects
GROUP BY client, name;
Thanks in advance if anyone could be of any help.
I'm trying to do a rank (?) across columns where a "Favorites" column is based on the highest value in specified columns.
How the current dataset is:
user
Tops Bought
pants bought
anna
50
12
jon
12
50
& What would like to do is this :
user
Tops Bought
pants bought
favorite item
anna
50
12
tops
jon
12
50
pants
Thank you!
You can use a case expression:
select t.*,
(case greatest(tops_bought, pants_bought)
when tops_bought then 'tops'
when pans_bought then 'pants'
end) as favorite_item
from t;
I want to join two tables and use the column they both share to group the results, including a null result for those accountIds which only appear in one table.
Table a
AccountId
productApurchases
Steve
1
Jane
5
Bill
10
Abed
2
Table b
AccountId
productApurchases
Allan
1
Jane
10
Bill
2
Abed
1
Mike
2
Desired output
AccountId
productApurchases
productBpurchases
Steve
1
0
Jane
5
10
Bill
10
2
Abed
2
1
Mike
0
2
I've been trying with various joins but cannot figure out how to group by all the account ids.
Any advice much appreciated, thanks.
Use full join:
select accountid,
coalesce(productApurchases, 0) as productApurchases,
coalesce(productBpurchases, 0) as productBpurchases
from a full join
b
using (accountid);
Assume I have a table with the following data:
Name TransID Cost
---------------------------------------
Susan 1 10
Johnny 2 10
Johnny 3 9
Dave 4 10
I want to find a way to sum the Costs per name (assume the Names are unique) so that I get a table like this:
Name Cost
---------------------------------------
Susan 10
Johnny 19
Dave 10
Any help is appreciated.
This is relatively straightforward: you need to use a GROUP BY clause in your query:
SELECT Name,SUM(Cost)
FROM MyTable
GROUP BY Name