How to apply the formula on a summary field - sql-server

I am developing an inventory system and I have developed the following function to convert between cartons and pieces.
Public Function convertQTY(ByVal units_case As Integer, ByVal quantity As QTY) As QTY
While quantity.pieces < 0 And quantity.cartons > 0
If quantity.pieces < 0 And quantity.cartons > 0 Then
quantity.pieces = units_case + quantity.pieces
quantity.cartons -= 1
End If
End While
If quantity.pieces >= units_case Then
quantity.cartons = quantity.cartons + (quantity.pieces \ units_case)
quantity.pieces = quantity.pieces Mod units_case
End If
Return quantity
End Function
For example if units/pieces per carton is 144 then the above code converts the pieces into cartons if the number exceeds 144 and if the pieces are less than 0 then the above code subtracts 1 from carton and adds units per carton into the number of pieces. So that the pieces never exceed total number of pieces in a carton and they are never shown to be less than 0.
I want the same effect in summary field in my crystal report. For showing sum of cartons and pieces at the end of each group. How can i achieve this?

By creating formulas for the number of cartons and remaining pieces, like so:
Number Of Cartons:
Sum ({MyTable.Pieces})\144
Remaining Pieces:
Sum ({MyTable.Pieces}) mod 144

Related

SQL Equivalent of Countif or Index Match on row level instead of columnar level

I have a table which comprises of 30 columns, all adjacent to one another. Of these 5 are text fields indicating certain details pertaining to that entry and 25 are value fields. Value fields have the column name as Val00, Val01, Val02 .....upto Val24
Based on a logic appearing elsewhere, these value fields input a value for n amount of columns and then drop to 0 for all of the subsequent fields
e.g.
When n is 5 the output will be
Val00
Val01
Val02
Val03
Val04
Val05
Val06
Val07
Val24
1.5
1.5
1.5
1.5
1.5
0
0
0
0
As can be seen, all values starting from val05 will drop to 0 and all columns from Val 05 to Val24 will be 0.
Given this output, I want to find what this n value is and create a new column ValCount to store this value in it.
In Excel this would be fairly straight forward to achieve with the below formula
=COUNTIF(Val00:Val24,">0")
However I'm not sure how we would go about in terms of SQL. I understand that the count function works on a columnar level and not on a row level.
I found a solution which is rather long but it should do the job so I hope it helps you.
SELECT SUM(SUM(CASE WHEN Val00 >= 1 THEN 1 ELSE 0 END)
+ SUM(CASE WHEN Val01 >= 1 THEN 1 ELSE 0 END)
+ SUM(CASE WHEN Val02 >= 1 THEN 1 ELSE 0 END)) As ValCount

How to add additional zero arrrays

I have the following problem in my simulation.
A is an array 24 x 2. I am going to split it and get 4 or 12 array. It means that I group 6 or 2 array. It will be ok, if I use even "split" coefficient. If it is odd, I can"t split A.[ I can't group 5 or 7, because of 24/5=4*5 + 4 ( or 5*5 -1) or 24/7=7*3+3.
That's why I going to do the following:
If I have 24 x 2 and need group every 5 together:
block 1 : A(1,:), A(2,:),A(3,:),A(4,:),A(5,:)
block 2 : A(6,:), A(7,:),A(8,:),A(9,:),A(10,:)
block 3 : A(11,:), A(12,:),A(13,:),A(14,:),A(15,:)
block 4 : A(16,:), A(17,:),A(18,:),A(19,:),A(20,:)
block 5 : A(21,:), A(22,:),A(23,:),A(24,:), ?
As you can see the 5th block is not full, Matlab gives me an error. My idea is to create A(25,:)=0. For my simulation it will be ok.
I am going to simulate it as function:
A=rand(m,n)
w- # number of a vector that i would like group together ( in ex., it is `5`)
if mod(w,2)==0
if mod(m,2)==0
% do....
else
% remainder = 0
end
else
if mod(m,2)==0
% remainder = 0
else
%do...
end
I was going to simulate like above, but then I have noticed that it doesn't work. Because 24/10 = 2*10+4. So I should write something else
I can find the reminder as r = rem(24,5). As an example above, MatLab gives me r=4. then I can find a difference c= w-r =1 and after that, I don't know how to do that.
Could you suggest to me how to simulate such a calculation?
Determine the number of blocks needed, calculate the virtual amount of rows needed to fill these blocks, and add as many zero rows to A as the difference between the virtual and actual amount of rows. Since you didn't mention, what the actual output should look like (array, cell array, ...), I chose a reshaped array.
Here's the code:
m = 24;
n = 2;
w = 5;
A = rand(m, n)
% Determine number of blocks
n_blocks = ceil(m / w);
% Add zero rows to A
A(m+1:w*n_blocks, :) = 0
% Reshape A into desired format
A = reshape(A.', size(A, 1) / n_blocks * n, n_blocks).'
The output (shortened):
A =
0.9164959 0.1373036
0.5588065 0.1303052
0.4913387 0.6540321
0.5711623 0.1937039
0.7231415 0.8142444
0.9348675 0.8623844
[...]
0.8372621 0.4571067
0.5531564 0.9138423
A =
0.91650 0.13730
0.55881 0.13031
0.49134 0.65403
0.57116 0.19370
0.72314 0.81424
0.93487 0.86238
[...]
0.83726 0.45711
0.55316 0.91384
0.00000 0.00000
A =
0.91650 0.13730 0.55881 0.13031 0.49134 0.65403 0.57116 0.19370 0.72314 0.81424
0.93487 0.86238 0.61128 0.15006 0.43861 0.07667 0.94387 0.85875 0.43247 0.03105
0.48887 0.67998 0.42381 0.77707 0.93337 0.96875 0.88552 0.43617 0.06198 0.80826
0.08087 0.48928 0.46514 0.69252 0.84122 0.77548 0.90480 0.16924 0.82599 0.82780
0.49048 0.00514 0.99615 0.42366 0.83726 0.45711 0.55316 0.91384 0.00000 0.00000
Hope that helps!

How to modify Totals row in Access 2013

I'm trying to modify a total query so that if the amount of the sum surpasses 1000 for example it changes the value in 2 fields. for example, 1000 grams change to 1 kilogram, and vice versa.
For example, if I have 20 items in grams, and the total of the sum of those 20 items is 1200 grams, how do I make it so that it automatically changes it to 1.2 kilograms. How do I make it detect that its over 1000 to convert it from grams to kilograms.
How about
Function presentFormattedWeight(Weight as Long) as String
Dim KgWeight as Long
Dim GramWeight as Long
KgWeight =0
if(Weight mod 1000)>0 then
KgWeight = Weight\1000
GramWeight = Weight mod 1000
else
GramWeight = Weight
end if
if KgWeight >0 then
presentFormattedWeight = KgWeight & "." & GramWeight & "Kg"
else
presentFormattedWeight = GramWeight & "grams"
end if
End Function

Truly drawing cards at random, and not just a random group? visual basic 2010

I realized a flaw on my coding for picking cards from my structure group arrays this morning for a tabletop card game I'm converting to computer.
Currently the code is set up to randomly pick an array of a specific card, but if quantity of cards in the group is < 1, then the group is skipped. For my deck this isn't being truly random because each card has its own quantity. Some cards have 10 cards, others 6, most 4. How can I code it to where it looks at all these quantity numbers as a whole and picks a random card out of these numbers.
If GameSize >= 3 Then
For StartHands = 10 To 14
Number = (DeckGroup(Rnd.Next(0, DeckGroup.Count)).ID)
'Cardslots Player3
If CardTypeArray(StartHands) = "" Then
If DeckGroup(Number).QuantityInteger > 0 Then
DeckGroup(Number).QuantityInteger -= 1
Player1HandGroup(Number).QuantityInteger3 += 1
CardTypeArray(StartHands) = Player1HandGroup(Number).CardType
Me.NumberArray(StartHands) = Number
Else
'Recall Procedure if Generated Random Number is not allowed
'due to QuantityInteger <= 0
Call StartButton_Click(sender, e)
End If
End If
Next StartHands
End If
when playing a card, and drawing a new card, I use this coding scheme, to prevent stackoverflow, but is virtual the same.
Dim temp As IEnumerable(Of LunchMoneyGame.LunchMoneyMainForm.Group) = From r In DeckGroup Where r.QuantityInteger > 0 Select r
If temp IsNot Nothing AndAlso temp.count > 0 Then
Number = (temp(Rnd.Next(0, temp.Count)).ID)
DeckGroup(Number).QuantityInteger -= 1
'Select the Player depending value of T
Select Case T
Case 0
Player1HandGroup(Number).QuantityInteger += 1
Case 1
Player1HandGroup(Number).QuantityInteger2 += 1
Case 2
Player1HandGroup(Number).QuantityInteger3 += 1
Case 3
Player1HandGroup(Number).QuantityInteger4 += 1
Case 4
Player1HandGroup(Number).QuantityInteger5 += 1
End Select
Edit:
Improved question:
How can I weight the probably of drawing a card based on how many of a particular card are left in the decks quantity integer for each card?
The idea of my solution is to generate a random number over the total number of availables cards in all deck groups. Finally we find the deck group this number targets within a loop:
Dim totalNumerOfCards As Integer = DeckGroup.Sum(Function(d) d.QuantityInteger)
Dim Number As Integer = Rnd.Next(totalNumerOfCards)
Dim numCards As Integer = 0
Dim groupIndex As Integer = 0
Dim cardIndex As Integer = 0
Dim i As Integer = 0
While i < DeckGroup.Length AndAlso numCards <= Number
groupIndex = i
cardIndex = Number - numCards
numCards += DeckGroup(i).QuantityInteger
i += 1
End While
Console.WriteLine("Your card is in DeckGroup({0}), card index {1}",
groupIndex, cardIndex);

Check overlapping series in C#

I am making an application where the user adds start and end to define a range
The condition is that the range should not overlap:
How to check whether a number range is not overlapping e.g.
Range 1 Start 5 End 15
Range 2 Start 1 End 4
Range 3 Start 16 End 20
Range 4 Start 2 End 4
So the Range 4 makes the set invalid, how do I check this in C#.
Further the user can add the range in any order as in the example above, the entire series should be non overlapping.
Thanks for the help suggestion.
Regards,
Sakshi
Answer:
I made the solution is it correct:
If start and end is the range which needs to be validated then
start >startRange and start less than endRange
end>startRange and end less than endRange
The above 2 condition validates that the series is overlapping.
Where startRange and endRange is the start and end for all existing ranges.
The OP suggests two checks to validate that a new range does not overlap with an existing range. However, these two checks do not cover all the possibilities. For instance, if the existing range is (4,10) and the new range is (2,12), it will not be flagged, because it starts prior to the start of the existing range, and ends afterwards.
Instead, I'd recommend the following approach:
if (newRangeStart <= existingRangeEnd && newRangeEnd >= existingRangeStart) {
// we have an overlap
}
Essentially, there are four possibilities of overlapping ranges:
A range which starts before the existing range and ends within the existing range
A range which starts within the existing range and ends afterward
A range which starts within the existing range and ends within the existing range
A range which starts before the existing range and ends after the existing range
Cases (1) and (2) include partial overlap, while cases (3) and (4) include complete overlap (either the existing range completely encloses the new range [case 3] or the new range completely encloses the existing range [case 4]).
The OP's code catches cases 1, 2 and 3, but not case 4. The code here catches all 4 possibilities of an overlapping range.
Ranges need to have multiple checks:
You can have many variations of overlap, so you'll need to do several tests.
|-----------| |--------------|
|------------------------|
|-------------|
|--------------------------------|
First Check:
Start of Range 1 >= Start of Range 2 and Start of Range 1 <= End of Range 2
Second Check:
Start of Range 2 >= Start of Range 1 and Start of Range 2 <= End of Range 1
Third Check:
End of Range 1 >= Start of Range 2 and End of Range 1 <= End of Range 2
Fourth Check:
End of Range 2 >= Start of Range 1 and End of Range 2 <= End of Range 1
These checks assume that End >= Start on both ranges. If not, you'll need to swap the start and end for the tests.
public static bool DoRangesOverlap(int p_start1, int p_end1, int p_start2, int p_end2)
{
if ((p_start1 >= p_start2 && p_start1 <= p_end2) || (p_start2 >= p_start1 && p_start2 <= p_end1) || (p_end1 >= p_start2 && p_end1 <= p_end2) || (p_end2 >= p_start1 && p_end2 <= p_end1))
{
return true;
}
return false;
}

Resources