About Me

My photo
Mumbai, Maharastra, India
He has more than 7.6 years of experience in the software development. He has spent most of the times in web/desktop application development. He has sound knowledge in various database concepts. You can reach him at viki.keshari@gmail.com https://www.linkedin.com/in/vikrammahapatra/ https://twitter.com/VikramMahapatra http://www.facebook.com/viki.keshari

Search This Blog

Monday, March 16, 2015

Leisure time out of most hectic QA release day, playing with Count and Null



It became a leisure time out of most hectic QA release day, when my  friend ask me to design 10 typical query, don’t know why this SQL Server is so interesting it fuels up energy to even a non living atmosphere. Here is my first query goes.

What will be the output of the third query if following first two query produce the below result

select count(*) as TotalCount from dbo.MyTable
TotalCount
-----------
9
select count(*) as WhereCount from dbo.MyTable where MyID = 4
WhereCount
-----------
3

select count(*) as MyCount from dbo.MyTable where MyID <> 4
WhereCount
-----------
??

Before come to any conclusion let’s stimulate the same problem

CREATE TABLE dbo.MyTable
    (
      MyID INT
    , MyChar VARCHAR(10)
    );
   GO
Command(s) completed successfully.

Our table is ready, let’s insert some data and prepare our environment for testing.

INSERT INTO dbo.MyTable
        ( MyID, MyChar )
    VALUES
        ( 1, 'A' ),
        ( 2, 'B' ),
        ( NULL, 'C' ),
        ( 4, 'D' ),
        ( NULL, 'E' ),
        ( 6, 'F' ),
        ( 6, 'G' ),
        ( 4, 'H' ),
        ( 4, 'I' ); 
   GO


Let’s find out the total count of rows in the table

select count(*) from dbo.MyTable
TotalCount
-----------
9

So its 9 in count, lets try to find out now of rows where “MyID” field has value equal to 4

select count(*) as WhereCount from dbo.MyTable where MyID = 4
MyCount
-----------
3

So here it comes out as 3, lets now come to our asked query output

select count(*) as MyCount from dbo.MyTable where MyID <> 4

Here we saw from previous two output that
Total row count = 9
Total row with MyId value equal to 4 is = 3

So if we are asked
Total row with value not equal to 4, in a simple mathematic term we can reply,
Total row with value not equal to 4=(Total row count) – (Total row with MyId value equal to 4)

Total row with value not equal to 4 = 9-3 = 6

Now let’s run the query to find the real answer for the following query

select count(*) as MyCount from dbo.MyTable where MyID <> 4
MyCount
-----------
4

The output is not footing to our thought, lets find out where we went wrong

Lets first query the table data
MyID        MyChar
----------- ----------
1           A
2           B
NULL        C
4           D
NULL        E
6           F
6           G
4           H
4           I

(9 row(s) affected)

From the output we can see total counts of rows are 9 in number and number of rows with MyID equal four are 3. Now if we check number of rows with values not equal to 4:
Considering Null is 6
Not considering Null is 4

Now if we recall our query the count came out as 4
select count(*) as MyCount from dbo.MyTable where MyID <> 4
MyCount
-----------
4

This means MyID <> 4 skip Null values while evaluating count(*) function.


Conclusion: While calculating count, SQL Server skip NULL values.
 
A cup of tea with SQL Server is the greatest combination :)

 

Post Reference: Vikram Aristocratic Elfin Share

Monday, March 2, 2015

A simple but tactical enough, Power of Group By - I


Today my mate draw closer to me for a straight forward script, he had a table which consist of dates in int (yyyymmdd) format, his requisite was to get the latest date of each month.

For example,
Out of 20150123, 20150130, 20150127 the output should be 20150130.

Let’s stimulate the problem by taking an example and lets see how we will approach towards the solution

CREATE table DemoMax
(id int identity(1,1),
mydate int)

insert into DemoMax values (20150226)
insert into DemoMax values (20150131)
insert into DemoMax values (20150329)
insert into DemoMax values (20150429)
insert into DemoMax values (20150530)
insert into DemoMax values (20150626)
insert into DemoMax values (20150726)
insert into DemoMax values (20150826)
insert into DemoMax values(20150226)
insert into DemoMax values(20150227)
insert into DemoMax values(20150328)


Here we have table created with few rows in it.

Our requirement is to get the highest date for each month, for finding so, let’s bifurcate the myDate to yyyy, mm and dd in three separate columns.

SUBSTRING(cast(mydate as varchar),1,4),
SUBSTRING(cast(mydate as varchar),5,2),

Now the second step is to group by the result on the above bifurcated column and applying the max aggregate function to find the largest among all date for a specific yyyy and mm.

max( cast(SUBSTRING(cast(mydate as varchar),7,2) as int) )

Lets formulate the final query

SELECT SUBSTRING(cast(mydate AS VARCHAR), 1, 4) as YYYY
       ,SUBSTRING(cast(mydate AS VARCHAR), 5, 2) as MM
       ,max(cast(SUBSTRING(cast(mydate AS VARCHAR), 7, 2) AS INT)) as LatestDate
FROM DemoMax
GROUP BY SUBSTRING(cast(mydate AS VARCHAR), 1, 4)
       ,SUBSTRING(cast(mydate AS VARCHAR), 5, 2)

YYYY MM   LatestDate
---- ---- -----------
2015 01   31
2015 02   27
2015 03   29
2015 04   29
2015 05   30
2015 06   26
2015 07   26
2015 08   26

(8 row(s) affected)

Reformulate the query to get the outout in yyyymmdd format,

with LatestDateCTE
as
(
SELECT SUBSTRING(cast(mydate AS VARCHAR), 1, 4) as YYYY
       ,SUBSTRING(cast(mydate AS VARCHAR), 5, 2) as MM
       ,max(cast(SUBSTRING(cast(mydate AS VARCHAR), 7, 2) AS INT)) as LatestDate
FROM DemoMax
GROUP BY SUBSTRING(cast(mydate AS VARCHAR), 1, 4)
       ,SUBSTRING(cast(mydate AS VARCHAR), 5, 2)
)

select (YYYY + MM + LatestDate) as MyDate from LatestDateCTE

MyDate
-----------
201532
201529
201532
201533
201535
201532
201533
201534

(8 row(s) affected)
 
A cup of tea with SQL Server is the greatest combination :)


Post Reference: Vikram Aristocratic Elfin Share