How to create a sequence number based on my result set? - oracle

This is how my table currently looks
Month Product Value
---------------------------------
1 Shoes 12.00
1 Jacket 15.00
2 Shirt 3.00
I need to add a column that generates a MonthlySaleID that increments for each month and resets each time the month changes, like so
Month Product Value MonthlySaleID
------------------------------------------------
1 Shoes 12.00 1
1 Jacket 15.00 2
2 Shirt 3.00 1
Any clues would be great

You can try with this view then:
CREATE OR REPLACE VIEW vw_rpt_sales AS
SELECT month, product, value, RANK() OVER (PARTITION BY month ORDER BY Product) AS MonthlySaleID
FROM
originaltable;
Couldn't test the rank function, in theory it should return what you want. I ordered by Product because it didn't seem in your question that you had a sale ID information. But if you have it, it's better to order by SaleID I suppose.

Related

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:

Is there a way when using the slicer in power bi that I get the associated value from other column but at their max count rather than filtered count?

Though I want the Sport column to be associated when corresponding ID is selected, I do not want the count to filter. I want the count to remain at Max when associated with their respective corresponding ID.
ID| Sport|
1 Basketball
2 Basketball
1 Basketball
2 Basketball
3 Tennis
3 Tennis
4 Tennis
4 Tennis
5 Baseball
5 Baseball
5 Baseball
6 Baseball
Normally if I use a slicer and filter at ID 1, then Basketball would be 2.If I filter by 5, Baseball would be 3.
Is there a way to keep Basketball, Baseball, or Tennis at their max count. For example if I select slicer = 1 instead of Basketball =2 i get Basketball =4.
If I select ID 3 in the slicer I get 4 for Tennis. If I select ID 4 I get 4 for tennis.
If I select 5 in the slicer I get 4 for Baseball as opposed to the standard way a slicer works I get a filter of 2 for Baseball.

updating a record based on a sum value

i have an item table that has an ID ,Product, cost ,quantity and status field. what i want is to up date an item based on the sum of the quantity of the products where the ID is the same.
so for example
ID Product cost quantity status
1 computer 100 10
1 tablet 200 10
2 ipad 50 15
now when the supplier is delivering these items i want when the quantity reaches 0 for the same Id the status must change to Received other than that it should say receiving.
eg if the supplier brings 2 computers the quantity will change to 8 and the status for ID 1 will say receiving, if the supplier brings 8 computers the status should still say receiving because the tablet has an outstanding balance of 10. but when the supplier brings 10 tablets and both ID 1 quantity is 0 the status for both should say received
ID Product cost quantity status
1 computer 100 0 Received
1 tablet 200 0 Received
2 ipad 50 15
what i have tried is
UPDATE item
SET update_status = CASE
when sum (quantity) >0 THEN
'Receiving'
ELSE
'Received'
END
where approvalid =:p5_approvalid;
but i am getting group function is not allowed here
I fully support mathguys comment. Also, reconsider your naming choices. A table column named ID better be a unique primary key.
If you still want to do this, use MERGE:
merge into item
using (
select id, sum(quantity) as sum_quantity
from item
group by id
) u
on (item.id = u.id)
when matched then update
set status = case when sum_quantity > 0 then 'Receiving' else 'Received' end
The using clause will calculate the quantity sums by ID, this way it wont be necessary to use any aggregation function in the set clause.

How can i solve this query in sql oracle?

It's an exercise that is not solved in the book in which I am studying.
The goal is to find the seller who has had the highest number of sales per month,
during all the months for which there is registered information. The problem is that I do not know how to divide tuples into periods of one month.
First table is:
Table Sellers
Id_seller
Name_Product
And the other one is:
Table Product
Name_Product
View_datetime
Budget
What did i do?
I made this query:
SELECT id_seller FROM(SELECT id_seller, COUNT(id_seller)
FROM SELLERS INNER JOIN PRODUCT
ON SELLERS.name_product = PRODUCT.name_product
GROUP BY id_seller HAVING COUNT(id_seller)>= 1
ORDER BY 2 DESC)
WHERE ROWNUM = 1;
The query returns me the seller that most sales has done, but not "per month since there are records" as the statement asks. Any ideas? I'm so lost...
The idea is to compare the total sales of each salesman in this month (sysdate), with those of a month ago, two months ago ... so long as there are older records. And get the maximum from each seller. And then you print the seller with more sales from the previous list. If a seller sells 400 products this month(April, the sysdate), but another seller sold in October last year 500, the result would be the second seller . That's what I do not know how to do.
Thanks ^^
You could try this query
select MonthName, id_seller, max(TotalSales) from (
select to_char(sysdate, 'Month') MonthName, sellers.id_seller, count(sellers.id_seller) TotalSales
from sellers inner join product
on sellers.name_product = product.name_product
group by to_char(view_datetime, 'Month'), sellers.id_seller
) tab
group by MonthName, id_seller
There are a few points to make...
The tables are weird. I assume your table sellers would better be called sales, right?
In this example, having count... >= 1 is a no-op. Count could only be 0 if there were no rows at all, in which case there would be no row in the group- by output. You can just leave this count away, here.
To get the sales per month, just add the month to the group by. I.e. group by id_seller, To_date(view_datetime,'YYYYMM').

How to add one month to month and year

I am working in oracle and new to coding and new to this site so I apologize in advance for the newbie question:
I have a script I am trying to run that will return the sum of next months' sales orders and compare that figure against our budgeted sales forecast. It was working great last month (November) when I set it up but now that it's December, I believe it's having problems figuring out that next month is a new year.
Essentially I just want to sum of our sales order records from the next month and compare that number against our forecast number.
Here is what I have so far (I'm sure I am making lots of grammatical mistakes so please be patient!)
select
"Backlog", "Forecast Amount" , round("Backlog"/"Forecast Amount",4) as "Backlog Percent"
from
(select round(sum(NVL(unit_price,0) *NVL( ship_quan,0)),2) as "Backlog"
from v_backlog_releases
where
(TO_CHAR(V_BACKLOG_RELEASES.PROMISE_DATE,'MM\YYYY') = TO_CHAR(sysdate,'MM\YYYY')+1)),
(select budamount as "Forecast Amount"
from
glbudget,
glperiods
where
glbudget.glperiods_id=glperiods.id and
TO_CHAR(GLPERIODS.START_DATE,'MM') = TO_CHAR(sysdate,'MM')+1)
The system won't let me post images of the output since I am too new. Essentially I should get something that looks like this:
Backlog | Forecast Amount | Backlog Percent
100,000 | 200,000 | .50
The backlog column is just a sum of ship quantities * price for all orders due to ship the following month.
Your issue is that for December TO_CHAR(sysdate, 'MM') + 1 is returning 13 instead of 1 of the next year. Obviously there is no month 13...
Try using ADD_MONTHS(sysdate, 1) instead and handle that result as appropriate. Best advice is to handle dates as dates instead of chars whenever possible.
Update based on comments:
Try using:
EXTRACT(MONTH FROM GLPERIODS.START_DATE) = EXTRACT(MONTH FROM ADD_MONTHS(sysdate, 1))
Documentation: https://docs.oracle.com/cd/B14117_01/server.101/b10759/functions045.htm

Resources