Derive value in a table column from other tables - ruby-on-rails-3

I have a revenue table with column amount and expense table with column tax and a profit table with column total, how do I derive the value into profit table by calculating amount minus profit, in Ruby on Rails 3

Related

cross tables query

Let us say we have 2 tables : A and B.
In table A we have payments for customers. 12 payments each year for each customer identified by numClient column and the amount of payment identified by payment column.
In table B there is the sum of payments of the year for every customer so just one row each year per customer still identified by a numClient column and payment identified by yearPayment column.
I would like a query that lists all customers (displaying numClient) whose yearPayment of table B is different from the sum of his payments in table A.
As those tables cover differents years, I would like to query only for 2018. In table A, the payment date is PaymentDate Column. In table B, the year of payment is YearPayment column.
The whole story sounds wrong. Not your words, but the model - why are you using table B? Keep payments where they are (table A). If you have to sum them, do so. Or create a view. But, keeping them separately in two tables just asks for a problem (the one you have now - finding a difference).
Anyway:
select a.id_customer, sum(a.payment), b.sum_payment
from a join b on a.id_customer = b.id_customer
where extract(year from a.date_column) = 2018
and extract(year from b.date_column) = 2018
group by a.id_customer, b.sum_payment
having b.sum_payment <> sum(a.payment)

adding a calculated column on QlikView

I'm new on QV and trying to add a column to an existing table.
I want to add to the sales table a sales revenue column that is the sum of salesprice from products table * quantity from sales table.
All of this because I wish to have a list box of sales revenue.
Thanks in advance to all the helpers!
You will need to in the unit price onto the Sales table and then do the calculation. You can't do sub-queries so it will have to be something of the following form;
Sales:
Load Product_No,
Quantity
from Sales.xls;
left join
Load Product_No
Unit_Price
from Products.xls;
Final_Sales:
load Product_No,
Quantity,
Unit_Price,
Quantity*Unit_Price as Revenue
resident Sales;
drop table Sales;
The final drop table statement is very important otherwise you will get a massive synkey between the table that you do not want

Populating fact table

I've a data warehouse for sales, it has 3 dimensions [product,time,store] and a fact table [sales_fact].
Primary key of 'sales_fact' table is made up of all primary keys of dimensions table, dimension tables are all filled up manually now I want to fill 'sales_fact' table with SUM of prices of products stored in a city for a specific month or 3 month period.
How should I sum up prices from product table which are related to a specific month and add it to fact table?
Considering that sum up prices from product table which are related to a specific month
is a measure, your query can be like below :
SELECT DS.City, DT.[Month], SUM(DP.Price)FROM
SalesFact AS S
LEFT JOIN DimProduct AS DP ON DP.ProductSK=S.ProductSK
LEFT JOIN DimTime AS DT ON DT.DateSK=S.DateSK
LEFT JOIN DimStore AS DS ON DS.StoreSK=S.StoreSK
WHERE [Date condition] --Add your date conditoon
GROUP BY DS.City, DT.[Month]
You can use a view for this measure.

Transforming table

I have a table like this
Type, Year, Month, Sales
I want to split the Type columns to several columns and transform the table to
year, month , type A sales, type B sales, type C sales
We actually dont know the number of type we have as well

SQL query (in ZOHO reports) to join two tables by subtotas :-O

if you are out there, please help me:
I have 2 different tables, with different columns and no primary key:
1 - daily actual sales data
2 - monthly budget sales data
I need:
- one consolidated table with monthly actual sales (counted) and montly budget sales, to compare monthly sales vs budget.
Is it possible?
Of course.
You can aggregate the numbers for every month from the actual sales data table using sum() and group by and can join into this the budget table via the month column.