updating a record based on a sum value - oracle

i have an item table that has an ID ,Product, cost ,quantity and status field. what i want is to up date an item based on the sum of the quantity of the products where the ID is the same.
so for example
ID Product cost quantity status
1 computer 100 10
1 tablet 200 10
2 ipad 50 15
now when the supplier is delivering these items i want when the quantity reaches 0 for the same Id the status must change to Received other than that it should say receiving.
eg if the supplier brings 2 computers the quantity will change to 8 and the status for ID 1 will say receiving, if the supplier brings 8 computers the status should still say receiving because the tablet has an outstanding balance of 10. but when the supplier brings 10 tablets and both ID 1 quantity is 0 the status for both should say received
ID Product cost quantity status
1 computer 100 0 Received
1 tablet 200 0 Received
2 ipad 50 15
what i have tried is
UPDATE item
SET update_status = CASE
when sum (quantity) >0 THEN
'Receiving'
ELSE
'Received'
END
where approvalid =:p5_approvalid;
but i am getting group function is not allowed here

I fully support mathguys comment. Also, reconsider your naming choices. A table column named ID better be a unique primary key.
If you still want to do this, use MERGE:
merge into item
using (
select id, sum(quantity) as sum_quantity
from item
group by id
) u
on (item.id = u.id)
when matched then update
set status = case when sum_quantity > 0 then 'Receiving' else 'Received' end
The using clause will calculate the quantity sums by ID, this way it wont be necessary to use any aggregation function in the set clause.

Related

how to sum day wise unique count to MTD

I want to calculate tasks per day(no of tasks/no of unique users) in my dashboard.
I used the distinct count to get the unique no of employees. When I put the measure in pivot, day wise calculation is okay but at the end in MTD it is not added the day wise unique no of emp.
Tasks per day:=divide(sum(fdata[resolved]),distinctcount(fdata[sap id]))
I expect the output like second table.
Model
Date ID Resolved
12-05-2019 a 1
12-05-2019 a 1
12-05-2019 b 1
13-05-2019 a 1
13-05-2019 b 1
13-05-2019 c 1
Expected Result
Date No of emp Resolved Task Per Day
12-05-2019 2 3 1.5
13-05-2019 3 3 1
Grand Total **5** 6 1.2
This will do the trick:
Tasks per day:=
IF(
ISFILTERED( fdata[date] )
, DISTINCTCOUNT( fdata[sap id] )
, COUNTROWS(
GROUPBY(
fdata
, fdata[date]
, fdata[sap id]
)
)
)

Laravel Eloquest join with max adn min value

Im trying to build a query that pulls the information from one table and also pulls the max and min value of that product in another table but cannot seem to get this to work.
This is what I have:
$rentals = Rentals::select('rentals.main_image','rentals.rental_name','rentals.slug','rentals.summary',DB::raw('max(prices.price) as price_high'),DB::raw('min(prices.price) as price_low'))
->leftjoin('prices', 'rentals.id', '=', 'prices.rental_id')
->where('destination',$destination_id)->paginate(6);
This returns 1 row ( there should be 88 ) and it gets me the highest and lowest price in the table regardless of rental_id. There are other factors that will filter into this like dates but for now I just need the rental info plus the highest and lowest price for that rental.

DAX AVG of Group Applied but returned for one specific Employee?

I have data as follows
EmployeeID Cycle Val Group
1 1 6 A
2 1 5 A
My desired result is as follows:
EmployeeID Cycle GroupVal
1 1 5.5
2 1 5.5
I have written 2 Measures as follows:
Emp_AVG: CALCULATE(AVERAGE(EmployeeFeedback, EmployeeFeedback[Val] > 0)
Group_AVG: CALCULATE(AVERAGEX(EmployeeFeedback[Emp_AVG],EmployeeFeedback[Emp_AVG] >0)
My thought process is that the Group_AVG is averaging the avg of all employees PER GROUP however since i need the results for a SPECIFIC employee, as soon as i introduce that column, it starts slicing by the Employee and the Group avg becomes inaccurate. I guess i need to generate Group Avgs before i do any employee filtering..how?
I am running a DAX query as follows:
EVALUATE SUMMARIZECOLUMNS(
EmployeeFeedback[EmployeeID],
EmployeeFeedback[Cycle],
"Group Val", [Group_AVG]
)
I need the EmployeeID to filter it down to an employee but because of EmployeeID, the Group AVG gets screwed. Without EmployeeID, Group AVG is correct but then there is no way to filter it for a specific Employee!
Thanks!
You could try to provide the ALLEXCEPT() argument to the CALCULATE function.
Btw, I believe there's an error in your Emp_AVG measure.
Try this:
Employee Average
Emp_AVG =
CALCULATE(
AVERAGE(EmployeeFeedback[Val]),
EmployeeFeedback[Val] > 0)
Group Average
Grp_Avg =
CALCULATE(EmployeeFeedback[Emp_AVG],
ALLEXCEPT(EmployeeFeedback,EmployeeFeedback[Group]))
Result:

obiee count distinct by as part of total count distinct

I need create following "numeric distribution" report in OBIEE :
item name,
count(distinct item_id by item_name),
count(distinct item_id by item_name)/count(distinct item_id)
(third column should be percent of count distinct by item name / count distinct by total)
How to get total count distinct ? I'm stuck and i will be appreciate any help.
The result should be:
Item 1 , 10, 50%
Item 2 , 20, 100%
where total number of customers is 20, and 10 od them buy Item_1, and 20 of them buy Item_2
Click on Measure in Pivot table then click on 'show Data as'>Percent of>Column.
Thats it

Advance mysql query with order by

I use 2 tables to combine data to become like shown as below
SELECT name, price, MIN(price) AS minprice
FROM c, cp
WHERE c.id = cp.id
GROUP BY id
ORDER BY minprice = 0, minprice ASC
For Example:
id name price
1 apple 0
1 green apple 20
2 orange 10
3 strawberry 0
As the data result above the minprice of the group 1 is 0 But I don't want the min price take zero, but this is incorrenct if I give condition having minprice > 0 cause
I wanna my result become like this
2 orange 10
1 green apple 20
3 strawberry 0
Is it possible?
Here is the answer:
SELECT
(
SELECT name
FROM yourtable
WHERE price = _inner._MIN AND id = _inner.id LIMIT 1
)
AS _NAME,
_inner._MIN
FROM
(
SELECT id, IFNULL(MIN(NULLIF(price, 0)),0) AS _MIN
FROM yourtable
GROUP BY id
)
AS _inner
where yourtable is the name of your table.
MIN(NULLIF(price, 0)) allows you to calculate minimum value while not counting a zero.
IFNULL(<...>,0) here just means, that we need a real zero instead of NULL in result.
LIMIT 1 is on the case if we have an items with the same id and price but with different names. I think, you can freely remove this statement.

Resources