Calculate sum of columns value in Ruby on Rails - ruby-on-rails-3

I want to calculate the sum of the values in a column. For example:
#items = Item.find_all_by_cart_id(cart)
Item has column quantity. I want the sum of the #items quantity.

items_quantity = #items.map(&:quantity).sum

Try this
#items = Item.find_all_by_cart_id(cart).sum(:quantity)

Related

Subtract values from one column based on values in another (TSQL)

I have table where I need to subtract values in one column based on conditions applied to the other. For example, I want to subtract values with code fst and trd, meaning (12 - 23).
I don't want to declare separate variables. Is it possible to make this with a query?
One method is conditional aggregation:
select sum(case when code = 'fst' then value else - value end)
from t
where code in ('fst', 'trd');
Assuming you have only one row for each code, you can also use a join:
select tf.value - tt.value
from t tf join
t tt
on tf.code = 'fst' and tt.code = 'trd'

Sum fields by invoicenumber

I need to do an update of fields in a table - but only if the sum of a field differs from the sum of fields in another table.
Something along these lines:
if sum (KundFaktura.FakturaBeloppUtl) <> SUM (KundFakturaHandelser.BeloppUtl) **then update**
The summation need these conditions:
KundFakturaHandelser.FakturaNr
KundFaktura.FakturaNr

Hive - How to calculate if all records inside a groupby have the same value in a specific column?

I am grouping by records base on one column. What I want to know if all of these records has got the same value in another column.
So far I achieve this with the following logic, which I believe is too complex:
select number,
if(flagSum = 0 OR flagSum = groupedrecords, "AllSame", "SomeDifferent") as AllIndicatorEqual
from
(select number,
sum(if(flag = 'Y', 1, 0)) as flagSum,
count(*) as groupedrecords
from table
where number = '1234'
group by number
)tab
So basically I group by number, and check if all records grouped has got the same flag value.
Is there a more efficient way to calculate this?
You could probably skip a subquery by using a distinct count :
select
number,
if(count(distinct flag) = 1, "AllSame", "SomeDifferent") as AllIndicatorEqual
from table
group by number;
http://sqlfiddle.com/#!9/20e024/6

Select inside select

A newbie here. So please be kind :-)
I have 2 Tables namely Item & Item Entries.
Relation is: Item.No = ItemEntries.No.
In Item Entries Table I have Columns as Qty, Entry type, Purchase Amount, Sales Amount
I like to have a report which shows as below,
Item No. | Opening Quantity | Purchase Amount | Sales Amount
To calculate Opening Inventory I summed up the quantity field and the result is as expected. No problem in that. Now From that dataset I like to run a sub query which Calculates/Sum the Purchase amount for an Item that is a part of first dataset and similarly for Sales Amount.
Select(Item No.,Sum(IE.Quantity) As Quantity, Select(......Purchase Amount),Select(....Sales Amount)
I hope I was able to clear my doubts to you guys.
Something like :
SELECT ItemNo, sum(quantity), purchaseAmount, SalesAmount FROM Item i INNER JOIN ItemEntities ie on i.no = ie.no GROUP BY ItemNo, PurchaseAmount, SalesAmount;
I believe (if I understand what you want) that this is the solution to your problem
Select Item.No ,
Sum(IE.Quantity) As Quantity,
(Select(......Purchase Amount)) As ColumnName1 ,
(Select(....Sales Amount)) As ColumnName2
From your need to "sum the purchase amount for an item that is part of the first dataset and similarly for Sales Amount" I think what you're trying to achieve is one row for each item on the Item table with a sum for each of the Qty, Sales Amount and Purchase Amount. If so, then you can simply use a 'group by' clause which groups results together which have matching values for the columns specified.
SELECT I.no, SUM(IE.qty), SUM(IE.purchase_amount), SUM(IE.sales_amount)
FROM item I JOIN item_entries IE
ON I.no = IE.no
GROUP BY I.no;
See the group_by_clause for more details and some examples.
N.B. The join from the item tables isn't strictly required in this example, but if you're producing a report I suspect you might want to get things like a description - in which case you'll need to add those columns to the group by clause too.
You might need the sum of three columns from
ItemEntries table(Qty,PurchaseAmount,SalesAmount) for each Item.No
Select A.No,
Sum(B.Qty),
Sum(B.Purchaseamount),
Sum(B.Salesamount)
From Item A,
Itementries B
Where A.No = B.No
Group By A.No;

SQL Server using ISNULL and getting back no results

I have tried both these queries and what I dont understand is why my first query returns '0' (when it should) but my second query returns nothing - its just blank
I understand the 1st query returns the total sum of the products that have the currencycode 'USD' and if it is not found it returns '0', and the second query should return 0 because there is no products with the currencycode 'USD'.
Is there something wrong with my second query?
select ISNULL(SUM(Cost), 0) as Amount from products where Currencycode = 'USD'
select ISNULL(COST,0) as amount FROM PRODUCTS where currencycode= 'USD'
Keeping in mind I have no NULL fields, so why does the first query return 0 and the second doesn't
Your first query is doing an aggregation of values with the SUM function and if there is no row the returned value is 0.
Your second query is returning a value for every occurrences that matches your WHERE clause, in your case if there is no row in the table, the query will simple not return anything.
The first query returns a scalar value because SUM is an aggregation, you are calculating the sum of all costs with currencycode=USD.
The second query returns no rows because the WHERE excludes all rows:
select ISNULL(COST,0) as amount FROM PRODUCTS where currencycode= 'USD'
As you've mentioned that are no products with currency=USD.
If you instead want to know in each row the USD-Cost this should work:
SELECT Usd_Cost = CASE WHEN currencycode = 'USD' THEN Cost Else 0 END
FROM PRODUCTS
Otherwise i have no idea what you expect the second query to return.
yes missl
because
select ISNULL(COST,0) as amount FROM PRODUCTS where currencycode= 'USD'
query have no rows selected
but when we use aggregate functions it returns even null also in one row so your first query returns 0
but in second query no rows returns so it blanks
First Query:
Summing the all cost values. So we don't have any Null value until entire columns is null
select ISNULL(SUM(Cost), 0) as Amount from products where Currencycode = 'USD'
Second Query:
There is no rollup. Directly if it is a null you will get or else No
select ISNULL(COST,0) as amount FROM PRODUCTS where currencycode= 'USD'