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

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'

Related

Time gap calculation in MS Access

I have a table (Access 2016) tbl_b with date/time registrations
b_customer (num)
b_date (date)
b_start (date/time)
b_end (date/time)
I want to make a chart of all time registrations per day in a selected month and the gaps between those times. For this I need a query or table showing all times as source for the chart. I’m a bit lost how to approach this.
I assume the chart source needs consecutive records with all date and time registrations to do this. My approach would be create a temporary table (tmp) calculating all time periods where the customer is null. The next step would be a union query to combine the tbl_b and tmp table.
The tbl_b does not have records for every day, so I use a query generating all days in the selected month which shall be used in the chart (found this solution here: [Create a List of Dates in Access Query)
The disadvantage of using a tmp table for the “time gaps” is that it is not updating real time, where a query would provide this opportunity. I have about 20 queries to perform the end result, but MS Access keeps giving (expected) errors that the queries are too difficult.
Every query looks for difference between the in the previous query found end time and the next start time. On the other hand this approach has a weaknes as well, I thought 15 steps would be enough (no more than 15 gaps expected), but this is not sure.
Can anyone give me a head start how this can be accomplished by an easier (and actual working) method? Maybe VBA?
Thx!
Art

How to find where a total condition exist

I am trying to create a report that will show how long an automated sprinkler system has run for. The system is comprised of several sprinklers, with each one keeping track of only itself, and then sends that information to a database. My problem is that each sprinkler has its own run time (I.E. if 5 sprinklers all ran at the same time for 10 minutes, it would report back a total run time of 50 minutes), and I want to know only the net amount of run time - in this example, it would be 10 minutes.
The database is comprised of a time stamp and a boolean, where it records the time stamp every time a sprinkler is shut on or off (its on/off state is indicated by the 1/0 of the boolean).
So, to figure out the total net time the system was on each day - whether it was 1 sprinkler running or all of them - I need to check the database for time frames where no sprinklers were turned at all (or where ANY sprinkler at all was turned on). I would think the beginning of the query would look something like
SELECT * FROM MyTable
WHERE MyBoolean = 0
AND [ ... ]
But I'm not sure what the conditional statements that would follow the AND would be like to check the time stamps.
Is there a query I can send to the database that will report back this format of information?
EDIT:
Here's the table the data is recorded to - it's literally just a name, a boolean, and a datetime of when the boolean was changed, and that's the entire database
Every time a sprinkler turns on the number of running sprinklers increments by 1, and every time one turns off the number decrements by 1. If you transform the data so you get this:
timestamp on/off
07:00:05 1
07:03:10 1
07:05:45 -1
then you have a sequence of events in order; which sprinklers they refer to is irrelevant. (I've changed the zeros to -1 for reasons that will become evident in a moment. You can do this with "(2 * value) - 1")
Now put a running total together:
select a.timestamp, (SELECT SUM(a.on_off)
FROM sprinkler_events b
WHERE b.timestamp <= a.timestamp) as run_total
from sprinkler_events a
order by a.timestamp;
where sprinkler_events is the transformed data I listed above. This will give you:
timestamp run_total
07:00:05 1
07:03:10 2
07:05:45 1
and so on. Every row in this which has a run total of zeros is a time at which all sprinklers were turned off, which I think is what you're looking for. If you need to sum the time they were on or off, you'll need to do additional processing: search for "date difference between consecutive rows" and you'll see solutions for that.
You might consider looking for whether all the sprinklers are currently off. For example:
SELECT COUNT (DISTINCT s._NAME) AS sprinkers_currently_off
FROM (
SELECT
_NAME,
_VALUE,
_TIMESTAMP,
ROW_NUMBER() OVER (PARTITION BY _NAME ORDER BY _TIMESTAMP DESC, _VALUE) AS latest_rec
FROM sprinklers
) s
WHERE
_VALUE = 0
AND latest_rec = 1
The inner query orders the records so that you can get the latest status of all the sprinklers, and the outer query counts how many are currently off. If you have 10 sprinklers you would report them all off when this query returns 10.
You could modify this by applying a date range to the inner query if you wanted to look into the past, but this should get you on the right track.

SQL Limit Average

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;

Tips on improving calculation speed in Access

I'm using MS Access 2013 (without any access to other SQL programs).
I have to filter data, based on daily breakdowns across 30 years (10800 rows to start with) for more than 50 customers (540.000 rows in total). I have an issue with the speed of my queries. After a few joins and filters the whole thing takes seconds to run. To speed up the calculation I tried:
breaking queries down (using as few joins as possible)
filtering for 'active data' only;
aggregating iteration queries;
indexed the dates (as Integers).
In need to add, for each calendar date, the summed DealValue, aggregated by Customer, for it's 'active period' (each day between the StartDate and EndDate of the given Deal). The deals may spread over 30 years. Looks like that LEFT JOIN is causing all the slow-down.
My final query looks like:
SELECT QMaster.CalDate,
QDeals.CustomerID,
Sum(IIf([QDeals]![Limit]="S",[QDeals.DealValue],0)) AS S,
Sum(IIf([QDeals]![Limit]="L",[QDeals.DealValue],0)) AS L
FROM QMaster
LEFT JOIN TDeals
ON (QMaster.CalDate> QDeals.DealStartDate)
AND (QMaster.CalDate< QDeals.DealEndDate)
GROUP BY QMaster.CalDate, QDeals.CustomerID;
-- where QMaster (540.000 rows) / QDeals (> 10.000 rows) / calculation time 5<>7s
-- the query may contain typos as I had to change the name description for disclosure.
Any tips on how to replace that LEFT JOIN?
Or any other speed improvements prior to this query?
I want to add an 'interactive' Access form over my 'master' query and that's turning to be very slow (not user friendly).

Calculating a percentage of two "Counts" in SQL in Microsoft Access 2010

I have a Microsoft Access 2010 database of thyroid surgeries. I have a query that counts the number of surgeries in the entire database. I have a second query that counts the number of surgeries performed between certain dates. I created a new query using SQL to calculate the percentage of surgeries in that date range (a percentage of the total surgery number). Here is the code:
SELECT
((select count(ThyroidSurgeryType)
from [Thyroid Surgeries]
HAVING ([Thyroid Surgeries].SurgeryDate) Between #1/1/2011# And #12/31/2012#)/(select count(ThyroidSurgeryType)
from [Thyroid Surgeries])) AS Percentage
FROM [Thyroid Surgeries];
I get .33 (I then set the row format to percentage to get 33%), but I get 6 rows of 33% as opposed to just one. Why is it displaying this way? Thanks
The way you're using inline queries, you're executing the same query per row of the outer query. So if your table has six rows, you'd be executing it six time (and getting the same result for each query, naturally). You can simplify things by removing the outer query and using an iif expression:
SELECT SUM (IIF (SurgeryDate Between #1/1/2011# And #12/31/2012#, 1, 0)) /
COUNT(ThyroidSurgeryType) AS Percentage
FROM [Thyroid Surgeries];