Looping through a sort order - sql

I'm not sure how to explain this, but hopefully this makes sense. If I require 10 items of stock to be removed from this table data below, is it possible to send the number 10 into a query
and it updates the table based on the sort order, basically, it will take all it can from the first line and the rest from the second line (or third or firth if you know what I mean):
This is the start
componentid
stocklevel
so
991
5
1
1063
16
2
This is what I am trying to achieve
componentid
stocklevel
so
991
0
1
1063
11
2
Almost like, if it gets to zero, move to the next row

Related

finding the leftside of the number from my query

i have a query which is getting updated on the basis that everytime something goes wrong, it adds 1 to the front of the exists number and every 1 added to the front has a different meaning
so update happens like this
update users
set i = i + 10000
where username = #username
i am writing a query to fetch the extreme 1 on the left side, if it has, display a message
because the number at the right side gets increasing too, so it always has a unique number
like 110
11000
110000
1100000
so if i pick last one and get the first 1 and measures against the remaining numbers count which is 6, i know which issue the user has
CAST the number as string and the you can chekc the string for the positions of the one
You can nest the CASE WHEN to the depth that you need
SELECT
value,
CASE CHARINDEX('1', CAST(value as CHAR(11)))
WHEN 0 THEN 'No number'
WHEN 1 THEN
CASE CHARINDEX('1', CAST(value as CHAR(11)), 2)
WHEN 0 THEN 'only one 1 at the beginning'
WHEN 2 THEN 'two ones at the beginning'
WHEN 3 THEN 'ones at the beginning and at the trind another'
WHEN 4 THEN 'ones at the beginning and at the forth another'
ELSE ' First 1 one and then later another'
END
WHEN 2 THEN 'only a one at the third position'
ELSE 'somewhere a one after the third'
END
FROM T
value
(No column name)
0
No number
1
only one 1 at the beginning
11
two ones at the beginning
100010
First 1 one and then later another
100011
First 1 one and then later another
101000
ones at the beginning and at the trind another
110000
two ones at the beginning
100001
First 1 one and then later another
100000
only one 1 at the beginning
100
only one 1 at the beginning
101
ones at the beginning and at the trind another
10
only one 1 at the beginning
1000
only one 1 at the beginning
1001
ones at the beginning and at the forth another
1010
ones at the beginning and at the trind another
fiddle

LINQ - Select rows based on whether their sum meets a condition

I’ve run into a problem, as I cannot get a proper working LINQ statement here.
Suppose I have a DataTable with x rows and I have to sort based on the sum of the Quantity column. Then I have a condition Requested Quantity = 20. I need to find the rows equal to the exact sum of RequestedQuantity, but only where the combination of 3 rows is equal to it.
+-----+----------+
| Bin | Quantity |
+-----+----------+
| 1 | 10 |
| 2 | 5 |
| 3 | 5 |
| 4 | 10 |
| 5 | 15 |
+-----+----------+
I can’t seem to figure out the proper LINQ syntax to get this to work. My starting point is this:
From row In StorageBins.AsEnumerable.GroupBy( _
Convert.ToDouble(Function (x) x("Quantity"), cultureInfo)).Sum( _
Function (y) Convert.ToDouble(y("Quantity"), cultureInfo) = _
Double.Parse(RequestedQuantity,cultureInfo))
Initially, I am just trying to get any rows that are equal to my condition. My end-goal, however, is getting any three rows that exactly sum up to my Requested quantity.
I’m not an expert in LINQ, unfortunately. I hope some of you might be!
Maybe I'm missing something, but this actually seems like a pretty complicated problem. Pick any 3 records, but only 3, that add up to exactly 20. How many rows are there in the database? Because this could get to be quite a few potential combinations pretty quickly. And what do you do after you get the 3? Do you have to go back through recursively and group up the other records as well? Or you just need the first set of 3 that add up to 20?
Assuming you just need the first 3, I would do something like this:
Get the first record that is less that 20. Remove it from your input list and put it into your target set.
Then get the first record that is less than 20 minus the first value. ie if the first value was a '5', get records that are less than 15 (20 minus 5). This ensures you 'leave room' for the third value. Remove it from the original list and into your target set.
Then get the first record that is exactly 20 minus number one minus number two. Remove it from the input list and into the target set.
Now you would have to do this in iterators. If there is no value that meets the third criterion, release the third value from your target set and put it back in your input list. Then go back to step 2 and pick the next record that matches step 2 (and ideally that is not equal to the previous value). And if you exhaust all of the iterations through step 2, go back to step one and pick the next value there, and start the whole thing over again...
Unless I'm misunderstanding your requirement...

VBA - representing hierarchical data in Excel

I am fairly new to VBA in Excel and was hoping for some guidance on this problem. I am given hierarchical data from a database to perform a report, and the user would like to format the information such that an entry's children lie directly below it (a parent can have multiple children, child has only one parent). This could perhaps be done by indenting the Notes tab of the data by a few spaces. I was wondering how to first perform the reordering of the data by hierarchy, and then visually showing an indentation by adding, say, 5 spaces to the beginning of the Notes tab.
For sample data:
ID Parent ID Notes
0 NULL This is number 0.
1 0 This is number 1.
2 0 This is number 2.
3 1 This is number 3.
4 3 This is number 4.
5 0 This is number 5.
I would then like the data to be presented as such:
ID Parent ID Notes
0 NULL This is number 0.
1 0 -----This is number 1.
3 0 -----This is number 3.
4 1 ----------This is number 4.
2 3 -----This is number 2.
5 0 -----This is number 5.
Thank you so much! Your help is greatly appreciated :)

sql more complicated querying measurements

I have two tables (sql server), as shown below:
locations
id cubicfeet order
-------------------------------------
1 5 1
2 10 1
3 6 1
items
id cubic feet order
--------------------------------------
1 6 1
2 6 1
3 6 1
I need a query to tell me if all the items will fit into all the locations (for a given order). If all items will not fit into 1 or all locations then I need to create a new location for that given order - and then move any items that DID fit into the locations before to the new location (as many as fit). The new location will only be given a certain amount of cubic feet also - say 17. In this example, sum won't work because all 3 records are 6 so the sum is 18, which is less than the sum of 5,10,6, but the location with volume 5 can't fit any of the items since they are all volume 6 cubic feet.
the only way I think I can do it is creating temp tables in my sp and using a while loop to go through them and update the locations 1 at a time to see if it still fits more...

SSRS Chart with Grouping like in Excel

I wasnt able to find anything like this yet... but here is what i need to do:
I have a query result like this:
ID Data1 Data2 Data3 Data4 ... Data7
1 12 13 15 1 ... 12
2 12 13 15 1 ... 12
3 12 13 15 1 ... 12
4 12 13 15 1 ... 12
I need to make a BarChart With 2 Values, 1 is the first row (ID=1) one is the last row (ID=4). The column headers DataX is what i need the series to be paired by.
Example:
ID Insured Uninsured Rejected
1 12 3 0
4 16 9 2
In the BarChart i need to see the number of insured or ID=1 and ID=2 next to each other, the number of Uninsured and rejected the same.
I feel like i have tried all ways possible but was not able to get anything besides a BarChart where all values of ID=1 where displayed and then all values for ID=2 where displayed next to each other.
Im sure this was a very confusing way to describe it, but i hope someone can understand what i am looking for.
NOTE: I tried to do this in Excel, and it worked within 2 minutes. I set the filter: Series on the 2 rows that i wanted, and set the Categories to the dataX Columns as described, and everything looked great. When i tried to translate this into SSRS i was able to do all the same things in the Series and Categories, but then i had to put in values and that screwed everything up.
PLEASE HELP!
I bet you need to add a grouping to your values by a spanning factor.