|
Hi,
i have an sql table which stores times of a machine's 'work start' and 'idle start' times, which is like ;
2014-09-03 12:50:15 start
2014-09-03 13:45:25 idle
2014-09-03 13:56:06 Start
2014-09-03 16:30:35 idle
.
.
and goes like that.
i need to calculate the time between first start and first idle time after the first start, then, time between second start and first idle after the second start, and it goes like that and there are too many lines. at the and i need total work time and total idle time.
Can anyone help me with this?
Thank you.
|
|
|
|
|
The easiest way would be to read all the times into a dataset and then go through them calculating the individual differences. See http://msdn.microsoft.com/en-us/library/1905yhe2(v=vs.110).aspx[^] for how to get the TimeSpan value between two DateTime objects. I assume that you are storing these values as DateTime types.
|
|
|
|
|
thank you for your response, yes i store these values as DateTime types, and also i am aware of TimeSpan. but the fact is, it is easy for just two values, but i have too many values and my real struggle is this, i couldn't create a logic to calculate for all that values.
|
|
|
|
|
It is just a matter of reading all the values one by one.
- Read the first time value, that is the starting time of work.
- Read the next time, that is the ending time of work.
- The difference between those two is added to the total work time.
- Set the end time as the new start time, that is the starting time of idle.
- Read the next time, that is the ending time of idle (and the next starting time of work).
- The difference between start and end should be added to idle time.
- Set the end time as the new start time (of work).
- Repeat the process until all times have been dealt with.
In cases such as this you should forget about programming and coding issues, and think about the logical steps needed to get your answer. Write down those steps and go through them a few times to ensure they make sense. Then when you are happy that your algorithm is correct you can write and test the actual code needed for your application.
|
|
|
|
|
thank you for your advice.
|
|
|
|
|
Are you trying to do this in C# or SQL?
|
|
|
|
|
C# but if you can offer a solution for SQL it is OK too
|
|
|
|
|
To do this in SQL, CROSS APPLY might help:
DECLARE @Data TABLE
(
TimeLogged datetime2(0) NOT NULL,
Action varchar(5) NOT NULL
);
INSERT INTO @Data
VALUES
('20140903 12:50:15', 'start'),
('20140903 13:45:25', 'idle'),
('20140903 13:56:06', 'Start'),
('20140903 16:30:35', 'idle')
;
SELECT
D1.Action,
Sum(DateDiff(second, D1.TimeLogged, D2.TimeLogged)) As DurationInSeconds
FROM
@Data As D1
CROSS APPLY
(
SELECT TOP 1
TimeLogged
FROM
@Data As D2
WHERE
D2.Action != D1.Action
And
D2.TimeLogged >= D1.TimeLogged
ORDER BY
TimeLogged
) As D2
GROUP BY
D1.Action
;
"These people looked deep within my soul and assigned me a number based on the order in which I joined."
- Homer
|
|
|
|
|
this seems very useful but i am not very good at sql. how can i use my table in this query?
|
|
|
|
|
Since you didn't post any details about the structure of your table, I had to make up an example. You'll want just the SELECT block; replace @Data with the name of your table, and replace TimeLogged and Action with your column names.
SELECT
D1.NameOfYourActionColumn,
Sum(DateDiff(second, D1.NameOfYourTimeLoggedColumn, D2.NameOfYourTimeLoggedColumn)) As DurationInSeconds
FROM
NameOfYourTable As D1
CROSS APPLY
(
SELECT TOP 1
NameOfYourTimeLoggedColumn
FROM
NameOfYourTable As D2
WHERE
D2.NameOfYourActionColumn != D1.NameOfYourActionColumn
And
D2.NameOfYourTimeLoggedColumn>= D1.NameOfYourTimeLoggedColumn
ORDER BY
NameOfYourTimeLoggedColumn
) As D2
GROUP BY
D1.NameOfYourActionColumn
;
"These people looked deep within my soul and assigned me a number based on the order in which I joined."
- Homer
|
|
|
|
|
it seems to work,
thank you very much
|
|
|
|
|
|
Who's zip file is it?
Give up, you don't stand a chance.
Regards,
Rob Philpott.
|
|
|
|
|
|
Jennifer Lawrence's
=========================================================
I'm an optoholic - my glass is always half full of vodka.
=========================================================
|
|
|
|
|
There is a tool I've heard about, but have not yet been able to verify if it really works.
It's called Ultimate CrIP Zacker, or something.
Cheers!
"I had the right to remain silent, but I didn't have the ability!"
Ron White, Comedian
|
|
|
|
|
Sorry but you're NEVER going to get help with this in any legitimate forum such as CP.
We have no idea who this ZIP file belongs to and we're not going to post any information that can be used to crack it as we're not going to be party to stealing someones information, even if it isn't you that's doing it. It could be some other schmuck who's Googling around trying to find information on how to do it themselves.
|
|
|
|
|
With that argumentation, we should REALLY stop explaining how sql-injection works.
Ban the bobby tables comic!
Bastard Programmer from Hell
If you can't read my code, try converting it here[^]
|
|
|
|
|
There's a significant difference between giving a detailed explanation of how to pick a lock, and explaining to someone that if they leave their door open, someone will come in and steal their stuff.
"These people looked deep within my soul and assigned me a number based on the order in which I joined."
- Homer
|
|
|
|
|
Glad you agree the comic should be banned.
Bastard Programmer from Hell
If you can't read my code, try converting it here[^]
|
|
|
|
|
But I don't.
In this analogy, "detailed instructions for picking a lock" is equivalent to "here's how to brute-force a password".
"Unlocked door => stolen stuff" is equivalent to "you've got a well-known and easily exploitable security vulnerability in your code, here's how it works, and here's how you fix it".
It's hard enough to get some people to take SQLi seriously. If you're not allowed to explain how it works, and you just have to rely on saying "bad stuff will happen", nobody will pay any attention until it's too late.
"These people looked deep within my soul and assigned me a number based on the order in which I joined."
- Homer
|
|
|
|
|
Richard Deeming wrote: But I don't. I know
A password on a zip-file is not a lock. It's not a door in a house - it'd be more someone elses locked box, inside your house.
Bastard Programmer from Hell
If you can't read my code, try converting it here[^]
|
|
|
|
|
I don't see it that way.
A SQL Injection question is more about telling the programmer (and I use that term lightly!) how to defend against the attack and produce higher quality code.
This is just a brute force, "crack a password so I can break in and get the data" question with no benefits to the quality or strength of his non-existent code.
|
|
|
|
|
I disagree.
I'm pretty sure that the first thing you should be told when you start any computing course is exactly how to perform an SQL injection attack, with examples, and encouraged to try it out as often as possible. Free T shirts with the instructions on would be good.
That way, the problem would die. Because no systems would dare be left open to it, and no tutor could possibly ignore it or not know about it enough to teach students the Right Way to do things. Because there is the problem: tutors who don't know enough, and don't teach about security in any meaningful way.
Forget seatbelts in cars to reduce injuries in an accident, reduce the number of damn accidents by making drivers aware of how dangerous they are!
You looking for sympathy?
You'll find it in the dictionary, between sympathomimetic and sympatric
(Page 1788, if it helps)
|
|
|
|
|
It's explained on the Wikipedia, and there are links to software that implements such.
I would link to the wikipedia, but I'm afraid that some members will ask for a book-burning.
Bastard Programmer from Hell
If you can't read my code, try converting it here[^]
|
|
|
|