How to sum two column from different Table in H2 Database - h2

I have two tables in database called purchase AND sales
Tables as follow
Table purchases
----------------------------
ID PRODUCT QTY
----------------------------
1 APPLE 100
2 BANANA 200
3 GRAPES 150
4 AVACADO 110
Table Sales
----------------------------
ID PRODUCT QTY
----------------------------
1 APPLE 50
2 BANANA 200
3 GRAPES 100
Here in the above tables i have purchase and sales i want to do subtraction from purchases quantity with sales quantity
and get the output like this
Output
----------------------------
ID PRODUCT QTY
----------------------------
1 APPLE 50
2 BANANA 0
3 GRAPES 50
4 AVACADO 110
i written query but not working
select id, product, (purchases.qty)-(sales.qty) from purchases left join sales on purchases.id=sales.id

Solution
SELECT purchases.id,purchases.product, (purchases.qty - IF (sales.qty IS NULL, 0, sales.qty)) as qty FROM pruchases LEFT JOIN sales ON purchases.product = sales.product

See all three Images your problem may be solved.
SALES TABLE [Same as Sales Table]1
PURCHASE TABLE [Same as Purchase Table]2
RESULTANT IMAGE [Resultant Image With Query]3

Related

How to count one column when another column is distinct (Oracle BIEE)

I want to have a new column showing how many orders that I have for the shipment.
How can I do that in obiee
Example Table:
Shipment
Order
Order Quantity
125
001
3
125
002
3
125
003
3
126
004
2
126
005
2
I can't have a new column with order quantity for each shipment
Add a sum for the "Shipment" attribute to the visualization and the tool does it automatically. Or create a measure using SUM("Order Quantity" by "Shipment") which you can then use to cross-calculate

How to put measures from multiple tables into one matrix in Power BI?

I have 8 tables with data of sold products. Each table is about a unique product. In Power BI, I want to create a matrix, containing the sold quantities (values) per product (rows), per month (columns), and the number of unique customers who bought the products.
Each of the 8 tables with the sales data has the following structure. So the App ID is different for each table, but is constantly the same within a table. Example for a Cars table:
Customer ID Month App ID
29273 2020-3 1
90283 2018-5 1
55824 2016-12 1
55824 2018-10 1
55824 2021-1 1
So, a bicycle table would have the same structure, but then the App ID's would be, for example 2, in the entire table.
I have two tables that are connected with the 8 product tables in a one-to-many relationship. The Calendar table based on the Month column, and the App table based on the App ID column.
The table Calendar:
Month
2015-1
2015-2
2015-3
2015-4
2015-5
...
...
The table Apps:
ID Name
1 Cars
2 Bicycle
3 Scooter
4 ...
So, the structure is:
I created the Calendar en Apps tables so that I could use them for the matrix, but it doesn't work like I want so far. At the end, I want to create a matrix like this (where P = the number of products sold, and C = the number of customers in that month for that product):
Product 2015-1 2015-2 2015-3 2015-4 2015-5 ...
P C P C P C P C P C
Cars 3 2 5 5 7 6 2 1 4 2
Bicycle 11 9 17 14 5 5 4 4 8 6
Scooter ...
Skateboard ...
As mentioned, I made that Calendar and App table so that I can use the columns from it to fill the labels in the rows and columns. What I am unable to do is create such a 'general variable' of the number of products sold per product, and the number of customers associated with it.
Can someone explain to me how I can fill the matrix with the numbers of products (and customers) sold, so that the matrix looks like the one described above?
I think this is pretty straight forward. You actually don't need the 'Calendar' table as it only contains the same info as is already in the 'Sales' table.
You should configure the matrix like this:
Rows: 'Name' (from the 'Apps' table)
Columns: 'Month' (from the
'Sales' table)
Values:
C = Count distinct of CustomerId (from 'Sales' table) [this counts the unique customers per month and app)
P = Count of CustomerId (from 'Sales' table) [this counts the rows of the 'Sales' table which is your number of products if every row represents 1 sale)
The different aggregations (count distinct, count) can be found under the Values' options:

Oracle Update Where Target Has Duplicates

Suppose I have the following tables
Target table
sales
ID ItemNum DiscAmt OrigAmt
1 123 20.00 NULL
2 456 30.00 NULL
3 123 20.00 NULL
Source Table
prices
ItemNum OrigAmt
123 25.00
456 35.00
I tried to update the OrigAmt in the Target Table using the OrigAmt in the Source Table using
UPDATE
( SELECT s.OrigAmt dests
,p.OrigAmt srcs
FROM sales s
LEFT JOIN prices p
ON s.ItemNum = p.ItemNum
) amnts
SET amnts.dests = amnts.srcs
;
but i get: ORA-01779: cannot modify a column which maps to a non key-preserved table
i also tried using the merge but i get: ORA-30926: unable to get a stable set of rows in the source tables
You cannot generally UPDATE the result of an arbitrary SELECT.
Single statement, assuming ItemNum is a primary key for prices:
UPDATE sales WHERE (SELECT count(price.ItemNum) FROM price
WHERE price.ItemNum = sales.ItemNum) > 0
SET OrigAmt =
(SELECT MAX(OrigAmt) FROM price
WHERE price.ItemNum = sales.ItemNum)
You might get away with omitting the WHERE and/or MAX.
Less convoluted: loop a cursor over
SELECT ItemNum, OrigAmt FROM price
performing a number of updates for each ItemNum from table prices:
UPDATE sales SET OrigAmt=? WHERE ItemNum=?

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

Consolidating multiple rows to a single row

Is it possible to join multiple row values to a single row? The stored procedure from where I acquire the data that I use return multiple, almost identical rows except that the category-column dfferentiates for products that have been assigned multiple categories. I would like to consolidate these categories to one column, separated by new lines. Example data:
Name Article number Sales Sales Category
------------------------------------------------
Product 1 2059102-1 20520 Retailer 1
------------------------------------------------
Product 1 2059102-1 20520 Retailer 2
------------------------------------------------
Product 1 2059102-1 20520 Retailer 3
------------------------------------------------
Product 2 2059102-2 2050 Retailer 1
------------------------------------------------
Product 2 2059102-2 5302 Retailer 3
Desired result:
Name Article number Sales Sales Category
------------------------------------------------
Product 1 2059102-1 20520 Retailer 1
Retailer 2
Retailer 3
------------------------------------------------
Product 2 2059102-2 2050 Retailer 1
Retailer 3
Thank you!
Create an RDL table and setup a Grouping on the Detail section of the table with two grouping expressions (so you have a single Grouping in the Table Details, but with multiple grouping expressions). The grouping expressions should be one for each of the fields: Name, Article number.
Then put a column in the RDL table for each field (Name, Article number, Sales, Sales Category). The trick is putting a List control in the Sales Category cell. In the list add a textbox for the "Sales Category" field and I think you'll get the result you want.
There is also some nasty SQL tricks for "row concatenation", but that is not very maintainable IMHO.

Resources