SQL: “The maximum number of stacked diagnostics areas has been exceeded.” - sql

The following formula in my SELECT text yields the aforementioned error when attempting to refresh.
SELECT CAPCSQ/((((ih01su + ih02su + ih03su + ih04su + ih05su + ih06su + ih07su + ih08su + ih09su + ih10su + ih11su + ih12su)/12)*1.32)/2)
The ih01su.... portion represents the last 12 months of sales data, divided by 12 to get an average. I then multiply by the 1.32 to get a projection of future sales in roughly 2 years based on store growth. I then divided by 2 because I really only want the value that will be 2 weeks worth of sales, not a full month.
The formula works fine until I attempt to divide the ‘CAPCSQ’ column by this calculated value. At this point I get the following error:
The maximum number of stacked diagnostics areas has been exceeded.

Related

updating the next several row values based on the value of a row in another column

I'm trying to figure out how to add the values of one column (the amount column) to the next few rows based on the condition of another column (the days column). If the condition of the days column is greater than 1, for each day greater than 1 I add the amount column to that many following rows. So if days is three, I add the amount to the next two rows (the first day is just the current row). I actually think this is easier if I make a copy of the amount column, so I made a copy called backlog.
So let's say I have an amount column that represents the amount of support tickets that need to be resolved each day. Each amount has a number of days it takes for the amount to be resolved. I need the total amount to be a sum of the value today and the sum of the outstanding tickets. So if I have an amount of 1 for 2 days, I have 1 ticket amount today and I add that same 1 tomorrow to the ticket amount of tomorrow. If this doesn't make sense, the below examples will. I have a solution as well, but my main issue is doing this efficiently.
Here is a sample dataframe to use:
amount = list(np.zeros(10)) + [random.randint(1,3) for val in range(15)]
random.shuffle(amount)
ex = pd.DataFrame({
'Amount': amount
})
ex.loc[ex['Amount']>0, 'Days'] = [random.randint(0,4) for val in range(15)]
ex.loc[ex['Amount']==0, 'Days'] = 0
ex['Days'] = ex['Days'].astype(int)
ex['Backlog'] = ex['Amount']
ex.head(10)
Input Dataframe:
Amount
Days
Backlog
2
0
2
1
3
1
2
2
2
3
0
3
Desired Output Dataframe:
Amount
Days
Backlog
2
0
2
1
3
1
2
2
3
3
0
6
In the last two values of the backlog column, I have a value of 3 (2 from the current day amount plus 1 from the prior day amount) and a value of 6 (3 for the current day + 2 from the previous day amount + 1 from two days ago).
I have made code for this below, which I think achieves the outcome:
for i in range(0, len(ex['Amount'])):
Days = ex['Days'].iloc[i]
if Days >= 2:
for j in range (1,Days):
if (i+j)>= len(ex['Amount']):
break
ex['Backlog'].iloc[i+j] += ex['Amount'].iloc[i]
The problem is that I'm already using two for loops to slice the data frame for two features first, so when this code is used as a function for a very large data frame it runs far too slowly, and my main goal has been to implement a faster way to do this. Is there a more efficient pandas method to achieve the same outcome? Possibly without having to use slow iteration or a nested for loop? I'm at a loss.

pandas transform moving average daily base to weekly base

I have a moving average with data points on everyday base e.g. 14 days MA.
Now I want to take this MA and display it on a bar chart e.g. one bar represents a week.
How I calculate the MA for this bar?
Is it the sum of the daily MA points over the week?
1,2,3,4,5,6,7
So the MA is 28? but the base is still daily?
Can someone try to explain if this makes sense and is correct?
Yes, Moving Average (MA) for the bar is the average of daily MA (daily count).
So
# Calculating MA
MA(week1) = (MA1 + MA2 + MA3 + MA4 + MA5 + MA6 + MA7)/7
MA(week1) = (1 + 2 + 3 + 4 + 5 + 6 + 7)/7 = 4
# Bar value 4
Incase your data is 14-day MA, you will have to make some assumptions and calculate the weekly MA. Have a look at the example to get a better understanding.
# 14-day MA -> Weekly MA
(W1 + W2)/2 = MA1
(W2 + W3)/2 = MA2
...
(Wn-1 + Wn)/2 = MAn-1
# Assume W1 == W2, you can estimate the per weekly MA
Example to calculate Moving Average

repeated sum of digits big o complexity

Lets say for example we have the number 12345.
This sums to 15 when you add 1 + 2 + 3 + 4 + 5, which sums to 6 when you add 1 + 5.
My question is, what would the time complexity be for a repetitive adding algorithm like this be? This process is happens until there is only a single digit left.
I know that for any given number, the # of digits is approximately ln(n). Im thinking that this means that the big o would look something like (ln(n))^k, for some k. However, I am not confident because each time you sum, the number of digits gets smaller (first summed 5 digits, then only 2).
How would I go about figuring this out?

Counting the number of datapoints within a Euclidean distance MS SQL

Have 2 data sets
list of 300 geocordinates
list of over 2million geocordinates
For each entry in list 1, I am trying to count the number of entries from list 2 that lie within 5 mile radius.
I've decided to use the euclidean distance as i am only dealing with relatively small distances.
Here is my code. It takes forever to run. Any suggestions on how I can improve the code.
Select
DistFilter.storenumber,
count(companynumber) as sohoCount
from
(Select
UKStoreCoord.storenumber,
UKStoreCoord.latitude as SLat,
UKStoreCoord.longitude as SLng,
SohoCoordinates.companynumber,
SohoCoordinates.latitude,
SohoCoordinates.longitude
from UKStoreCoord, SohoCoordinates
where abs(UKStoreCoord.latitude - SohoCoordinates.latitude)<0.1 and abs(SohoCoordinates.longitude - UKStoreCoord.longitude)<0.1
group by
UKStoreCoord.storenumber,
UKStoreCoord.latitude,
UKStoreCoord.longitude,
SohoCoordinates.companynumber,
SohoCoordinates.latitude,
SohoCoordinates.longitude) as DistFilter
where (((Distfilter.latitude - Distfilter.SLat) * 69) ^2 + ((Distfilter.longitude - Distfilter.SLng) * 46) ^2) <25
group by
DistFilter.storenumber
cheers

SQL Exam - estimating table size problem

I'm preparing to the SQL Server exam (70-431). I have the book from Sybex "SQL Server 2005 - Implementation and Maintenance". I'm little confused about estimating a size of a table.
In the 2nd chapter there is explained how to do this:
Count the row size from the formula: Row_Size = Fixed_Data_Size + Variable_Data_Size + Null_Bitmap + Row_Header.
Fixed_Data_Size is a sum of all sizes of fixed length columns (simple sum)
Variable_Data_Size = 2 + (num_variable_columns × 2) + max_varchar_size, num_variable_columns - number of columnt with variable length, max_varchar_size - maximum size of varchar column
null_bitmap = 2 + ((number of columns + 7) ÷ 8) (rounded down)
Row_header always equals 4.
Calculating rows per page from the formula: Rows_Per_Page = 8096 ÷ (Row_Size + 2) (rounded down)
Estimating the number of rows in the table. Let's say that table has 1,000 rows.
Calculating the number of pages needed: No_Of_Pages = 1,000 / Rows_Per_Page (rounded up)
Total size: Total_Size = No_Of_Pages * 8,192, where 8,192 is the size of one page.
So everything is perfectly clear for me. I made one example and checked with the answers in the book that my calculations are correct. But there is one question which confuses me.
The question is: we have a table with the following schema:
Name Datatype
-------------------
ID Int
VendorID Int
BalanceDue Money
DateDue Datetime
It is expected that in this table there will be about 5,000 rows. Question (literaly): "How much space will the Receivables table take?"
So my answer is simple:
null_bitmap = 2 + ((4+7) / 8) = 3.375 = 3 (rounded)
fixed_datasize = 4 + 4 + 8 + 8 = 24
variable_datasize = 0
row_header = 4 (always)
row_size = 3 + 24 + 0 + 4 = 31
But in the answer they omit row_header and they don't add 4. Is it a mistake in the book or row_header is added only in some cases (which are not mentioned in the book)? I was thinking that maybe row_header is added only if there are variable-length fields in the table, but there is another exercise in which there are not variable-length fields and row_header is added. I would appreciate if someone explains me that. Thanks.
Inside the Storage Engine: Anatomy of a record says all records have a record header:
The record structure is as follows:
record header
4 bytes long
two bytes of record metadata (record type)
two bytes pointing forward in the record to the NULL bitmap