Site Sponsored By: SQLDSC - SQL Server Desired State Configuration
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.
Hi All !!! I have the following Sales Table[CODE]TEL_NUMBER ACTS DEACTS4165551234 1 04165551234 0 -14165551234 0 04165551235 1 04165551235 0 04165551235 0 0[/CODE]AS you can see, there's multiple telephone numbers for each record. I need the query to output results ONLY if there's a "1" but no "-1" for ANY occurrence for that telephone number.For example, the query out should exclude 4165551234 but include 4165551235 ....[CODE]TEL_NUMBER ACTS DEACTS4165551235 1 0[/CODE]I'm not quite sure how to do this, Any ideas? Thanks![CODE]SELECT TEL_NUMBER, ACTS, DEACTS FROM SALES T1WHERE ACTS=1[/CODE]
Based on sample data:SELECT TEL_NUMBER, 1 AS ACTS, 0 AS DEACTS FROM SALESGROUP BY TEL_NUMBERHAVING MAX(ACTS) = 1AND MIN(DEACTS) = 0;
visakh16
Very Important crosS Applying yaK Herder
52326 Posts
Posted - 2013-11-07 : 02:54:18
[code]SELECT TEL_NUMBER,ACTS,DEACTSFROM(SELECT MIN(DEACTS) OVER (PARTITION BY TEL_NUMBER ) AS MinDeacts,MAX(ACTS) OVER (PARTITION BY TEL_NUMBER ) MaxActs,*FROM @Foo)tWHERE MinDeacts = 0AND MaxActs = 1[/code]------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/https://www.facebook.com/VmBlogs