Home Forums SQL Server 2008 T-SQL (SS2K8) Need help with a script to identify count of negative and positive numbers in a record RE: Need help with a script to identify count of negative and positive numbers in a record

  • I tried for 6 numbers with the below query.

    select col1, col2, col3, col4, col5, Col6

    ,(s1+abs(s1)+s2+abs(s2)+s3+abs(s3)+s4+abs(s4)+s5+abs(s5)+ s6+abs(s6))/2 as Positive

    ,abs((s1-abs(s1)+s2-abs(s2)+s3-abs(s3)+s4-abs(s4)+s5-abs(s5)+ s6 - abs(s6))/2) as Negative

    from (select *,sign(col1) s1,sign(col2) s2,sign(col3) s3,sign(col4) s4,sign(col5) s5, sign(Col6) s6

    from MyTable) sn

    select *, 6-Positives as Negatives

    from (select *,(sign(col1)+sign(col2)+sign(col3)+sign(col4)+sign(col5)+sign(Col6)+ 6)/2 Positives

    from MyTable) sn

    It works fine.

    what is the secret behind this mathematical formula? It would be great if you explain it in detail.

    I am always your fan for such a mathematical formula ( The one you used to generate the sequence number by using BIT WISE & operator)

    karthik