Search code examples
mysqltelecommunication

Sql Query to get count from another row


MenuID  PreMenuID  Type
P2000   BS02        p
P8001   P2000       x

Hi Experts i have table structure given above which indicates menu id as main id and PremenuId Indicates the previous menu from which call is transferred.

Now type=x means there is a exception in the call

from the above table menuid=p8001 is transfer from P2000 as shown in premenuid

i want to get the count of exception generated in table but in mainId

the output should be smthng like ths

MenuID  Exception  OnId
p2000     1        p8001

Thanks.


Solution

  • If I get your requirements right, a simple group by-Statement will do the job:

    select PreMenuID as `MenuID`, count(Type) as `Exception`, MenuID as `OnID`
      where Type = x
      group by `MenuID`, `OnID`