Drupal commerce Sql select max price for current taxonomy - sql

Hello (sorry for broken english). I try to select min and max prices (drupal commerce) for current catalog tag. ATM i put in header php code:
$max = db_query("SELECT MAX(`commerce_price_amount`) FROM {`field_data_commerce_price`}")->fetchField();
so I can get max price for all products. same for min.
but i need for current tag. my sql tables looks like this:
table field_data_field_product_catalog:
http://postimage.org/image/tahrk3y29/
table field_data_commerce_price:
http://postimage.org/image/w1yvkh8kn/
some advice plz?

$catalog=arg(2);
$max = db_query("SELECT MAX(`commerce_price_amount`) FROM {`field_data_field_product_catalog`},{`field_data_commerce_price`}
WHERE {field_data_commerce_price.entity_id}={field_data_field_product_catalog.entity_id}
AND {field_product_catalog_tid}=:cnid", array(':cnid' => $catalog))->fetchField();
$max=Ceil($max/100);

Related

Django ORM equivalent of SQL Sum

I have a django model as follows:
class Order(models.Model):
cash=models.DecimalField(max_digits=11,decimal_places=2,default=0)
balance=models.DecimalField(max_digits=11,decimal_places=2,default=0)
current_ac=models.DecimalField(max_digits=11,decimal_places=2,default=0)
added_by = models.ForeignKey(User)
There can be multiple Orders and multiple users can create orders.
How can I get the sum of all orders for each columns for a particular user using Django queries, an SQL equivalent would be something like
Select sum(cash), sum (balance), sum(current_ac) from Orders where added_by = 1
You can get your expected result from the code below
from django.db.models import Sum
result = Order.objects.filter(added_by_id=1).aggregate(total_cash=Sum('cash'), total_balance =Sum('balance'), total_current_ac=Sum('current_ac'))
and it will give you a dictionary like this:
{'total_cash': Decimal('110.00'), 'total_balance': Decimal('110.00'), 'total_current_ac': Decimal('110.00')}

multiplie outputs with different wheres

What do I have to change to get different results from different names.The table should give me the debts of each of them, this is calculated by the amount and the price of the drink. Now it should show all the names with the corresponding invoice that happens after the select
%sql select name, sum(getraenk.preis*schulden.menge) schulden from schulden \
join person on (fk_person = person.id)\
join getraenk on (fk_getraenk = getraenk.id)\
where name like ("dani")
Edit: it should spend all the names with their debts, that is:
dani = 8.5
michael = 12.5
...
Just in case your problem is very simple, you should be able to see all names and values with an SQL that looks like this:
select name, getraenk.preis*schulden.menge schulden
from schulden
join person on (fk_person = person.id)
join getraenk on (fk_getraenk = getraenk.id)
Note that I removed the where clause... this was the part that limited it to one name.
You also don't need the sum clause here unless you are doing a group by
Have you considered simply using GROUP BY name at the end of this query?
https://www.w3schools.com/sql/sql_groupby.asp
This will give you the sum of total debt for all names in your table which sounds like the result you are looking for.
You're missing
GROUP BY name
in the query.

Complex SQL postgres query into ActiveRecord notation?

Hoping an ActiveRecord wizard can help me determine if this kind of query can be done with a AR statement, i.e. without executing the raw SQL or diving into Arel?
I'm getting close but can't seem to figure out how to add the subquery in. I'm on Rails 5.1.1.
SELECT s.id as stock_id, s.count, adjustments.count as
adjustment_count, adjustments.reason as adjustment_reason
FROM
(SELECT * FROM stocks WHERE inventory_id = 6) AS s
LEFT JOIN adjustments
ON (s.id = adjustments.stock_id AND adjustments.reason='run_use')
The end result should be all Stock that have inventory id 6 with the extra columns showing what Adjustment was made to them, if they exist.
If I run the RAW SQL in Navicat I get this result, which is what I'm trying to get via ActiveRecord:
This gets me close, but it just produces stock_ids 11 and 10:
Stock.where(inventory:6)
.left_joins(:adjustments)
.where("adjustments.reason = 'run_use'")
One of these should work for you:
Stock.eager_load(:adjustments)
.where("adjustments.reason = ? OR stocks.inventory_id = ?", "run_use", 6)
or
Stock.eager_load(:adjustments)
.where(adjustments: { reason: "run_use" }, inventory_id = 6)
Hm. It seems you're trying to say:
Give me all Stock that belong to Inventory with id = 6 that also have Adjustments with a reason of 'run use'.
How about something like:
Stock.
where(inventory_id: 6).
joins(:adjustments).
where(adjustments: { reason: 'run_use' })
from the Guides

Where Multiple Values SQL SERVER

For this code I need the sum of the serv1 - serv8 where the student is receiving "Speech Language Therapy". I can not for the life of me figure out how to get those. If I say where serv1 equals 'SLT' then it will show up as null for serv 2. If I do it for serv2 it will show up as another service like DD for serv1.
My sum isn't working I would like to sum all of the services up by school but I'll take what anyone is willing to help me with. Any help is appreciated
SELECT SPE_Student_Profile_VW.school_code,
SPE_Student_Profile_VW.organization_name AS [[[School Name]]]]]]],
SPE_Student_Profile_VW.sis_number,
SPE_Student_Profile_VW.primary_disability_code,
SPE_Student_Profile_VW.secondary_disability_code,
SPE_Student_Profile_VW.tertiary_disability_code,
SPE_Student_Profile_VW.serv1,
SUM (SPE_Student_Profile_VW.mins1),
SPE_Student_Profile_VW.serv2,
SPE_Student_Profile_VW.mins2,
SPE_Student_Profile_VW.serv3,
SPE_Student_Profile_VW.mins3,
SPE_Student_Profile_VW.serv4,
SPE_Student_Profile_VW.mins4,
SPE_Student_Profile_VW.serv5,
SPE_Student_Profile_VW.mins5,
SPE_Student_Profile_VW.serv6,
SPE_Student_Profile_VW.mins6,
SPE_Student_Profile_VW.serv7,
SPE_Student_Profile_VW.mins7,
SPE_Student_Profile_VW.serv8,
SPE_Student_Profile_VW.mins8
FROM DBO.SPE_Student_Profile_VW SPE_Student_Profile_VW
WHERE ( SPE_Student_Profile_VW.serv1 = 'Speech Language Therapy'
AND SPE_Student_Profile_VW.school_code = '47')
GROUP BY SPE_Student_Profile_VW.school_code,
SPE_Student_Profile_VW.organization_name,
SPE_Student_Profile_VW.sis_number,
SPE_Student_Profile_VW.primary_disability_code,
SPE_Student_Profile_VW.secondary_disability_code,
SPE_Student_Profile_VW.tertiary_disability_code,
SPE_Student_Profile_VW.serv1,
SPE_Student_Profile_VW.mins1,
SPE_Student_Profile_VW.serv2,
SPE_Student_Profile_VW.mins2,
SPE_Student_Profile_VW.serv3,
SPE_Student_Profile_VW.mins3,
SPE_Student_Profile_VW.serv4,
SPE_Student_Profile_VW.mins4,
SPE_Student_Profile_VW.serv5,
SPE_Student_Profile_VW.mins5,
SPE_Student_Profile_VW.serv6,
SPE_Student_Profile_VW.mins6,
SPE_Student_Profile_VW.serv7,
SPE_Student_Profile_VW.mins7,
SPE_Student_Profile_VW.serv8,
SPE_Student_Profile_VW.mins8
the column you are trying to sum should not be in the group by clause, all the others should.
What I mean is the SELECT clause has this:
SUM (SPE_Student_Profile_VW.mins1),
and the GROUP BY has this part which should not be there:
SPE_Student_Profile_VW.mins1,
because it is the column he is trying to sum and you can't group by a column that you are aggregating with a function and that is why SUM isn't working
not sure of what is required but from what I get, you can probably get what you need if you add another
OR
in your
WHERE
Something like
WHERE (SPE_Student_Profile_VW.serv1 = 'Speech Language Therapy'
AND SPE_Student_Profile_VW.school_code = '47') OR
(SPE_Student_Profile_VW.serv2 = 'Speech Language Therapy'
AND SPE_Student_Profile_VW.school_code = '47') OR
SPE_Student_Profile_VW.serv3 = 'Speech Language Therapy'
AND SPE_Student_Profile_VW.school_code = '47'
and so on.
Thanks.
In Group by clause use all columns which you are used in select statement.Except the column you are calculating sum in select statement.
Thanks.

Rails 3 Sum Product of two fields

I need to calculate the sum of the product of two fields in my Rails 3 app (i.e. the equivalent to Excel's sumproduct function). Is there a method in Rails that will help with this and if not, then what would be the rails code using custom sql?
For example, a Hotel has many Rooms. A Room has attributes of sqft (square feet), quantity (of that size) and hotel_id. I would like to calculate the total sqft of all the Rooms in a given Hotel. In SQL, for a Hotel.id = 8, I believe the following statement will work:
select sum(rooms.sqft * rooms.quantity) as SumSqft from rooms inner join hotels on rooms.hotel_id = hotels.id where hotels.id = 8;
Yep :
Room.where(hotel_id: 8).sum("sqft * quantity")
def calculate
#hotel=Hotel.find(params[:id]
#rooms=Room.all.where(:hotel_id=>#hotel.id)
sum=0
#rooms.each do |room|
sum=sum+room.sqft*room.quantity
end
return sum
end