| Author |
Topic |
|
JJ297
Aged Yak Warrior
940 Posts |
Posted - 2011-09-21 : 09:48:47
|
This statement works until I get to the else section. If you are in 'S41' then the else statement runs as well and shows all of the S41.So both of these are running:if @doc in ('V01', 'S67', 'S41', 'S66')Begin select * from pendingddswhere doc in ('V01', 'S67', 'S41', 'S66')EndelseSelect FO, DOC, Clm, COS, ddsrcpt, FileDate = max(FileDate), Age = Datediff(day,max(filedate), getdate()), Title = case when min(Title) <> max(Title) then 'Concurr' else min(Title) endFrom pendingddswhere doc =@docGroup By FO, DOC, CLM, COS, ddsrcptHere's the entire query:@doc varchar(3)asif @doc = 'ALL' beginSelect FO, DOC, Clm, COS,ddsrcpt, FileDate = max(FileDate), Age = Datediff(day,max(filedate), getdate()), Title = case when min(Title) <> max(Title) then 'Concurr' else min(Title) endFrom pendingddswhere doc in ('S09', 'S0B','S0L','S11', 'S23', 'S41', 'S66','S67','S10','S53','S92','S93','S55','S91')Group By FO, DOC, CLM, COS, ddsrcptendif @doc in ('V01', 'S67', 'S41', 'S66')Begin select * from pendingddswhere doc in ('V01', 'S67', 'S41', 'S66')End if @doc in ('S88', 'S53', 'S10', 'S92', 'S93','S0L')Beginselect * from pendingddswhere doc in ('S88', 'S53', 'S10', 'S92', 'S93','S0L')end if @doc in ('V12', 'S55', 'S91')Beginselect * from pendingddswhere doc in ('V12', 'S55', 'S91')end elseSelect FO, DOC, Clm, COS, ddsrcpt, FileDate = max(FileDate), Age = Datediff(day,max(filedate), getdate()), Title = case when min(Title) <> max(Title) then 'Concurr' else min(Title) endFrom pendingddswhere doc =@docGroup By FO, DOC, CLM, COS, ddsrcptHow do I get it to only run the first statement? If it's too confusing to understand I will send some table info.Thanks! |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2011-09-21 : 10:21:54
|
| better to show how you populate @docno------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/ |
 |
|
|
rajarajan
Starting Member
48 Posts |
Posted - 2011-09-21 : 10:31:39
|
| ITS JUST SAMPLE LET ME KNOW YOUR THOUGHTS declare @doc varchar (80)set @doc ='s41'if @doc in ('V01', 'S67', 'S41', 'S66')Begin select Getdate()EndelsebeginSELECT CONVERT(VARCHAR(10),GETDATE() ,101)END |
 |
|
|
JJ297
Aged Yak Warrior
940 Posts |
Posted - 2011-09-21 : 11:45:53
|
when you run all of the below you will see how S41 produces two results. I only want it to come out in the if statement not the else.Here's the table and insert info:CREATE TABLE [dbo].[PDTest]( [fo] [varchar](3) NOT NULL, [doc] [varchar](4) NOT NULL, [clms] [char](6) NOT NULL, [cos] [char](6) NOT NULL, [FileDate] [char](30) NULL, [DDSRcpt] [char](30) NULL, [Title] [varchar](3) NOT NULL)insert into PDTestselect '007', 'S67', '193568', '193568', '09/02/2010', '09/02/2010', 'T16' union allselect '008', 'S67', '569842', '569842', '10/27/2010', '09/14/2010', 'T2' union allselect '031', 'S66', '166789', '166789', '12/30/2010', '12/21/2010', 'T2' union allselect '111', 'S67', '597326', '597326', '08/05/2011', '12/30/2010', 'T2' union allselect '111', 'S67', '120569', '120569', '05/03/2011', '12/06/2010', 'T16' union allselect '172', 'S66', '199546', '199546', '02/09/2011','11/05/2010', 'T16' union allselect '172', 'S66', '175896', '175896', '07/14/2011', '01/05/2011', 'T2' union allselect '194', 'S66', '055896', '055896', '03/10/2011', '01/04/2011', 'T2' union allselect '203', 'S41', '046895', '046895', '09/02/2011', '10/01/2010', 'T16' union allselect '203', 'S41', '178569', '178569', '07/13/2011', '08/05/2010', 'T2' union allselect '203', 'S41', '199325', '199325', '10/05/2010', '08/10/2010', 'T16' union allselect '204', 'S41', '019865', '019865', '05/18/2011', '12/15/2010', 'T2' union allselect '208', 'S41', '184569', '184569', '01/05/2011', '01/03/2011', 'T2' union allselect '208', 'S41', '206458', '206458', '08/24/2011', '08/22/2010', 'T16' union allselect '220', 'S41', '164566', '164566', '01/03/2011', '12/29/2010', 'T16' Then run this query and use S41 so you will see what I mean:create procedure ddstest1 --'S41'@doc varchar(3)asif @doc = 'ALL' beginSelect FO, DOC, Clms, COS,ddsrcpt, FileDate = max(FileDate), Age = Datediff(day,max(filedate), getdate()), Title = case when min(Title) <> max(Title) then 'Concurr' else min(Title) endFrom pdtestwhere doc in ('S09', 'S0B','S0L','S11', 'S23', 'S41', 'S66','S67','S10','S53','S92','S93','S55','S91')Group By FO, DOC, CLMs, COS, ddsrcptendif @doc in ('V01', 'S67', 'S41', 'S66')Begin Select FO, DOC, Clms, COS, ddsrcpt, FileDate = max(FileDate), Age = Datediff(day,max(filedate), getdate()), Title = case when min(Title) <> max(Title) then 'Concurr' else min(Title) endfrom pdtestwhere doc in ('V01', 'S67', 'S41', 'S66')Group By FO, DOC, CLMs, COS, ddsrcptEnd if @doc in ('S88', 'S53', 'S10', 'S92', 'S93','S0L')BeginSelect FO, DOC, Clms, COS, ddsrcpt, FileDate = max(FileDate), Age = Datediff(day,max(filedate), getdate()), Title = case when min(Title) <> max(Title) then 'Concurr' else min(Title) endfrom pdtestwhere doc in ('S88', 'S53', 'S10', 'S92', 'S93','S0L')Group By FO, DOC, CLMs, COS, ddsrcptend if @doc in ('V12', 'S55', 'S91')BeginSelect FO, DOC, Clms, COS, ddsrcpt, FileDate = max(FileDate), Age = Datediff(day,max(filedate), getdate()), Title = case when min(Title) <> max(Title) then 'Concurr' else min(Title) endfrom pdtestwhere doc in ('V12', 'S55', 'S91')Group By FO, DOC, CLMs, COS, ddsrcptend elseSelect FO, DOC, Clms, COS, ddsrcpt, FileDate = max(FileDate), Age = Datediff(day,max(filedate), getdate()), Title = case when min(Title) <> max(Title) then 'Concurr' else min(Title) endFrom pdtestwhere doc =@docGroup By FO, DOC, CLMs, COS, ddsrcptThanks!Rajarajan that wouldn't work because I have several other numbers that would go in the else statement. |
 |
|
|
ehorn
Master Smack Fu Yak Hacker
1632 Posts |
Posted - 2011-09-21 : 12:21:40
|
| One option would be to convert the [ELSE] statement to an [IF] statement with a NOT IN filter which comprises all the previous conditions.HTH. |
 |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2011-09-21 : 12:22:57
|
the reason is simple. you've add them as individual ifs so else is coupled with last if and since that if evaluates to false its getting executed causing second resultset. just do small modification as below and seecreate procedure ddstest1 --'S41'@doc varchar(3)asif @doc = 'ALL' beginSelect FO, DOC, Clms, COS,ddsrcpt, FileDate = max(FileDate), Age = Datediff(day,max(filedate), getdate()), Title = case when min(Title) <> max(Title) then 'Concurr' else min(Title) endFrom pdtestwhere doc in ('S09', 'S0B','S0L','S11', 'S23', 'S41', 'S66','S67','S10','S53','S92','S93','S55','S91')Group By FO, DOC, CLMs, COS, ddsrcptendelse if @doc in ('V01', 'S67', 'S41', 'S66')Begin Select FO, DOC, Clms, COS, ddsrcpt, FileDate = max(FileDate), Age = Datediff(day,max(filedate), getdate()), Title = case when min(Title) <> max(Title) then 'Concurr' else min(Title) endfrom pdtestwhere doc in ('V01', 'S67', 'S41', 'S66')Group By FO, DOC, CLMs, COS, ddsrcptEndelse if @doc in ('S88', 'S53', 'S10', 'S92', 'S93','S0L')BeginSelect FO, DOC, Clms, COS, ddsrcpt, FileDate = max(FileDate), Age = Datediff(day,max(filedate), getdate()), Title = case when min(Title) <> max(Title) then 'Concurr' else min(Title) endfrom pdtestwhere doc in ('S88', 'S53', 'S10', 'S92', 'S93','S0L')Group By FO, DOC, CLMs, COS, ddsrcptend else if @doc in ('V12', 'S55', 'S91')BeginSelect FO, DOC, Clms, COS, ddsrcpt, FileDate = max(FileDate), Age = Datediff(day,max(filedate), getdate()), Title = case when min(Title) <> max(Title) then 'Concurr' else min(Title) endfrom pdtestwhere doc in ('V12', 'S55', 'S91')Group By FO, DOC, CLMs, COS, ddsrcptend elseSelect FO, DOC, Clms, COS, ddsrcpt, FileDate = max(FileDate), Age = Datediff(day,max(filedate), getdate()), Title = case when min(Title) <> max(Title) then 'Concurr' else min(Title) endFrom pdtestwhere doc =@docGroup By FO, DOC, CLMs, COS, ddsrcpt------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/ |
 |
|
|
JJ297
Aged Yak Warrior
940 Posts |
Posted - 2011-09-21 : 13:35:48
|
| Wow thanks so much!!! So putting the else in front of the if makes them all on statement. Thanks for explaning too! |
 |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2011-09-21 : 13:38:31
|
| yep..that will do the trickwelcome------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/ |
 |
|
|
JJ297
Aged Yak Warrior
940 Posts |
Posted - 2011-09-21 : 14:19:52
|
Thanks again!I'm back...thought I was done huh Concurr isn't working for S67Add these four records to the existing table:insert into PDTestselect '007', 'S67', '193444', '193444', '09/02/2010', '09/02/2010', 'T16' union allselect '007', 'S67', '193444', '193444', '08/18/2010', '09/02/2010', 'T2' union allselect 'C85', 'S66', '094689', '094689', '01/07/2011', '01/07/2011', 'T16' union allselect 'C85', 'S66', '094689', '094689', '12/13/2010', '12/15/2010', 'T2' Then run this stored procedure and go down to the bottom of the table and look how C85 Title should be Concurr.If you run S67 (as the doc instead of S57) you will see it comes up as concurr alter procedure ddstest1 --'S67'@doc varchar(3)asif @doc = 'ALL' beginSelect FO, DOC, Clms, COS,ddsrcpt, FileDate = max(FileDate), Age = Datediff(day,max(filedate), getdate()), Title = case when min(Title) <> max(Title) then 'Concurr' else min(Title) endFrom pdtestwhere doc in ('S09', 'S0B','S0L','S11', 'S23', 'S41', 'S66','S67','S10','S53','S92','S93','S55','S91')Group By FO, DOC, CLMs, COS, ddsrcptendelse if @doc in ('V01', 'S67', 'S41', 'S66')Begin Select FO, DOC, Clms, COS, ddsrcpt, FileDate = max(FileDate), Age = Datediff(day,max(filedate), getdate()), Title = case when min(Title) <> max(Title) then 'Concurr' else min(Title) endfrom pdtestwhere doc in ('V01', 'S67', 'S41', 'S66')Group By FO, DOC, CLMs, COS, ddsrcptEndelse if @doc in ('S88', 'S53', 'S10', 'S92', 'S93','S0L')BeginSelect FO, DOC, Clms, COS, ddsrcpt, FileDate = max(FileDate), Age = Datediff(day,max(filedate), getdate()), Title = case when min(Title) <> max(Title) then 'Concurr' else min(Title) endfrom pdtestwhere doc in ('S88', 'S53', 'S10', 'S92', 'S93','S0L')Group By FO, DOC, CLMs, COS, ddsrcptend else if @doc in ('V12', 'S55', 'S91')BeginSelect FO, DOC, Clms, COS, ddsrcpt, FileDate = max(FileDate), Age = Datediff(day,max(filedate), getdate()), Title = case when min(Title) <> max(Title) then 'Concurr' else min(Title) endfrom pdtestwhere doc in ('V12', 'S55', 'S91')Group By FO, DOC, CLMs, COS, ddsrcptend elseSelect FO, DOC, Clms, COS, ddsrcpt, FileDate = max(FileDate), Age = Datediff(day,max(filedate), getdate()), Title = case when min(Title) <> max(Title) then 'Concurr' else min(Title) endFrom pdtestwhere doc =@docGroup By FO, DOC, CLMs, COS, ddsrcpt |
 |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2011-09-21 : 22:25:12
|
C85 Title wont be Concurr as its max(title) and min(title) are same. Thats because you've included ddsrcpt also in grouping and its values are different. use modified code below and see the changealter procedure ddstest1 --'S67'@doc varchar(3)asif @doc = 'ALL' beginSelect FO, DOC, Clms, COS,ddsrcpt, FileDate = max(FileDate), Age = Datediff(day,max(filedate), getdate()), Title = case when min(Title) <> max(Title) then 'Concurr' else min(Title) endFrom pdtestwhere doc in ('S09', 'S0B','S0L','S11', 'S23', 'S41', 'S66','S67','S10','S53','S92','S93','S55','S91')Group By FO, DOC, CLMs, COS, ddsrcptendelse if @doc in ('V01', 'S67', 'S41', 'S66')Begin Select FO, DOC, Clms, COS, -- ddsrcpt, FileDate = max(FileDate), Age = Datediff(day,max(filedate), getdate()),min(Title),max(Title), Title = case when min(Title) <> max(Title) then 'Concurr' else min(Title) endfrom pdtestwhere doc in ('V01', 'S67', 'S41', 'S66')Group By FO, DOC, CLMs, COS--, ddsrcptEndelse if @doc in ('S88', 'S53', 'S10', 'S92', 'S93','S0L')BeginSelect FO, DOC, Clms, COS, ddsrcpt, FileDate = max(FileDate), Age = Datediff(day,max(filedate), getdate()), Title = case when min(Title) <> max(Title) then 'Concurr' else min(Title) endfrom pdtestwhere doc in ('S88', 'S53', 'S10', 'S92', 'S93','S0L')Group By FO, DOC, CLMs, COS, ddsrcptend else if @doc in ('V12', 'S55', 'S91')BeginSelect FO, DOC, Clms, COS, ddsrcpt, FileDate = max(FileDate), Age = Datediff(day,max(filedate), getdate()), Title = case when min(Title) <> max(Title) then 'Concurr' else min(Title) endfrom pdtestwhere doc in ('V12', 'S55', 'S91')Group By FO, DOC, CLMs, COS, ddsrcptend elseSelect FO, DOC, Clms, COS, ddsrcpt, FileDate = max(FileDate), Age = Datediff(day,max(filedate), getdate()), Title = case when min(Title) <> max(Title) then 'Concurr' else min(Title) endFrom pdtestwhere doc =@docGroup By FO, DOC, CLMs, COS, ddsrcpt------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/ |
 |
|
|
JJ297
Aged Yak Warrior
940 Posts |
Posted - 2011-09-22 : 08:21:36
|
| Thanks for your reply I tried it but I need that column. Is there another way? If not it's just one record and I will keep it. |
 |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2011-09-22 : 11:49:03
|
| whats the requirment by the way? it clearly has two different titles so should be concurr. why should you want it be considered as concurr then?------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/ |
 |
|
|
JJ297
Aged Yak Warrior
940 Posts |
Posted - 2011-09-22 : 13:29:25
|
| if the title is T16 and T2 and they are both have the same clms number then they should be called concurr.Look at clm - 193444 it comes out as Concurr |
 |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2011-09-22 : 13:36:02
|
thats because as suggested earlier their ddsrcpt are same and here its different. so including it in group splits it up. or if you just want to return one of ddsrcpt values in random and make it single record you can do this......else if @doc in ('V01', 'S67', 'S41', 'S66')Begin Select FO, DOC, Clms, COS, MIN(ddsrcpt) AS ddsrcpt, FileDate = max(FileDate), Age = Datediff(day,max(filedate), getdate()),min(Title),max(Title), Title = case when min(Title) <> max(Title) then 'Concurr' else min(Title) endfrom pdtestwhere doc in ('V01', 'S67', 'S41', 'S66')Group By FO, DOC, CLMs, COSEnd....------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/ |
 |
|
|
JJ297
Aged Yak Warrior
940 Posts |
Posted - 2011-09-22 : 14:21:07
|
| Okay thanks I like that. I put it in the other if statements so I wouldn't have that problem again. Thanks again!!!!So when I want distinct records don't include the field I want distinct records in the group by clause? |
 |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2011-09-22 : 22:22:34
|
| depends on value in field. if value in field is not same for all in group they will come as multiple records------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/ |
 |
|
|
|
|
|