Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 SQL Server 2005 Forums
 Transact-SQL (2005)
 Sql Query - Subtract row from previous row ...

Author  Topic 

yasinirshad
Starting Member

18 Posts

Posted - 2010-06-27 : 07:13:26
Hello,
From this select query,
SELECT '26/06/2010' as Date, max(hr)+1 hr
FROM StackedChart where day(date_g)='26' and month(date_g)='06'
group by status
order by hr
My output is :
Date hr
26/06/2010 3
26/06/2010 7
26/06/2010 21
26/06/2010 24

How to get result as below :
Date hr Difference
26/06/2010 3 3
26/06/2010 7 4
26/06/2010 21 14
26/06/2010 24 3

What i need is a new column 'Differece', which comes by subtracting hr from previous hr i.e., 24-21 = 3, 21-7 = 14, 7-3=4, 3-0 = 3 ...assuming first row value is subtracted by '0' ...

Can anyone pls get me sql query for this ...

Thanks...

Sachin.Nand

2937 Posts

Posted - 2010-06-27 : 07:41:13
[code]
set dateformat dmy
declare @tbl as table(id int identity,dt datetime ,hr int)
insert into @tbl
select '26/06/2010', 3 union all
select '26/06/2010', 7 union all
select '26/06/2010', 21 union all
select '26/06/2010', 24

select
t1.dt,
t1.hr,
coalesce(t1.hr-T.hr,t1.hr) as difference
from @tbl t1
outer apply(select * from @tbl t2 where t1.id=t2.id+1)T
[/code]


Limitations live only in our minds. But if we use our imaginations, our possibilities become limitless.

PBUH
Go to Top of Page

yasinirshad
Starting Member

18 Posts

Posted - 2010-06-28 : 06:31:53
Hi,
Thanks for the reply... But i am not getting it ... Well i have a table which has data like this :
ORIGINAL TABLE
SNo Date hr Status
1 26/06/2010 3 4
2 26/06/2010 3 4
3 26/06/2010 3 4
4 26/06/2010 7 5
5 26/06/2010 7 5
.. ......... .. .. (And so on) ...

Ok now, in above table i have Sno,date,hr and status ... It means that Hr 3 has status 4 (3 times), hour 7 will have status 5 (7 times) and so on ....

Now from this below select query,
SELECT '26/06/2010' as Date, max(hr)+1 hr
FROM StackedChart where day(date_g)='26' and month(date_g)='06'
group by status
order by hr
I get below o/p ...
My output is :
Date hr
26/06/2010 3
26/06/2010 7
26/06/2010 21
26/06/2010 24

How to get result as below : (FROM MY ORIGINAL TABLE LISTED ABOVE)
Date hr Difference
26/06/2010 3 3
26/06/2010 7 4
26/06/2010 21 14
26/06/2010 24 3

I TRIED IDERA'S QUERY, BUT not getting desired result ... below is query,

select '26/06/2010' as Date,t1.hr,coalesce(t1.hr-T.hr,t1.hr) as difference
from StackedChart t1
outer apply(select * from StackedChart t2 where t1.id=t2.id+1)T
where day(t1.date_g)='26' and month(t1.date_g)='06'
group by t1.status,t1.hr,T.hr
order by t1.hr

And output is :
Date Hr Difference
26/06/2010 0 0
26/06/2010 0 0
26/06/2010 1 1
26/06/2010 1 0
26/06/2010 2 1
26/06/2010 2 0
26/06/2010 3 1
26/06/2010 3 0
26/06/2010 4 1
26/06/2010 4 0
26/06/2010 5 1
26/06/2010 5 0
26/06/2010 6 1
26/06/2010 6 0
26/06/2010 7 1
26/06/2010 7 0
26/06/2010 8 1
26/06/2010 8 0
26/06/2010 9 1
26/06/2010 9 0
26/06/2010 10 1
26/06/2010 10 0
26/06/2010 11 1
26/06/2010 11 0
26/06/2010 12 1
26/06/2010 12 0
26/06/2010 13 1
26/06/2010 13 0
26/06/2010 14 1
26/06/2010 14 0
26/06/2010 15 1
26/06/2010 15 0
26/06/2010 16 1
26/06/2010 16 0
26/06/2010 17 1
26/06/2010 17 0
26/06/2010 18 1
26/06/2010 18 0
26/06/2010 19 1
26/06/2010 19 0
26/06/2010 20 1
26/06/2010 20 0
26/06/2010 21 1
26/06/2010 21 0
26/06/2010 22 1
26/06/2010 22 0
26/06/2010 23 1
26/06/2010 23 0

As u see above, each record is repeating twice ... Kindly help.
Go to Top of Page

Sachin.Nand

2937 Posts

Posted - 2010-06-28 : 06:58:56
What is the o/p of this query?



select * from StackedChart




Limitations live only in our minds. But if we use our imaginations, our possibilities become limitless.

PBUH
Go to Top of Page

yasinirshad
Starting Member

18 Posts

Posted - 2010-06-28 : 07:06:11
O/P of select * from StackedChart is :

SNO Index Date Hr Status
1 2 6/26/10 0:00 0 4
2 4 6/26/10 0:00 0 4
3 6 6/26/10 0:00 0 4
4 8 6/26/10 0:00 0 4
5 120 6/26/10 0:00 0 4
6 3 6/26/10 0:00 0 4
7 5 6/26/10 0:00 0 4
8 7 6/26/10 0:00 0 4
9 9 6/26/10 0:00 0 4
10 7 6/26/10 0:00 1 4
11 9 6/26/10 0:00 1 4
12 3 6/26/10 0:00 1 4
13 5 6/26/10 0:00 1 4
14 6 6/26/10 0:00 1 4
15 8 6/26/10 0:00 1 4
16 2 6/26/10 0:00 1 4
17 4 6/26/10 0:00 1 4
18 120 6/26/10 0:00 1 4
19 6 6/26/10 0:00 2 4
20 8 6/26/10 0:00 2 4
21 2 6/26/10 0:00 2 4
22 4 6/26/10 0:00 2 4
23 120 6/26/10 0:00 2 4
24 7 6/26/10 0:00 2 4
25 9 6/26/10 0:00 2 4
26 3 6/26/10 0:00 2 4
27 5 6/26/10 0:00 2 4
28 6 6/26/10 0:00 3 3
29 7 6/26/10 0:00 3 3
30 8 6/26/10 0:00 3 3
31 9 6/26/10 0:00 3 3
32 2 6/26/10 0:00 3 3
33 3 6/26/10 0:00 3 3
34 4 6/26/10 0:00 3 3
35 5 6/26/10 0:00 3 3
36 120 6/26/10 0:00 3 3
37 6 6/26/10 0:00 4 3
38 8 6/26/10 0:00 4 3
39 2 6/26/10 0:00 4 3
40 4 6/26/10 0:00 4 3
41 120 6/26/10 0:00 4 3
42 7 6/26/10 0:00 4 3
43 9 6/26/10 0:00 4 3
44 3 6/26/10 0:00 4 3
45 5 6/26/10 0:00 4 3
46 7 6/26/10 0:00 5 3
47 9 6/26/10 0:00 5 3
48 3 6/26/10 0:00 5 3
49 5 6/26/10 0:00 5 3
50 6 6/26/10 0:00 5 3
51 8 6/26/10 0:00 5 3
52 2 6/26/10 0:00 5 3
53 4 6/26/10 0:00 5 3
54 120 6/26/10 0:00 5 3
55 7 6/26/10 0:00 6 3
56 9 6/26/10 0:00 6 3
57 3 6/26/10 0:00 6 3
58 5 6/26/10 0:00 6 3
59 6 6/26/10 0:00 6 3
60 8 6/26/10 0:00 6 3
61 2 6/26/10 0:00 6 3
62 4 6/26/10 0:00 6 3
63 120 6/26/10 0:00 6 3
64 6 6/26/10 0:00 7 6
65 9 6/26/10 0:00 7 6
66 3 6/26/10 0:00 7 6
67 5 6/26/10 0:00 7 6
68 7 6/26/10 0:00 7 6
69 8 6/26/10 0:00 7 6
70 2 6/26/10 0:00 7 6
71 4 6/26/10 0:00 7 6
72 120 6/26/10 0:00 7 6
73 7 6/26/10 0:00 8 6
74 9 6/26/10 0:00 8 6
75 3 6/26/10 0:00 8 6
76 5 6/26/10 0:00 8 6
77 6 6/26/10 0:00 8 6
78 8 6/26/10 0:00 8 6
79 2 6/26/10 0:00 8 6
80 4 6/26/10 0:00 8 6
81 120 6/26/10 0:00 8 6
82 7 6/26/10 0:00 9 6
83 9 6/26/10 0:00 9 6
84 3 6/26/10 0:00 9 6
85 5 6/26/10 0:00 9 6
86 6 6/26/10 0:00 9 6
87 8 6/26/10 0:00 9 6
88 2 6/26/10 0:00 9 6
89 4 6/26/10 0:00 9 6
90 120 6/26/10 0:00 9 6
91 7 6/26/10 0:00 10 6
92 9 6/26/10 0:00 10 6
93 3 6/26/10 0:00 10 6
94 5 6/26/10 0:00 10 6
95 6 6/26/10 0:00 10 6
96 8 6/26/10 0:00 10 6
97 2 6/26/10 0:00 10 6
98 4 6/26/10 0:00 10 6
99 120 6/26/10 0:00 10 6
100 6 6/26/10 0:00 11 6
101 8 6/26/10 0:00 11 6
102 2 6/26/10 0:00 11 6
103 4 6/26/10 0:00 11 6
104 120 6/26/10 0:00 11 6
105 7 6/26/10 0:00 11 6
106 9 6/26/10 0:00 11 6
107 3 6/26/10 0:00 11 6
108 5 6/26/10 0:00 11 6
109 6 6/26/10 0:00 12 6
110 8 6/26/10 0:00 12 6
111 2 6/26/10 0:00 12 6
112 4 6/26/10 0:00 12 6
113 120 6/26/10 0:00 12 6
114 7 6/26/10 0:00 12 6
115 9 6/26/10 0:00 12 6
116 3 6/26/10 0:00 12 6
117 5 6/26/10 0:00 12 6
118 7 6/26/10 0:00 13 6
119 9 6/26/10 0:00 13 6
120 3 6/26/10 0:00 13 6
121 5 6/26/10 0:00 13 6
122 6 6/26/10 0:00 13 6
123 8 6/26/10 0:00 13 6
124 2 6/26/10 0:00 13 6
125 4 6/26/10 0:00 13 6
126 120 6/26/10 0:00 13 6
127 7 6/26/10 0:00 14 6
128 9 6/26/10 0:00 14 6
129 3 6/26/10 0:00 14 6
130 5 6/26/10 0:00 14 6
131 6 6/26/10 0:00 14 6
132 8 6/26/10 0:00 14 6
133 2 6/26/10 0:00 14 6
134 4 6/26/10 0:00 14 6
135 120 6/26/10 0:00 14 6
136 6 6/26/10 0:00 15 6
137 8 6/26/10 0:00 15 6
138 2 6/26/10 0:00 15 6
139 4 6/26/10 0:00 15 6
140 9 6/26/10 0:00 15 6
141 7 6/26/10 0:00 15 6
142 120 6/26/10 0:00 15 6
143 3 6/26/10 0:00 15 6
144 5 6/26/10 0:00 15 6
145 6 6/26/10 0:00 16 6
146 8 6/26/10 0:00 16 6
147 2 6/26/10 0:00 16 6
148 4 6/26/10 0:00 16 6
149 9 6/26/10 0:00 16 6
150 7 6/26/10 0:00 16 6
151 120 6/26/10 0:00 16 6
152 3 6/26/10 0:00 16 6
153 5 6/26/10 0:00 16 6
154 6 6/26/10 0:00 17 6
155 8 6/26/10 0:00 17 6
156 2 6/26/10 0:00 17 6
157 4 6/26/10 0:00 17 6
158 9 6/26/10 0:00 17 6
159 7 6/26/10 0:00 17 6
160 120 6/26/10 0:00 17 6
161 3 6/26/10 0:00 17 6
162 5 6/26/10 0:00 17 6
163 6 6/26/10 0:00 18 6
164 7 6/26/10 0:00 18 6
165 8 6/26/10 0:00 18 6
166 120 6/26/10 0:00 18 6
167 2 6/26/10 0:00 18 6
168 3 6/26/10 0:00 18 6
169 9 6/26/10 0:00 18 6
170 4 6/26/10 0:00 18 6
171 5 6/26/10 0:00 18 6
172 6 6/26/10 0:00 19 6
173 8 6/26/10 0:00 19 6
174 2 6/26/10 0:00 19 6
175 9 6/26/10 0:00 19 6
176 5 6/26/10 0:00 19 6
177 7 6/26/10 0:00 19 6
178 120 6/26/10 0:00 19 6
179 3 6/26/10 0:00 19 6
180 4 6/26/10 0:00 19 6
181 7 6/26/10 0:00 20 6
182 120 6/26/10 0:00 20 6
183 3 6/26/10 0:00 20 6
184 4 6/26/10 0:00 20 6
185 6 6/26/10 0:00 20 6
186 8 6/26/10 0:00 20 6
187 2 6/26/10 0:00 20 6
188 9 6/26/10 0:00 20 6
189 5 6/26/10 0:00 20 6
190 6 6/26/10 0:00 21 5
191 8 6/26/10 0:00 21 5
192 2 6/26/10 0:00 21 5
193 9 6/26/10 0:00 21 5
194 5 6/26/10 0:00 21 5
195 7 6/26/10 0:00 21 5
196 120 6/26/10 0:00 21 5
197 3 6/26/10 0:00 21 5
198 4 6/26/10 0:00 21 5
199 6 6/26/10 0:00 22 5
200 8 6/26/10 0:00 22 5
201 2 6/26/10 0:00 22 5
202 9 6/26/10 0:00 22 5
203 5 6/26/10 0:00 22 5
204 7 6/26/10 0:00 22 5
205 120 6/26/10 0:00 22 5
206 3 6/26/10 0:00 22 5
207 4 6/26/10 0:00 22 5
208 6 6/26/10 0:00 23 5
209 8 6/26/10 0:00 23 5
210 2 6/26/10 0:00 23 5
211 9 6/26/10 0:00 23 5
212 5 6/26/10 0:00 23 5
213 7 6/26/10 0:00 23 5
214 120 6/26/10 0:00 23 5
215 3 6/26/10 0:00 23 5
216 4 6/26/10 0:00 23 5
217 5 6/27/10 0:00 0 4
218 4 6/27/10 0:00 1 5
219 6 6/27/10 0:00 1 5
220 5 6/27/10 0:00 1 5

This is just a partial list for 26 June 2010 and some of 27th June data ... Table will have data starting from March 01 till date ...
Go to Top of Page

Sachin.Nand

2937 Posts

Posted - 2010-06-28 : 07:16:30
I ran the query for the first 10 records of the data you posted.

set dateformat mdy
declare @tbl as table(id int ,hr int,Indexdt datetime,Status int)
insert into @tbl
select 1 ,2, '6/26/10 00:00:00' ,4 union all
select 2 ,4, '6/26/10 00:00:00' ,4 union all
select 3 ,6, '6/26/10 00:00:00' ,4 union all
select 4 ,8, '6/26/10 00:00:00' ,4 union all
select 5 ,120, '6/26/10 00:00:00',4 union all
select 6 ,3, '6/26/10 00:00:00' ,4 union all
select 7 ,5, '6/26/10 00:00:00' ,4 union all
select 8 ,7, '6/26/10 00:00:00' ,4 union all
select 9 ,9, '6/26/10 00:00:00' ,4 union all
select 10 ,7, '6/26/10 00:00:00' ,4

select * from @tbl



select
t1.Indexdt,
t1.hr,
coalesce(t1.hr-T.hr,t1.hr) as difference
from @tbl t1
outer apply(select * from @tbl t2 where t1.id=t2.id+1)T



OUTPUT



Indexdt hr difference
2010-06-26 00:00:00.000 2 2
2010-06-26 00:00:00.000 4 2
2010-06-26 00:00:00.000 6 2
2010-06-26 00:00:00.000 8 2
2010-06-26 00:00:00.000 120 112
2010-06-26 00:00:00.000 3 -117
2010-06-26 00:00:00.000 5 2
2010-06-26 00:00:00.000 7 2
2010-06-26 00:00:00.000 9 2
2010-06-26 00:00:00.000 7 -2




Now tell me what is wrong with the above o/p?


Limitations live only in our minds. But if we use our imaginations, our possibilities become limitless.

PBUH
Go to Top of Page

yasinirshad
Starting Member

18 Posts

Posted - 2010-06-29 : 01:12:04
Hello Idera,
Thanks ... But my problem is this ...
I tried to run with below query which is my original scenario ...

set dateformat mdy
declare @tbl as table(id int ,hr int,Indexdt datetime,Status int)
insert into @tbl
select 1 ,12, '6/26/10 00:00:00' ,4 union all
select 2 ,14, '6/26/10 00:00:00' ,4 union all
select 3 ,14, '6/26/10 00:00:00' ,4 union all
select 4 ,24, '6/26/10 00:00:00' ,3 union all
select 5 ,6, '6/27/10 00:00:00',3 union all
select 6 ,6, '6/27/10 00:00:00' ,3 union all
select 7 ,6, '6/27/10 00:00:00' ,3 union all
select 8 ,24, '6/27/10 00:00:00' ,3 union all
select 9 ,19, '6/28/10 00:00:00' ,3 union all
select 10 ,24, '6/28/10 00:00:00' ,3

Please Note above i have HRS repeating for everyday ... i.e., for 6/26/10 i have 12,14,14,24 and for 6/27/10 i got 6,6,6,24 and so on ... The data in my original table exists in this way ...

select * from @tbl

select t1.id,
t1.Indexdt,
t1.hr,t1.Status,
coalesce(t1.hr-T.hr,t1.hr) as difference
from @tbl t1
outer apply(select * from @tbl t2 where t1.id=t2.id+1)T

And O/P is :
1 2010-06-26 00:00:00.000 12 4 12
2 2010-06-26 00:00:00.000 14 4 2
3 2010-06-26 00:00:00.000 14 4 0
4 2010-06-26 00:00:00.000 24 3 10
5 2010-06-27 00:00:00.000 6 3 -18
6 2010-06-27 00:00:00.000 6 3 0
7 2010-06-27 00:00:00.000 6 3 0
8 2010-06-27 00:00:00.000 24 3 18
9 2010-06-28 00:00:00.000 19 3 -5
10 2010-06-28 00:00:00.000 24 3 5

Ok now i have 2 issues with above o/p ...
1.) If Hrs are repeating for same status then i dont want them to be selected (ex: ID3, ID 6 and ID 7 in above o/p) i.e., how to select distinct values (group by Status) ...
2.) When there is a change in date (from 2010-06-26 to 2010-06-27), i see difference is negative .. For ex: in ID 5 difference is -18 (6-24=-18) ... but i want ID 5 Difference to be 6 as its start of new day ... same way difference for ID 9 should be 19 ...

I need this to plot bar chart ...

Thanks to help !!!
Go to Top of Page

Sachin.Nand

2937 Posts

Posted - 2010-06-29 : 03:33:52
[code]
select * from
(
select t1.id,
t1.Indexdt,
t1.hr,t1.Status,
coalesce(t1.hr-T.hr,t1.hr) as difference
from @tbl t1
outer apply(select top 1 hr from @tbl t2 where t1.id=t2.id+1 and t1.Indexdt=t2.Indexdt)T
)T where difference<>0

[/code]


Limitations live only in our minds. But if we use our imaginations, our possibilities become limitless.

PBUH
Go to Top of Page

yasinirshad
Starting Member

18 Posts

Posted - 2010-06-29 : 04:21:24
Hi,
Thanks a lot for replyn me n sorry but i ddnt get wat i want ... below is my o/p on running ur query ..
ID Date Hour Status Difference
10 2010-06-26 00:00:00.000 1 4 1
19 2010-06-26 00:00:00.000 2 4 1
28 2010-06-26 00:00:00.000 3 3 1
37 2010-06-26 00:00:00.000 4 3 1
46 2010-06-26 00:00:00.000 5 3 1
55 2010-06-26 00:00:00.000 6 3 1
64 2010-06-26 00:00:00.000 7 6 1
73 2010-06-26 00:00:00.000 8 6 1
82 2010-06-26 00:00:00.000 9 6 1
91 2010-06-26 00:00:00.000 10 6 1
100 2010-06-26 00:00:00.000 11 6 1
109 2010-06-26 00:00:00.000 12 6 1
118 2010-06-26 00:00:00.000 13 6 1
127 2010-06-26 00:00:00.000 14 6 1
136 2010-06-26 00:00:00.000 15 6 1
145 2010-06-26 00:00:00.000 16 6 1
154 2010-06-26 00:00:00.000 17 6 1
163 2010-06-26 00:00:00.000 18 6 1
172 2010-06-26 00:00:00.000 19 6 1
181 2010-06-26 00:00:00.000 20 6 1
190 2010-06-26 00:00:00.000 21 5 1
199 2010-06-26 00:00:00.000 22 5 1
208 2010-06-26 00:00:00.000 23 5 1
218 2010-06-27 00:00:00.000 1 5 1

Now in my original table, i have hours starting from 0 to 23, but in above output hr 0 is ignored ... maybe b'coz of the condition where difference<>0 its not coming ... so how can we make it to come ...

Can you pls pls get me a query which will result in below o/p :

Date Status Status_Add
26/06/2010 4 2
26/06/2010 3 4
26/06/2010 6 14
26/06/2010 5 3
27/06/2010 5 1

I need o/p to have Date,Status and Status_Add (Status_Add is number of times the status appears for a particular day (26/6/2010)... As we see from first o/p Status '4' is repeated '2' times, Status '3' is repeated '4' times, status '6' for '14' times & so on (Then Status '5' for '1' time for DATE 27/6/2010 which is next day)... Now when we add this (2+4+14+3 it should come to '24' (24 hour period) but here it comes to '23' mayb bcoz of the condition "where difference<>0")...

This is my last request ... Kindly help me out ... Hope i get it ...Thanks once again !!!
Go to Top of Page

Sachin.Nand

2937 Posts

Posted - 2010-06-29 : 05:41:54
See you are simply changing your requirements from time to time.First you said.
quote:

If Hrs are repeating for same status then i dont want them to be selected



Now with this data

10 2010-06-26 00:00:00.000 1 4 1
19 2010-06-26 00:00:00.000 2 4 1
28 2010-06-26 00:00:00.000 3 3 1
37 2010-06-26 00:00:00.000 4 3 1
46 2010-06-26 00:00:00.000 5 3 1
55 2010-06-26 00:00:00.000 6 3 1
64 2010-06-26 00:00:00.000 7 6 1
73 2010-06-26 00:00:00.000 8 6 1
82 2010-06-26 00:00:00.000 9 6 1


you are saying that the o/p is wrong.In the above data the hrs are not repeating with same status.That is why the difference is displayed as 1.Please clarify it.

As for your second requirement here is the query

select
Indexdt,
status,
COUNT(status)as cnt
from @tbl
group by Indexdt,Status
order by Indexdt



Limitations live only in our minds. But if we use our imaginations, our possibilities become limitless.

PBUH
Go to Top of Page

yasinirshad
Starting Member

18 Posts

Posted - 2010-06-29 : 06:16:17
Really sorry for that...my bad ...anywayz with the query u gave,

select
date_g,
status,
COUNT(hr)as cnt
from StackedChart
group by date_g,Status
order by date_g

i got

2010-06-26 00:00:00.000 3 36
2010-06-26 00:00:00.000 4 27
2010-06-26 00:00:00.000 5 27
2010-06-26 00:00:00.000 6 126
2010-06-27 00:00:00.000 4 1
2010-06-27 00:00:00.000 5 3

which is not wat i want...

Ok ... Now with this data

ID Date Hour Status
10 2010-06-26 00:00:00.000 1 4
19 2010-06-26 00:00:00.000 2 4
28 2010-06-26 00:00:00.000 2 4
37 2010-06-26 00:00:00.000 3 3
46 2010-06-26 00:00:00.000 3 3
55 2010-06-26 00:00:00.000 3 3
64 2010-06-26 00:00:00.000 4 6
73 2010-06-26 00:00:00.000 5 6
82 2010-06-26 00:00:00.000 5 6
83 2010-06-27 00:00:00.000 1 6
84 2010-06-27 00:00:00.000 1 6

I need result like below
Date Status No_Of_Times_Status_For_Hrs
2010-06-26 00:00:00.000 4 2
2010-06-26 00:00:00.000 3 1
2010-06-26 00:00:00.000 6 2
2010-06-27 00:00:00.000 6 1

So, Status '4' occurs '2' times for 26/6/2010 on Hours '1','2' ...
Status '3 occurs '1 time for 26/6/2010 on Hours '3' ...
Status '6 occurs '2' times for 26/6/2010 on Hours '4,'5 ...

And again
Status '6 occurs '1 time for 27/6/2010 on Hours '1' ...

Thanks ...




Go to Top of Page

Sachin.Nand

2937 Posts

Posted - 2010-06-29 : 06:53:26
Just change the query to this


select
Indexdt,
status,
COUNT(distinct hr)as cnt
from @tbl
group by Indexdt,Status
order by Indexdt




Limitations live only in our minds. But if we use our imaginations, our possibilities become limitless.

PBUH
Go to Top of Page

yasinirshad
Starting Member

18 Posts

Posted - 2010-06-29 : 07:23:18
Thanks a lot... am almost near to what i want from this query:

select date_g,status,
COUNT(distinct hr)as cnt
from StackedChart
group by date_g,Status
order by date_g

My o/p is :
Date Status cnt
2010-06-26 00:00:00.000 3 4
2010-06-26 00:00:00.000 4 3
2010-06-26 00:00:00.000 5 3
2010-06-26 00:00:00.000 6 14

Here i feel Status is sorted 3,4,5,6... but in my table its of the order 4,3,6,5

So how can my o/p be like below :
Date Status cnt
2010-06-26 00:00:00.000 4 3
2010-06-26 00:00:00.000 3 4
2010-06-26 00:00:00.000 6 14
2010-06-26 00:00:00.000 5 3

Thanks Again !!!

Go to Top of Page
   

- Advertisement -