SQL SERVER – DENSE_RANK() – ORDER BY SOME COLUMN BUT WE SHOULD ADD WHERE FILTER WHILE ASSIGNING RANK

Just another day I received following question from my friend and I find it very interesting. I decided to try it out on SQL Server. The question was – “Can we apply Dense_Rank function order by some Column but we should add where filter while assigning rank? ”





Interesting, isn’t it? So i took this as challenge and try to resolve this requrement as puzzle . So i took below small piece of paper and done some pseudo code. Then developed coding to get the final output

             


 --drop table #TEMPCASE  
 DECLARE  @StudentDetail TABLE  
  (    
    -- ID int identity(1,1),  
     StudentCode VARCHAR(10),  
     SchoolCode  VARCHAR(10),  
     StartDate DATE  
   )  
 INSERT INTO @StudentDetail VALUES ('C00001', 'P00001', '1-Sep-2014')  
 INSERT INTO @StudentDetail VALUES ('C00001', 'P00002', '1-Oct-2014')  
 INSERT INTO @StudentDetail VALUES ('C00001', 'P00003', '10-Oct-2014')  
 INSERT INTO @StudentDetail VALUES ('C00001', 'P00004', '25-Oct-2015')  
 INSERT INTO @StudentDetail VALUES ('C00002', 'P00001', '1-Oct-2014')  
 INSERT INTO @StudentDetail VALUES ('C00002', 'P00002', '1-Nov-2014')  
 INSERT INTO @StudentDetail VALUES ('C00002', 'P00001', '1-Oct-2015')  
 INSERT INTO @StudentDetail VALUES ('C00002', 'P00002', '1-Nov-2015')  
 INSERT INTO @StudentDetail VALUES ('C00003', 'P00002', '1-Nov-2015')  
 select * from @StudentDetail  
 select ROW_NUMBER() over(order by (StudentCode)) as ID,  
 ROW_NUMBER() over(partition by StudentCode order by StartDate) as RowID,cast(Null as int) as RankID,*   
 INTO #TEMPCASE from @StudentDetail  
 order by 1   
 --SELECT * FROM #TEMPCASE  
 declare @i int=1,@count int=(select MAX(RowID) from #TEMPCASE)  
 declare @RankID int  
 while @i<=@count  
  Begin  
   set @RankID=(select MAX(isnull(RankID,0)) from #TEMPCASE)  
           --IF exists(SELECT 1 FROM #TEMPCASE where RankID is null)   
           --      CONTINUE   
           --  ELSE  
           --      BREAK  
   update tt  
   set RankID=@RankID+t.RankID  
   from #TEMPCASE tt  
   join (  
   select *,DENSE_RANK() over(order by StudentCode) as RankID  
   from   
   (  
   select distinct t1.ID,t1.StudentCode from #TEMPCASE t1  
   join #TEMPCASE t2  
   on t1.StudentCode=t2.StudentCode  
   where t1.RankID is null and t2.RankID is null and t1.RowID=1 and t2.RowID>1  
   and DATEDIFF(dd,t1.StartDate,t2.StartDate)<=90  
   union all  
   select distinct t2.ID,t2.StudentCode from #TEMPCASE t1  
   join #TEMPCASE t2  
   on t1.StudentCode=t2.StudentCode  
   where t1.RankID is null and t2.RankID is null and t1.RowID=1 and t2.RowID>1  
   and DATEDIFF(dd,t1.StartDate,t2.StartDate)<=90  
   union all  
   select distinct t1.ID,t1.StudentCode from #TEMPCASE t1  
   where t1.RankID is null and not exists (select 1 from #TEMPCASE t2 where t1.StudentCode=t2.StudentCode   
   and t2.RankID is null  
   group by t2.StudentCode having COUNT(*)>1)  
  ) t  
  ) t on tt.ID=t.ID  
  update t1  
  set RowID=t.RowID  
  from #TEMPCASE t1  
  join(  
  select ID, StudentCode,ROW_NUMBER() over(PARTITION by StudentCode order by StartDate ) as RowID from #TEMPCASE  
  where RankID is null  
  ) t  
  on t.ID=t1.ID  
  set @i=@i+1  
  End  
 SELECT * FROM #TEMPCASE  



SQL SERVER – SEQUENTIAL NUMBERS FAST LOAD TO TABLE

Problem
I need to load very fastly some sample data to database table with Sequential Numbers. We can load Sequential Numbers with Loops but it is a performance issue because it will load one record in one iteration . What is an alternative for loops to load Sequential Numbers data into table ?

Solution
By using iteratative Common table exepresion(CTE) , we can able to get the results very fastly . In the below query every CTE will sequres the amount of result come from previous CTE results.

CREATE TABLE #T1(Id int NOT NULL PRIMARY KEY, X INT NOT NULL)

CREATE INDEX Ix ON #T1 (X, Id)

;WITH 
    Pass0 AS (SELECT 1 AS C UNION ALL SELECT 1), --2 rows 
    Pass1 AS (SELECT 1 AS C FROM Pass0 AS A, Pass0 AS B),--4 rows 
    Pass2 AS (SELECT 1 AS C FROM Pass1 AS A ,Pass1 AS B),--16 rows
    Pass3 AS (SELECT 1 AS C FROM Pass2 AS A ,Pass2 AS B),--256 rows
    Pass4 AS (SELECT 1 AS C FROM Pass3 AS A ,Pass3 AS B),--65536 rows
    Pass5 AS (SELECT 1 AS C FROM Pass4 AS A ,Pass2 AS B),--1048576 rows
    Cte AS (SELECT  ROW_NUMBER() OVER (ORDER BY C) AS Number FROM   Pass5)
INSERT INTO #T1
SELECT Number, Number
FROM  Cte
WHERE Number <= 100000

SQL SERVER – HOW TO DELETE ALL TABLES DATA EXCEPT TOP N RECORDS IN EACH TABLE?

I always likes logical work than regular boring work. So when I am browsing on Google, I found some interesting question like how to delete all tables’ data in SQL Server except top N records in each table?

Here is the script for delete all tables data SQL Server DB except top N records in each table

 
Note:

Considering all tables do not have any inter dependency on each other


SQL SERVER -- SQL SERVER 2016 WHAT'S NEW ?

1      Query Store
a)      Captures queries, query plans, runtime statistics, etc.
b)      Can enforce polices to direct SQL Server Query processor to execute in specific manner
c)      Contains query work load history
d)      Helps system-wide or database level performance analysis and troubleshooting
e)      New system views are added to support Query store

2      Live Query Statistics
a)      Live Query plans with system resources consumption such as CPU, memory for all running queries in the plan
b)      You can drill down further into live running queries from its query plan
c)      Helps you watch change of statistics during query execution

SSIS – CONFIGURE ENVIRONMENTS AND ENVIRONMENT VARIABLES

As we see above, we can create folder in SSISDB. Each folder we create, it will automatically create two subfolders called Projects andEnvironments 
Projects: A place holder for deployed projects inside Folder
Environments: A placed holder for environments like DEV and QA
To create an environment, right click on the Environments subfolder and click on Create Environment menu item as shown below:




















SSIS – DEPLOY DEVELOPED SSIS PROJECT TO INTEGRATION SERVICES CATALOG

After development of our SSIS package to SSISDB, to deploy SSIS Project,just right click on project file and choose Deploy

















SSIS – DEVELOP SIMPLE SSIS PROJECT

SSIS Project creation remains same from SSIS 2008 to SSIS 2012. To create SSIS project file, we need to open BIDS and choose Integration services .If you are new to SSIS , please go through basis of SSIS project creation and package development
In my example,  I have two similar tables (CountryDetails) in two different databases (SampleDataBase1, SampleDataBase2).  I created one simple package, logic is to insert one row to CountryDetails table in either of SampleDataBase1, SampleDataBase2 databases based on environment configuration

Table Structure:

CREATE TABLE [dbo].[CountryDetails](
       [ID] [float] NULL,
       [StateProvinceCode] [nvarchar](255) NULL,
       [CountryRegionCode] [nvarchar](255) NULL,
       [IsOnlyStateProvinceFlag] [float] NULL,
       [Name] [nvarchar](255) NULL,
       [TerritoryID] [float] NULL

) ON [PRIMARY]

SSIS – DEPLOYED SSIS PROJECTS VERSIONS AND RESTORE TO PREVIOUS VERSION

Good thing in Project Deployment Model is we can store multiple versions of SSIS projects inside SSISDB. And also if we want to rollback to previous versions of deployed packages, we can simply restore to desired version. To see the previous versions of deployed projects, just right click on project and choose Versions 

























SSIS – REDEPLOYING THE PROJECT TO INTEGRATION SERVICES CATALOG

Nothing is constant ,we usually gets so many change requests in our process with respect to Functionality, Performance and  fix fixes .So we need to change our Packages according to new requirements.  Once we changed our SSIS packages in Project and if we want to redeploy SSIS packages to same location in Integration Services Catalog, just right click on Project and choose Deploy option. Redeploying Packages is not different from deploying SSIS packages first Time. 





















SSIS – VALIDATE THE DEPLOYED PROJECT OR PACKAGE

Once we deploy the SSIS Packages to Integration Services Catalog, we need to validate before we execute our packages to ensure that they will execute properly. In validation, it will show list of errors if we have any issues with our packages


SSIS – HOW TO OPEN .ISPAC IN BIDS (VISUAL STUDIO)

Sometimes we need to edit the deployed SSIS packages, so to edit we need to open deployed .ispac in BIDS. We can open .ispac in two ways by using integration services import project wizard

SSIS – EXECUTE DEPLOYED PROJECT/PACKAGE WITH THE ENVIRONMENT

Once we develop and deployed SSIS project to Integration Services Catalog, configured environment/environments variables and given reference for environments. We need to unit test by executing deployed packages, whether our environments variables inside environments are consuming or not. 

SSIS – CONFIGURE ENVIRONMENT REFERENCE IN THE DEPLOYED PROJECT

We have configured Integration Services Catalog and deployed project and created two environments. Now we need to make relation between environments and deployed projects. To do that, right click on deployed project and choose configure 
















SSIS – PROJECT DEPLOYMENT MODEL

As a MSBI developer, I always enjoys while deploying of SSRS reports and SSAS cubes but not in case of SSIS packages deployment because we have project level single click deployment in SSRS and SSAS, here our life is so easy but I always feel difficult with  SSIS packages legacy deployment and management of SSIS packages. There we need to deploy SSIS packages individually either in file system or system database MSDB
Good News for SSIS Developers is, in SQL server 2012 Microsoft introduced SSIS Package Deployment Model i.e. single click project deployment. Also it introduced new specific database for only SSIS Package Deployment called SSISDB. Now in this topic I cover what it is SSIS Package Deployment Model and how to deploy your SSIS packages to SSISDB

SQL SERVER – HOW TO UPDATE SQL JOB STEP SCRIPT DYNAMICALLY

Generally we create SQL Server Jobs to execute some SQL scripts, SSIS packages or power shell commands when user calls job manually or call automatically by Scheduler. Sometimes we need to change SQL server Job steps script dynamically based on some dynamic values. Now let’s see, how to change SQL server Job steps command dynamically
To show this, I have taken example of SQL Job step with T-SQL Command. Before this I have created one simple stored procedure in ADVENTUREWORKSDW2012 DB to insert values into DIMPRODUCT table.

SQL SERVER – HOW TO DELETE ALL TABLES DATA

By using below script we can able to delete all tables data in SQL SERVER without having any issues. In the script I have followed below steps

·         Disable all constraints on all tables. Due to that we can delete all tables without having any constraints issues by using system stored procedure sp_MSForEachTable
·         Disable all TRIGGERs on all tables
·         Delete all tables data
·         If any tables having identity, we need to set identity to 0
·         Once we delete all tables data , we will enable all CONSTRAINTS
·         Once we delete all tables data , we will enable all TRIGGERs

SQL SERVER – SEARCH RECORDS WHICH ARE SEPARATED WITH PIPE DELIMITER


Today I got phone call from one of my friend regarding some query to fetch records from one table let's called Person table and column Role  .But Column Role having multiple values separated with comma delimiter . Now he need to find records where input value matches any value in column Role from Person table. Then I have given one stupid answer with long query like by split the record into multiple records based on delimiter and then search record based on input and then club the record. Finally his requirement meets and he is happy but I am very upset with that big query .Then I try to write get same result with some different logic like below.

SQL SERVER – DATABASE MAIL ENABLE AND CONFIGURING USING T-SQL

Today I got requirement like after my ETL packages execution I need to send one email to team regarding what is status of package execution and how many records got success and fail . To do that I decided to use database mail component in SQL Server. We can configure database mail by two ways
1. Using GUI
2. Using SQL Script 
In Previous post Configure DB Mail using GUI, we have seen how to configure DB Mail using GUI. Now we see how to configure using T-SQL

SQL SERVER – DATABASE MAIL ENABLE AND CONFIGURING USING GUI

Today I got requirement like after my ETL packages execution I need to send one email to team regarding what is status of package execution and how many records got success and fail . To do that I decided to use database mail component in SQL Server. We can configure database mail by two ways
1. Using GUI
2. Using SQL Script

SQL SERVER – HOW TO DISABLE AND REBUILD ALL INDEXES IN TABLE


Some time while loading data to tables due to having of indexes, data population makes slow. So while loading data to table we need to disable non clustered indexes and after loading of data we need to rebuild indexes.

Below script will disable existing indexes and rebuild with the parameter of table name

www.CodeNirvana.in

Powered by Blogger.

Translate

Total Pageviews

Copyright © Journey to MSBI World With Kiran Reddy | Blogger Templates | Designed By Code Nirvana