Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. Database & SysAdmin
  3. Database
  4. How to get rows having sum equal to given value

How to get rows having sum equal to given value

Scheduled Pinned Locked Moved Database
questiontutorial
11 Posts 7 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • H Offline
    H Offline
    Hum Dum
    wrote on last edited by
    #1

    There is table contain

    ID Qty

    1 2
    2 4
    3 1
    4 5

    Now if i had to choose rows where sum of Qty equals to 10, How can i do this ? like 2+4+1 = 7 but if i add 5 then 12 so ignore 2, then 4+1+5 = 10 How can i achieve this ? I want id's of that rows which contain combination/sum equal to 10 (number i put)

    B J S L L 5 Replies Last reply
    0
    • H Hum Dum

      There is table contain

      ID Qty

      1 2
      2 4
      3 1
      4 5

      Now if i had to choose rows where sum of Qty equals to 10, How can i do this ? like 2+4+1 = 7 but if i add 5 then 12 so ignore 2, then 4+1+5 = 10 How can i achieve this ? I want id's of that rows which contain combination/sum equal to 10 (number i put)

      B Offline
      B Offline
      Blue_Boy
      wrote on last edited by
      #2

      declare @qty as decimal(18,2) set @qty=(select qty from mytable where id=4) select sum(qty)-@qty from myTable By this example you will have result: 2+4+1=7


      I Love T-SQL "Don't torture yourself,let the life to do it for you." If my post helps you kindly save my time by voting my post. www.cacttus.com

      1 Reply Last reply
      0
      • H Hum Dum

        There is table contain

        ID Qty

        1 2
        2 4
        3 1
        4 5

        Now if i had to choose rows where sum of Qty equals to 10, How can i do this ? like 2+4+1 = 7 but if i add 5 then 12 so ignore 2, then 4+1+5 = 10 How can i achieve this ? I want id's of that rows which contain combination/sum equal to 10 (number i put)

        J Offline
        J Offline
        Jagz W
        wrote on last edited by
        #3

        I think this cannot be done in a single query. You need to create combination by looping One person's data is another person's program. --J.Walia

        1 Reply Last reply
        0
        • H Hum Dum

          There is table contain

          ID Qty

          1 2
          2 4
          3 1
          4 5

          Now if i had to choose rows where sum of Qty equals to 10, How can i do this ? like 2+4+1 = 7 but if i add 5 then 12 so ignore 2, then 4+1+5 = 10 How can i achieve this ? I want id's of that rows which contain combination/sum equal to 10 (number i put)

          S Offline
          S Offline
          Simon_Whale
          wrote on last edited by
          #4

          To help you more how large would the table be that you have to loop through?

          Lobster Thermidor aux crevettes with a Mornay sauce, served in a Provençale manner with shallots and aubergines, garnished with truffle pate, brandy and a fried egg on top and Spam - Monty Python Spam Sketch

          H 1 Reply Last reply
          0
          • H Hum Dum

            There is table contain

            ID Qty

            1 2
            2 4
            3 1
            4 5

            Now if i had to choose rows where sum of Qty equals to 10, How can i do this ? like 2+4+1 = 7 but if i add 5 then 12 so ignore 2, then 4+1+5 = 10 How can i achieve this ? I want id's of that rows which contain combination/sum equal to 10 (number i put)

            L Offline
            L Offline
            Lost User
            wrote on last edited by
            #5

            I do not think that it is possible to write a single query to achieve what you are trying to do. There could be more than a single set of rows whose sum of qty = 10, how would you handle that?

            1 Reply Last reply
            0
            • H Hum Dum

              There is table contain

              ID Qty

              1 2
              2 4
              3 1
              4 5

              Now if i had to choose rows where sum of Qty equals to 10, How can i do this ? like 2+4+1 = 7 but if i add 5 then 12 so ignore 2, then 4+1+5 = 10 How can i achieve this ? I want id's of that rows which contain combination/sum equal to 10 (number i put)

              L Offline
              L Offline
              Luc Pattyn
              wrote on last edited by
              #6

              So you want to solve a knapsack problem[^] in SQL? You do realize the problem is NP complete? :)

              Luc Pattyn [My Articles] Nil Volentibus Arduum

              The quality and detail of your question reflects on the effectiveness of the help you are likely to get.
              Please use <PRE> tags for code snippets, they improve readability.
              CP Vanity has been updated to V2.4

              K H 2 Replies Last reply
              0
              • L Luc Pattyn

                So you want to solve a knapsack problem[^] in SQL? You do realize the problem is NP complete? :)

                Luc Pattyn [My Articles] Nil Volentibus Arduum

                The quality and detail of your question reflects on the effectiveness of the help you are likely to get.
                Please use <PRE> tags for code snippets, they improve readability.
                CP Vanity has been updated to V2.4

                K Offline
                K Offline
                konduc
                wrote on last edited by
                #7

                Exactly - ,subset sum problem[^]

                L 1 Reply Last reply
                0
                • K konduc

                  Exactly - ,subset sum problem[^]

                  L Offline
                  L Offline
                  Luc Pattyn
                  wrote on last edited by
                  #8

                  :thumbsup:

                  Luc Pattyn [My Articles] Nil Volentibus Arduum

                  The quality and detail of your question reflects on the effectiveness of the help you are likely to get.
                  Please use <PRE> tags for code snippets, they improve readability.
                  CP Vanity has been updated to V2.4

                  1 Reply Last reply
                  0
                  • L Luc Pattyn

                    So you want to solve a knapsack problem[^] in SQL? You do realize the problem is NP complete? :)

                    Luc Pattyn [My Articles] Nil Volentibus Arduum

                    The quality and detail of your question reflects on the effectiveness of the help you are likely to get.
                    Please use <PRE> tags for code snippets, they improve readability.
                    CP Vanity has been updated to V2.4

                    H Offline
                    H Offline
                    Hum Dum
                    wrote on last edited by
                    #9

                    the link you had given pointed me to Backpack I think what you mean is Knapsack_problem I realize the complexity. But at any given time max rows can b only 50-100,i am getting these rows from Select statement. Then from these rows i have to get rows whose sums equals to Qty given (input). So, will you please help me finding an algorithm to solve. Regards

                    L 1 Reply Last reply
                    0
                    • S Simon_Whale

                      To help you more how large would the table be that you have to loop through?

                      Lobster Thermidor aux crevettes with a Mornay sauce, served in a Provençale manner with shallots and aubergines, garnished with truffle pate, brandy and a fried egg on top and Spam - Monty Python Spam Sketch

                      H Offline
                      H Offline
                      Hum Dum
                      wrote on last edited by
                      #10

                      at any given time max rows can b only 50-100,i am getting these rows from Select statement. I put them in temp table, iterate over them in loop, something like that ?? Then from these rows i have to get rows whose sums equals to Qty given (input). Regards

                      1 Reply Last reply
                      0
                      • H Hum Dum

                        the link you had given pointed me to Backpack I think what you mean is Knapsack_problem I realize the complexity. But at any given time max rows can b only 50-100,i am getting these rows from Select statement. Then from these rows i have to get rows whose sums equals to Qty given (input). So, will you please help me finding an algorithm to solve. Regards

                        L Offline
                        L Offline
                        Luc Pattyn
                        wrote on last edited by
                        #11

                        The problem is actually known as the subset sum problem as konduc pointed out, with a useful link. As your number of rows is limited, you should load them all in memory and go for a normal solution, not a database-centric SQL one. Now start studying the keywords and links given to you, and help yourself with Google and/or some books. No one is going to do your job for you. :)

                        Luc Pattyn [My Articles] Nil Volentibus Arduum

                        The quality and detail of your question reflects on the effectiveness of the help you are likely to get.
                        Please use <PRE> tags for code snippets, they improve readability.
                        CP Vanity has been updated to V2.4

                        1 Reply Last reply
                        0
                        Reply
                        • Reply as topic
                        Log in to reply
                        • Oldest to Newest
                        • Newest to Oldest
                        • Most Votes


                        • Login

                        • Don't have an account? Register

                        • Login or register to search.
                        • First post
                          Last post
                        0
                        • Categories
                        • Recent
                        • Tags
                        • Popular
                        • World
                        • Users
                        • Groups