Highest values among corresponding field in oracle - datatable

I have two supplier_type (Direct and Indirect) for one supply code. I want to produce a new column supplier_type based on supplier_Name for one supply_code like
Example:
If supply_Name is Apple,Dell,Hp,Lenovo then supply_type as Direct
If supply_Name is laptops,mouse,keyboard then indirect for the supplier_code 2023.
Here I want to get the highest value of supplier_type by counting how many supplier_type are Direct and indirect for each supplier_code.
Sample table:
S_CODE S_NAME S_TYPE
----------------------
2023. APPLE. DIRECT
2023. DELL. DIRECT
2023. MOUSE. INDIRECT
2023. LENOVO. DIRECT
2022. DELL. DIRECT
2022. LAPTOP. INDIRECT
2022. MOUSE. INDIRECT
Here, we have supplier code 2023 and 2022
For supplier code 2023 we have 3 direct and 1 indirect, so we need to pass the value as Direct in supplier_type column
And for supplier code 2022 we have 1 direct and 2 indirect, so we need to pass the value as Indirect in supplier_type column.
select supplier_type
from supplier_table
where count_of_supplier in (select distinct max(count_of_supplier)
from supplier table
group by supplier_type)
order by supplier_code

Related

Adding a filter on date with different relations in Power BI

I have three tables based on year
Lecturer table - the year represents the time the lecturer was
recruited
Student table - the year represents the time the student
first registered
Course table - the year represents the time the
course was given
I added a year table with an updating column (based on the Today function) which calculates how many years have passed since that year.
I would like to create different visualizations, each using a slicer/filter on the updating column with a different context. For students I would like to slice according to their academic year, for lecturers I would like to slice according to seniority and for courses, I want to slice according to how recent they are.
Is there an option to define a slicer/filter that chooses the context of the relationship? I am particularly asking about filters and I prefer not to duplicate yearSinceToday column in all my tables.
sample data
studentId
studentName
registrationYear
s1
John
2022
s2
Jack
2023
s3
Jill
2022
lecturerId
lecturerName
lecturerRecruitementYear
l1
Luke
2019
l2
Leia
2018
l3
Lando
2022
courseId
courseName
coursYear
c1.2022
Python
2022
c1.2023
Python
2023
c2.2022
Java
2022
courseId
lecturerId
c1.2022
l1
c1.2023
l2
c2.2022
l3
c2.2022
l2
courseId
studentId
c1.2022
s1
c1.2022
s2
c1.2023
s2
c1.2023
s3
c2.2022
s3
year
yearSinceToday
2018
5
2019
4
2020
3
2021
2
2022
1
2023
0
I would like to be able to ask questions like:
How many courses from year X there are with a lecturer with seniority at least Y?
How many students of the academic year of at least X take each course from year Y?
and so on
Is there an option to define a slicer/filter that chooses the context
of the relationship?
You don't define the slicer this way - you define your measure. Your "yearSinceToday" table is known as a role-playing dimension and you would create active and inactive relationships between it and your other tables. You would then activate the appropriate relationship using USERELATIONSHIP() and have the measure return the appropriate data to filter on.

Query to prevent booking overlap

I'm doing an app in Apex Oracle and trying to find a query that could prevent people from booking a room already booked. I managed to find a query that can prevent picking a date that starts or ends in between the booking time but I can't find how to prevent overlaping. By that I mean if someone books a conference room feb 2nd to feb 5th, someone can book the same room from feb 1st to feb 7th. That is what I'm trying to prevent. Thanks for the help!
Here's my first query
SELECT RES_ID_LOC FROM WER_RES
WHERE (CAST(RES_DATE_ARRIVE AS DATE) < CAST(TRY_RESERVE_START_DATE AS DATE) OR CAST(RES_DATE_DEPART AS DATE)
CAST(TRY_RESERVE_START_DATE AS DATE))
AND (CAST(RES_DATE_ARRIVE AS DATE) < CAST(TRY_RESERVE_END_DATE AS DATE) OR CAST(RES_DATE_DEPART AS DATE) > CAST(TRY_RESERVE_END_DATE AS DATE))
The main issue you'll have here is concurrency, namely (in chronological order)
User 1
runs overlap check query, see Room 5 is free, and inserts a row to book it
User 2
runs overlap check query, see Room 5 is free, and inserts a row to book it
User 1
commits
User 2
commits
and voila! You have a data corruption, even though the code all ran as you expected.
To avoid this, you'll need some way to lock a resource that multiple might want to book. Thus lets say you have a ROOMS table (list of available rooms) and a BOOKINGS table which is a child of ROOM.
Then your logic will need be something like:
select from ROOM where ROOM_NO = :selected_room for update;
This gives someone exclusive access to the room to check for bookings.
Now you can run your overlap check on that room against the BOOKINGS table. If that passes, then you insert your booking and commit the change to release the lock on the ROOMS row.
As an aside, take care with simply casting strings to dates, because you're at the whim of the format mask of the item matching that default of the database. Better to explicitly use a known format mask and TO_DATE

Microsoft Power BI, DAX - tricky ALLSELECTED with 2 values in slicer, but show only TOP 1 row in visual

This question is an extension of an already answered question, which I posted this week.
I have the below situation in Microsoft Power BI.
I have 2 simple tables:
1) CountryTable
2) YearTable
There is a 1-M relationship between YearTable and CountryTable.
The latter (YearTable) is used to feed values into a slicer.
(In my client database, Year has some alphabetical prefixes, such as Q1-2022, so I prefer to use YearOrder column to sort the Year column at the backend, while the slicer will display the Year column.)
The former (CountryTable) is the main table, with just a few sample rows.
These two tables are related via the Year column.
The Year slicer always has EXACTLY 2 values chosen in my Power BI report.
I need the Maximum of these two values of the year slicer as a measure, for each row of my visual.
At the same time, these two year values of the slicer must remove the unwanted rows in my report visual, based on the slicer selection of year values.
For example, when the slicer has 2019 and 2020 chosen, I need the value as in the DesiredOutput1 page.
Similarly, you can see DesiredOutput2 (Slicer values are 2020 and 2022); DesiredOutput3 (Slicer values are 2019 and 2022) pages.
I have indeed successfully obtained DesiredOutput1, DesiredOutput2, DesiredOutput3. Thanks to all the folks who helped me attain this.
Now, my main requirement in this posting, is this:
After obtaining the DesiredOutputs above, I need the following output:
Show only the TOP 1 row (ASC order of Year column, which is the minimum value of the slicer).
Essentially:
Year column of the visual: Minimum value of the slicer
MaxYear_Measure_SlicerSelection: Maximum value of the slicer (maximum of the two values chosen in the slicer)
You can see below:
Note: MaxYear_Measure_SlicerSelection measure can refer to any one of the two measures [MaxYear] or [MaxYearMeasure_Community] (see the .pbix file for the formulas of the measures).
Any idea ?
I prefer the Year column of the visual not to be converted to a new measure. Would RANKX help in this case ? Any thoughts?

How do I subtract two rows from a POWERBI custom table?

Project
Cost
January
323
Feb
323
I have a table as followed seen above which ROW is month (filtered by a certain project) and values are cost of the project. I want to calcuate the difference between two months, but I am having trouble.
How do I subtract two rows from each other.
In the code I wrote:
Variance = [Cost] - CALCULATE([Cost],PREVIOUSMONTH('Month'[Month))
I get the following error, A column specified in the call to function is not of type date.
Is there a way to manual subtract two months?
The best way to do it is to replace your month with an actual Date value. The first of the month for example. The you should be able to do something like this assuming your month dates are unique: If they are not unique you should create a Dates table (See Microsoft's Guidance on date table) and join.
variance = [Cost] - Calculate([COST],(PARALLELPERIOD(Month[Month],-1,Month)
You can use EARLIER function here but, only when the Months have an Id. Such as
1 Jan
2 Feb
3 March
...
Link for details
However, I would suggest creating a date table and then having a relationship from the date table to your table. By using date table you can easily achieve using in-buit date functions.

How to get MDX openingperiod() to work as calculated measure in ssas cube

I want to use the MDX functions openingperiod() and closingperiod() in our cube, but cannot get it to work properly.
Is this actually possible, or are these functions only applicable in a MDX-query?
The main two articles I used to learn about openingperiod() are:
https://www.sqlservercentral.com/steps/stairway-to-mdx-level-12-mdx-timedate-series-functions-openingperiod-and-closingperiod-functions
https://learn.microsoft.com/en-us/sql/mdx/openingperiod-mdx?view=sql-server-2017
In the above documentation the two functions openingperiod() and closingperiod() are only used in a query, but I want to integrate them in the cube.
The code I used as a regular query is:
sum(
openingperiod
(
[Date Booking].[Year Month],
[Date Booking].[Year Month].currentmember
),
[Measures].[Goods Amount])
The result should be the amount of goods at the start of the month, but the result is NULL.
I used sum to get a valid syntax that points to the right member.
I will try to give you the idea from Adventure Works sample database.
This should be the syntax to create your calculated member in the cube ([Adventure Works] is the cube name in this example), so you can call it later:
create member [Adventure Works].Measures.MyMeasure4
as
(OpeningPeriod([Date].[Calendar].[Date],[Date].[Calendar].Currentmember.Parent), [Measures].[Internet Sales Amount])
And then you should call it like this to see the result:
select {Measures.MyMeasure4, [Measures].[Internet Sales Amount]} on 0,
[Date].[Calendar].members on 1
from [Adventure Works]
So for January, MyMeasure4 will have the value of January the 1st member (value for opening period), for February it will have the value from Feb the 1st, if that was your intention.

Resources