Results 1 to 4 of 4

Thread: SQL Query (Random Records With Sum) Help

  1. #1
    Join Date
    Sep 2005
    Posts
    6

    Exclamation SQL Query (Random Records With Sum) Help

    Im try to write quesry to select random records from tables
    with condition of .. Records number = 4 and the total sum of points = 45
    table look like this

    ID Points
    1 10
    2 15
    3 20
    4 10
    5 5
    6 15
    7 30
    8 12
    9 11
    10 10


    Need Result To Be like for Example

    ID Points
    1 10
    3 20
    5 5
    10 10
    as u can see the sum of points = 45

    Thanks
    Last edited by dell_10; 09-08-2005 at 07:51 PM.

  2. #2
    Join Date
    Sep 2005
    Posts
    6
    No Ideas ????

  3. #3
    Join Date
    Sep 2005
    Posts
    6
    no one can help me ??

  4. #4
    Join Date
    Dec 2004
    Posts
    502
    Relax. Chill. Is this what you want?

    select top 1 D.id, ABC.Aid, ABC.Bid, ABC.Cid, D.points + ABC.Apoints + ABC.Bpoints + ABC.Cpoints
    from YourTable D join
    (select C.id Cid, AB.Aid, AB.Bid, C.points Cpoints, AB.Apoints, AB.Bpoints
    from YourTable C join
    (select A.id Aid, A.points Apoints, B.id Bid, B.points Bpoints
    from YourTable A join YourTable B on A.id <> B.id) as AB
    on C.id <> AB.Aid and C.id <> AB.Bid) as ABC
    on D.id <> ABC.Aid and D.id <> ABC.Bid and D.id <> ABC.Cid
    where D.points + ABC.Apoints + ABC.Bpoints + ABC.Cpoints = 45

Posting Permissions

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