There's my sql data:
code name total
---------------
3 Sprite 2400
17 Coke 1500
6 Dew 1000
17 Coke 3000
6 Dew 2000
But code and name has duplicated values and I want to sum total from each duplicated field.
Something like this:
code name total
---------------
3 Sprite 2400
17 Coke 4500
6 Dew 3000
How could I do that in sql?
SELECT code, name, sum(total) AS total FROM table GROUP BY code, name
Related
I have two tables:
Employees:
employee_id field max_amount
3 a 3000
4 a 3000
1 a 1600
2 a 500
4 b 4000
2 b 4000
3 b 1700
ordered by employee, field, amount desc.
Amounts (pol, premia,field):
pol premia field **assign_to_employee**
11 900 a 3
44 1000 a 3
55 1400 a 4
77 500 a 3
88 1300 a 1
22 800 b 4
33 3900 b 2
66 1300 b 4
Assign Stats Table:
employee_id field max_amount true_amount remain
3 a 3000 2400 600
4 a 3000 1400 1600
1 a 1600 1300 300
2 a 500 0 500
4 b 4000 2100 1900
2 b 4000 3900 100
3 b 1700 0 1700
The output : assign_to_employee field (merged to amounts table).
Algoritem wise : The method is to assign pol's to employees until the premia needs to be added to the cumulative_sum is bigger then the max amount per employee listed in the employees table. You always start with the employess with most max amount until you cannot add any other pols to the employee.
I start with the employees with the grater max_amount per field.
I keep doing this until no pols remains to be assign.
Can you help me solve this?
Thank you.
I'm looking to create a prior rolling 4 quarter Median. Some entries have less than 4 quarters, some have more. I want this by Employee. Needs to account for different tenure for different employees.
Result for 2021-1 should represent the prior 4 quarters median (and not account for current quarter).
I was able to figure out a rolling average with partitioning but not sure how to tackle a rolling median.
Thanks!
Employee ID
Quarter
Sales
EXPECTED RESULT
A
2020-1
1000
NULL
A
2020-2
2000
1000
A
2020-3
3000
1500
A
2020-4
4000
2000
A
2021-1
5000
2500
A
2021-2
4000
3500
B
2020-3
8000
NULL
B
2020-4
7000
8000
B
2021-1
6000
7500
B
2021-2
5000
7000
B
2021-3
1000
6500
C
2021-1
5000
NULL
C
2021-2
0
5000
C
2021-3
4000
2500
I have a table that looks like this:
customer_id item price cost
1 Shoe 120 36
1 Bag 180 50
1 Shirt 30 9
2 Shoe 150 40
3 Shirt 30 9
4 Shoe 120 36
5 Shorts 65 14
I am trying to find the most expensive item each customer bought along with the cost of item and the item name.
I'm able to do the first part:
SELECT customer_id, max(price)
FROM sales
GROUP BY customer_id;
Which gives me:
customer_id price
1 180
2 150
3 30
4 120
5 65
How do I get this output to also show me the item and it's cost in the output? So output should look like this...
customer_id price item cost
1 180 Bag 50
2 150 Shoe 40
3 30 Shirt 9
4 120 Shoe 36
5 65 Shorts 14
I'm assuming its a Select statement within a Select? I would appreciate the help as I'm fairly new to SQL.
One method that usually has good performance is a correlated subquery:
select s.*
from sales s
where s.price = (select max(s2.price)
from sales s2
where s2.customer_id = s.customer_id
);
how to split one value from sql database and devide into two rows (one row set 1000000, one row is the balance) Others less than 1000000.or any idea using coldfusion . Thanks in advance.
Example
No. Code Name Account No Total
1 123 black 123456789 160000.00
2 124 red 111111222 5200.00
3 124 blue 444555666 121000.00
I want This result
No. Code Name Account No Total
1 123 black 123456789 100000.00
2 123 black 123456789 60000.00
3 124 red 111111222 5200.00
4 124 blue 444555666 100000.00
5 124 blue 444555666 21000.00
First you need to insert a new row:
INSERT INTO TABLE (CODE, NAME, ACCOUNT_NO, TOTAL)
SELECT
CODE,
NAME,
ACCOUNT_NO,
TOTAL MOD 100000 AS NEW_TOTAL
FROM TABLE
WHERE TOTAL > 100000;
That command will insert the second row. Now you need to fix the first row.
UPDATE TABLE
SET TOTAL = TOTAL - (TOTAL MOD 100000)
WHERE TOTAL > 100000;
I have a model where I have Revenue table that has revenue2016 column
another table Programs where i have
program | min
I would like to add a calculated column to programs table so that it sums revenue that is grater than the min like so
=CALCULATE(SUM(Revenue[revenue2016 ]),Revenue[revenue2016]>=Programs[min])
this gave me an error
The data should look like this
#Revenue
Revenue
10
10
10
10
10
100
100
100
100
100
1000
1000
1000
1000
1000
#Programs
program | min | summed rev
a | 10 | 5550
b | 100 | 5500
c | 1000 | 5000
Just After I posted it I found the answer, I'll share it if someone else came across same issue
=calculate(sum(Revenue[revenue2016]),filter(Revenue,Revenue[revenue2016]>=Programs[Min]))