This question already has answers here:
How can I find the right size box for each product?
(2 answers)
Closed 2 years ago.
This is a hard question and I hope I can get the answer here.
The question is to find the right size box which allow the logistic business to save the money when shipping.
We have 2 tables which are boxes and products.
Boxes table contains each ID and dimensions per box.
'w' for wide, 'd' for depth and 'h' for height. Please assume we have just 3 box samples for our convenience.
Products table includes also product ID, dimensions. Dimensions has the same meaning as boxes table.
'layable' means the product can be packaged not only straight position but also layable position. For instance product 'g' is a fragile bottle not being able to put horizontal position in the box. Thus this is 'n' in layable column.
This question needs to query each product ID with the right size box.
The right size box means the product needs to be shipped with box that is the least space.
Hope your kind help. Thanks.
boxes:
size
w
d
h
S
353
250
25
M
450
350
160
L
610
460
460
products:
ID
w
d
h
layable
a
350
250
25
y
b
450
250
160
y
c
510
450
450
y
d
350
250
25
y
e
550
350
160
y
f
410
400
430
n
g
350
240
25
n
h
450
350
160
n
i
310
360
430
n
Expected output:
ID
size
a
S
b
M
...
....
...
....
...
....
g
S
Hmmm . . . I'm not quite sure how "layable" fits in. But you want the smallest box that is as big as or bigger than each dimension. The basic idea is:
select p.*,
(select b.size
from boxes b
where b.w >= p.w and b.d >= p.d and b.h >= p.h
order by b.size desc -- happens to works because S > M > L
limit 1
) as size
from products p
Related
I have this dataframe:
pd.DataFrame({'ids':['a','b','c','d','e','f']
,'id_order':[1,2,3,4,5,6]
,'value':[1000,500,3000,2000,1000,5000]})
What I want is to iterate over the rows and get the maximum value of all previous rows.
For example, when I iterate to id_order==2 I would get 1000 (from id_order 1).
When I move forward to id_order==5 I would get 3000 (from id_order 3)
The desired outcome should be as follows:
pd.DataFrame({'ids':['a','b','c','d','e','f']
,'id_order':[1,2,3,4,5,6]
,'value':[1000,500,2000,3000,1000,5000]
,'outcome':[0,1000,1000,2000,3000,3000]})
This will be done on a big dataset so efficiency is also a factor.
I would greatly appreciate your help in this.
Thanks
You can shift the value column and take the cumulative maximum:
df["outcome"] = df.value.shift(fill_value=0).cummax()
Since shifting nullifies the first entry we fill it with 0.
>>> df
ids id_order value outcome
0 a 1 1000 0
1 b 2 500 1000
2 c 3 3000 1000
3 d 4 2000 3000
4 e 5 1000 3000
5 f 6 5000 3000
Consider the following table, describing a patients medication plan. For example, the first row describes that the patient with patient_id = 1 is treated from timestamp 0 to 4. At time = 0, the patient has not yet become any medication (kum_amount_start = 0). At time = 4, the patient has received a kumulated amount of 100 units of a certain drug. It can be assumed, that the drug is given in with a constant rate. Regarding the first row, this means that the drug is given with a rate of 25 units/h.
patient_id
starttime [h]
endtime [h]
kum_amount_start
kum_amount_end
1
0
4
0
100
1
4
5
100
300
1
5
15
300
550
1
15
18
550
700
2
0
3
0
150
2
3
6
150
350
2
6
10
350
700
2
10
15
700
1100
2
15
19
1100
1500
I want to add the two columns "kum_amount_start_last_6hr" and "kum_amount_end_last_6hr" that describe the amount that has been given within the last 6 hours of the treatment (for the respective timestamps start, end).
I'm stuck with this problem for a while now.
I tried to tackle it with something like this
SUM(kum_amount) OVER (PARTITION BY patient_id ROWS BETWEEN "dynmaic window size" AND CURRENT ROW)
but I'm not sure whether this is the right approach.
I would be very happy if you could help me out here, thanks!
I want to display 3 most viewed photos for 7 days. Let's say that I have 4 images, A, B, C and D.
Number of total views:
A - 300
B - 305
C - 310
D - 400
Number of views in the last 7 days from now:
A - 100
B - 90
C - 95
D - 45
So the 3 most viewed photos for 7 days are A, B and C.
But after 10 minutes it changed! Let's say that someone visited photo D a lot of times, and the table looks like this:
A - 95 (yeah, it can decrease)
B - 92
C - 98
D - 105
Now, the 3 most viewed photos for 7 days are B, C and D.
The question is: how to structure the database? I only have information about a total number of views and the date when the photo was published.
I've got a list of items and two locations (X and Y) that these items can be in.these two locations have these items in different quantities.
So When someone places an order for a few items, the items can be pulled from either of these two locations.
Below is the 'Orders' table I've created but it shows two columns for two locations and available stock.
ItemNumber Location Stock X Stock Y
A X 12 32
B X 10 54
C X 5 23
A Y 54 30
C Y 65 36
D Y 76 23
E X 12 31
F X 32 19
F Y 72 40
What I want to see is available stock for the requested location in a column, not both locations and stock availability in two columns as I've done above.
Result table I Want to see is,
ItemNumber Location Avail Stock
A X 12
B X 10
C X 5
A Y 30
C Y 36
D Y 23
E X 12
F X 32
F Y 40
I just cant get my head around this to do it. great if anyone could help or tell me if its even possible.
Thanks
You can use a CASE WHEN expression:
SELECT ItemNumber,
Location,
CASE WHEN Location = 'X' THEN [Stock X]
WHEN Location = 'Y' THEN [Stock Y]
END Avail_Stock
FROM Orders
You have the union tag, so:
SELECT ItemNumber,
Location,
[Stock X] AS Avail_Stock
FROM Orders
WHERE Location = 'X'
UNION
SELECT ItemNumber,
Location,
[Stock Y] AS Avail_Stock
FROM Orders
WHERE Location = 'Y'
I have a worked example of how to compute the capacity of a hard disk, could anyone explain where the BOLD figures came out of?
RPM: 7200
no of sectors: 400
no of platters: 6
no of heads: 12
cylinders: 17000
avg seek time: 10millisecs
time to move between adj cylinders: 1millisec
the first line of the answer given to me is:
12 x 17 x 4 x 512 x 10^5
I just want to know where the parts in bold came from.The 512 I dont know. The 10 is from the seek time but its power 5?
The answer is
heads x cylinder x sectors x 512 (typical size of one sector in bytes)
so this is
12 x 17000 x 400 x 512
which is the same as
12 x 17 x 1000 x 4 x 100 x 512
and
100 = 10^2
1000 = 10^3
10^2 x 10^3 = 10^5
As you want the capacity, you don't need any times here.
A reference for the 512 bytes can be found at Wikipedia, for example (and it also has a similar example with the same formula a bit below).