How to get zplid that have zfeature key value on all rows?

ahmed salah 3,216 Reputation points
2020-12-19T20:35:16.097+00:00

Exactly I need zplid and codetypeid that have feature value
on all rows .

if I have Null on zfeature key on one row then I don't Need it .

I need to get zplid that have features Not Null on all rows as zplid 9714
but 9823,5890 have Null so I don't Need it

this is my sample data

create table #gen
(
zplid int,
CodetypId int,
ZfeatureKey nvarchar(20)
)
insert into #gen(zplid,CodetypId,ZfeatureKey)
values
(9714,849774,1501290046),
(9714,849774,1501099991),
(9714,849774,1501555555),
(9823,9732410,1501290046),
(9823,9732410,1501099991),
(9823,9732410,NULL),
(5890,838651,1501290046),
(5890,838651,NULL),
(5890,838651,NULL)
expected result is

zplid CodetypId ZfeatureKey
9714 849774 1501290046
9714 849774 1501099991
9714 849774 1501555555

I tried to do as below :

select zplid,CodetypId,ZfeatureKey
from #gen
where ZfeatureKey is not null

but it give me zplid and codetypeid that have values ON Zfeature key on all rows of all zplid
and this I don't need

SQL Server
SQL Server
A family of Microsoft relational database management and analysis systems for e-commerce, line-of-business, and data warehousing solutions.
13,961 questions
Transact-SQL
Transact-SQL
A Microsoft extension to the ANSI SQL language that includes procedural programming, local variables, and various support functions.
4,652 questions
0 comments No comments
{count} votes

Accepted answer
  1. Erland Sommarskog 112.7K Reputation points MVP
    2020-12-19T21:04:55.79+00:00

    I am not sure that this is the most efficient query, but it does the job:

    ; WITH CTE AS (
       SELECT zplid, CodetypId, ZfeatureKey, 
              totalcnt = COUNT(*) OVER (PARTITION BY zplid, CodetypId),
              nonnullcount = COUNT(ZfeatureKey) OVER(PARTITION BY zplid, CodetypId)
       FROM #gen
    )
    SELECT zplid, CodetypId, ZfeatureKey 
    FROM   CTE
    WHERE  totalcnt = nonnullcount
    
    0 comments No comments

1 additional answer

Sort by: Most helpful
  1. MelissaMa-MSFT 24,201 Reputation points
    2020-12-21T02:07:35.46+00:00

    Hi @ahmed salah ,

    Please also refer below:

    ;with cte as (  
     select zplid,  
     sum(iif(Zfeaturekey is null,1,0)) sumnull  
     from #gen group by Zplid)  
     select * from #gen  
     where Zplid in (select Zplid from cte where sumnull=0)  
    

    Output:

    zplid	CodetypId	ZfeatureKey  
    9714	849774	1501290046  
    9714	849774	1501099991  
    9714	849774	1501555555  
    

    Best regards
    Melissa


    If the answer is helpful, please click "Accept Answer" and upvote it.
    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

    Hot issues November--What can I do if my transaction log is full?
    Hot issues November--How to convert Profiler trace into a SQL Server table


Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.