Dynamic numberranges via sequence - sql-server

Business wants different numberranges depending which type of number they want to add. I get the Numberranges always beforehand.
NumberPool
id
name
range_start
range_end
sequence_name
1
item
0
1000
seq_items
2
document
500
1500
seq_document
3
item
1001
3000
seq_items2
4
item
10000
11000
seq_items3
NumberTable
Number_Type
number
more_metadata
item
1
foo
item
2
bar
item
...
bar[numbers to 3 to 999]
item
1000
foos
item
5000
bar
document
500
bar
I wanted to use identity or sequences to circumvent collisions. I can set start values and max values but I have to react if they are exhausted.
With a procedure I could catch "has reached its minimum or maximum value." and use a different sequence if I have one remaining. Is there a better way to avoid race conditions and still have dynamic number ranges?

Related

Change formula array for every X number of rows in Excel

I have large number of rows of data in Excel where I need to change the row array of the formula for every 3 rows but I can't figure out how to adjust the formula without an error.
How do I add a formula like this to the formula below?
=INT(((ROW(a1)-1)/11))*1+1
This is the formula I have been using, but I need to change it for every 3 rows.
=IF(COUNTIF($N$4:$N$6, ""), "",MAX($N$4:$N$6))
=IF(COUNTIF($N$7:$N$9, ""), "",MAX($N$7:$N$9))
And so on
Example
I have 3 approvers, if "product" is approved, the date is for the approval date of last said approval, if no approval has been made then the cell is blank. Outcome is what I want to collect from column 3 when product was approved from all 3 approvers which is the newest date of the 3 rows, if one approver has not approved, then I'd like column 4 to be blank.
Product
Approvers
Dates
Outcome
A
1
04.01.2016
04.01.2016
A
2
17.12.2015
04.01.2016
A
3
21.12.2015
04.01.2016
B
1
11.04.2017
11.04.2017
B
2
30.01.2017
11.04.2017
B
3
04.04.2017
11.04.2017
C
1
C
2
13.10.2016
C
3
14.02.2017
D
1
01.03.2022
01.03.2022
D
2
02.12.2019
01.03.2022
D
3
30.01.2020
01.03.2022
Picture of data
Two options:
To answer the original question, to make your formula change every n rows, use =-MOD(ROW()+c, n) to adjust this (where 'c' is a constant just to get them in line, if your data starts on row 2 then c would be 1).
Your formula for row 2 would be:
=IF(COUNTIF(OFFSET($N2,-MOD(ROW($N2)+1,3),0,3),""),"",MAX(OFFSET($N2,-MOD(ROW($N2)+1,3),0,3)))
Another option, not as direct an answer to the question but potentially useful if the number of products changed in future from 3 to something else, would be:
=IF(COUNTIFS(L:L, L2, N:N, ""), "", MAX(IF(L:L=L2, L:L)))
and click Ctrl+Shift+Enter after typing that in (because it's an Array formula, see here, here and here).
The advantage of this approach is that it looks at all rows where the product column is the same (I'm assuming unique products), so no need to limit it to 3 rows per product or have those 3 rows next to each other.

Nested 'for' loops in native Google Sheets (no scripts)

I'm aware that it's possible to emulate a for loop in Google Sheets via the following:
=ARRAYFORMULA(IF(LEN({range_0 to iterate over}),{function_0 for each element in range_0}))
I've not been able to expand on the above approach to achieve the effect of a multi nested for loop. Is this possible? Ideally this would look something like:
=ARRAYFORMULA(IF(LEN({range_0 to iterate over}),IF(LEN({range_0.1 to iterate over for each parent element in range_0}),{function_0.1 for each element in range_0.1},{OPTIONAL: function_0 for each element in range_0})))
Example application:
Iterating over each cell in a row for each row in a column (see below; I'm sure there are ways to achieve this effect via other methods, but I have use for the requested and specified method for cases where a single dynamic formula is necessary to keep up with an unknown and changing number of rows, columns, and functions involved):
Raw data:
Thing
Type
Feature
Cost
Apple
Fruit
Seeds
$1
Car
Automobile
Wheels
$6,000
Laptop
Computer
Keyboard
$500
Grape
Fruit
Tastiness
$0.50
Theoretical formula:
=ARRAYFORMULA(IF(LEN(`Thing`),IF(LEN(COLUMNS({current row of `Thing`})),{current cell in current row of `Thing`} & " of column " & COLUMN({*current cell*}) & " and row " & ROW(`Thing`),""),""))
Output of above theoretical formula:
Thing
Type
Feature
Cost
Apple of column 1 and row 3
Fruit of column 2 and row 3
Seeds of column 3 and row 3
$1 of column 4 and row 3
Car of column 1 and row 4
Automobile of column 2 and row 4
Wheels of column 3 and row 4
$6,000 of column 4 and row 4
Laptop of column 1 and row 5
Computer of column 2 and row 5
Keyboard of column 3 and row 5
$500 of column 4 and row 5
Grape of column 1 and row 6
Fruit of column 2 and row 6
Tastiness of column 3 and row 6
$0.50 of column 4 and row 6
Thank you for your help!
try:
=ARRAYFORMULA(IF(LEN(A3:E7), A3:E7&" of column "&COLUMN(A3:E7)&" and row "&ROW(A3:E7), ))

Vlookup max / min of multiple results

I have a list of values in EXCEL
X Y
5 3000
5 3200
5 2800
10 1600
10 2900
10 3500
15 1800
15 4500
I want to get the list of X without duplicates and corresponding MIN / MAX values Y. Something like this for MAX values:
X Y
5 3200
10 3500
15 4500
How do I proceed?
A simple pivot table will do that.
Insert Pivot Table
Drag X to the rows area
Drag y to the Values area twice
Adjust value settings for one Y to MAX
Adjust value settings for one Y to MIN
Have a look at PowerQuery:
Select any cell within your data range.
Click the Data tab, then > From Table/Range.
If prompted, in the From Table dialog box, you can click the Range Selection button to select a specific range to use as a data source.
If the table or range of data has column headers, you can check My table has headers. The header cells are used to define the column names for the query.
Within PowerQuery AKA Get&TransForm
Click the Transform tab, then > Group by.
In the dialog box, click Advanced and under Group by you choose X
Choose a New column name, e.g.: maxVals. Furthermore choose Max as an Operation and under Column you choose Y.
Confirm through OK and close PowerQuery.
You'll now end up with your desired output. Replicate this for the minVals.
For MAX values you could sort your data: column X ascending & column Y descending and then apply an advanced filter (only to column X), checking the option for unique values only for MIN values you repeat the process but specify ascending as the sort order for column Y
Recording macros of these actions will provide you with the VBA code, which you should be able to make dynamic as you are asking your question in a Lecture after the Sections on Variables and Range References.
I hope this will be helpful

How do I eliminate data that falls within a range of other data in the same query?

I have a query that returns results that describe a numeric range, with some of these data falling within the range of other data returned in the same query. How can I easily eliminate those?
I have the following data:
Code Start End
----- ------- -------
abc 1 1
abc 2 2
abc 3 8
abc 4 4
abc 5 5
xyz 1 1
xyz 2 5
xyz 3 3
In this case, where code is "abc", there are two rows: start=4,end=4 and start=5,end=5. But preceding them is a row where start=3,end=8. So both of those rows should not be returned in my result set.
I can do with with a temp table, cursor, etc. But I'd like to know if there's an elegant way to do this within the query.
I would do this with a WHERE NOT EXISTS() clause.
The EXISTS() function would be to check for another row where the Start is less than or equal to my Start and the End is greater than or equal to my End.
There are no exact duplicate rows in your sample data, but if it's possible for them to exist in your real data, you will have to consider what you want to do with those as well.

How to model arbitrarily ordering items in database?

I accepted a new feature to re-order some items by using Drag-and-Drop UI and save the preference for each user to the database. What's the best way to do so?
After reading some questions on StackOverflow, I found this solution.
Solution 1: Use decimal numbers to indicate order
For example,
id item order
1 a 1
2 b 2
3 c 3
4 d 4
If I insert item 4 between item 1 and 2, the order becomes,
id item order
1 a 1
4 d 1.5
2 b 2
3 c 3
In this way, every new order = order[i-1] + order[i+1] / 2
If I need to save the preference for every user, then I need to another relationship table like this,
user_id item_id order
1 1 1
1 2 2
1 3 3
1 4 1.5
I need num_of_users * num_of_items records to save this preference.
However, there's a solution I can think of.
Solution 2: Save the order preference in a column in the User table
This is straightforward by adding a column in the User table to record the order. Each value would be parsed as an array of item_ids that ranked by the index of the array.
user_id . item_order
1 [1,4,2,3]
2 [1,2,3,4]
Is there any limitation of this solution? Or is there any other ways to solve this problem?
Usually, an explicit ordering deals with the presentation or some specific processing of data. Hence, it's a good idea to separate entities of theirs presentation/processing. For example
users
-----
user_id (PK)
user_login
...
user_lists
----------
list_id, user_id (PK)
item_index
item_index can be a simply integer value :
ordered continuously (1,2...N): DELETE/INSERT of the whole list are normally required to change the order
ordered discretely with some seed (10,20...N): you can insert new items without reordering the whole list
Another reason to separate entity data and lists: reordering lists should be done in transaction that may lead to row/table locks. In case of separated tables only data in list table is impacted.

Resources