SQL Limit Average - sql

I have a database with 169461 records. Within one field is begin_milepost that numerically counts up from 0 at 0.01 intervals. I want to change the interval from 0.01 to 0.1 by making a new column that averages ten records at a time to create one record, and having it do this all the way down. I'm using access 2013.
Example would be 0.01,0.02,0.03,0.04,0.05,0.06,0.07,0.08,0.09,0.1 being turned into 0.055 and repeating on the next ten records.

See if this gives you what you are looking for:
SELECT (([Marker]-0.01)*100)\10 AS MarkerGroup, Sum([Marker])/Count([Marker]) AS AvgMarker
FROM tblMarkers
GROUP BY (([Marker]-0.01)*100)\10;

Related

SQL - Finding Percent Of a Total and subtracting to get two new totals

In my SQL course I'm trying to answer a problem in which i'm being asked to find X% of a Total then subtracting that result from the original total to produce another result. Then putting these results (the X% of the total and the total-X% of total) in two new columns.
Example: We need to know how much money we owe Tom and Ted. We have total up sales to $1,000,000.00. We owe Tom 75% of that total. The remainder goes to Ted.
I can't seem to find anything in my readings/videos about this nor a google search that isn't an answers that produces ratios or comparing to other records in the table. Also, not sure about how to get the results into their own columns. Thanks for any advice!
Example of what I got so far:
SELECT SUM(Sale_Amount) From Order_Table
Now I have to find the % of that SUM then subtract it from the SUM and push both results to two new columns, one for the percent of the SUM(Sale_Amount) and one for the remainder.
Given it's an SQL course (and it's not 100% clear what's being asked), I'm not going to give you the total answer, but I'll give you components but you'll need to understand them to put them together.
In SQL, you can
Get totals using SUM and GROUP BY
Do normal maths e.g., SELECT 10000 * 60/100 to get percentages of totals
'Save' results by a) having columns/fields to save them in, and b) UPDATE those fields with relevant data
Note if you're not saving data, and simply reporting them, you can just add those to a SELECT statement e.g., SELECT 100000 AS Total, 100000 * 0.75 AS Toms_Share, 100000 * 0.25 AS Teds_Share.

Average 3 rows that contain mm:hh:ss

In my table I have 3 colmuns
T1 T2 T3
I want to average the time in all 3.
My query in design view is set as follows:
Score: Avg([swim]+[bike]+[run])
In total, I have it done as an expression.
When I run the query an example result shows as: 8.81406810035843E-02
Any ideas how I can get it to show the average time over all 3 correctly?
I have also changed the format to hh:nn:ss - The results looks better but not correct
I hope you are looking for this
Score: Avg(([swim]+[bike]+[run])/3)
Using the Avg() funciton, you are making average of every time in column. Adding them together, the resulting time is too huge to show for access. When you average something in one row, you have to add it up and then divide by number of each member.

SQL: Minimising rows in subqueries/partitioning

So here's an odd thing. I have limited SQL access to a database - the most relevant restriction here being that if I create a query, a maximum of 10,000 rows is returned.
Anyway, I've been trying to have a query return individual case details, but only at busy times - say when 50+ cases are attended to in an hour. So, I inserted the following line:
COUNT(CaseNo) OVER (PARTITION BY DATEADD(hh,
DATEDIFF(hh, 0, StartDate), 0)) AS CasesInHour
... And then used this as a subquery, selecting only those cases where CasesInHour >= 50
However, it turns out that the 10,000 rows limit affects the partitioning - when I tried to run over a longer period nothing came up, as it was counting the cases in any given hour from only a (fairly random) much smaller selection.
Can anyone think of a way to get around this limit? The final total returned will be much lower than 10,000 rows, but it will be looking at far more than 10,000 as a starting point.
If this is really MySQL we're talking about, sql_big_selects and max_join_size affects the number of rows examined, not the number of rows "returned". So, you'll need to reduce the number of rows examined by being more selective and using proper indexes.
For example, the following query may be examining over 10,000 rows:
SELECT * FROM stats
To limit the selectivity, you might want to grab only the rows from the last 30 days:
SELECT * FROM stats
WHERE created > DATESUB(NOW(), INTERVAL 30 DAY)
However, this only reduces the number of rows examined if there is an index on the created column and the cardinality of the index is sufficient to reduce the rows examined.

How to Calculate Time Remaining - Based on estimated time x rows avaiable

For security reasons, we're using a front end application, where we're uploading a txt file, deleting 2500 records from a SQL database.
I'm using SQL Server 2008 Mgt Studio to query the progress of that delete. But this just shows me how many rows I have left, not how much time is left.
How can I add into my query?
A calculation of the estimate 'in minutes', remaining on a record deletes?
While there is no fixed amount of time it takes to complete, its been averaging 1.5min to delete each record.
I figured simple math (2500 x 1.5min = est time remaining), I just don't know how to write into the query as a new column. Here is where my query is at now:
SELECT COUNT (UNITS) AS Orders_Remaining
FROM ORDERS
WHERE UNITS BETWEEN '0001' and '2500'
Do you mean this?
SELECT COUNT (UNITS) AS Orders_Remaining, COUNT(UNITS) * 1.5 AS Minutes_Remaining
FROM ORDERS
WHERE UNITS BETWEEN '0001' and '2500'

In Crystal Report print only first record in group and leave it summable

I have a table that lists every task an operator completed during a day. This is gathered by a Shop Floor Control program. There is also a column that has the total hours worked that day, this field comes from their time punches. The table looks something like this:
Operator 1 Bestupid 0.5 8 5/12/1986
Operator 1 BeProductive 0.1 8 5/12/1986
Operator 1 Bestupidagain 3.2 8 5/12/1986
Operator 1 Belazy 0.7 8 5/13/1986
Operator 2 BetheBest 1.7 9.25 5/12/1986
I am trying to get an efficiency out of this by summing the process hours and comparing it to the hours worked. The problem is that when I do any kind of summary on the hours worked column it sums EVERY DETAIL LINE.
I have tried:
If Previous (groupingfield) = (groupingfield) Then
HoursWorked = 0
Else
HoursWorked = HoursWorked
I have tried a global three formula trick, but neither of the above leave me with a summable field, I get "A summary has been specified on a non-recurring field"
I currently use a global variable, reset in the group header, but not WhilePrintinganything. However it is missing some records and upon occasion I will get two hoursworked > 0 in the same group :(
Any ideas?
I just want to clarify, I have three groups:
Groups: Work Center --> Operator --> Date
I can summarize the process hours across any group and that's fine. However, the hours worked prints on every detail line even though it really should only print once per Date. Therefore when I summarize the Hours Worked for an operator the total is WAY off because it is adding up 8hours for each entry instead of 8 hours for each day.
Try grouping by the operators. Then create a running total for the process hours that sum for each record and reset on change of group. In the group footer you can display the running total and any other stats for that operator you care to.
Try another running total for the daily hours but pick maximum as the type of summary. Since all the records for the day will have the same hours work the maximum will be correct. Reset with the change of the date group and you should be good to go.