Author |
Topic |
sebastian11c
Posting Yak Master
129 Posts |
Posted - 2012-04-19 : 15:10:19
|
hi there i have a table called "proccess"and i need to do a select but only want to show the record that have a specific conditionmy tableidproccess(identity) iditem state1 142 begin2 142 half3 142 complete4 190 begin5 190 half6 270 completei need a select that show me the iditem of the record that have all 3 states complete ( have begin, and half and complete )my result in this case will beonly the iditem =142i dont know hoew to do this select properlymany many thanks in advanced |
|
sunitabeck
Master Smack Fu Yak Hacker
5155 Posts |
Posted - 2012-04-19 : 15:19:00
|
If you just want the item numbers use thisselect iditem from Process where state in ('begin','half','complete')group by iditem having count(distinct state) = 3 If you want to get all three rows:select * from Processwhere state in ('begin','half','complete')and iditem in (select iditem from Process group by iditem having count(distinct state) = 3); |
 |
|
sebastian11c
Posting Yak Master
129 Posts |
Posted - 2012-04-19 : 16:02:42
|
thanks you so much for your answer it works greatnow i have another question, how could i refuse an insert to my table if doesnt exist the state before,let me explain, if im trying to insert in my table the record ( iditem=700 and state ='complete' ) doesnt complete the transaction because the 'half' state doesnt exist and get an error message 0"you cannot insert the complete state , because you dont have the half state, first insert the half state :idproccess(identity) iditem state1 700 beginthanks in advanced |
 |
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2012-04-19 : 16:35:44
|
sounds like addition of a check constraint to me.------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/ |
 |
|
sebastian11c
Posting Yak Master
129 Posts |
Posted - 2012-04-19 : 17:19:16
|
im sorry but my knowledge about sql is to lowvisakh16, could you explain me how to add check constraint , that solve my problemany example will be appreciatethanks in advanced |
 |
|
sunitabeck
Master Smack Fu Yak Hacker
5155 Posts |
Posted - 2012-04-20 : 08:58:41
|
If you are only inserting into or updating this table, a constraint would work. But, if you also delete the data from it and still want to maintain the condition that begin must exist if half exists etc., you would need a trigger.If you don't delete, or if you don't care that when you delete the sequence need to be maintained, then you can use a constraint.To do that, assuming the name of your table is XYZ, first create a function like shown below. CREATE FUNCTION dbo.CheckStateSequencesOnXYZ( @iditem INT, @state VARCHAR(32))RETURNS INTASBEGIN DECLARE @result INT = 0; IF (@state = 'begin') SET @result = 1; IF (@state = 'half' AND EXISTS ( SELECT * FROM dbo.XYZ WHERE STATE = 'begin' AND iditem = @iditem ) ) SET @result = 1; IF ( @state = 'complete' AND EXISTS ( SELECT * FROM dbo.XYZ WHERE STATE = 'half' AND iditem = @iditem ) ) SET @result = 1; RETURN @result;END It looks long, so there may be things you can do to make it shorter/simpler, but would need to implement same/similar logic based on your requirements.Then add a constraint on your table like this:ALTER TABLE dbo.XYZ ADD CONSTRAINT XYZ_CheckStates CHECK (dbo.CheckStateSequencesOnXYZ(idItem,STATE) = 1); There is a cost to doing this because the function will get called every time you want to insert/update the table.Depending on your business logic/requirements, you may also want to add a unique constraint - for example like this:ALTER TABLE dbo.XYZ ADD CONSTRAINT XYZ_UniqueIdItemState UNIQUE CLUSTERED (iditem, state); |
 |
|
SwePeso
Patron Saint of Lost Yaks
30421 Posts |
|
sebastian11c
Posting Yak Master
129 Posts |
Posted - 2012-04-20 : 09:17:35
|
thanks sunitabeck and SwePesothanks for alwayss helping the beginners ,ill try this solutionsmany many thanks |
 |
|
sunitabeck
Master Smack Fu Yak Hacker
5155 Posts |
|
|