Partitioning by counts in SQL - sql-server

This issue is related to question I asked here. I have a table that looks like this:
Item Count
1 1
2 4
3 8
4 2
5 6
6 3
I need to group items that are, for example, less than 5 into a new group and the total of each groups should be at least 5. The result should look like this:
Item Group Count
1 1 1
2 1 4
3 2 8
4 3 2
5 4 6
6 3 3
How do I achieve this? Many thanks.

Why isn't this a correct result?
Item Group Count
1 1 1
2 2 4
3 3 8
4 4 2
5 5 6
6 1 3
Or this?
Item Group Count
1 1 1
2 2 4
3 3 8
4 4 2
5 5 6
6 6 3
Seems to me that you're trying to solve the answer 'how to group the items as to minimize the number of groups and maximize the number of items in each group, w/o exceeding the limit 5'. Which sounds a lot like the Knapsack problem. Perhaps a you should read the Celko's SQL Stumper: The Class Scheduling Problem and the solutions proposed. Others have also approached this problem, eg. And now for a completely inappropriate use of SQL Server. Heads up: this is no a trivial problem by any means. Any naive algorithm will die a slow death attempting to solve it on a 1M rows table...

Related

what is minimum number of operations required to sort an array if:-

In each operation we can either push an element to the end of the array or at the beginning of it
for example an array 3 2 5 1 4 6 would take 4 steps.
after first operation 2 3 5 1 4 6
after second operation 1 2 3 5 4 6
after third operation 2 3 1 4 6 5
after fourth operation 1 2 3 4 5 6
I think in the best case, the array is already sorted - 0 operations needed.
In the worst case, its sorted already, but in the opposite order (eg 6 5 4 3 2 1), you gonna need number of elements-1 operations.

How to create Orthogonal array?

Suppose we have following three factors:
Factor A: 5 possible values
Factor B: 4 possible values
Factor C: 2 possible values
How can I construct an Orthogonal array for these?
Main thing which I don't understand is making the combinations. I remember we used to follow '11112222', '11221122', '12121212' this kinda combinations, but it seems everyone has different approach for filling the values in array.
Is there any standard approach?
There isn't a single neat algorithm that generates orthogonal arrays to order. Instead there are a variety of constructions that have been discovered in a host of different areas of mathematics, and some techniques for modifying orthogonal arrays to change their parameters in some way or another. For instance see http://www.itl.nist.gov/div898/handbook/pri/section3/pri33a.htm and http://www.win.tue.nl/~aeb/preprints/oa3.pdf. Many statistics packages have an orthogonal array design utility which uses these rules and a list of known orthogonal arrays to try and find an orthogonal array that will satisfy the requirements it has been given.
In your case I can find nothing closer at the moment than the six five-level factors design at http://www.york.ac.uk/depts/maths/tables/l25.htm using 25 runs. You can certainly discard three columns. Where you have e.g. five levels in the design and only 4 (or 2) levels in the experiment I would be inclined to consistently relabel e.g. {1,2,3,4,5} -> {1,2,3,4,4} and {1,2,3,4,5} => {1,2,1,2,1} but I have no clear idea of what this does to the experimental properties.
The computing of orthogonal arrays can be computationally expensive, so designs are generally made available in the form of a library.
The R package DOE.base has a oa.design() function that retrieves a design with a given number of factors and factor levels. For example, to retrieve a design with 3 factors and levels of 3, 4 and 5, use these commands.
library(DOE.base)
oa.design(nlevels=c(3,4,5))
In this case, the returned design is a full factorial with 60 runs. This still is an orthogonal array, but a much more expensive experiment than the alternatives with equal factor levels.
To obtain an orthogonal array 3 factors with 5 levels each, use:
oa.design(nlevels=c(5,5,5))
A B C
1 1 5 4
2 2 1 5
3 3 4 5
4 3 5 2
5 5 2 4
6 3 3 3
7 5 5 5
8 5 4 3
9 2 5 3
10 5 1 2
11 4 1 3
12 5 3 1
13 4 4 4
14 1 1 1
15 1 2 3
16 3 2 1
17 2 3 4
18 4 3 2
19 4 5 1
20 3 1 4
21 1 3 5
22 1 4 2
23 4 2 5
24 2 2 2
25 2 4 1
The entering 3 factors with 4 levels each returns an orthogonal array of 16 runs and entering 3 factors of 3 levels returns an orthogonal array of 9 runs.
Alternatively, the Python package OApackage is available in PyPi (https://pypi.org/project/OApackage/).
For more information, see:
Complete Enumeration of Pure-Level and Mixed-Level Orthogonal Arrays, E.D. Schoen, P.T. Eendebak, M.V.M. Nguyen, Journal of Combinatorial Designs, Volume 18, Issue 2, pages 123-140, 2010.
Two-Level Designs to Estimate All Main Effects and Two-Factor Interactions, Pieter T. Eendebak, Eric D. Schoen, Technometrics Vol. 59 , Iss. 1, 2017

Need some simple logic help, been stuck for a few hours

The problem is asking to take any amount of numbers, and find the highest possible sum of difference(using absolute value) between consecutive numbers. For example numbers 1 2 and 3 would be arranged 3 1 2 to get a sum of 3 (3-1 = 2, and 1-2 = 1).
Now my first thoughts were to take the highest number in the list followed by the lowest number and arrange in that way through the end, but that doesnt work out as the end of the list will end up having all of the numbers in the middle accumulating almost no differences. The only other thing I have thought of is to find every single possible order and return the highest sum, but with a longer list this will take way too long and I assume there might be a better way.
For reference here are some sample input and output numbers
9 2 5 3 1 -> 21
7 3 4 5 5 7 6 8 5 4 -> 24
Any help at all would be much appreciated, even if its just pointing me in the right direction.
There are 2 approaches to this problem.
Approach 1:
Brute force.
Approach 2:
Figure out an algorithm for how to arrange the numbers.
I always like approach 2 better if it is feasible.
It seems reasonable that you would get a high sum if you order the numbers high-low-high-low-high...
So start by sorting the numbers and then divide them into two equally large groups of low and high numbers. If there is an odd number of numbers the middle number will be left over.
Then you just pick numbers alternately from the two groups.
It is easy to prove that the order of the interior numbers doesn't matter as long as you stick with the high-low-high-low ordering.
However, since the start and end number only has one neighbour, the first and last number should be the middle numbers.
Finally, if you have an odd number of numbers, place the last number at the start or end, whatever gives the biggest difference.
Example:
7 3 4 5 5 7 6 8 5 4 -> [sort] -> 3 4 4 5 5 5 6 7 7 8
high numbers: 5 6 7 7 8
low numbers: 3 4 4 5 5
Arranged:
5 3 6 4 7 4 7 5 8 5 = 24
Example:
9 2 5 3 1 -> [sort] -> 1 2 3 5 9
high numbers: 5 9
low numbers: 1 2
left over: 3
Arranged:
3 5 1 9 2 = 21 (3 goes at the start, because |3-5| > |3-2|)

All row-combinations of a matrix in a new matrix with matlab

I have got a question regarding all the combinations of matrix-rows in Matlab.
I currently have a matrix with the following structure:
1 2
1 3
1 4
2 3
2 4
3 4
Now I want to get all the possible combinations of these "pairs" without using a number twice in the same row:
1 2 3 4
1 3 2 4
1 4 2 3
And it must be possible to make it with n-"doublecolumns". Which means, when my pair-matrix goes for example until "5 6", i want to create the matrix with 3 of these doublecolumns:
1 2 3 4 5 6
1 2 3 5 4 6
1 2 3 6 4 5
1 3 2 4 5 6
1 3 2 5 4 6
....
I hope you understand what I mean :)
Any ideas how to solve this?
Thanks and best regard
Jonas
M = [1 2
1 3
1 4
2 3
2 4
3 4]; %// example data
n = floor(max(M(:))/2); %// size of tuples. Compute this way, or set manually
p = nchoosek(1:size(M,1), n).'; %'// generate all n-tuples of row indices
R = reshape(M(p,:).', n*size(M,2), []).'; %// generate result...
R = R(all(diff(sort(R.'))),:); %'//...removing combinations with repeated values

Updating syntax

I have the following scenario:
Table is _etblpricelistprices
Columns are as follows:
iPriceListNameID iPricelistNameID iStockID fExclPrice
1 1 1 10
2 2 1 20
3 3 1 30
4 4 1 40
5 5 1 100
6 6 1 200
7 7 1 300
8 8 1 400
9 1 2 1000
10 2 2 2000
11 3 2 3000
12 4 2 4000
13 5 2 50
14 6 2 40
15 7 2 30
16 8 2 20
There are only two stock items here, but a lot more in the DB. The first column is the PK which auto-increments. The second column is the Pricelist. The pricelist is split as follows. (1-4) is current pricing and (5-8) is future pricing. the third column is the stock item's ID, and the fourth column, the pricing of the item.
I need a script to update this table to swap the future and current pricing per item. Please help
Observe, if you will, that swapping the iPricelistNameID values will achieve the same overall effect as swapping the fExclPrice values, and can be perfomed using a formula:
UPDATE _etblpricelistprices
SET
iPricelistNameID = CASE
WHEN iPricelistNameID > 4 THEN iPricelistNameID - 4
ELSE iPricelistNameID + 4
END

Resources