| Author |
Topic |
|
anumodhc
Starting Member
13 Posts |
Posted - 2012-02-21 : 23:55:32
|
| Hi All,I have a result set as belowName Date HoursA 1-Feb 10:00A 2-Feb 8:30B 3-Feb 11:00A 4-Feb 10:00A 5-Feb 10:00i want to group the records (to get total Hours) according to name in the order result is populated. I need total hours for First 2 records, then 3 record and for 4th and 5th records.Please helpAnumodH |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2012-02-22 : 00:12:51
|
| so what should be the output?------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/ |
 |
|
|
anumodhc
Starting Member
13 Posts |
Posted - 2012-02-22 : 00:16:51
|
| I need the out put like A 18:30B 11:00A 20:00AnumodH |
 |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2012-02-22 : 00:27:10
|
| [code]SELECT t.Name,CAST(SUM(DATEDIFF(ss,0,CAST(t.Date AS time))) AS time)FROM Table tCROSS APPLY (SELECT TOP 1 Date FROM table WHERE Date < t.Date AND Name <> t.Name ORDER BY Date DESC) t1GROUP BY t.Name,t1.Date[/code]------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/ |
 |
|
|
anumodhc
Starting Member
13 Posts |
Posted - 2012-02-22 : 02:27:35
|
| Hi VisakhThanks for the helpBut it did not helped me to solve this problem.Thanks for the helpAnumodH |
 |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2012-02-22 : 09:43:22
|
quote: Originally posted by anumodhc Hi VisakhThanks for the helpBut it did not helped me to solve this problem.Thanks for the helpAnumodH
why whats the issuecan you show what your expected format is and what you got by my suggestion?------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/ |
 |
|
|
Lamprey
Master Smack Fu Yak Hacker
4614 Posts |
Posted - 2012-02-22 : 13:50:26
|
| [code]DECLARE @Foo TABLE (Name CHAR(1), [Date] VARCHAR(5), [Hours] TIME)INSERT @Foo (Name, [Date], [Hours])VALUES('A', '1-Feb', '10:00'),('A', '2-Feb', '8:30'),('B', '3-Feb', '11:00'),('A', '4-Feb', '10:00'),('A', '5-Feb', '10:00');WITH Cte AS( SELECT *, ROW_NUMBER() OVER (ORDER BY Date, Name) - ROW_NUMBER() OVER (PARTITION BY Name ORDER BY Name) AS GroupNum FROM @Foo AS A)SELECT Name, CAST(DATEADD(MINUTE, SUM(DATEDIFF(MINUTE, '0:00', [Hours])), '0:00') AS TIME)FROM CteGROUP BY GroupNum, Name[/code]If you go over 24 hours, then I'm not if the results are going to be what you expect. |
 |
|
|
anumodhc
Starting Member
13 Posts |
Posted - 2012-02-22 : 23:02:49
|
| Hey thanks alli got the solution. DECLARE @TestTable AS TABLE (Name char(1), [Date] datetime, [Hours] varchar(5))INSERT INTO @TestTable (Name,[Date],[Hours])SELECT 'A' ,'1-Feb-2012', '10:00' UNION ALLSELECT 'A', '2-Feb-2012', '8:30' UNION ALLSELECT 'B', '3-Feb-2012', '11:00' UNION ALLSELECT 'A', '4-Feb-2012', '10:00' UNION ALLSELECT 'A', '5-Feb-2012', '10:00' SELECT Name, SUM(CAST(REPLACE(hours, ':', '.') AS decimal(4, 2)))FROM (SELECT *, ROW_NUMBER() OVER (ORDER BY Name) - ROW_NUMBER() OVER (ORDER BY [date]) grp FROM @TestTable) aGROUP BY Name, Grpthanks a lotAnumodH |
 |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2012-02-23 : 08:58:28
|
quote: Originally posted by anumodhc Hey thanks alli got the solution. DECLARE @TestTable AS TABLE (Name char(1), [Date] datetime, [Hours] varchar(5))INSERT INTO @TestTable (Name,[Date],[Hours])SELECT 'A' ,'1-Feb-2012', '10:00' UNION ALLSELECT 'A', '2-Feb-2012', '8:30' UNION ALLSELECT 'B', '3-Feb-2012', '11:00' UNION ALLSELECT 'A', '4-Feb-2012', '10:00' UNION ALLSELECT 'A', '5-Feb-2012', '10:00' SELECT Name, SUM(CAST(REPLACE(hours, ':', '.') AS decimal(4, 2)))FROM (SELECT *, ROW_NUMBER() OVER (ORDER BY Name) - ROW_NUMBER() OVER (ORDER BY [date]) grp FROM @TestTable) aGROUP BY Name, Grpthanks a lotAnumodH
this will not work correct alwaysAdd this sample data and checkA 4-Feb 5:40A 5-Feb 3:50------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/ |
 |
|
|
SwePeso
Patron Saint of Lost Yaks
30421 Posts |
Posted - 2012-02-23 : 09:36:19
|
[code]Scenario 1 Scenario 2--------------------- ---------------------'A', '1-Feb', '10:00' 'A', '1-Feb', '10:00' 'A', '2-Feb', '8:30' 'B', '2-Feb', '7:00' * These two rows are dated the same day'B', '2-Feb', '7:00' 'A', '2-Feb', '8:30' * Since nothing tells them apart, there is no solution'B', '3-Feb', '11:00' 'B', '3-Feb', '11:00'--------------------- ---------------------'A', '18:30' 'A', '10:00''B', '18:00' 'B', '7:00' 'A', '8:30' 'B', '11:00'[/code] N 56°04'39.26"E 12°55'05.63" |
 |
|
|
|
|
|