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;
Related
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
I am trying to get a COUNTIFS from excel type of result
Here is the products table:
Name Product
john car
john football
john image
max food
max tv
max laptop
max image
max image
max image
alex tv
alex laptop
alex image
alex cake
alex cake
alex cake
alex cake
alex car
The output should be:
Name Product Number of products per person Number of products of the same type
john car 1 2
john football1 1
john image 1 5
max food 1 1
max tv 1 2
max laptop 1 2
max image 3 5
alex tv 1 2
alex laptop 1 2
alex image 1 5
alex cake 4 4
alex car 1 2
Number of products per person is count of products by name by product
and Number of products of the same type is based on the total count by product
for example image is repeated 3 times for max so in col C the answer is 3 but it there 5 times in the table so answer in col D is 5
I tried but not getting the correct answer:
SELECT
name,
product,
COUNT(*),
COUNT(*) OVER (PARTITION BY product),
from products
GROUP BY 1,2
ORDER BY 1
You are quite close. You need to sum the COUNT(*). You can do this directly in the aggregation query:
SELECT name, product,
COUNT(*),
SUM(COUNT(*)) OVER (PARTITION BY product)
FROM products
GROUP BY 1, 2
ORDER BY 1
#standardSQL
SELECT name, product, product_per_person,
SUM(product_per_person) OVER(PARTITION BY product) product_total
FROM (
SELECT
name,
product,
COUNT(*) product_per_person
FROM `project.dataset.products`
GROUP BY 1,2
)
ORDER BY 1
if to apply to your sample data - result should be
Row name product product_per_person product_total
1 alex cake 4 4
2 alex car 1 2
3 alex image 1 5
4 alex laptop 1 2
5 alex tv 1 2
6 john car 1 2
7 john football 1 1
8 john image 1 5
9 max food 1 1
10 max image 3 5
11 max laptop 1 2
12 max tv 1 2
use group by name and product
SELECT name,
product,
COUNT(*),
COUNT(*) OVER (partition by product)
from products
GROUP BY name,product
ORDER BY 1
I need to report the sum of sales by user and include recursive totals. The problem is, each account has 2 account managers, and the account managers can come from different teams. At each parent level, I want return the distinct sum of each relevant account at the group level.
For example:
Name LEVEL AccountNum Sales
James 1 A1 1000
A2 2000
A3 5000
Mike 2 A1 1000
A2 2000
Sally 3 A1 1000
John 3 A1 1000
Mary 3 A2 2000
Matt 2 A2 2000
A3 5000
Andy 3 A2 2000
A3 5000
Bob 3 A3 5000
I want to return the totals only and not show the account details:
Name Sales
James 8000
Mike 3000
Sally 1000
John 1000
Mary 2000
Matt 7000
Andy 7000
Bob 5000
I'm a relative newbie to SSRS, so would massively appreciate any advice on how to achieve the above group totals! Thanks!
I tried using recursive queries in SQL, but the totals only work up to Level 2, because of the cross-team accounts. Using partition by ranking also only works up to Level 2. (e.g.
{row_number() over (partition by AccountNum, TeamLeaderKey ORDER BY AccountNum ASC))}) in order to exclude any with rank > 1. This falls down at level 1 when there are account with account managers from different teams at level 2.
In case anyone else ever comes across this problem, this is how I solved it. I introduced two new fields in my dataset:
IntraTeamRank:
Case When ROW_NUMBER() OVER (PARTITION BY AccountNum, TeamLeaderKey ORDER BY AccountNum ASC) > 1 Then 0 Else 1 End
InterTeamRank:
Case When ROW_NUMBER() OVER (PARTITION BY AccountNum ORDER BY AccountNum ASC) > 1 Then 0 Else 1 End
I have a parameter set up in SSRS to return the max level.
I then used the following expression in SSRS:
IIf( Level() < (Parameters!MaxLevel.Value - 1),
SUM((Fields!InterTeamRank.Value * Fields!Amount.Value), "StaffName", Recursive),
IIf( Level() = (Parameters!MaxLevel.Value - 1),
SUM((Fields!IntraTeamRank.Value * Fields!Amount.Value), "StaffName", Recursive),
SUM(Fields!Amount.Value), "StaffName")
)
)
Maybe not the most elegant solution, but it worked for my purposes. It would break if an account were shared between regions, as opposed to just teams, but it is unlikely in my scenario. I'd still be interested to know if anyone has a more robust solution.
I have two SQL tables:
PROPERTY
PID Address
1 123 Center Road
2 23 North Road
3 3a/34 Crest Avenue
5 49 Large Road
6 2 Kingston Way
7 4/232 Center Road
8 2/19 Ash Grove
9 54 Vintage Street
10 15 Charming Street
PROPERTY_FEATURE
P.PID Feature
1 Wine Cellar
1 Helipad
2 Tennis Court
2 Showroom
7 Swimming Pool - Above Ground
9 Swimming Pool - Below Ground
9 Wine Cellar
I want to Select the properties which contains specific features. For example, I would like to select the property ID which has the features Wine Cellar and Helipad, it would return the Property with the ID of 1.
Any ideas?
You can do this using Group By and Having clause
select PID
From PROPERTY_FEATURE
Group by PID
Having COUNT(case when Feature = 'Wine Cellar' then 1 end) > 0 --1
and COUNT(case when Feature = 'Helipad' then 1 end) > 0 -- 2
1 ) Counts only when Feature = 'Wine Cellar' & > 0 will make sure atleast one 'Wine Cellar' exist for each PID
2) Counts only when Feature = 'Helipad' & > 0 will make sure atleast one 'Helipad' exist for each PID
AND will make sure both 1 & 2 is satisfied then return the PID
You can do this by filtering on the required features, and then grouping and counting in a HAVING clause. You could also group directly (without filtering first) but if the table is very large, with many pid's, that will result in a lot of unnecessary grouping of rows that won't be used in the end.
Something like this:
select pid
from property_feature
where feature in ('Wine Cellar', 'Helipad')
group by pid
having count(feature) = 2;
This assumes there are no duplicates in the table (so you can't have 1 'Helipad' twice, messing up the count). If there can be duplicates, change the last line to count (distinct feature) = 2.
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