SQL Server Query to find records with aggregate funct on one column but multiple columns in select clause -


here minimized version of customer table. there can customers having same account number mapped different group . looking find out customer numbers mapped more 1 group. using sybase query below working fine. same query not work in sql server.

can both custaccnt , corresponding custid in 1 query below.

select distinct lt.custaccnt, lt.custid val_customers lt  lt.eligible = 'y' group lt.custaccnt  having count(distinct lt.custid) > 1  +----------+-----------+---------+----------+ | custname | custaccnt | custid  | eligible | +----------+-----------+---------+----------+ | joe      | ab1vu1235 |   43553 | y        | | joe      | ab1vu1235 |  525577 | y        | | lucy     | cdnmy4568 |  332875 | y        | | lucy     | cdnmy4568 |  211574 | y        | | lucy     | cdnmy4568 |  211345 | y        | | manie    | tzmm7s009 |  123890 | y        | | tom      | yfdu1235  | 1928347 | y        | | tom      | yfdu1235  |  204183 | y        | | chef     | tnote6573 |  734265 | y        | +----------+-----------+---------+----------+ 

result :-

+-----------+---------+ | ab1vu1235 |   43553 | | ab1vu1235 |  525577 | | cdnmy4568 |  332875 | | cdnmy4568 |  211574 | | cdnmy4568 |  211345 | | yfdu1235  | 1928347 | | yfdu1235  |  204183 | +-----------+---------+ 

there many ways tackle this. here couple of them should work.

select lt.custaccnt     , lt.custid  val_customers lt  cross apply (     select c.custaccnt     val_customers c     c.custaccnt = lt.custaccnt     group c.custaccnt     having count(*) > 1 ) x lt.eligible = 'y'   select lt.custaccnt     , lt.custid  val_customers lt  lt.eligible = 'y' , lt.custaccnt in (     select c.custaccnt     val_customers c     group c.custaccnt     having count(*) > 1 )  

Comments

Popular posts from this blog

Command prompt result in label. Python 2.7 -

javascript - How do I use URL parameters to change link href on page? -

amazon web services - AWS Route53 Trying To Get Site To Resolve To www -