Author |
Topic |
dhinasql
Posting Yak Master
195 Posts |
Posted - 2013-10-08 : 07:19:42
|
Friends,I have transaction log table with QueryNumber, StatusID. So every status change of the Record it will be maintained in this table. ExampleTableName: QueryLogColumn Name : ID, QueryNumber, StatusIDSample Value1001 | 5001 | 11002 | 5001 | 21003 | 5001 | 31004 | 5001 | 41005 | 5001 | 51006 | 5002 | 11007 | 5002 | 21008 | 5002 | 31009 | 5002 | 41010 | 5003 | 11011 | 5003 | 21012 | 5003 | 31013 | 5003 | 4I need the Query to Return the Records Status Which is in 3 Or 4 and Not in 5.Expected ResultQueryNumber-----------50025003Looking forward to hear |
|
madhivanan
Premature Yak Congratulator
22864 Posts |
Posted - 2013-10-08 : 07:21:29
|
select QueryNumber from table group by QueryNumber having max(case when StatusID=5 then 1 else 0 end)=0MadhivananFailing to plan is Planning to fail |
|
|
dhinasql
Posting Yak Master
195 Posts |
Posted - 2013-10-08 : 07:25:02
|
Thank you for your Reply.NOT IN Value '5' is not fixed value always.NOT IN value will dynamically change, So sometime i need to check Where STATUSID NOT IN 2. |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2013-10-08 : 07:26:07
|
[code]SELECT QueryNumberfrom table GROUP BY QueryNumberHAVING MAX(StatusID) IN (3,4)[/code]------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/https://www.facebook.com/VmBlogs |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2013-10-08 : 07:27:23
|
quote: Originally posted by madhivanan select QueryNumber from table group by QueryNumber having max(case when StatusID=5 then 1 else 0 end)=0MadhivananFailing to plan is Planning to fail
this wil still return cases which are currently in Status < 3add this to sample data and see1014 | 5004 | 11015 | 5004 | 2------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/https://www.facebook.com/VmBlogs |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2013-10-08 : 07:28:00
|
quote: Originally posted by dhinasql Thank you for your Reply.NOT IN Value '5' is not fixed value always.NOT IN value will dynamically change, So sometime i need to check Where STATUSID NOT IN 2.
so what determines that value?is it user passed value as a parameter?------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/https://www.facebook.com/VmBlogs |
|
|
dhinasql
Posting Yak Master
195 Posts |
Posted - 2013-10-08 : 07:31:39
|
Thanks Visakh,Yes User Will pass it as Parameter to Stored Procedure. |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2013-10-08 : 07:35:09
|
If its a range they pass then you can do likeCREATE PROC YourProc@MinVal int,@MaxVal intASSELECT QueryNumberfrom table GROUP BY QueryNumberHAVING MAX(StatusID) BETWEEN @MinVal AND @MaxValGO if its single max limit value you can get only all records from start till thatlikeCREATE PROC YourProc@MaxVal intASSELECT QueryNumberfrom table GROUP BY QueryNumberHAVING MAX(StatusID) < @MaxValGO ------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/https://www.facebook.com/VmBlogs |
|
|
dhinasql
Posting Yak Master
195 Posts |
Posted - 2013-10-08 : 07:45:01
|
Thanks Visakh,But i am not looking for Min Value and Max value prospective data retrival.I need to check the Queries with QueryNumber that is available in One StatusID and Not available in Another StatusID.StatusID Status1 | New2 | Processing by Admin3 | Processing by Contributor4 | Closed by Admin5 | Closed by Contributor6 | Closed by User7 | Rejected By AdminSo for my Requirement I always have fixed STATUSID for IN Clause , Ex: STATUSID IN (3,4) BUT the NOT IN Clause will dynamically passed from my .Net programming as a Parameter to SP.For example : Sometime I need to check QUERYNUMBER Where STATUSID in (3,4) and NOT IN (2)QUERYNUMBER Where STATUSID in (3,4) and NOT IN (5)QUERYNUMBER Where STATUSID in (3,4) and NOT IN (7) etc..Please let me know if you need more details. |
|
|
madhivanan
Premature Yak Congratulator
22864 Posts |
Posted - 2013-10-08 : 07:51:06
|
select QUERYNUMBER from table as t1where STATUSID in (3,4) and not exists (select * from table as t2 where t1.QUERYNUMBER=t2.QUERYNUMBER and STATUSID in (@statusid))MadhivananFailing to plan is Planning to fail |
|
|
dhinasql
Posting Yak Master
195 Posts |
Posted - 2013-10-08 : 08:27:45
|
Thanks madhi, I got it. |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2013-10-08 : 08:28:44
|
quote: Originally posted by dhinasql Thanks Visakh,But i am not looking for Min Value and Max value prospective data retrival.I need to check the Queries with QueryNumber that is available in One StatusID and Not available in Another StatusID.StatusID Status1 | New2 | Processing by Admin3 | Processing by Contributor4 | Closed by Admin5 | Closed by Contributor6 | Closed by User7 | Rejected By AdminSo for my Requirement I always have fixed STATUSID for IN Clause , Ex: STATUSID IN (3,4) BUT the NOT IN Clause will dynamically passed from my .Net programming as a Parameter to SP.For example : Sometime I need to check QUERYNUMBER Where STATUSID in (3,4) and NOT IN (2)QUERYNUMBER Where STATUSID in (3,4) and NOT IN (5)QUERYNUMBER Where STATUSID in (3,4) and NOT IN (7) etc..Please let me know if you need more details.
SELECT QUERYNUMBER FROM TableGROUP BY QUERYNUMBERHAVING COUNT(DISTINCT CASE WHEN StatusID IN (3,4) THEN StatusID END) =2AND SUM(CASE WHEN StatusID = @StatusID THEN 1 ELSE 0 END) = 0 ------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/https://www.facebook.com/VmBlogs |
|
|
SwePeso
Patron Saint of Lost Yaks
30421 Posts |
Posted - 2013-10-08 : 17:59:07
|
[code]SELECT QueryNumberFROM dbo.Table1WHERE StatusID IN (3, 4, 5)GROUP BY QueryNumberHAVING MAX(StatusID) = 4;[/code] Microsoft SQL Server MVP, MCT, MCSE, MCSA, MCP, MCITP, MCTS, MCDBA |
|
|
|