Manufacturer Scheduler - schedule

I have a offline scheduling problem which aims to find the schedule which complete all clients order before deadline or report error if it is not possible to do so.
The situation is that a manufacturer have some kinds of products. They have one machine to produce their products and each kinds of products can be produced at most Mi items at once with a makespan Ti mins(Mi and Ti depends on each product).
There are some clients and each client will order a set of products at time t. This order will have a deadline depends on their order type. The deadline of a fast order is t+Df while The deadline of a slow order is t+Ds. Ds and Df are constant.
for example,
product A can be produced 5 items for 5 mins
product B can be produced 2 items for 1 mins
Ds = 7 mins, Df = 6 mins
there is two client order
a fast service {A,A,B} at t=2 (then the deadline of this order is at 8)
a slow service {A,A,A,B} at t=7 (then the deadline of this order is at 14)
then the solution will be:
at t=2 start to produce 2 items of A (we cannot start the production before the order received)
at t=7,the 2 items of A completed and we can start to produce 2 items of B for order 1 and 2(since at t=7, we receive the second order, we can start the production of second order)
at t=8,the 2 items of B completed(the first order complete at t=8 which is before the deadline and fulfill this client order), and we can start to produce 3items of A for order 2
at t=13, the 3 items of A completed(the second order complete at t=13 which is also before the deadline and fulfill this client order).
A possible schedule will be:
at t=2 produce 2 A for order 1
at t=7 produce 2 B for order 1 and 2
at t=8 produce 3 A for order 2
How could I find the schedule so that all clients orders completed before the deadline?

Related

SQL query to find the average time differences between two statuses

I am trying to find the time between status changes for tickets. The statuses are A,B,C,D,E. I need to identify where the bottlenecks are in the system. The table looks something like this:
ticket_no
created_at
current_status
next_status
1
12/2/2022
A
B
1
12/3/2022
B
C
1
12/3/2022
C
B
1
12/4/2022
B
C
1
12/4/2022
C
E
2
12/4/2022
A
C
2
12/5/2022
C
D
2
12/7/2022
D
E
As you can see for ticket 1, it cycled between statuses B and C before finally ending at E. I want to calculate the average time tickets take to move between specific statuses (say A->C, C->E). It’s a bit confusing because tickets can return to previous statuses and tickets don’t need to move through every status. There is an order to the statuses but you can return to a previous state.
Any ideas?
I’ve tried a bunch of things, like lagging (only looks at previous/next), or even pivoting with case statements and subtracting but it doesn't seem to work.
Again the ask is to find the time spent (on average) to go between 2 specific statuses, such as A->C or C->E
Here's my query so far. The idea is to pivot things and just subtract, but I'm really not sure this is gonna be valid
with pv_times as (
select ticket_no,
max(case when current_status='A' and next_status='B' then created_at else null end) as ab_time,
max(case when current_status='A' and next_status='C' then created_at else null end) as ac_time
FROM statuses
GROUP BY 1
)
select * from pv_times
# subtract times to find diff...but is this even valid?
time spent to go between 2 specific statuses
Enumerate all such statuses.
This is the lower diagonal triangle of a 5 × 5 matrix.
Then do a JOIN (.merge) to aggregate all observed
transitions against that vector of possibilities,
.count()'ing the number of them we observed.

SQL Query - solving duplicate data

Here are the 2 tables I am using currently with some example data:
Finances
FinanceID Variation Total
1 0 £1,000.00
5 1 £250.00
24 2 £500.00
A Project can contain multiple Finances, Variation 0 is the Original Order, and each Finance line here after is 1 greater than the previous.
Application
ApplicationID ApplicationNumber PercentageComplete Value
5 1 20% £200.00
17 2 50% £300.00
35 3 75% £250.00
91 4 90% £150.00
The Application table above references the Original Finance Line, NOT the Variations
and here is an example of my problem which I will explain in more detail after:
Application 5 PercentageComplete Value
Contains no Variations
Application 17 PercentageComplete Value
Contains Variation 1 40% £100.00
Application 35 PercentageComplete Value
Contains Variation 1 100% £150.00
Contains Variation 2 25% £125.00
Application 91 PercentageComplete Value
Contains Variation 1 100% £0.00
Contains Variation 2 60% £175.00
An Application can contain multiple Variations
Once an Application contains a Variation, it needs to be automatically added to the next Application that is created.
So using the above example, the user would manually add Variation 1 to Application 17, enter its percentage complete, and the the value would be calculated automatically
MY PROBLEM:
Now for Application 35, I want the variation line from the previous Application added to this one AUTOMATICALLY, however when the infomation is edited (now at 100%) i do not want this to affect Application 17.
is my only option to keep duplicating the data for each Variation Line or is there a more efficient method someone could help me with? I have tried writing a Query to do this also, which was a lot more difficult than i anticipated so if this is the only method, some pointers or an example would be great help.
For the Query, I created this table to try it:
VariationLine ApplicationID VariationID PreviousPercentage NewPercentage Value
1 17 1 0% 40% £100.00
2 35 1 40% 100% £150.00
3 35 2 0% 25% £125.00
4 91 1 100% 100% £0.00
5 91 2 25% 60% £175.00
If I was to add a 5th Application, then I would need to insert the 2 previous VariationLines for the new Project (4 and 5)
Short Version of my problem:
I have an 4 Application's... The 1st has no Variations and just its own cost. For the 2nd Application the user manually adds Variation 1 with a percentage complete of 40% (£100.00)... when the user creates the 3rd Application, I would like SQL Server to automatically add the 1st Variation to this Project, as a new Variation line so that i can ammend the percentage complete and not affect the previous Application
Entity Relationship Diagram
I have created a view to first determine the the previous VariationLines based upon a changedate column, and when a new Application is added, then this view below would select all of the Applications Variations based on the previous Application. If the Application was a 1st, then nothing is added at first, a user has the option to add in Variations to the Application at any point in time.
CREATE VIEW NextApplicationVariations AS
SELECT variationid,
(SELECT TOP (1) applicationid
FROM dbo.applications
ORDER BY changedate DESC) AS ApplicationID,
(SELECT TOP (1) applicationnumber
FROM dbo.applications
ORDER BY changedate DESC) AS ApplicationCounter,
variationpercentage,
0 AS VariationValue,
variationpercentage AS PreviousPercentage,
projectreference,
totalvariationvalue,
0 AS VariationRetention
FROM dbo.variationline
WHERE ( projectreference = (SELECT TOP (1) projectreference
FROM dbo.projectcosts
ORDER BY changedate DESC) )
AND ( applicationcounter = (SELECT TOP (1) applicationnumber
FROM dbo.applications
ORDER BY changedate DESC) - 1 )
Now all thats left to do is insert the new lines based upon this view, and now this query is ran in a view, I can use the NOT EXISTS in the query to determine whether the latest Application has had its VariationLines added automatically
--INSERT PREVIOUS VARIATIONS INTO NEW APPLICATIONS
INSERT INTO variationline
(variationid,
applicationid,
applicationcounter,
variationpercentage,
variationvalue,
previouspercentage,
projectreference,
totalvariationvalue,
variationretention)
SELECT nextvariationline.variationid,
nextvariationline.applicationid,
nextvariationline.applicationcounter,
nextvariationline.variationpercentage,
nextvariationline.variationvalue,
nextvariationline.previouspercentage,
nextvariationline.projectreference,
nextvariationline.totalvariationvalue,
nextvariationline.variationretention
FROM nextvariationline
WHERE NOT EXISTS (SELECT *
FROM variationline
WHERE nextvariationline.applicationid =
variationline.applicationid)

Omit item from Sum SQL

I'm very new to programming and SQL, I can't figure this one out, perhaps I haven't learned the concept yet, but I'm hoping you can help me. Sorry if it's too easy and boring.
/*2.37 Write an SQL statement to display the WarehouseID and the sum of
QuantityOnHand,grouped by WarehouseID. Omit all SKU items that have 3 or more items
on hand from the sum, and name the sum TotalItemsOnHandLT3 and display the results
in descending order of TotalItemsOnHandLT3.*/
SELECT WarehouseID, SUM(QuantityOnHand) AS TotalItemsOnHandLT3
FROM INVENTORY
GROUP BY WarehouseID
HAVING COUNT(WarehouseID) >= 3
ORDER BY TotalItemsOnHandLT3 DESC
"Omit all SKU items that have 3 or more items on hand from the sum", sounds more like :
FROM INVENTORY WHERE QuantitiyOnHand < 3
rather than :
HAVING COUNT(WarehouseID) >= 3
INVENTORY is the list of products (SKU = Stock Keeping Unit = Individual Product Stored in the warehouse) where every product has a WarehouseID. This warehouseID presumably determines where the product is stored.
By Omit all SKU items, it asks you to only display those products that are stored in minimum 3 places in the warehouse. This can be done with the having clause,
HAVING COUNT(WarehouseID) >= 3
I do not know the structure and data of your INVENTORY table, but simply put, Consider your data is like this:
SKUID WareHouseID QuantityOnHand
1 1 10
1 2 10
2 1 10
1 3 5
2 2 20
In the above case, Product = 1 (SKUID), is stored in 3 different warehouses whereas product 2 is stored in 2 warehouses. Hence,
SKUID COUNT(WareHouseID) SUM(QuantityOnHand)
1 3 25
2 2 30
In this case, your query will only "Omit" product 1, and not the product 2.
Its says Omit, they why
HAVING COUNT(WarehouseID) >= 3
and not
HAVING COUNT(WarehouseID) < 3

sequential numbering of rows

I have to customize a SQL view to be able to perform a system integration test. One table keeps track of the transactions and the LINE ITEMS per transactions (for example, transaction 2 has three items, so there are three consecutive rows corresponding to the same transaction number) What I need to accomplish is to get a column where I keep track of the number of items for THAT transaction, always starting from 1 for each transaction.
For example, the first column is TransactionNumber and the second is DesiredOutput:
1 1
1 2
2 1
2 2
2 3
3 1
4 1
4 2
ETC...
I know how to number consecutive rows on the WHOLE table, but I cannot find any reference to this numbering that depends on a value on another row.
For SQL Server 2005+ and Oracle you can use the following:
SELECT TransNumber, ROW_NUMBER() OVER(PARTITION BY Transnumber ORDER BY Something) [DESIRED COLUMN OUTPUT]
FROM YourTable

MDX query to count number of rows that match a certain condition (newest row for each question, client group)

I have the following fact table:
response_history_id client_id question_id answer
1 1 2 24
2 1 2 27
3 1 3 12
4 1 2 43
5 2 2 39
It holds history of client answers to some questions. The largest response_history_id for each client_id,question_id combination is the latest answer for that question and client.
What I want to do is to count the number of clients whose latest answer falls within a specific range
I have some dimensions:
question associated with question_id
client associated with client_id
response_history_id associated with response_history_id
range associated with answer. 0-20 low, 20-40 = medium, >40 is high
and some measures:
max_history_id as max(response_history_id)
clients_count as disticnt count(client_id)
Now, I want to group only the latest answers by range:
select
[ranges].members on 0,
{[Measures].[clients_count]} on 1
from (select [question].[All].[2] on 1 from [Cube])
What I get is:
Measures All low medium high
clients_count 2 0 2 1
But what I wanted (and I can't get) is the calculation based on the latest answer:
Measures All low medium high
clients_count 2 0 1 1
I understand why my query doesn't give me the desired result, it's more for demonstration purpose. But I have tried a lot of more complex MDX queries and still couldn't get any good result.
Also, I can't generate a static view from my fact table because later on I would like to limit the search by another column in fact table which is timestamp, my queries must eventually be able to get _the number of clients whose latest answer to a question before a given timestamp falls within a specific range.
Can anyone help me with this please?
I can define other dimensions and measures and I am using iccube.