Monday, August 2, 2010
Pivot or Sum
Here are the two queries:
Select [Reviewed] + [Logged] as [Queue], [Open] As Development, [Testing], [Pending Imp],
[Open] + [Testing] + [Pending Imp] as [Total In Progress],
[Reviewed] + [Logged] + [Open] + [Testing] + [Pending Imp] as [Grand Total]
from (Select [state] FROM dbo.tblCombinedSnapshto) Datatable
Pivot
(
Count([state])
For [state]
In ([Reviewed], [Logged], [Open], [Testing], [Pending Imp])
) Pivottable;
Select Sum(case when [state] = 'Reviewed' or [state] = 'Logged' then 1 else 0 end) as [Queue],
Sum(case when [state] = 'Open' then 1 else 0 end) as Development,
Sum(case when [state] = 'Testing' then 1 else 0 end) as Testing,
Sum(case when [state] = 'Pending Imp' then 1 else 0 end) as [Pending Imp],
Sum(case when [state] = 'Open' or[state] = 'Testing'
or [state] = 'Pending Imp' then 1 else 0 end) as [Total In Progress],
Count(*) as [Grand Total]
from dbo.tblCombinedSnapshto;
The results were:
PIVOT timing
SQL Server Execution Times:
CPU time = 0 ms, elapsed time = 38 ms.
IO Table 'tblCombinedSnapshto'. Scan count 1, logical reads 8, physical reads 1, read-ahead reads 48, lob logical reads 0, lob physical reads 0, lob read-ahead reads 0.
2000 timing
SQL Server Execution Times:
CPU time = 0 ms, elapsed time = 50 ms.
IO Table 'tblCombinedSnapshto'. Scan count 1, logical reads 8, physical reads 1, read-ahead reads 48, lob logical reads 0, lob physical reads 0, lob read-ahead reads 0.
So for disk usage they were equal. For speed Pivot wins out.
Only a small scale test but worth doing now and then.
Monday, May 10, 2010
IsNull and Dates
DECLARE @x as datetime;
set @x = NULL;
SELECT IsNull(@x,-1) AS Col_1, IsNull(NULL,-1) AS Col_2;
The actual result was 1899-12-31 00:00:00.000,-1
Why did it happen?
The reason it does this is because dates can be given INT values where 0 = 1900-01-01
All other dates are relative to that so:
1 = 1900-01-02
-1 = 1899-12-31
SELECT IsNull(@x,-1) AS Col_1;
Is the same as set @x = -1;
Which is the same as
set @x = '1899-12-31 00:00:00.000';
So it did assign the value -1 to the date which then translated to Dec 31st 1899
Monday, April 19, 2010
SQL Cursors
I had thought that if an item came within teh scope of a cursor it would remain there until removed. However, cursors work a little like views, if you cange a value which would remove an item from a view you will no longer see it in the view. If you change the value of a data row which would put it outside the scope of teh cursor the cursor will drop that row.
This was tested on SQL 2005 sp3 – I assume it will be the same on other versions but I haven’t tested them.
I have a simple table:
CREATE TABLE [dbo].[test](
[pkid] [int] IDENTITY(1,1) NOT NULL,
[descript] [varchar](5) NOT NULL,
[value1] [int] NOT NULL,
[value2] [int] NOT NULL,
CONSTRAINT [PK_test] PRIMARY KEY CLUSTERED
(
[pkid] ASC
);
The table has 3 rows:
| Pkid | descript | value1 | value2 |
| 1 | test1 | 0 | 0 |
| 2 | test2 | 0 | 0 |
| 3 | test1 | 0 | 0 |
I then execute this script against the table:
Declare @dbName as varchar(5);
Declare @x as int;
Set @x = 1;
DECLARE dbCursor CURSOR Local Fast_Forward FOR
select descript From dbo.Test
WHERE value2 = 0;
OPEN dbCursor
FETCH NEXT FROM dbCursor INTO @dbName
WHILE @@FETCH_STATUS = 0
BEGIN
Print @dbName
Update dbo.Test
Set value2 = @x
Where descript = @dbname;
Set @x = @x+1;
FETCH NEXT FROM dbCursor INTO @dbName;
END
CLOSE dbCursor;
DEALLOCATE dbCursor;
The results in the table are:
| Pkid | descript | value1 | value2 |
| 1 | test1 | 0 | 1 |
| 2 | test2 | 0 | 2 |
| 3 | test1 | 0 | 1 |
This shows that because the value2 value of pkid 3 changed from 0 to 1 on the first update it dropped out of the cursors scope.