PostgreSQL product table and sales table - postgresql

I have the following problem.
I need all the products in a table and at the same time put the sum of the quantities sold of each product.
I need to see each product in the product table with the total sum of sales, according to the date range.
If there is no sales record in that range, it must be zero.
My query is as follows, but it doesn't work.
Product Table : sto_producto
Product sales movement table: sto_movdet
SELECT
sto_producto.pro_codprod AS cod_product,
sto_producto.pro_desc AS name_product,
sum(sto_movdet.mvd_cant) AS total_sale,
AVG(sto_movdet.mvd_costo) AS cost_product_sale
FROM sto_producto
INNER JOIN sto_movdet ON (sto_producto.pro_codprod = sto_movdet.mvd_codprod)
WHERE mvd_fecha BETWEEN '2020301' and '20200716'
GROUP BY pro_codprod, pro_desc
I expect a result similar to
cod_product name_product total_sale cost_product_sale
0004 mousered 45 $ 2.355
0071 pc laptop 0 $ 1.000

Related

Equivalent of MySQL Sum(X) group by Y in Crystal reports?

This is how my report looks like now :
I'd like the Quantity summed up for each Product , similar to how we do it in a SQL query :
select product_id ,sum(quantity) from my_table group by product_id;
This is the desired output :
product
quantity
Aluminuim
204
Bois
216
Dynamo Bicyclette
36
Guidon Bicyclette
12
Necklace string
5,530
Another option is to simply insert a CrossTab. Use the Product as the row element and Quantity as the metric to sum.

ERROR CODE 1241 :OPERAND SHOULD CONTAIN 1 COLUMN(S)

My goal is to retrieve all accounts where the balance is greater than zero.
I have two table the tblloancontract and the tblloanpayment.
tblloancontract has two columns: accnum, and idmember.
tblloanpayment has: idpayment, balance, and accnum (a foreign key from tblloancontract).
Below is the MYSQL code I have written so far:
SELECT * FROM tblloanpayment WHERE accnum IN
(SELECT MAX(idpayment), tblloanpayment.accnum, MIN(tblloanpayment.balance)
FROM tblloanpayment JOIN tblloancontract ON tblloanpayment.accnum=tblloancontract.accnum
GROUP BY idmember) AND balance > 0;
Don't you just want all accounts where the lowest balance is greater than 0? In that case, why do you need the tblloancontract table at all?
SELECT tlp.accnum, MIN(tlp.balance)
FROM tblloanpayment tlp
GROUP BY tlp.accnum
HAVING MIN(tlp.balance) > 0;
That's not really a very good way to store loan information. The "contract" table should have the initial balance and the current balance, and the "payment" table should have the payment amounts.

Calculate median sales price with using 3 variables Tableau 10

I would like calculate the median sales price and the median rental price for an apartment in NYC in each of the 5 boroughs, Brooklyn, Bronx Manhattan, Queens and Staten Island. In Tableau the sales and and rentals are groups of ListPrice -- Variables ListPrice is NUMBER(decimal) Type (includes Sales & Rentals, Borough
Any help is appreciated
I tried using Tableau's table calculation feature but that did not work, I tried
WINDOW_MEDIAN(SUM([ListPrice])-1, -1)
ERROR: WINDOW_MEDIAN is being called with (float, integer), did mean
(float,integer,integer)
Data
Type Borough ListPrice
RentalType1 Manhattan $5,000
RentalType2 Bronx $3,000
RentalType2 Brooklyn $3,000
SalesType2 Manhattan $900,000
SalesType1 Brooklyn $100,000
SalesType1 Bronx $500,000
SalesType2 Queens $800,000
SalesType2 Staten Island $400,000
Table calculations takes 3 arguments, Expression, First row of the partition and last row of the partition. In your formula you haven't given last row of the partition.
Run the function for type in each Borough and calculate for each Borough.
So your formula would be:
WINDOW_MEDIAN(SUM(INT([List Price])),FIRST(),LAST())
are you looking to get values below:
Here calculation2 is median value

Retrieve field based on record field

I have 3 tables
items,
item_units,
order_items
First table items has the list of items that can be ordered.
Second table item_units has units for the items as well as the amount of those items in this unit
Third table has items that were ordered... ie... item_code , unit, qty
Here are the columns for items
[item_code]
,[desc]
Here are the columns for item_units
,[item_code]
,[unit]
,[amount]
,[price]
,[default_sprd_sht]
Here are the columns for order_items
,[order_id]
,[item_code]
,[item_desc]
,[unit]
,[qty]
,[price]
Note the [default_sprd_sht]. This field is a boolean. If it's set to true this unit is never put into order_items table. This field will be used as calculation field.
For example:
If 1 customer orders 2 6 packs of bread and another orders 3 dozens of bread, the baker needs to know how many pans of bread to make.
Now a 6 pack unit has 6 breads as amount, meaning 2 * 6 = 12. And a dozen unit has 12 breads.. 12 * 3 = 36. A pan bread unit has 20 breads. So i need to add up all the bread units amounts and divide them by the pan amount like so
((2*6) + (12 * 3)) / 20 = 2.4
So the first thing I did to create a report for the baker was
Create a group for order_items.item_code and then order_item.unit.
This needs to be done since the same item and unit cobination will be repeated in different orders. The baker needs to see how many bagels or breads he needs to bake in total.
in the order_item.unit group header I created a formula field that multiplies the order_item.unit by item.amount
Sum ({order_items.qty}, {order_items.unit}) * {items_units.amount}
That was easy.
But I aslo need to group all order items if there exists a record in the items_units with the same item_code and with [default_sprd_sht] set to true
This would look like so
(Sum ({order_items.qty}, {order_items.unit}) * {items_units.amount}) / (get amount for unit with the same item_code and [default_sprd_sht] = 1)
I have two problems accomplishing this.
How to check if this order item has a unit with same item_code and
[default_sprd_sht] = 1?
How to further group order items only if there is a unit with same
item_code and [default_sprd_sht] = 1?

Adding Columns heading to report if no data in a given period

I have a dataset for each record it has a CompanyID, RevenueMonth, RevenueYear, Revenue
When I create the report, I am grouping each CompanyID and showing their monthly revenue for a given year.
But in a given year, not all companies have any revenues for a particular month.
Example:
A sample record would look like:
CompanyID, RevenueMonth, RevenueYear, Revenue
1,05,2013,5.00
1,08,2013,6.00
1,03,2013,3.00
End Result, I would like my report to look like this with CompanyID 1.
Company ID|01|02|03|04|05|06|07|08|09|10|11|12
1 0.00|0.00|3.00|0.00|5.00|0.00|0.00|6.00|0.00|0.00|0.00|0.00
In my current Report, it will only fill column headings with March (03), May (05) and August (08).
Company ID|03|05|08
1 3.00|5.00|6.00
How do I get my Report to add the missing months for the year?
I hope my questions is clear.
Database level
Since you're only returning a year at a time, you can create a calendar table and add this to your result set:
Keeping it as simple as possible, with the date table coming from a CTE:
with months as -- get required year/months
(
select RevenueYear = 2013
, RevenueMonth = 1
union all
select RevenueYear = 2013
, RevenueMonth = RevenueMonth + 1
from months
where RevenueMonth < 12
)
select CompanyID = coalesce(r.CompanyID, c.companyID)
, RevenueMonth = coalesce(r.RevenueMonth, m.RevenueMonth)
, RevenueYear = coalesce(r.RevenueYear, m.RevenueYear)
, Revenue = isnull(r.Revenue, 0.0)
from months m
cross join (select distinct CompanyID from records) c -- make sure all companies included
left join records r on m.RevenueYear = r.RevenueYear
and m.RevenueMonth = r.RevenueMonth
SQL Fiddle with demo.
This will return a year/month for each company in the result set.
In the long run it would be better to move from a CTE to a permanent calendar table in the database.
You can then implement this in the report using a matrix style tablix.
Report level
If you'd prefer to do this at the report level, you can set up a table-style tablix with 12 permanent columns, one for each month, then populate the month revenue cells with expressions like:
=Sum(IIf(Fields!RevenueMonth.Value = 2, Fields!Revenue.Value, Nothing)
For the February column.
This would work with your existing dataset without any database code changes.