Stored procedure returning 1 - sql-server

I am not able to understand that why is stored procedure is returning Allow = 1 in the result set. I have edited to add more context to the the questions basically this stored procedure does authorization based on the result, if the result set is say Allow:0, Disclose: 1, Cache:1, corresponding URI's is not registered however if the result set is say Allow:1, Disclose: 1, Cache:1, a URI's is registered.
CREATE PROCEDURE [Auth].[Authorize]
#Router nvarchar(64),
#Realm nvarchar(64),
#Action nvarchar(64),
#URI nvarchar(256),
#Match nvarchar(64) = NULL,
#SessionID bigint,
#AuthProvider nvarchar(64),
#AuthMethod nvarchar(64),
#AuthID nvarchar(64),
#AuthRole nvarchar(64),
#TransportPeer nvarchar(64),
#TransportType nvarchar(64),
#TransportProtocol nvarchar(64),
#TransportUserAgent nvarchar(256) = NULL,
#TransportForwardedFor nvarchar(64) = NULL,
#F5AuthenticatedDN nvarchar(256) = NULL,
#F5AuthenticatedClient nvarchar(64) = NULL,
#Groups GroupsType READONLY
AS
SELECT
CAST(
CASE
WHEN
COUNT(*) > 0 AND
COUNT(DISTINCT CASE WHEN pp.IsAllowed = 0 THEN 1 END) = 0
THEN 1
ELSE 0
END
AS bit) AS Allow,
CAST(1 AS bit) AS Disclose,
CAST(1 AS bit) AS Cache
FROM Auth.Permissions AS pm
JOIN WAMP.MessageTypes AS mt ON pm.MessageTypeID = mt.ID
JOIN Auth.PrincipalPermissions AS pp ON pm.ID = pp.PermissionID
JOIN Auth.Principals AS pr ON pp.PrincipalID = pr.ID
JOIN Auth.PrincipalTypes AS pt ON pr.PrincipalTypeID = pt.ID
WHERE
(
(pt.Name = 'role' AND pr.Name = 'system')
/***
OR
(pt.Name = 'system' AND pr.Name = 'UATDSG')
OR
(pt.Name = 'group' AND pr.Name IN (SELECT Name FROM #Groups))
**/
) AND
pr.IsEnabled = 1 AND
pm.IsEnabled = 1 AND
mt.Name = 'publish' AND
'com.XXXX.XX.systems.XXXXX.heartbeat' LIKE pm.URI ESCAPE '\'
RETURN 0
I am not very good at stored procedure and this stored procedure.
we have following tables in the database,
CREATE TABLE [Auth].[Permissions](
[ID] [int] IDENTITY(1,1) NOT FOR REPLICATION NOT NULL,
[URI] [nvarchar](128) NOT NULL,
[MessageTypeID] [int] NOT NULL,
[Description] [nvarchar](512) NULL,
[IsEnabled] [bit] NOT NULL,
CREATE TABLE [Auth].[PrincipalPermissions](
[ID] [bigint] IDENTITY(1,1) NOT FOR REPLICATION NOT NULL,
[PrincipalID] [int] NOT NULL,
[PermissionID] [int] NOT NULL,
[IsAllowed] [bit] NOT NULL,
CREATE TABLE [Auth].[Principals](
[ID] [int] IDENTITY(1,1) NOT FOR REPLICATION NOT NULL,
[PrincipalTypeID] [int] NOT NULL,
[Name] [nvarchar](64) NOT NULL,
[IsEnabled] [bit] NOT NULL,
CREATE TABLE [Auth].[PrincipalTypes](
[ID] [int] IDENTITY(1,1) NOT FOR REPLICATION NOT NULL,
[Name] [nvarchar](64) NOT NULL,
CREATE TABLE [WAMP].[MessageTypes](
[ID] [int] NOT NULL,
[Name] [nvarchar](32) NOT NULL,
Result Set:
Allow Disclose Cache
1 1 1
SELECT TOP (1000) [ID]
,[URI]
,[MessageTypeID]
,[Description]
,[IsEnabled]
FROM [Auth].[Permissions]
ID URI MessageTypeID Description IsEnabled
3 % 32 NULL 1
4 % 16 NULL 1
5 % 64 NULL 1
6 % 48 NULL 1
131 com.XXXX.XX.systems.%.heartbeat 16 NULL 1
157 com.XXXX.XX.systems.mfg-%.heartbeat 16 NULL 1
160 com.XXXX.XX.systems.mfg-%.heartbeat 32 NULL 1
161 com.XXXX.XX.systems.%.heartbeat 32 NULL 1
SELECT TOP (1000) [ID]
,[PrincipalID]
,[PermissionID]
,[IsAllowed]
FROM [Auth].[PrincipalPermissions]
ID PrincipalID PermissionID IsAllowed
1 1 1 1
4 1 9 1
5 1 10 1
6 2 1 1
7 2 12 1
8 2 13 1
9 3 1 1
10 3 3 1
11 3 4 1
12 3 5 1
13 3 6 1
14 5 10 1
15 6 10 1
16 1 15 1
17 1 16 1
18 1 17 1
19 1 18 1
20 1 19 1
21 1 20 1
22 1 21 1
23 1 22 1
24 1 23 1
25 1 24 1
26 1 25 1
27 1 26 1
28 1 27 1
29 1 28 1
30 1 29 1
31 1 30 1
32 1 31 1
33 1 32 1
34 1 33 1
35 1 34 1
36 1 35 1
40 4 39 1
42 7 40 1
47 1 42 1
48 2 43 1
49 1 45 1
50 1 44 1
51 8 38 1
52 6 46 1
53 6 47 1
55 6 1 1
57 5 50 1
58 5 51 1
59 5 41 1
60 7 41 1
61 9 41 1
62 7 52 1
63 7 51 1
64 4 53 1
65 7 54 1
66 10 55 1
67 11 1 1
68 6 38 1
69 6 56 1
70 8 47 1
71 12 56 1
72 12 57 1
73 12 58 1
74 12 59 1
75 12 60 1
76 12 38 1
77 5 61 1
78 5 39 1
80 13 63 1
81 5 63 1
82 14 64 1
83 14 52 1
84 7 65 1
85 7 39 1
86 15 63 1
87 7 63 1
88 10 66 0
89 6 67 1
90 6 68 1
91 12 66 1
92 16 39 1
93 16 63 1
94 17 69 1
95 14 69 1
96 14 39 1
97 7 70 1
98 7 71 1
99 7 69 1
100 6 72 1
101 6 73 1
102 17 74 1
103 12 1 1
104 12 75 1
105 12 76 1
106 3 49 1
107 5 77 1
108 10 63 1
109 10 1 1
110 10 56 1
111 11 3 1
112 11 78 1
113 5 78 1
114 5 79 1
115 11 6 1
116 1 80 1
117 1 81 1
118 2 82 1
119 2 83 1
120 1 84 1
121 1 85 1
122 1 86 1
123 1 87 1
124 1 88 1
125 1 89 1
126 1 90 1
127 1 91 1
128 1 92 1
129 1 93 1
130 1 94 1
131 1 95 1
132 1 96 1
133 1 97 1
134 1 98 1
135 1 99 1
136 1 100 1
137 1 101 1
138 1 102 1
139 1 103 1
140 1 104 1
141 1 105 1
143 18 10 1
144 18 50 1
145 18 51 1
146 18 41 1
147 18 61 1
148 18 39 1
149 18 63 1
150 18 77 1
151 18 78 1
152 18 79 1
153 5 3 1
154 5 4 1
155 5 5 1
156 5 6 1
157 1 107 1
158 4 3 1
159 4 4 1
160 4 5 1
161 4 6 1
162 11 4 1
163 11 5 1
164 20 1 1
165 20 3 1
166 20 78 1
167 20 6 1
168 20 4 1
169 20 5 1
170 19 1 1
171 19 3 1
172 19 78 1
173 19 6 1
174 19 4 1
175 19 5 1
176 19 1 1
177 19 3 1
178 19 78 1
179 19 6 1
180 19 4 1
181 19 5 1
182 21 1 1
183 21 3 1
184 21 78 1
185 21 6 1
186 21 4 1
187 21 5 1
188 1 108 1
189 1 109 1
190 22 1 1
191 22 3 1
192 22 78 1
193 22 6 1
194 22 4 1
195 22 5 1
196 23 1 1
197 23 3 1
198 23 78 1
199 23 6 1
200 23 4 1
201 23 5 1
202 24 1 1
203 24 3 1
204 24 78 1
205 24 6 1
206 24 4 1
207 24 5 1
208 25 1 1
209 25 3 1
210 25 78 1
211 25 6 1
212 25 4 1
213 25 5 1
214 26 1 1
215 26 3 1
216 26 78 1
217 26 6 1
218 26 4 1
219 26 5 1
220 27 1 1
221 27 3 1
222 27 78 1
223 27 6 1
224 27 4 1
225 27 5 1
226 28 1 1
227 28 3 1
228 28 78 1
229 28 6 1
230 28 4 1
231 28 5 1
232 28 1 1
233 28 3 1
234 28 78 1
235 28 6 1
236 28 4 1
237 28 5 1
238 29 1 1
239 29 3 1
240 29 78 1
241 29 6 1
242 29 4 1
243 29 5 1
244 30 1 1
245 30 3 1
246 30 78 1
247 30 6 1
248 30 4 1
249 30 5 1
257 1 106 1
258 1 110 1
259 1 111 1
260 1 112 1
261 33 113 1
262 33 114 1
263 33 115 1
264 1 117 1
265 1 118 1
266 1 119 1
267 1 120 1
268 1 121 1
269 1 122 1
270 1 123 1
271 1 124 1
272 1 125 1
273 1 126 1
274 1 127 1
275 1 128 1
276 1 129 1
277 1 130 1
278 1 131 1
279 1 132 1
280 1 133 1
281 1 134 1
282 1 135 1
283 1 136 1
284 1 137 1
285 1 138 1
286 1 139 1
287 1 140 1
288 1 141 1
289 1 142 1
290 1 143 1
291 1 144 1
292 1 145 1
293 1 146 1
294 1 147 1
295 1 148 1
296 1 149 1
297 1 150 1
298 1 151 1
299 1 152 1
300 1 153 1
301 1 154 1
302 1 155 1
303 1 156 1
304 4 117 1
305 4 118 1
306 4 119 1
307 4 120 1
308 4 121 1
309 4 122 1
310 4 123 1
311 4 124 1
312 4 125 1
313 4 126 1
314 4 127 1
315 4 128 1
316 4 129 1
317 4 130 1
318 4 131 1
319 4 132 1
320 4 133 1
321 4 134 1
322 4 135 1
323 4 136 1
324 4 137 1
325 4 138 1
326 4 139 1
327 4 140 1
328 4 141 1
329 4 142 1
330 4 143 1
331 4 144 1
332 4 145 1
333 4 146 1
334 4 147 1
335 4 148 1
336 4 149 1
337 4 150 1
338 4 151 1
339 4 152 1
340 4 153 1
341 4 154 1
342 4 155 1
343 4 156 1
344 6 161 1
/****** Script for SelectTopNRows command from SSMS ******/
SELECT TOP (1000) [ID]
,[PrincipalTypeID]
,[Name]
,[IsEnabled]
FROM [Auth].[Principals]
ID PrincipalTypeID Name IsEnabled
1 3 system 1
2 3 user 1
3 3 service 1
35 4 ManufacturingSystem 1
SELECT TOP (1000) [ID]
,[Name]
FROM [Auth].[PrincipalTypes]
ID Name
2 group
3 role
4 system
1 user
/****** Script for SelectTopNRows command from SSMS ******/
SELECT TOP (1000) [ID]
,[Name]
FROM [WAMP].[MessageTypes]
ID Name
3 abort
5 authenticate
48 call
49 cancel
4 challenge
8 error
36 event
6 goodbye
1 hello
69 interrupt
68 invocation
16 publish
17 published
64 register
65 registered
50 result
32 subscribe
33 subscribed
66 unregister
67 unregistered
34 unsubscribe
35 unsubscribed
2 welcome
70 yield

Related

query max date of all customers that falls under a where condition

I would like to query the max date only of all customers that has a value of 25.
Table1
cust_id Date Value
1 2019-10-01 25
1 2019-10-19 35
1 2018-08-27 29
1 2019-07-09 35
1 2019-10-15 55
2 2019-09-26 45
2 2019-10-19 31
2 2019-07-19 8
2 2019-10-02 28
3 2019-09-02 36
3 2019-08-15 39
4 2019-10-15 37
4 2019-10-16 36
4 2018-11-18 27
wrote this query:
select
t1.value,
max(t1.date) as max_date
from table1 t1
where
(t1.date >= '2019-09-30 and t1.date <= 2019-10-31)
and t1.value > 25
group by t1.value
but I am getting:
cust_id Date Value
1 2019-10-01 25
1 2019-10-19 35
1 2019-10-15 55
2 2019-10-19 31
2 2019-10-02 28
4 2019-10-15 37
4 2019-10-16 36
would like to only see the last data entry that is over 25. Something like this:
cust_id Date Value
1 2019-10-19 35
2 2019-10-19 31
4 2018-11-16 36

Average value depending on cumulative value from other column aka running total

I've sifted through the various sql-server tagged threads using AVERAGE and Cumulative as search terms. Various desperate answers, but I can't cobble them together for my needs. The use case is to find the initial average value (cumulative value/cumulative days on) for a time period when cumulative days on is greater than 60 and less than 90.
Below is a table where ID identifies the object, VALUE is the amount reported on a monthly basis and DAYSON is the number of days in that month where the object ran to produce the value. YEARMONTH is date value on which on can sort.
ID VALUE DASYON YEARMONTH
1 166 27 201502
1 1 2 201505
1 569 19 201507
1 312 19 201508
2 364 27 201502
2 328 31 201503
2 242 29 201504
2 273 31 201505
2 174 30 201506
2 188 25 201507
2 203 25 201508
3 474 28 201502
3 521 31 201503
3 465 30 201504
3 473 31 201505
3 434 30 201506
3 404 31 201507
I would like to create a summary table that averages the cumulative value divided by the cumulative days uniquely for each ID where cumulative days is greater than 60 and less than 90. Below is a table that with the cumulative values. (I generated this in Excel)
ID VALUE cumValue DASYON cumDaysOn YEARMONTH
1 166 166 27 27 201502
1 1 167 2 29 201505
1 569 736 19 48 201507
1 312 1048 19 67 201508
2 364 364 27 27 201502
2 328 692 31 58 201503
2 242 934 29 87 201504
2 273 1207 31 118 201505
2 174 1381 30 148 201506
2 188 1569 25 173 201507
2 203 1772 25 198 201508
3 474 474 28 28 201502
3 521 505 31 59 201503
3 465 535 30 89 201504
3 473 566 31 120 201505
3 434 596 30 150 201506
3 404 627 31 181 201507
I try this based on other threads:
SELECT
ID,
Value,
SUM(Value) OVER (ORDER BY ID, YearMonth) [cumValue],
DaysOn,
SUM (DaysOn) OVER (Order by ID, YearMonth) as cumDaysOn,
YearMonth
FROM table
WHERE DAYSON > 0 and Liquid > 0 and YearMonth > 201501
GROUP BY ID, YearMonth, Value, DaysOn
ORDER BY ID, yearmonth
I can't get it to iterate over the ID; it just keeps summing down the column. If I could create a table or view like the one above, then I could always use a select statement and divide cumvalue by cumdayson.
Below is a table to show where I would get the initial average value (InititalAverageValue) based on the criteria:
ID VALUE cumValue DASYON cumDaysOn YEARMONTH InitalAvgValue
1 166 166 27 27 201502
1 1 167 2 29 201505
1 569 736 19 48 201507
1 312 1048 19 67 201508 55
2 364 364 27 27 201502
2 328 692 31 58 201503
2 242 934 29 87 201504 32
2 273 1207 31 118 201505
2 174 1381 30 148 201506
2 188 1569 25 173 201507
2 203 1772 25 198 201508
3 474 474 28 28 201502
3 521 505 31 59 201503
3 465 535 30 89 201504 18
3 473 566 31 120 201505
3 434 596 30 150 201506
3 404 627 31 181 201507
Ultimately what I desire is table as such:
ID InitalAvgValue
1 55
2 32
3 18
Thanks in advance for any help.
The crux is that you need a running total. There are several approaches to calculating running totals, but they have various tradeoffs between simplicity and performance. The "best" approach depends on the expected size of your data set and whether you are using SQL Server 2012 or an earlier version. The following article describes some different options along with the pros and cons:
http://sqlperformance.com/2012/07/t-sql-queries/running-totals
Here's a quick example using correlated subqueries, which may be reasonable for small data sets, but likely would not scale well to larger data:
SELECT
ID,
ROUND(AVG(CAST(CumulativeValue AS FLOAT) / CAST(CumulativeDaysOn AS FLOAT)), 1) AS Average
FROM
(
SELECT
ID,
Value,
DaysOn,
(SELECT SUM(Value) FROM ExampleTable t2 WHERE t1.ID = t2.ID and t2.YearMonth <= t1.YearMonth) AS CumulativeValue,
(SELECT SUM(DaysOn) FROM ExampleTable t2 WHERE t1.ID = t2.ID and t2.YearMonth <= t1.YearMonth) AS CumulativeDaysOn
FROM
ExampleTable t1
) AS ExampleWithTotals
WHERE
CumulativeDaysOn > 60 AND CumulativeDaysOn < 90
GROUP BY
ID
ORDER BY
ID
;
Output:
ID Average
1 15.6
2 10.7
3 16.4

Advanced sql with windowing claus

SELECT a.*,
SUM(s.amount) over(ORDER BY s.month rows unbounded preceding) AS a ,
SUM(s.amount) over(PARTITION BY s.month ORDER BY s.month rows unbounded preceding) AS b,
SUM(s.amount) over(PARTITION BY s.month ) AS c_1,
SUM(s.amount) over(PARTITION BY s.month ORDER BY s.month rows BETWEEN unbounded preceding AND unbounded following) AS c,
SUM(s.amount) over(PARTITION BY s.month ORDER BY s.month rows BETWEEN 1 preceding AND unbounded following) AS d,
SUM(s.amount) over(PARTITION BY s.month ORDER BY s.month rows BETWEEN 1 preceding AND 1 following) AS e,
SUM(s.amount) over(PARTITION BY s.month ORDER BY s.month rows BETWEEN unbounded preceding AND 1 following) AS f,
SUM(s.amount) over(PARTITION BY s.month ORDER BY s.month rows CURRENT ROW) AS g
FROM all_sales s,
(SELECT *
FROM all_sales) a
WHERE s.rowid = a.rowid;
/
--above query give the result shown below what is difference between c_1 and c column.
YEAR MONTH PRD_TYPE_ID EMP_ID AMOUNT A B C_1 C D E F G
1 2006 1 1 21 1.00 1 1 10 10 10 3 3 1
2 2006 1 1 21 2.00 3 3 10 10 10 6 6 2
3 2005 1 2 21 3.00 6 6 10 10 9 9 10 3
4 2005 1 2 22 4.00 10 10 10 10 7 7 10 4
5 2006 2 1 21 5.00 15 5 11 11 11 11 11 5
6 2005 2 1 21 6.00 21 11 11 11 11 11 11 6
7 2005 3 1 21 21 7 7 7 7 7
8 2006 3 2 21 7.00 28 7 7 7 7 7 7 7
9 2005 4 1 21 8.00 36 8 17 17 17 17 17 8
10 2006 4 2 21 9.00 45 17 17 17 17 17 17 9
11 2006 5 2 21 45 10 10 10 10 10
12 2005 5 1 21 10.00 55 10 10 10 10 10 10 10
13 2006 6 1 21 11.00 66 11 23 23 23 23 23 11
14 2005 6 1 21 12.00 78 23 23 23 23 23 23 12
15 2005 7 2 21 13.00 91 13 27 27 27 27 27 13
16 2006 7 1 21 14.00 105 27 27 27 27 27 27 14
17 2005 8 2 21 15.00 120 15 31 31 31 31 31 15
18 2006 8 1 21 16.00 136 31 31 31 31 31 31 16
19 2005 9 2 21 17.00 153 17 35 35 35 35 35 17
20 2006 9 1 21 18.00 171 35 35 35 35 35 35 18
21 2005 10 2 21 19.00 190 19 39 39 39 39 39 19
22 2006 10 1 21 20.00 210 39 39 39 39 39 39 20
23 2006 11 1 21 21.00 231 21 43 43 43 43 43 21
24 2005 11 1 21 22.00 253 43 43 43 43 43 43 22
25 2006 12 2 21 23.00 276 23 47 47 47 47 47 23
26 2005 12 1 21 24.00 300 47 47 47 47 47 47 24
You have the same result because your statements are preaty the same:
SUM(s.amount) over(PARTITION BY s.month ) AS c_1,
SUM(s.amount) over(PARTITION BY s.month ORDER BY s.month rows BETWEEN unbounded preceding AND unbounded following) AS c
The cause of it is condition BETWEEN unbounded preceding AND unbounded following because it covers the entire range of partition.
FROM MSDN:
UNBOUNDED PRECEDING - Specifies that the window starts at the first row of the partition. UNBOUNDED PRECEDING can only be specified as window starting point.
UNBOUNDED FOLLOWING - Specifies that the window ends at the last row of the partition. UNBOUNDED FOLLOWING can only be specified as a window end point. For example RANGE BETWEEN CURRENT ROW AND UNBOUNDED FOLLOWING defines a window that starts with the current row and ends with the last row of the partition.
When together they are equal to PARTITION BY s.month

Order By using case with conditional results and partition row_number()

I have a SQL that I've not been able to return the order correctly. Below is example of the rows and the order I want them to appear in. I tried ORDER BY WITH CONDITIONAL CASE AND ROW_NUMBER OVER PARTION without success. I want ColA to be the primary sort and ColB secondary only when ColC has a length < 3 otherwise ColB is primary and ColA is secondary
ColA ColB ColC
5 750 15
5 750 15
3 984 13
3 984 13
5 1021 15
5 1021 15
4 1602 14
4 1602 14
4 1823 14
4 1823 14
6 4099 16
6 4099 16
11 4099 240990
0 10880 10
0 10880 10
3 10881 13
3 10881 13
2 11053 12
8 11053 211053
6 10891 16
6 10891 16
2 11034 12
10 11034 211034
ColA ColB ColC
0 10880 10
0 10880 10
2 11034 12
10 11034 211034
2 11053 12
8 11053 211053
3 984 13
3 984 13
3 10881 13
3 10881 13
4 1602 14
4 1602 14
4 1823 14
4 1823 14
5 750 15
5 750 15
5 1021 15
5 1021 15
6 4099 16
6 4099 16
11 4099 240990
6 10891 16
6 10891 16
order by case when len(ColC) < 3 then ColA else ColB end,
case when len(ColC) < 3 then ColB else ColA end
SQL Fiddle Example

TSQL : Incrementing a column value based on another column's value (SQL Server)

I have a legacy stored procedure that I'm trying to modify.
This is a dump of the data that's in the table that's being modified.
Each time the PayrollRateID (2nd column, example values shown here 25, 27, 28 etc) increments the 'Incrementer' column needs incrementing.
The ID column is generated using Row_Number()
Whoever wrote the stored procedure is using a column to go down each row in the table, and set the value of the incrementer column each time the PayrollRateID value cahnges. This, unfortunately, is taking a couple of minutes to do 20,000 odd rows. That's just a few days data, if I run with a month's worth, well, you can imagine the performance issue I'm getting.
The logic of the sproc is to start off with an incrementer value of 0, and with the first PayrollRateID (in the example - 25), give all those 0's, when the PayrollRateID changes, then increment the count in the incrementer column, and give all the next value (in the example - 27's) a 1, when it changes to the next value (in my example 28) then give the incrementer column a 2, etc.
Date
PayrollRateID
DayNum
Variance
VarianceID
Incrementer
ID
2011-07-25 00:00:00.000 25 1 1.00 0 0 1
2011-07-26 00:00:00.000 25 2 1.00 0 0 2
2011-07-27 00:00:00.000 25 3 1.00 0 0 3
2011-07-28 00:00:00.000 25 4 1.00 0 0 4
2011-07-29 00:00:00.000 25 5 1.00 0 0 5
2011-07-30 00:00:00.000 25 6 1.00 0 0 6
2011-07-31 00:00:00.000 25 7 1.00 0 0 7
2011-08-01 00:00:00.000 25 1 1.00 0 0 8
2011-08-02 00:00:00.000 25 2 1.00 0 0 9
2011-08-03 00:00:00.000 25 3 1.00 0 0 10
2011-08-04 00:00:00.000 25 4 1.00 0 0 11
2011-08-05 00:00:00.000 25 5 1.00 0 0 12
2011-08-06 00:00:00.000 25 6 1.00 0 0 13
2011-08-07 00:00:00.000 25 7 1.00 0 0 14
2011-08-08 00:00:00.000 25 1 1.00 0 0 15
2011-08-09 00:00:00.000 25 2 1.00 0 0 16
2011-08-10 00:00:00.000 25 3 1.00 0 0 17
2011-08-11 00:00:00.000 25 4 1.00 0 0 18
2011-08-12 00:00:00.000 25 5 1.00 0 0 19
2011-08-13 00:00:00.000 25 6 1.00 0 0 20
2011-08-14 00:00:00.000 25 7 1.00 0 0 21
2011-07-25 00:00:00.000 27 1 1.00 0 1 22
2011-07-26 00:00:00.000 27 2 1.00 0 1 23
2011-07-27 00:00:00.000 27 3 1.00 0 1 24
2011-07-28 00:00:00.000 27 4 1.00 0 1 25
2011-07-29 00:00:00.000 27 5 1.00 0 1 26
2011-07-30 00:00:00.000 27 6 1.00 0 1 27
2011-07-31 00:00:00.000 27 7 1.00 0 1 28
2011-08-01 00:00:00.000 27 1 1.00 0 1 29
2011-08-02 00:00:00.000 27 2 1.00 0 1 30
2011-08-03 00:00:00.000 27 3 1.00 0 1 31
2011-08-04 00:00:00.000 27 4 1.00 0 1 32
2011-08-05 00:00:00.000 27 5 1.00 0 1 33
2011-08-06 00:00:00.000 27 6 1.00 0 1 34
2011-08-07 00:00:00.000 27 7 1.00 0 1 35
2011-08-08 00:00:00.000 27 1 1.00 0 1 36
2011-08-09 00:00:00.000 27 2 1.00 0 1 37
2011-08-10 00:00:00.000 27 3 1.00 0 1 38
2011-08-11 00:00:00.000 27 4 1.00 0 1 39
2011-08-12 00:00:00.000 27 5 1.00 0 1 40
2011-08-13 00:00:00.000 27 6 1.00 0 1 41
2011-08-14 00:00:00.000 27 7 1.00 0 1 42
2011-07-25 00:00:00.000 28 1 1.00 0 2 43
2011-07-26 00:00:00.000 28 2 1.00 0 2 44
2011-07-27 00:00:00.000 28 3 1.00 0 2 45
2011-07-28 00:00:00.000 28 4 1.00 0 2 46
2011-07-29 00:00:00.000 28 5 1.00 0 2 47
2011-07-30 00:00:00.000 28 6 1.00 0 2 48
2011-07-31 00:00:00.000 28 7 1.00 0 2 49
2011-08-01 00:00:00.000 28 1 1.00 0 2 50
2011-08-02 00:00:00.000 28 2 1.00 0 2 51
2011-08-03 00:00:00.000 28 3 1.00 0 2 52
2011-08-04 00:00:00.000 28 4 1.00 0 2 53
2011-08-05 00:00:00.000 28 5 1.00 0 2 54
2011-08-06 00:00:00.000 28 6 1.00 0 2 55
2011-08-07 00:00:00.000 28 7 1.00 0 2 56
2011-08-08 00:00:00.000 28 1 1.00 0 2 57
2011-08-09 00:00:00.000 28 2 1.00 0 2 58
2011-08-10 00:00:00.000 28 3 1.00 0 2 59
2011-08-11 00:00:00.000 28 4 1.00 0 2 60
2011-08-12 00:00:00.000 28 5 1.00 0 2 61
2011-08-13 00:00:00.000 28 6 1.00 0 2 62
2011-08-14 00:00:00.000 28 7 1.00 0 2 63
2011-07-25 00:00:00.000 34 1 1.00 0 3 64
2011-07-26 00:00:00.000 34 2 1.00 0 3 65
2011-07-27 00:00:00.000 34 3 1.00 0 3 66
2011-07-28 00:00:00.000 34 4 1.00 0 3 67
2011-07-29 00:00:00.000 34 5 1.00 0 3 68
2011-07-30 00:00:00.000 34 6 1.00 0 3 69
2011-07-31 00:00:00.000 34 7 1.00 0 3 70
2011-08-01 00:00:00.000 34 1 1.00 0 3 71
2011-08-02 00:00:00.000 34 2 1.00 0 3 72
2011-08-03 00:00:00.000 34 3 1.00 0 3 73
2011-08-04 00:00:00.000 34 4 1.00 0 3 74
2011-08-05 00:00:00.000 34 5 1.00 0 3 75
2011-08-06 00:00:00.000 34 6 1.00 0 3 76
2011-08-07 00:00:00.000 34 7 1.00 0 3 77
2011-08-08 00:00:00.000 34 1 1.00 0 3 78
2011-08-09 00:00:00.000 34 2 1.00 0 3 79
2011-08-10 00:00:00.000 34 3 1.00 0 3 80
2011-08-11 00:00:00.000 34 4 1.00 0 3 81
2011-08-12 00:00:00.000 34 5 1.00 0 3 82
2011-08-13 00:00:00.000 34 6 1.00 0 3 83
2011-08-14 00:00:00.000 34 7 1.00 0 3 84
2011-07-25 00:00:00.000 38 1 1.00 0 4 85
2011-07-26 00:00:00.000 38 2 1.00 0 4 86
2011-07-27 00:00:00.000 38 3 1.00 0 4 87
2011-07-28 00:00:00.000 38 4 1.00 0 4 88
2011-07-29 00:00:00.000 38 5 1.00 0 4 89
2011-07-30 00:00:00.000 38 6 1.00 0 4 90
2011-07-31 00:00:00.000 38 7 1.00 0 4 91
2011-08-01 00:00:00.000 38 1 1.00 0 4 92
2011-08-02 00:00:00.000 38 2 1.00 0 4 93
2011-08-03 00:00:00.000 38 3 1.00 0 4 94
2011-08-04 00:00:00.000 38 4 1.00 0 4 95
2011-08-05 00:00:00.000 38 5 1.00 0 4 96
2011-08-06 00:00:00.000 38 6 1.00 0 4 97
2011-08-07 00:00:00.000 38 7 1.00 0 4 98
2011-08-08 00:00:00.000 38 1 1.00 0 4 99
2011-08-09 00:00:00.000 38 2 1.00 0 4 100
2011-08-10 00:00:00.000 38 3 1.00 0 4 101
2011-08-11 00:00:00.000 38 4 1.00 0 4 102
2011-08-12 00:00:00.000 38 5 1.00 0 4 103
2011-08-13 00:00:00.000 38 6 1.00 0 4 104
2011-08-14 00:00:00.000 38 7 1.00 0 4 105
2011-07-25 00:00:00.000 40 1 1.00 0 5 106
2011-07-26 00:00:00.000 40 2 1.00 0 5 107
2011-07-27 00:00:00.000 40 3 1.00 0 5 108
2011-07-28 00:00:00.000 40 4 1.00 0 5 109
2011-07-29 00:00:00.000 40 5 1.00 0 5 110
2011-07-30 00:00:00.000 40 6 1.00 0 5 111
2011-07-31 00:00:00.000 40 7 1.00 0 5 112
2011-08-01 00:00:00.000 40 1 1.00 0 5 113
2011-08-02 00:00:00.000 40 2 1.00 0 5 114
2011-08-03 00:00:00.000 40 3 1.00 0 5 115
2011-08-04 00:00:00.000 40 4 1.00 0 5 116
2011-08-05 00:00:00.000 40 5 1.00 0 5 117
2011-08-06 00:00:00.000 40 6 1.00 0 5 118
2011-08-07 00:00:00.000 40 7 1.00 0 5 119
2011-08-08 00:00:00.000 40 1 1.00 0 5 120
2011-08-09 00:00:00.000 40 2 1.00 0 5 121
2011-08-10 00:00:00.000 40 3 1.00 0 5 122
2011-08-11 00:00:00.000 40 4 1.00 0 5 123
2011-08-12 00:00:00.000 40 5 1.00 0 5 124
2011-08-13 00:00:00.000 40 6 1.00 0 5 125
2011-08-14 00:00:00.000 40 7 1.00 0 5 126
2011-07-25 00:00:00.000 41 1 1.00 0 6 127
2011-07-26 00:00:00.000 41 2 1.00 0 6 128
2011-07-27 00:00:00.000 41 3 1.00 0 6 129
2011-07-28 00:00:00.000 41 4 1.00 0 6 130
2011-07-29 00:00:00.000 41 5 1.00 0 6 131
2011-07-30 00:00:00.000 41 6 1.00 0 6 132
2011-07-31 00:00:00.000 41 7 1.00 0 6 133
2011-08-01 00:00:00.000 41 1 1.00 0 6 134
2011-08-02 00:00:00.000 41 2 1.00 0 6 135
2011-08-03 00:00:00.000 41 3 1.00 0 6 136
2011-08-04 00:00:00.000 41 4 1.00 0 6 137
2011-08-05 00:00:00.000 41 5 1.00 0 6 138
2011-08-06 00:00:00.000 41 6 1.00 0 6 139
2011-08-07 00:00:00.000 41 7 1.00 0 6 140
2011-08-08 00:00:00.000 41 1 1.00 0 6 141
2011-08-09 00:00:00.000 41 2 1.00 0 6 142
2011-08-10 00:00:00.000 41 3 1.00 0 6 143
2011-08-11 00:00:00.000 41 4 1.00 0 6 144
2011-08-12 00:00:00.000 41 5 1.00 0 6 145
2011-08-13 00:00:00.000 41 6 1.00 0 6 146
2011-08-14 00:00:00.000 41 7 1.00 0 6 147
2011-07-25 00:00:00.000 42 1 1.00 0 7 148
2011-07-26 00:00:00.000 42 2 1.00 0 7 149
2011-07-27 00:00:00.000 42 3 1.00 0 7 150
2011-07-28 00:00:00.000 42 4 1.00 0 7 151
2011-07-29 00:00:00.000 42 5 1.00 0 7 152
2011-07-30 00:00:00.000 42 6 1.00 0 7 153
2011-07-31 00:00:00.000 42 7 1.00 0 7 154
2011-08-01 00:00:00.000 42 1 1.00 0 7 155
2011-08-02 00:00:00.000 42 2 1.00 0 7 156
2011-08-03 00:00:00.000 42 3 1.00 0 7 157
2011-08-04 00:00:00.000 42 4 1.00 0 7 158
2011-08-05 00:00:00.000 42 5 1.00 0 7 159
2011-08-06 00:00:00.000 42 6 1.00 0 7 160
2011-08-07 00:00:00.000 42 7 1.00 0 7 161
2011-08-08 00:00:00.000 42 1 1.00 0 7 162
2011-08-09 00:00:00.000 42 2 1.00 0 7 163
2011-08-10 00:00:00.000 42 3 1.00 0 7 164
2011-08-11 00:00:00.000 42 4 1.00 0 7 165
2011-08-12 00:00:00.000 42 5 1.00 0 7 166
2011-08-13 00:00:00.000 42 6 1.00 0 7 167
2011-08-14 00:00:00.000 42 7 1.00 0 7 168
2011-07-25 00:00:00.000 44 1 1.00 0 8 169
2011-07-26 00:00:00.000 44 2 1.00 0 8 170
2011-07-27 00:00:00.000 44 3 1.00 0 8 171
2011-07-28 00:00:00.000 44 4 1.00 0 8 172
2011-07-29 00:00:00.000 44 5 1.00 0 8 173
I'm going to try and group on PayrollRateID, and use Row_Number() to generate the value for the Incrementer column.
Any better suggestions would be very much appreciated.
SQL Server 2008.
What about this:
Update a
Set Incrementer = b.Incrementer
From dbo.YourTable a
Join
(
Select PayRollId,
Row_Number() Over(Order By PayRollId) as [Incrementer]
From dbo.YourTable
Group By PayRollId
)b on a.PayRollId = b.PayRollId
Not sure if I understand the logic in the stored proc, but, could you create another table say [PayrollIncrementer] that has an ID identity seed value and inserting into the table when you need to increment the ID.
CREATE TABLE [dbo].[PayrollIncrementer]
(
[Id] INT NOT NULL IDENTITY(1),
[DateAdded] DATETIME NOT NULL
)
Your stored proc would use this table something like this
BEGIN
-- PROC BEGIN
DECLARE #id INT
INSERT INTO [dbo].[PayrollIncrementer] ([DateAdded]) VALUES (GETDATE())
SET #id = SCOPE_IDENTITY()
-- remainder of proc & use the identity
-- PROC END
END
Sorry if you have already thought of this, but I don't really understand the expected outcome.
How about just using DENSE_RANK instead of ROW_NUMBER? That will do everything at once.

Resources