Skip to content
Advertisement

Group by + sum for different thresholds (preferably in a single query)

Let’s say I have a table with 3 columns:

Column A: Client_ID (not unique, can appear in multiple rows)

Column B: Price paid for a product

Column C: Product (type)

I want to get 6 columns:

i.e

With Client_ID x Product unique

I think the first part can be achieved with a simple GROUP BY

However, how can I get Number_of_transactions_over_1000 & Sum_of_transactions_over_1000 also? I know that I can obtain them with a separate query (by adding where B > 1000) and I can left join the query for Number_of_transactions & Sum_of_transactions with the query for Number_of_transactions_over_1000 & Sum_of_transactions_over_1000 afterwards, but can it be done in a single query (with a single SELECT instructions)?

Advertisement

Answer

Note: This will not extrapolate product_type, but gives correct results. Your input data table is named as data_p in following query, change accordingly. Also see if SAS supports CASE WHEN operation.[Because of no live SAS DB with me, i could not check it on SAS but this SQL is generic enough.]

User contributions licensed under: CC BY-SA
8 People found this is helpful
Advertisement