December 31, 2013 at 6:22 am
I have a table
id Name starttime value
--- ----- --------- ------
1 A 2013-12-07 46
2 A 2013-12-08 9
3 A 2013-12-14 59
1 B 2013-12-05 10
2 B 2013-12-10 1
3 B 2013-12-11 12
1 C 2013-12-04 16
2 C 2013-12-13 2
3 C 2013-12-15 20
And want to get the Difference between MAx And MIN Values of a Date's Value.
My Result must be shown below:
Name MAX(starttime) MIN(STARTTIME) DIFFERENCE
----------------------------------------------------------
A 2013-12-14 2013-12-07 13
B 2013-12-11 2013-12-05 2
C 2013-12-15 2013-12-04 4
Could you please send me the SELECT Statement ?
December 31, 2013 at 7:23 am
I didn't check it (next time pleas write a script that creates the table and inserts the data), but I think that the query bellow will do it for you:
select Name, min(starttime) as Min_stattime, max(statrttime) as Max_stattime, max(value)-min(value) as Differnce
from MyTable
group by Name
Adi
--------------------------------------------------------------
To know how to ask questions and increase the chances of getting asnwers:
http://www.sqlservercentral.com/articles/Best+Practices/61537/
For better answers on performance questions, click on the following...
http://www.sqlservercentral.com/articles/SQLServerCentral/66909/
December 31, 2013 at 7:55 am
This is not working, The Result is not i appreciated.
I want to get
max(starttime).value -min(starttime).value as Differnce
Here is the Runtime DDL
declare @MyTable table (id int,Name char(1),starttime Datetime, value int);
insert into @MyTable values (1,'A','2013-12-07',46);
insert into @MyTable values (2,'A','2013-12-08', 9);
insert into @MyTable values (3,'A','2013-12-14',59);
insert into @MyTable values (1,'B','2013-12-05',10);
insert into @MyTable values (2,'B','2013-12-10',1);
insert into @MyTable values (3,'C','2013-12-11',12);
insert into @MyTable values (1,'C','2013-12-04',16);
insert into @MyTable values (2,'C','2013-12-13',2);
insert into @MyTable values (3,'C','2013-12-15',20);
select * from @MyTable
December 31, 2013 at 8:20 am
Hi,
To get better and faster answers, you should post DDL and sample data in a consumable format as I did this time for you. (It seems that you edited your post while I was writing mine :-D)
Here's an option, but there might be a better one.
CREATE TABLE #Sample(
idint,
Name char(1),
starttime date,
value int)
INSERT INTO #Sample VALUES(
1, 'A', '2013-12-07', 46),(
2, 'A', '2013-12-08', 9),(
3, 'A', '2013-12-14', 59),(
1, 'B', '2013-12-05', 10),(
2, 'B', '2013-12-10', 1),(
3, 'B', '2013-12-11', 12),(
1, 'C', '2013-12-04', 16),(
2, 'C', '2013-12-13', 2),(
3, 'C', '2013-12-15', 20);
WITH Dates AS(
SELECT Name,
MAX( starttime) Max_Time,
MIN( starttime) Min_Time
FROM #Sample
GROUP BY Name
)
SELECT s.Name,
d.Max_Time,
d.Min_Time,
MAX(CASE WHEN s.starttime = d.Max_Time THEN s.value END)
- MAX(CASE WHEN s.starttime = d.Min_Time THEN s.value END)
FROM #Sample s
JOIN Dates d ON s.Name = d.Name
GROUP BY s.Name,
d.Max_Time,
d.Min_Time
December 31, 2013 at 9:13 am
Thanks a lot. Happy New Year.
December 31, 2013 at 9:28 am
Thinky Night (12/31/2013)
Thanks a lot. Happy New Year.
Let's start the new year off right. Do you understand how and why the solution works?
--Jeff Moden
Change is inevitable... Change for the better is not.
January 1, 2014 at 11:11 pm
I believe that this may be an alternate solution.
SELECT Name
,[MAX(starttime)]=MAX(CASE WHEN rn = c THEN starttime END)
,[MIN(STARTTIME)]=MIN(CASE WHEN rn = 1 THEN starttime END)
,[DIFFERENCE]=MAX(value)-MIN(value)
FROM
(
SELECT id, Name, starttime, value
,rn=ROW_NUMBER() OVER (PARTITION BY Name ORDER BY starttime)
,c=COUNT(*) OVER (PARTITION BY Name)
FROM @MyTable
) a
WHERE rn IN (1,c)
GROUP BY Name;
This solution seems to do a single table scan, but will have some overhead due to the COUNT(*) using the window aggregate.
My thought question: Have you ever been told that your query runs too fast?
My advice:
INDEXing a poor-performing query is like putting sugar on cat food. Yeah, it probably tastes better but are you sure you want to eat it?
The path of least resistance can be a slippery slope. Take care that fixing your fixes of fixes doesn't snowball and end up costing you more than fixing the root cause would have in the first place.
Need to UNPIVOT? Why not CROSS APPLY VALUES instead?[/url]
Since random numbers are too important to be left to chance, let's generate some![/url]
Learn to understand recursive CTEs by example.[/url]
[url url=http://www.sqlservercentral.com/articles/St
Viewing 7 posts - 1 through 6 (of 6 total)
You must be logged in to reply to this topic. Login to reply