S
S
Stanislav2019-07-12 13:17:41
SQL
Stanislav, 2019-07-12 13:17:41

How can I calculate the "cost" of different combinations of fields in a table with one query?

There is a table with fields:

  • task
  • user
  • param1
  • param2
  • param3
  • param4

How to build a query that, depending on one or another "cost" of combinations of param(n) fields, will display the sum of these "costs" for each user? That is, in the output I want to get something like this:
| task 	| user 	| formula_1_cost 	| formula_1_cost 	| formula_1_cost 	|
|------	|------	|----------------	|----------------	|----------------	|
| 1    	| 1    	| 5              	| 5              	| 25             	|
| 1    	| 2    	| 0              	| 0              	| 26             	|
| 1    	| 3    	| 23             	| 0              	| 4              	|

The number of formulas, of course, does not depend on the number of param(n) fields. Ask if something needs to be added, in the comments to the question, so far I can hardly understand what else needs to be provided to solve this problem.

Answer the question

In order to leave comments, you need to log in

2 answer(s)
L
Lazy @BojackHorseman, 2019-07-12
@lamo4ok

SELECT
     CASE WHEN <condition> THEN <formula1> ELSE <formula2> END
...

D
d-stream, 2019-07-12
@d-stream

If "formulas" are a countable number and they are known in advance - then case from Ivan 's variant
's variant Otherwise - expand unpivot/pivot and get some expanded multidimensionality, where one dimension is a user, the second is a parameter, the third is a derivative of the formula identifier...

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question