Results 1 to 6 of 6
  1. #1
    Join Date
    Feb 2012
    Posts
    3

    Unanswered: Removing duplicate rows in grouping

    i've to generate a notepad using this query in vb.net :


    strSql = "Select count(*), d.ShareholderId, d.UsufructId, d.BnkAccount, b.SBMCode, " & _

    "LTRIM(ISNULL(d.TitleCode + ' ', '')) + LTRIM(ISNULL(d.Forename + ' ', '')) + d.Surname as ShName," & _

    "d.BankCode, (select count(*) from (select ShareholderId from dividend " & _

    "where CompCode = 'L1' and PaymentMode = 'B' group by ShareholderId, UsufructId, " & _

    "BnkAccount, BankCode ) dividend) as Temps " & _

    "from dividend d join Bank b on d.BankCode = b.BankCode " & _

    "group by d.ShareholderId, d.UsufructId, d.BnkAccount,d.BankCode,d.TitleCode,d.Forename,d.S urname," & _

    "b.SBMCode " & _

    "ORDER BY d.BankCode, d.ShareholderId"



    In the select statement i need to select "d.amount" as well. When i do so, it ask me to insert it in the group by option or
    in an aggregate function.

    Grouping by "d.amount" returns extra field as there can be 2 similar "d.shareholderId" but with different amount.

    My question is, how to group the "d.amount" without having repetition in the "d.shareholderid" ??

  2. #2
    Join Date
    Apr 2002
    Location
    Toronto, Canada
    Posts
    20,002
    use SUM(d.amount) in the SELECT clause
    rudy.ca | @rudydotca
    Buy my SitePoint book: Simply SQL

  3. #3
    Join Date
    Feb 2012
    Posts
    3
    Actually i also need a breakdown of each amount for a shareholder. And a shareholder might have 2 different amount. So i need to sum up the amount per shareholder id and group it.

    Thx for helping.

  4. #4
    Join Date
    Apr 2002
    Location
    Toronto, Canada
    Posts
    20,002
    Quote Originally Posted by Ke c View Post
    So i need to sum up the amount per shareholder id and group it.
    all rightee then

    so put d.amount into both the SELECT and GROUP BY clauses
    rudy.ca | @rudydotca
    Buy my SitePoint book: Simply SQL

  5. #5
    Join Date
    Feb 2012
    Posts
    3
    i tried this 1. When i group by d.amount, there are extra rows generated because the amount is not the same !

    wat is need is, how to add up 2 different amount if the shareholderId is same.

    Thx indeed for helping.

  6. #6
    Join Date
    Apr 2002
    Location
    Toronto, Canada
    Posts
    20,002
    Quote Originally Posted by Ke c View Post
    i tried this 1. When i group by d.amount, there are extra rows generated because the amount is not the same !
    but that's what you said you wanted!!!!!1!
    rudy.ca | @rudydotca
    Buy my SitePoint book: Simply SQL

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •