Welcome, Guest: Register On Nairaland / LOGIN! / Trending / Recent / New
Stats: 3,154,749 members, 7,824,156 topics. Date: Saturday, 11 May 2024 at 01:01 AM

Select All Rows Where The Sum Of Column Equals A Value - Programming - Nairaland

Nairaland Forum / Science/Technology / Programming / Select All Rows Where The Sum Of Column Equals A Value (2987 Views)

Mysql Help:please I Need Help Transposing Row Into Column / Please Help!!! Sum 2 Or More Same ID Within A Date Range / Please Someone Should Help Vba To Compute The Product And Sum Of H.C.F And L.C.M (2) (3) (4)

(1) (Reply) (Go Down)

Select All Rows Where The Sum Of Column Equals A Value by BobAxelrod: 5:07pm On Dec 24, 2016
I have a simple table:

col1 col2
1 2
2 3
3 7
4 1
5 9


I want to select random rows of (col1) where the cumulative sum of column (col2) equals a value

example:
select all random rows of col1 where the sum of col2 equals 17
returning: col1: 3, 4 and 5


Any ideas?

@dhtml and everyone
Re: Select All Rows Where The Sum Of Column Equals A Value by aubventure: 5:38pm On Dec 24, 2016
BobAxelrod:
I have a simple table:

col1 col2
1 2
2 3
3 7
4 1
5 9


I want to select random rows of (col1) where the cumulative sum of column (col2) equals a value

example:
select all random rows of col1 where the sum of col2 equals 17
returning: col1: 3, 4 and 5


Any ideas?

@dhtml and everyone



Which language are you working on
Re: Select All Rows Where The Sum Of Column Equals A Value by Noblecx: 5:52pm On Dec 24, 2016
Explain better, maybe with another illustration
Re: Select All Rows Where The Sum Of Column Equals A Value by lekropasky(m): 7:29pm On Dec 24, 2016
Larisoft
Re: Select All Rows Where The Sum Of Column Equals A Value by BobAxelrod: 9:45pm On Dec 24, 2016
aubventure:




Which language are you working on

It's database, I'm using mssql
Re: Select All Rows Where The Sum Of Column Equals A Value by BobAxelrod: 9:49pm On Dec 24, 2016
Noblecx:
Explain better, maybe with another illustration

The table has just two columns col 1 and col 2

Col 1 is the ID, and col 2 is the amount

Let's say I need to select all rows where the sum equals 5 .
It should return row 1 and 2 coz the values there are 2 and 3 i.e 2+3 =5

How do I go about it
Re: Select All Rows Where The Sum Of Column Equals A Value by Noblecx: 10:11pm On Dec 24, 2016
BobAxelrod:


The table has just two columns col 1 and col 2

Col 1 is the ID, and col 2 is the amount

Let's say I need to select all rows where the sum equals 5 .
It should return row 1 and 2 coz the values there are 2 and 3 i.e 2+3 =5

How do I go about it

OK, if I should get you clearly, you need to get all rows whereby the sum of col1 and col2 = 5?
Even can the sum be greater than 5?
Re: Select All Rows Where The Sum Of Column Equals A Value by BobAxelrod: 10:23am On Dec 25, 2016
Noblecx:


OK, if I should get you clearly, you need to get all rows whereby the sum of col1 and col2 = 5?
Even can the sum be greater than 5?

No, it shouldn't be greater than, if 5 is not available then the database shouldn't return any result
Re: Select All Rows Where The Sum Of Column Equals A Value by godofbrowser(m): 12:42pm On Dec 25, 2016
BobAxelrod:
I have a simple table:

col1 col2
1 2
2 3
3 7
4 1
5 9


I want to select random rows of (col1) where the cumulative sum of column (col2) equals a value

example:
select all random rows of col1 where the sum of col2 equals 17
returning: col1: 3, 4 and 5


Any ideas?

@dhtml and everyone

Oh! if i get you clearly, you need rows where after summing their col2 values you get 17?

Example: col2: 7+9+1 = 17 hence you get rows 3,4, and 5 ?
Re: Select All Rows Where The Sum Of Column Equals A Value by BobAxelrod: 1:13pm On Dec 25, 2016
godofbrowser:


Oh! if i get you clearly, you need rows where after summing their col2 values you get 17?

Example: col2: 7+9+1 = 17 hence you get rows 3,4, and 5 ?
yes, do you know how I can achieve that
Re: Select All Rows Where The Sum Of Column Equals A Value by Noblecx: 1:38pm On Dec 25, 2016
BobAxelrod:


No, it shouldn't be greater than, if 5 is not available then the database shouldn't return any result
OK, Got that, You gonna need a sub query, I will get back to you
Re: Select All Rows Where The Sum Of Column Equals A Value by imustsaymymindo: 1:40pm On Dec 25, 2016
Did I try?

Select Rand(col1) from table_name where sum (col2) is Not Null
Re: Select All Rows Where The Sum Of Column Equals A Value by Craigston: 2:19pm On Dec 25, 2016
How precise do you want the results to be? Do you want to carry out an exhaustive search of all such possible rows, and thus obtain every possible answer?
This seems like a combinatorial problem, and if you want an exhaustive enumeration of the problem, the cost of computing increases fast as the number of rows increases.
Also, are negative integers allowed in the rows? Are the numbers restricted to any set of numbers (integers, natural numbers, complex numbers, real numbers...).
I don't know how to implement this in a database, but you can do it in a program using a 1 x n matrix that maps to your table.

See these SO links, [1] and [2], for similar problems.
This problem is not one at which SQL can shine. It requires much use of repetition and the running time grows exponentially. In fact, I wouldn't attempt it for N results where N > 3, N being the number of rows whose sum matches the expected result.

However you do it, I wish you good luck. I don't know what I'm saying, so do not assume it to be credible advice. I just came to say hello.
DanielTheGeek, dhtml18, Jregz, seunthomas, FincoApps, cbrass, FrankLampard, godofbrowser, thewebcraft, Javanian, booyakasha, larisoft, stack1, yawatide, tr3y, Knownpal, rayval, blueyedgeek, jidez007, KvnqPrezo, Jenifa123, VenantCode, directonpc, DavidTheGeek, crotonite, Blenyo11, CodeNister, CodeHouse, logicalhumour, Asalimpo, lekropasky...
Someone needs help.

Disclaimer: the mentioned monikers were semi-automatically generated.
Merry Christmas and a Happy New Year to all devs and techies.

[1] http://stackoverflow.com/questions/21020619/sql-server-select-rows-whos-sum-matches-a-value

[2] http://stackoverflow.com/questions/6289314/how-to-get-rows-having-sum-equal-to-given-value

1 Like

Re: Select All Rows Where The Sum Of Column Equals A Value by Noblecx: 2:26pm On Dec 25, 2016
BobAxelrod:


No, it shouldn't be greater than, if 5 is not available then the database shouldn't return any result
This is what I was able to come up with.


Select col1, col2
From yourTable
Where sum = 5
IN
(Select col1, col2, (col1 + col2) as sum
From YourTable)

2 Likes

Re: Select All Rows Where The Sum Of Column Equals A Value by BobAxelrod: 8:26pm On Dec 25, 2016
imustsaymymindo:
Did I try?

Select Rand(col1) from table_name where sum (col2) is Not Null
Nah this isn't what I'm looking for
Re: Select All Rows Where The Sum Of Column Equals A Value by BobAxelrod: 8:29pm On Dec 25, 2016
Craigston:
How precise do you want the results to be? Do you want to carry out an exhaustive search of all such possible rows, and thus obtain every possible answer?
This seems like a combinatorial problem, and if you want an exhaustive enumeration of the problem, the cost of computing increases fast as the number of rows increases.
Also, are negative integers allowed in the rows? Are the number restricted to any set of numbers (integers, natural numbers, complex numbers, real numbers...).
I don't know how to implement this in a database, but you can do it in a program using a 1 x n matrix that maps to your table.

See these SO links, [1] and [2], for similar problems.
This problem is not one at which SQL can shine. It requires much use of repetition and the running grows exponentially. In fact, I wouldn't attempt it for N results where N > 3, N being the number of rows whose sum matches the expected result.

However you do it, I wish you good luck. I don't know what I'm saying, so do not assume it to be credible advice. I just came to say hello.
DanielTheGeek, dhtml18, Jregz, seunthomas, FincoApps, cbrass, FrankLampard, godofbrowser, thewebcraft, Javanian, booyakasha, larisoft, stack1, yawatide, tr3y, Knownpal, rayval, blueyedgeek, jidez007, KvnqPrezo, Jenifa123, VenantCode, directonpc, DavidTheGeek, crotonite, Blenyo11, CodeNister, CodeHouse, logicalhumour, Asalimpo, lekropasky...
Someone needs help.

Disclaimer: the mentioned monikers were semi-automatically generated.
Merry Christmas and a Happy New Year to all devs and techies.

[1] http://stackoverflow.com/questions/21020619/sql-server-select-rows-whos-sum-matches-a-value

[2] http://stackoverflow.com/questions/6289314/how-to-get-rows-having-sum-equal-to-given-value

Too much grammar, thanks for the links though, seems the problem is not a small one after all
Re: Select All Rows Where The Sum Of Column Equals A Value by BobAxelrod: 8:32pm On Dec 25, 2016
Noblecx:

This is what I was able to come up with.


Select col1, col2
From yourTable
Where sum = 5
IN
(Select col1, col2, (col1 + col2) as sum
From YourTable)

which database is this, not working for me
Re: Select All Rows Where The Sum Of Column Equals A Value by Noblecx: 10:37pm On Dec 25, 2016
BobAxelrod:
which database is this, not working for me

MSSQL

1 Like

Re: Select All Rows Where The Sum Of Column Equals A Value by remotecoder: 11:23pm On Dec 25, 2016
This kind requires SQL CURSOR or SQL lF statement

1 Like

Re: Select All Rows Where The Sum Of Column Equals A Value by BobAxelrod: 12:23am On Dec 26, 2016
Noblecx:


MSSQL
which result did you get after running dz query
Re: Select All Rows Where The Sum Of Column Equals A Value by Noblecx: 12:25am On Dec 26, 2016
BobAxelrod:
which result did you get after running dz query

Bro, I didn't have such database..
Why not show me your sql code that you used.
Re: Select All Rows Where The Sum Of Column Equals A Value by escapefromusa(f): 6:47am On Dec 26, 2016
Re: Select All Rows Where The Sum Of Column Equals A Value by Craigston: 1:57pm On Dec 27, 2016
BobAxelrod:


Too much grammar, thanks for the links though, seems the problem is not a small one after all
Please just return this problem to wherever you got it from. It's holiday man. This problem sef.
Re: Select All Rows Where The Sum Of Column Equals A Value by Nobody: 12:26pm On Dec 29, 2016
Craigston:

Please just return this problem to wherever you got it from. It's holiday man. This problem sef.
DI'd not get the mention nairaland's notification stuff is pretty "dumb".. I don't even know that much sql though
Re: Select All Rows Where The Sum Of Column Equals A Value by Craigston: 12:44pm On Dec 29, 2016
Jregz:

DI'd not get the mention nairaland's notification stuff is pretty "dumb".. I don't even know that much sql though
I think it's a little better than Facebook's notifications. At least I don't get notified of someone's comment on a snake picture. Oh wait, 'posts by people you're following'. Whoops!

1 Like

Re: Select All Rows Where The Sum Of Column Equals A Value by Viruses: 5:36pm On Dec 29, 2016
BobAxelrod:
I have a simple table:

col1 col2
1 2
2 3
3 7
4 1
5 9


I want to select random rows of (col1) where the cumulative sum of column (col2) equals a value

example:
select all random rows of col1 where the sum of col2 equals 17
returning: col1: 3, 4 and 5


Any ideas?

@dhtml and everyone
MSSQL SERVER:

select SimpleTable.col1 from SimpleTable where (select sum(SimpleTable.col2) from SimpleTable) = 17

PM me if it does not work.

1 Like

Re: Select All Rows Where The Sum Of Column Equals A Value by BobAxelrod: 8:12am On Dec 30, 2016
Viruses:

MSSQL SERVER:

select SimpleTable.col1 from SimpleTable where (select sum(SimpleTable.col2) from SimpleTable) = 17

PM me if it does not work.

Syntax error
Re: Select All Rows Where The Sum Of Column Equals A Value by godofbrowser(m): 9:40pm On Sep 05, 2017
It's obvious this cannot be achieved using an sql query. You should try a dynamic programming.

1 Like 1 Share

Re: Select All Rows Where The Sum Of Column Equals A Value by godofbrowser(m): 9:42pm On Sep 05, 2017
BobAxelrod:
yes, do you know how I can achieve that
It's obvious this cannot be achieved using an sql query. You should try a dynamic programming.
Re: Select All Rows Where The Sum Of Column Equals A Value by ANTONINEUTRON(m): 10:07pm On Sep 06, 2017
Why Don't U Create Another Column (sum) Where The Sum Of The Rows Will Be Held

So You'll Do A Simple Query "Select * Where Sum='5'"

1 Like

Re: Select All Rows Where The Sum Of Column Equals A Value by Nobody: 11:47pm On Sep 06, 2017
BobAxelrod:
I have a simple table:

col1 col2
1 2
2 3
3 7
4 1
5 9


I want to select random rows of (col1) where the cumulative sum of column (col2) equals a value

example:
select all random rows of col1 where the sum of col2 equals 17
returning: col1: 3, 4 and 5


Any ideas?

@dhtml and everyone

you need to make col C, this will hold the total of col B.

So now you simply ask: select * where C is 17
Re: Select All Rows Where The Sum Of Column Equals A Value by michaelwilli(m): 3:31pm On Sep 08, 2017
Craigston:
How precise do you want the results to be? Do you want to carry out an exhaustive search of all such possible rows, and thus obtain every possible answer?
This seems like a combinatorial problem, and if you want an exhaustive enumeration of the problem, the cost of computing increases fast as the number of rows increases.
Also, are negative integers allowed in the rows? Are the numbers restricted to any set of numbers (integers, natural numbers, complex numbers, real numbers...).
I don't know how to implement this in a database, but you can do it in a program using a 1 x n matrix that maps to your table.

See these SO links, [1] and [2], for similar problems.
This problem is not one at which SQL can shine. It requires much use of repetition and the running time grows exponentially. In fact, I wouldn't attempt it for N results where N > 3, N being the number of rows whose sum matches the expected result.

However you do it, I wish you good luck. I don't know what I'm saying, so do not assume it to be credible advice. I just came to say hello.
DanielTheGeek, dhtml18, Jregz, seunthomas, FincoApps, cbrass, FrankLampard, godofbrowser, thewebcraft, Javanian, booyakasha, larisoft, stack1, yawatide, tr3y, Knownpal, rayval, blueyedgeek, jidez007, KvnqPrezo, Jenifa123, VenantCode, directonpc, DavidTheGeek, crotonite, Blenyo11, CodeNister, CodeHouse, logicalhumour, Asalimpo, lekropasky...
Someone needs help.

Disclaimer: the mentioned monikers were semi-automatically generated.
Merry Christmas and a Happy New Year to all devs and techies.

[1] http://stackoverflow.com/questions/21020619/sql-server-select-rows-whos-sum-matches-a-value

[2] http://stackoverflow.com/questions/6289314/how-to-get-rows-having-sum-equal-to-given-value
U don craze. Lollz

(1) (Reply)

What Was The Hardest Language You've Learnt / Please I Need Help In Deciding / Rsa Pin Generator

(Go Up)

Sections: politics (1) business autos (1) jobs (1) career education (1) romance computers phones travel sports fashion health
religion celebs tv-movies music-radio literature webmasters programming techmarket

Links: (1) (2) (3) (4) (5) (6) (7) (8) (9) (10)

Nairaland - Copyright © 2005 - 2024 Oluwaseun Osewa. All rights reserved. See How To Advertise. 45
Disclaimer: Every Nairaland member is solely responsible for anything that he/she posts or uploads on Nairaland.