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.
| Author |
Topic |
|
rds207
Posting Yak Master
198 Posts |
Posted - 2010-05-18 : 13:36:13
|
| HiI have a stored procedure which selects the unique jobname values from a table , below is my store procedure SELECT t.*FROM BatchRecords_TEMP t LEFT JOIN BatchRecords j ON j.Jobname = t.JobnameWHERE j.Jobname IS NULLI just observed that this is selecting the duplicate jobnames , with different cases, i mean if there is a job in upper case i get the same job in lower case also ...Could anybody please suggest me how to i avoid this , i need only unique jobsHere is the example of the jobnames :Here both are same jobs but just the case is different ...M7630AABBQOAAM12091000_bringup_17MAY2010_050738pmM7630AABBQOAAM12091000_BRINGUP_17MAY2010_050738pm |
|
|
tkizer
Almighty SQL Goddess
38200 Posts |
|
|
rds207
Posting Yak Master
198 Posts |
Posted - 2010-05-18 : 13:47:20
|
could you please tell me what is collation?i actually have a jobid associated with each job, even if i change the syntax of the stored procedure toSELECT t.*FROM BatchRecords_TEMP t LEFT outer JOIN BatchRecords j ON j.JobName = t.JobNameWHERE j.JobID is nulli am getting the data with duplicate jobid's I cannot understand why this is happening because i have almost 10-12 stored procedures with same syntax and no procedure allows duplicate values ...except this one ....quote: Originally posted by tkizer You could change the collation to one that's case insensitive or use the UPPER or LOWER function on the data.Tara KizerMicrosoft MVP for Windows Server System - SQL Serverhttp://weblogs.sqlteam.com/tarad/Subscribe to my blog
|
 |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2010-05-18 : 14:03:56
|
quote: Originally posted by rds207 could you please tell me what is collation?i actually have a jobid associated with each job, even if i change the syntax of the stored procedure toSELECT t.*FROM BatchRecords_TEMP t LEFT outer JOIN BatchRecords j ON j.JobName = t.JobNameWHERE j.JobID is nulli am getting the data with duplicate jobid's I cannot understand why this is happening because i have almost 10-12 stored procedures with same syntax and no procedure allows duplicate values ...except this one ....quote: Originally posted by tkizer You could change the collation to one that's case insensitive or use the UPPER or LOWER function on the data.Tara KizerMicrosoft MVP for Windows Server System - SQL Serverhttp://weblogs.sqlteam.com/tarad/Subscribe to my blog
are all the other stored procedure looking for same column? if not, are the other columns having same collation?------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/ |
 |
|
|
rds207
Posting Yak Master
198 Posts |
Posted - 2010-05-18 : 14:14:45
|
No its a different column but similar to jobid , every table has an id on which i am having the above stored procedure , like where jobid is null and it gives me all the new jobid's...from temp(batchrecords_temp)table which are not there in main table (batchrecords), but only this stored procedure is giving me data with duplicate jobid'squote: Originally posted by visakh16
quote: Originally posted by rds207 could you please tell me what is collation?i actually have a jobid associated with each job, even if i change the syntax of the stored procedure toSELECT t.*FROM BatchRecords_TEMP t LEFT outer JOIN BatchRecords j ON j.JobName = t.JobNameWHERE j.JobID is nulli am getting the data with duplicate jobid's I cannot understand why this is happening because i have almost 10-12 stored procedures with same syntax and no procedure allows duplicate values ...except this one ....quote: Originally posted by tkizer You could change the collation to one that's case insensitive or use the UPPER or LOWER function on the data.Tara KizerMicrosoft MVP for Windows Server System - SQL Serverhttp://weblogs.sqlteam.com/tarad/Subscribe to my blog
are all the other stored procedure looking for same column? if not, are the other columns having same collation?------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/
|
 |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2010-05-18 : 14:20:05
|
| may be this is column where you've same data existing in different case------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/ |
 |
|
|
rds207
Posting Yak Master
198 Posts |
Posted - 2010-05-18 : 14:24:08
|
Jobid looks something like : 1746858Thats the reason i have changed the syntax to where jobid is null, that mean it should give only unique jobid right?quote: Originally posted by visakh16 may be this is column where you've same data existing in different case------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/
|
 |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2010-05-18 : 14:25:25
|
| nope. how will it give unique jobid just when you filter NULL values alone. it depends on whether duplicate exist in it. if exists, you need to use distinct or group by------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/ |
 |
|
|
rds207
Posting Yak Master
198 Posts |
Posted - 2010-05-18 : 14:29:02
|
Yes duplicates exists as well as NULLS also ...could you please let me know how do i change the syntax to handle this?quote: Originally posted by visakh16 nope. how will it give unique jobid just when you filter NULL values alone. it depends on whether duplicate exist in it. if exists, you need to use distinct or group by------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/
|
 |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2010-05-18 : 14:34:17
|
| use DISTINCt or GROUP BYlikeSELECT jobIDFROM TableGROUP BY JobID------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/ |
 |
|
|
rds207
Posting Yak Master
198 Posts |
Posted - 2010-05-18 : 14:47:24
|
i changed to below,SELECT distinct t.*FROM BatchRecords_TEMP t LEFT JOIN BatchRecords j ON j.BatchRecordID = t.BatchRecordIDWHERE j.BatchRecordID is null and j.JobName is nullgroup by t.BatchRecordID,t.ApplicationName,t.Archived,t.BatchENV,t.JobName,t.BatchQueueName,t.UserName,t.Site,t.SubmitTime,t.SubmitTimeSecs,t.StartTime,t.StartTimeSecs,t.EndTime,t.EndTimeSecs,t.GMTDiffSecs,t.BatchServerName,t.JobCommandsreceiving error :Msg 306, Level 16, State 2, Line 7The text, ntext, and image data types cannot be compared or sorted, except when using IS NULL or LIKE operator.Msg 421, Level 16, State 1, Line 1The text data type cannot be selected as DISTINCT because it is not comparable.quote: Originally posted by visakh16 use DISTINCt or GROUP BYlikeSELECT jobIDFROM TableGROUP BY JobID------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/
|
 |
|
|
rds207
Posting Yak Master
198 Posts |
Posted - 2010-05-18 : 15:19:52
|
SELECT distinct t.JobName FROM BatchRecords_TEMP t LEFT JOIN BatchRecords j ON j.BatchRecordID = t.BatchRecordIDWHERE j.BatchRecordID is null and j.JobID is nullabove gives me single jobname, but i need to get all columns in select statement ..Please Helpquote: Originally posted by rds207 i changed to below,SELECT distinct t.*FROM BatchRecords_TEMP t LEFT JOIN BatchRecords j ON j.BatchRecordID = t.BatchRecordIDWHERE j.BatchRecordID is null and j.JobName is nullgroup by t.BatchRecordID,t.ApplicationName,t.Archived,t.BatchENV,t.JobName,t.BatchQueueName,t.UserName,t.Site,t.SubmitTime,t.SubmitTimeSecs,t.StartTime,t.StartTimeSecs,t.EndTime,t.EndTimeSecs,t.GMTDiffSecs,t.BatchServerName,t.JobCommandsreceiving error :Msg 306, Level 16, State 2, Line 7The text, ntext, and image data types cannot be compared or sorted, except when using IS NULL or LIKE operator.Msg 421, Level 16, State 1, Line 1The text data type cannot be selected as DISTINCT because it is not comparable.quote: Originally posted by visakh16 use DISTINCt or GROUP BYlikeSELECT jobIDFROM TableGROUP BY JobID------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/
|
 |
|
|
rds207
Posting Yak Master
198 Posts |
Posted - 2010-05-18 : 20:01:40
|
| Could body please assit me with this query ...i am trying to select the records which are not there in batchrecords but which exists in batchrecords_temp ...Here i have duplicate jobnames with unique batchrecordid , so i used over partition by clause to select unique jobnames ordered by batchrecordid , so where ever rownum =1 and if that particular job with rownum =1 is not there is batchrecords then i want that recordi tried below query , but seems to not work ....select * from (select *, row_number() over (partition by JobName order by batchrecordid) as Rownum from BatchRecords_TEMP)xleft join BatchRecords j ON j.BatchRecordID = x.BatchRecordIDwhere x.Rownum = 1 and j.JobID is null---------------------------------------(select *, row_number() over (partition by JobName order by batchrecordid) as Rownum from BatchRecords_TEMP)--------------------------------------------The above part works fine , but not sure how to select new jobs from batchrecords_temp where rownum =1 and which does not exits in batchrecords table.....Please Help |
 |
|
|
Lamprey
Master Smack Fu Yak Hacker
4614 Posts |
|
|
rds207
Posting Yak Master
198 Posts |
Posted - 2010-05-19 : 12:31:33
|
Here is the DDL of 2 tables :CREATE TABLE [dbo].[BatchRecords]( [BatchRecordID] [int] NOT NULL, [JobName] [varchar](160) NOT NULL, [ApplicationName] [varchar](50) NOT NULL, [UserName] [varchar](50) NOT NULL, [Site] [varchar](50) NOT NULL, [SubmitTime] [datetime] NOT NULL, [SubmitTimeSecs] [int] NOT NULL, [StartTime] [datetime] NULL, [StartTimeSecs] [int] NULL, [EndTime] [datetime] NULL, [EndTimeSecs] [int] NULL, [GMTDiffSecs] [int] NOT NULL, [BatchServerName] [varchar](50) NOT NULL, [BatchQueueName] [varchar](50) NOT NULL, [JobCommands] [text] NOT NULL, [JobPath] [varchar](255) NOT NULL, [JobLimit] [int] NOT NULL, [JobCount] [int] NOT NULL, [RunSite] [varchar](50) NULL, [RunGMTDiffSecs] [int] NULL, [HoldFlag] [bit] NOT NULL, [DayLightFlag] [bit] NOT NULL, [BatchStatus] [varchar](15) NULL, [Archived] [bit] NULL, [ArchiveDate] [datetime] NULL, [BatchENV] [varchar](50) NULL, [JobID] [varchar](50) NULL, [JobStatus] [varchar](50) NULL, [PoolName] [varchar](50) NULL, [PoolSize] [varchar](50) NULL, [PoolUsed] [varchar](50) NULL, [TerminationUser] [varchar](50) NULL, [TerminationHost] [varchar](50) NULL) ON [PRIMARY] TEXTIMAGE_ON [PRIMARY]Table 2:CREATE TABLE [dbo].[BatchRecords_TEMP]( [BatchRecordID] [int] NOT NULL, [JobName] [varchar](160) NOT NULL, [ApplicationName] [varchar](50) NOT NULL, [UserName] [varchar](50) NOT NULL, [Site] [varchar](50) NOT NULL, [SubmitTime] [datetime] NOT NULL, [SubmitTimeSecs] [int] NOT NULL, [StartTime] [datetime] NULL, [StartTimeSecs] [int] NULL, [EndTime] [datetime] NULL, [EndTimeSecs] [int] NULL, [GMTDiffSecs] [int] NOT NULL, [BatchServerName] [varchar](50) NOT NULL, [BatchQueueName] [varchar](50) NOT NULL, [JobCommands] [text] NOT NULL, [JobPath] [varchar](255) NOT NULL, [JobLimit] [int] NOT NULL, [JobCount] [int] NOT NULL, [RunSite] [varchar](50) NULL, [RunGMTDiffSecs] [int] NULL, [HoldFlag] [bit] NOT NULL, [DayLightFlag] [bit] NOT NULL, [BatchStatus] [varchar](15) NULL, [Archived] [bit] NULL, [ArchiveDate] [datetime] NULL, [BatchENV] [varchar](50) NULL, [JobID] [varchar](50) NULL, [JobStatus] [varchar](50) NULL, [PoolName] [varchar](50) NULL, [PoolSize] [varchar](50) NULL, [PoolUsed] [varchar](50) NULL, [TerminationUser] [varchar](50) NULL, [TerminationHost] [varchar](50) NULL, [RowNum] [int] IDENTITY(1,1) NOT NULL) ON [PRIMARY] TEXTIMAGE_ON [PRIMARY]The Stored Procedure i have been using to insert data into bathcrecords from batchrecords_temp , but this is giving me duplicate jobnames, as batchrecordid is a unique row and each jobname has a unique batchrecordid...What i need is unique jobnames .........Expected Results :If the data in batchrecords_temp is , JOBNAME BATCHRECORDIDM7630AABBQOAAM12091000_bringup_17MAY2010_050738pm 452369M7630AABBQOAAM12091000_BRINGUP_17MAY2010_050738pm 452370Expected output is (not really specific about the case , i just need one jobname)M7630AABBQOAAM12091000_bringup_17MAY2010_050738pm 452369What i have tried :This statement assignes a rownum to the jobs and i want to select the jobname with rownum as 1 , But the below statement does not work,select * from (select *, row_number() over (partition by JobName order by batchrecordid) as Rownum from BatchRecords_TEMP)xleft join BatchRecords j ON j.BatchRecordID = x.BatchRecordIDwhere x.Rownum = 1 and j.JobID is nullThis statement(select *, row_number() over (partition by JobName order by batchrecordid) as Rownum from BatchRecords_TEMP), works fine for assigning rownum but i cannot understand how to select the all columns from temp table with one jobname SELECT t.*FROM BatchRecords_TEMP t LEFT JOIN BatchRecords j ON j.BatchRecordID = t.BatchRecordIDWHERE j.BatchRecordID is nullquote: Originally posted by Lamprey Please post DDL, DML and Expected output. This link will help you to provide that information so we can help you better:http://weblogs.sqlteam.com/brettk/archive/2005/05/25/5276.aspx
|
 |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2010-05-19 : 12:41:30
|
quote: Originally posted by rds207 i changed to below,SELECT distinct t.*FROM BatchRecords_TEMP t LEFT JOIN BatchRecords j ON j.BatchRecordID = t.BatchRecordIDWHERE j.BatchRecordID is null and j.JobName is nullgroup by t.BatchRecordID,t.ApplicationName,t.Archived,t.BatchENV,t.JobName,t.BatchQueueName,t.UserName,t.Site,t.SubmitTime,t.SubmitTimeSecs,t.StartTime,t.StartTimeSecs,t.EndTime,t.EndTimeSecs,t.GMTDiffSecs,t.BatchServerName,t.JobCommandsreceiving error :Msg 306, Level 16, State 2, Line 7The text, ntext, and image data types cannot be compared or sorted, except when using IS NULL or LIKE operator.Msg 421, Level 16, State 1, Line 1The text data type cannot be selected as DISTINCT because it is not comparable.quote: Originally posted by visakh16 use DISTINCt or GROUP BYlikeSELECT jobIDFROM TableGROUP BY JobID------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/
you cant take DISTINCT on above fields. for taking DISTINCT try casting them to varchar(max),nvarchar(max),..------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/ |
 |
|
|
rds207
Posting Yak Master
198 Posts |
Posted - 2010-05-19 : 12:43:36
|
Hey , I just tried the below statement , which gives me unique jobnames , Now how do i select new jobnames which are not there in batchrecords and there in batchrecords_temp?;with cte as (SELECT BatchRecordID,JobName,ApplicationName,USERNAME,site,submittime,SubmitTimeSecsstarttime,StartTimeSecs,EndTime,EndTimeSecs,GMTDiffSecs,BatchServerName,BatchQueueName,JobCommands,JobPath,JobLimit,jobcount,runsite,rungmtdiffsecs,holdflag,daylightflag,batchstatus,archived,archivedate,batchenv,jobid,jobstatus,poolname,poolsize,poolused,terminationuser,TerminationHost, row_number() over( PARTITION BY JOBNAME ORDER BY batchrecordid) as RowNumByJobName FROM BatchRecords_TEMP)select * from cte where RowNumByJobName = 1quote: Originally posted by visakh16
quote: Originally posted by rds207 i changed to below,SELECT distinct t.*FROM BatchRecords_TEMP t LEFT JOIN BatchRecords j ON j.BatchRecordID = t.BatchRecordIDWHERE j.BatchRecordID is null and j.JobName is nullgroup by t.BatchRecordID,t.ApplicationName,t.Archived,t.BatchENV,t.JobName,t.BatchQueueName,t.UserName,t.Site,t.SubmitTime,t.SubmitTimeSecs,t.StartTime,t.StartTimeSecs,t.EndTime,t.EndTimeSecs,t.GMTDiffSecs,t.BatchServerName,t.JobCommandsreceiving error :Msg 306, Level 16, State 2, Line 7The text, ntext, and image data types cannot be compared or sorted, except when using IS NULL or LIKE operator.Msg 421, Level 16, State 1, Line 1The text data type cannot be selected as DISTINCT because it is not comparable.quote: Originally posted by visakh16 use DISTINCt or GROUP BYlikeSELECT jobIDFROM TableGROUP BY JobID------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/
you cant take DISTINCT on above fields. for taking DISTINCT try casting them to varchar(max),nvarchar(max),..------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/
|
 |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2010-05-19 : 12:44:43
|
quote: Originally posted by rds207select * from (select *, row_number() over (partition by JobName order by batchrecordid) as Rownum from BatchRecords_TEMP)xleft join BatchRecords j ON j.BatchRecordID = x.BatchRecordIDwhere x.Rownum = 1 and j.JobID is nullThis statement(select *, row_number() over (partition by JobName order by batchrecordid) as Rownum from BatchRecords_TEMP), works fine for assigning rownum but i cannot understand how to select the all columns from temp table with one jobname
the query looks fine. why it didnt work for you? what was the error?------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/ |
 |
|
|
rds207
Posting Yak Master
198 Posts |
Posted - 2010-05-19 : 12:53:47
|
I am not getting any error , i am getting the data where the jobid is null , that means i have data where jobid is NULL , what i was expecting was new jobid 'squote: Originally posted by visakh16
quote: Originally posted by rds207select * from (select *, row_number() over (partition by JobName order by batchrecordid) as Rownum from BatchRecords_TEMP)xleft join BatchRecords j ON j.BatchRecordID = x.BatchRecordIDwhere x.Rownum = 1 and j.JobID is nullThis statement(select *, row_number() over (partition by JobName order by batchrecordid) as Rownum from BatchRecords_TEMP), works fine for assigning rownum but i cannot understand how to select the all columns from temp table with one jobname
the query looks fine. why it didnt work for you? what was the error?------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/
|
 |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2010-05-19 : 13:00:21
|
quote: Originally posted by rds207 I am not getting any error , i am getting the data where the jobid is null , that means i have data where jobid is NULL , what i was expecting was new jobid 'squote: Originally posted by visakh16
quote: Originally posted by rds207select * from (select *, row_number() over (partition by JobName order by batchrecordid) as Rownum from BatchRecords_TEMP)xleft join BatchRecords j ON j.BatchRecordID = x.BatchRecordIDwhere x.Rownum = 1 and j.JobID is nullThis statement(select *, row_number() over (partition by JobName order by batchrecordid) as Rownum from BatchRecords_TEMP), works fine for assigning rownum but i cannot understand how to select the all columns from temp table with one jobname
the query looks fine. why it didnt work for you? what was the error?------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/
but you're checking JobID iS NULL in where filter. then you will definitely get the ones with no match in BatchRecords table------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/ |
 |
|
|
Next Page
|
|
|
|
|