|
Thread Rules 1. This is not a "do my homework for me" thread. If you have specific questions, ask, but don't post an assignment or homework problem and expect an exact solution. 2. No recruiting for your cockamamie projects (you won't replace facebook with 3 dudes you found on the internet and $20) 3. If you can't articulate why a language is bad, don't start slinging shit about it. Just remember that nothing is worse than making CSS IE6 compatible. 4. Use [code] tags to format code blocks. |
On January 08 2014 21:51 nunez wrote:Show nested quote +On January 08 2014 20:05 darkness wrote:On January 08 2014 09:50 nunez wrote:On January 08 2014 07:55 darkness wrote:On January 08 2014 04:36 Arnstein wrote:CTRL+C does the trick  I just wanted to look at how/if the number would go to 3.5 if I would take the average of X dice rolls, and then I could just CTRL+C when I wanted to stop. Then while (true) may be better to write instead. It's more readable than while (1) Or even better: bool loopShouldContinue = true; while (loopShouldContinue) { // do your work // when you're finished loopShouldContinue = false; // or you could write break; // but then that may make the point of the boolean variable irrelevant // it just depends on what you want to do exactly }
Don't forget to import #include <stdbool.h>
if you use the boolean approach. ############################# I've got a question. When you don't need a class instance to do something (e.g. to get an unsorted array and return a sorted one), is it advisable to make that method static in general? loopShouldContinue clearly does express the intention of the programmer precisely enough, it's even less expressive than while(1)! try: while(sinceThisProgramIsJustForMyOwnExperimentationThisLoopWillJustRunUntilITerminatedAtMyDiscretinonWithCtrlC) Don't be so grumpy. The name of the boolean variable can be easily renamed, and it was just an example. You're just acting like an ass at the moment with your sarcasm. your post was well-intentioned, but misguided and misplaced nitpicking, borderline overbearing... it persists!
Well, it's hard to give a proper name for a boolean that is used for while condition if you're unclear about context. E.g. boolean stringFound when searching for a string. Yes, "isDone" / "done" may be better than "loopShouldContinue" but it's still as generic. You just nitpicked about the variable's name, while it was just an example, hence the guy is supposed to rename it. 
And yes, I use higher level languages (Java and Objective-C) which may explain why I prefer while (true) rather than while (1)
|
On January 08 2014 23:19 darkness wrote:Show nested quote +On January 08 2014 21:51 nunez wrote:On January 08 2014 20:05 darkness wrote:On January 08 2014 09:50 nunez wrote:On January 08 2014 07:55 darkness wrote:On January 08 2014 04:36 Arnstein wrote:CTRL+C does the trick  I just wanted to look at how/if the number would go to 3.5 if I would take the average of X dice rolls, and then I could just CTRL+C when I wanted to stop. Then while (true) may be better to write instead. It's more readable than while (1) Or even better: bool loopShouldContinue = true; while (loopShouldContinue) { // do your work // when you're finished loopShouldContinue = false; // or you could write break; // but then that may make the point of the boolean variable irrelevant // it just depends on what you want to do exactly }
Don't forget to import #include <stdbool.h>
if you use the boolean approach. ############################# I've got a question. When you don't need a class instance to do something (e.g. to get an unsorted array and return a sorted one), is it advisable to make that method static in general? loopShouldContinue clearly does express the intention of the programmer precisely enough, it's even less expressive than while(1)! try: while(sinceThisProgramIsJustForMyOwnExperimentationThisLoopWillJustRunUntilITerminatedAtMyDiscretinonWithCtrlC) Don't be so grumpy. The name of the boolean variable can be easily renamed, and it was just an example. You're just acting like an ass at the moment with your sarcasm. your post was well-intentioned, but misguided and misplaced nitpicking, borderline overbearing... it persists! Well, it's hard to give a proper name for a boolean that is used for while condition if you're unclear about context. E.g. boolean stringFound when searching for a string. Yes, "isDone" / "done" may be better than "loopShouldContinue" but it's still as generic. You just nitpicked about the variable's name, while it was just an example, hence the guy is supposed to rename it.  And yes, I use higher level languages (Java and Objective-C) which may explain why I prefer while (true) rather than while (1)
haha, yes, i am aware of the irony. ;>
|
On January 08 2014 07:55 darkness wrote:Show nested quote +On January 08 2014 04:36 Arnstein wrote:CTRL+C does the trick  I just wanted to look at how/if the number would go to 3.5 if I would take the average of X dice rolls, and then I could just CTRL+C when I wanted to stop. Then while (true) may be better to write instead. It's more readable than while (1) Or even better: bool loopShouldContinue = true; while (loopShouldContinue) { // do your work // when you're finished loopShouldContinue = false; // or you could write break; // but then that may make the point of the boolean variable irrelevant // it just depends on what you want to do exactly }
Don't forget to import #include <stdbool.h>
if you use the boolean approach. ############################# I've got a question. When you don't need a class instance to do something (e.g. to get an unsorted array and return a sorted one), is it advisable to make that method static in general?
Thanks for the suggestion. I suppose i's always important to consider different approaches. However, in this case, when I was just experimenting on my own, I do not think it is necessary to do all that stuff. All I wanted to do in the end, was to check how the average of many dice rolls would end up being around 3,5. What I learnt from this, is that you should use %d to printf ints, and %f to printf floats, so by just doing that stupid thing, I actually learnt something! I love it when my curiosity ends up teaching me something 8)
|
On January 09 2014 01:15 Arnstein wrote: All I wanted to do in the end, was to check how the average of many dice rolls would end up being around 3,5.
And your program doesn't do that. Dice rolls can be simulated by a PRNG. Your program doesn't generate PRNG numbers. Why? Your srand(time(NULL)) is inside the while loop, which means every time you initialize the PRNG with a specific seed. Since you execute the while loop multiple times per second (unless you use a potato) the seed is identical. Which means rand() returns the same value for every execution in a second. You should invoke srand exactly once outside your while loop.
|
I know the thing I posted here doesn't even divide by the number of throws. I just had to figure out how to print ints and floats before doing the rest. I have a background from C++, and it's the same srand function, but I forgot that it should be outside until the first time it printed the sum when aI realized it :p I can post the complete code if you are interested, but I'm not at the computer at the moment.
Edit: Here it is:
+ Show Spoiler + #include <stdio.h> #include <time.h>
int main() { unsigned long int sum = 0; unsigned int n = 1; double average; srand(time(NULL)); while ( 1 ) { sum += rand() % 6 + 1; average = (double)sum / (double)n; n += 1; if ( (n % 1000000) == 0 ) printf("The average is:%f\n", average);
} return 0; }
|
Was hoping someone in here could help me with an SQL query, it's an MS 2008 R2 client.
The following is that i have a table with receipts, there are columns for how big the bank sale of of each receipt was. I need a query that pretty much goes "where x + x = y" the following simple try returns some issues:
where subtotal + subtotal = '100,50' and created >= '2014-01-01'
This can return the same receipt twice, aka taking the same receipt x 2 = y. I need to ensure that it takes two separate ones and try to match them to the Y value, something i cannot figure out how to do despite decent google skills. Anyone that can help me out? I assume i have to assign something to each subtotal colum in the query so that it is something like where subtotal(x) + subtotal(z) (WHERE subtotal(x) <> subtotal(z)) = y but i am very clueless about the syntax.
|
On January 09 2014 19:35 unkkz wrote: Was hoping someone in here could help me with an SQL query, it's an MS 2008 R2 client.
The following is that i have a table with receipts, there are columns for how big the bank sale of of each receipt was. I need a query that pretty much goes "where x + x = y" the following simple try returns some issues:
where subtotal + subtotal = '100,50' and created >= '2014-01-01'
This can return the same receipt twice, aka taking the same receipt x 2 = y. I need to ensure that it takes two separate ones and try to match them to the Y value, something i cannot figure out how to do despite decent google skills. Anyone that can help me out? I assume i have to assign something to each subtotal colum in the query so that it is something like where subtotal(x) + subtotal(z) (WHERE subtotal(x) <> subtotal(z)) = y but i am very clueless about the syntax. Well without knowing the table structure or the query as a whole, it will be hard to help and even understand this problem. I read it several times and still can not understand what is exactly the issue here.
You mention that you are getting duplicated values, but this would be only possible if you would join the table with another table. Perhaps there are receipts with the same values.
Seeing you mention something like subtotals, I take it there is a GROUP BY in the query, or a window calculation. But not seeing the query itself (or at least the conditions and GROUP BY clause) it will be hard to help you out.
|
On January 09 2014 19:35 unkkz wrote: Was hoping someone in here could help me with an SQL query, it's an MS 2008 R2 client.
The following is that i have a table with receipts, there are columns for how big the bank sale of of each receipt was. I need a query that pretty much goes "where x + x = y" the following simple try returns some issues:
where subtotal + subtotal = '100,50' and created >= '2014-01-01'
This can return the same receipt twice, aka taking the same receipt x 2 = y. I need to ensure that it takes two separate ones and try to match them to the Y value, something i cannot figure out how to do despite decent google skills. Anyone that can help me out? I assume i have to assign something to each subtotal colum in the query so that it is something like where subtotal(x) + subtotal(z) (WHERE subtotal(x) <> subtotal(z)) = y but i am very clueless about the syntax.
is there any reason you are going so generic with your description? it makes it hard to help...
are you joining back on your own table or using two different columns from the same table for your columns? posting your table and query so far would make this a ton easier.
with that being said if you are joining to yourself and you have some id column just add and id1 != id2 to your where clause... if you are using multiple columns add col1 != col2
edit: too slow ... shouldn't have used my tablet or I could have been first
|
On January 09 2014 20:24 Roman666 wrote:Show nested quote +On January 09 2014 19:35 unkkz wrote: Was hoping someone in here could help me with an SQL query, it's an MS 2008 R2 client.
The following is that i have a table with receipts, there are columns for how big the bank sale of of each receipt was. I need a query that pretty much goes "where x + x = y" the following simple try returns some issues:
where subtotal + subtotal = '100,50' and created >= '2014-01-01'
This can return the same receipt twice, aka taking the same receipt x 2 = y. I need to ensure that it takes two separate ones and try to match them to the Y value, something i cannot figure out how to do despite decent google skills. Anyone that can help me out? I assume i have to assign something to each subtotal colum in the query so that it is something like where subtotal(x) + subtotal(z) (WHERE subtotal(x) <> subtotal(z)) = y but i am very clueless about the syntax. Well without knowing the table structure or the query as a whole, it will be hard to help and even understand this problem. I read it several times and still can not understand what is exactly the issue here. You mention that you are getting duplicated values, but this would be only possible if you would join the table with another table. Perhaps there are receipts with the same values. Seeing you mention something like subtotals, I take it there is a GROUP BY in the query, or a window calculation. But not seeing the query itself (or at least the conditions and GROUP BY clause) it will be hard to help you out.
That is my query, there isn't anything else, i think you overestimate me a little bit heh i'm being so generic because i am not good at SQL and i notice that i forgot to mention that subtotal was a column, which is why you guys are even more confused i bet, sorry about that. I mention duplicate values because running the simple where subtotal + subtotal = '100' and created >= '2014-01-01' gives me one single hit that has half the value i am looking for, aka it takes subtotal x2.
The issue is that i need to find two separate values from the column "subtotal" that equals another value that i want to find. I am looking for say 220 and want to find all possible combinations that equals 220 if u add them together from the column subtotal. I actually have something to differentiate them by, another column named receiptid which is unique per entry in the table. I am also doing this query just straight into the table itself which i dont know if its the proper way or not.
|
On January 09 2014 19:35 unkkz wrote: Was hoping someone in here could help me with an SQL query, it's an MS 2008 R2 client.
The following is that i have a table with receipts, there are columns for how big the bank sale of of each receipt was. I need a query that pretty much goes "where x + x = y" the following simple try returns some issues:
where subtotal + subtotal = '100,50' and created >= '2014-01-01'
This can return the same receipt twice, aka taking the same receipt x 2 = y. I need to ensure that it takes two separate ones and try to match them to the Y value, something i cannot figure out how to do despite decent google skills. Anyone that can help me out? I assume i have to assign something to each subtotal colum in the query so that it is something like where subtotal(x) + subtotal(z) (WHERE subtotal(x) <> subtotal(z)) = y but i am very clueless about the syntax.
I assume you have an inner join there and what you are doing is t1.subtotal + t2.subtotal where t1, t2 are both the same table from inner join, the only thing you need is to add t1.id != t2.id and it won't compare sums of the same record
|
On January 09 2014 20:59 artynko wrote:Show nested quote +On January 09 2014 19:35 unkkz wrote: Was hoping someone in here could help me with an SQL query, it's an MS 2008 R2 client.
The following is that i have a table with receipts, there are columns for how big the bank sale of of each receipt was. I need a query that pretty much goes "where x + x = y" the following simple try returns some issues:
where subtotal + subtotal = '100,50' and created >= '2014-01-01'
This can return the same receipt twice, aka taking the same receipt x 2 = y. I need to ensure that it takes two separate ones and try to match them to the Y value, something i cannot figure out how to do despite decent google skills. Anyone that can help me out? I assume i have to assign something to each subtotal colum in the query so that it is something like where subtotal(x) + subtotal(z) (WHERE subtotal(x) <> subtotal(z)) = y but i am very clueless about the syntax. I assume you have an inner join there and what you are doing is t1.subtotal + t2.subtotal where t1, t2 are both the same table from inner join, the only thing you need is to add t1.id != t2.id and it won't compare sums of the same record
Mind giving me an example? Pretty SQL noob here
|
select * from t1, t2 where t1.id != t2.id - and it is not an inner join, for some reason I blanked and called it incorrectly
|
On January 09 2014 21:09 unkkz wrote:Show nested quote +On January 09 2014 20:59 artynko wrote:On January 09 2014 19:35 unkkz wrote: Was hoping someone in here could help me with an SQL query, it's an MS 2008 R2 client.
The following is that i have a table with receipts, there are columns for how big the bank sale of of each receipt was. I need a query that pretty much goes "where x + x = y" the following simple try returns some issues:
where subtotal + subtotal = '100,50' and created >= '2014-01-01'
This can return the same receipt twice, aka taking the same receipt x 2 = y. I need to ensure that it takes two separate ones and try to match them to the Y value, something i cannot figure out how to do despite decent google skills. Anyone that can help me out? I assume i have to assign something to each subtotal colum in the query so that it is something like where subtotal(x) + subtotal(z) (WHERE subtotal(x) <> subtotal(z)) = y but i am very clueless about the syntax. I assume you have an inner join there and what you are doing is t1.subtotal + t2.subtotal where t1, t2 are both the same table from inner join, the only thing you need is to add t1.id != t2.id and it won't compare sums of the same record Mind giving me an example? Pretty SQL noob here  So basically you want to find subsets of receipts which values sum up to a given value. Well, this is not a straight forward query, but it is doable.
If you can confirm that this is exactly what you want I can show you how to write such query, however I can do it later in the afternoon as I have to go out now and writing this query and explanations for it is not something that will take 5 minutes. Also I will need to check out availability of certain query mechanisms on MS SQL, but I think this will be possible to be done.
|
On January 09 2014 21:42 Roman666 wrote:Show nested quote +On January 09 2014 21:09 unkkz wrote:On January 09 2014 20:59 artynko wrote:On January 09 2014 19:35 unkkz wrote: Was hoping someone in here could help me with an SQL query, it's an MS 2008 R2 client.
The following is that i have a table with receipts, there are columns for how big the bank sale of of each receipt was. I need a query that pretty much goes "where x + x = y" the following simple try returns some issues:
where subtotal + subtotal = '100,50' and created >= '2014-01-01'
This can return the same receipt twice, aka taking the same receipt x 2 = y. I need to ensure that it takes two separate ones and try to match them to the Y value, something i cannot figure out how to do despite decent google skills. Anyone that can help me out? I assume i have to assign something to each subtotal colum in the query so that it is something like where subtotal(x) + subtotal(z) (WHERE subtotal(x) <> subtotal(z)) = y but i am very clueless about the syntax. I assume you have an inner join there and what you are doing is t1.subtotal + t2.subtotal where t1, t2 are both the same table from inner join, the only thing you need is to add t1.id != t2.id and it won't compare sums of the same record Mind giving me an example? Pretty SQL noob here  So basically you want to find subsets of receipts which values sum up to a given value. Well, this is not a straight forward query, but it is doable. If you can confirm that this is exactly what you want I can show you how to write such query, however I can do it later in the afternoon as I have to go out now and writing this query and explanations for it is not something that will take 5 minutes. Also I will need to check out availability of certain query mechanisms on MS SQL, but I think this will be possible to be done.
Yeah that would be it. Wouldn't something like select * from dbo.poslog where subtotal.id1 + subtotal.id2 = '553,90' and subtotal.id1 != subtotal.id2 except with proper syntax work? Trying to figure out how to get id's on columns. I can't alter stuff since this is a prod server.
|
On January 09 2014 21:59 unkkz wrote:Show nested quote +On January 09 2014 21:42 Roman666 wrote:On January 09 2014 21:09 unkkz wrote:On January 09 2014 20:59 artynko wrote:On January 09 2014 19:35 unkkz wrote: Was hoping someone in here could help me with an SQL query, it's an MS 2008 R2 client.
The following is that i have a table with receipts, there are columns for how big the bank sale of of each receipt was. I need a query that pretty much goes "where x + x = y" the following simple try returns some issues:
where subtotal + subtotal = '100,50' and created >= '2014-01-01'
This can return the same receipt twice, aka taking the same receipt x 2 = y. I need to ensure that it takes two separate ones and try to match them to the Y value, something i cannot figure out how to do despite decent google skills. Anyone that can help me out? I assume i have to assign something to each subtotal colum in the query so that it is something like where subtotal(x) + subtotal(z) (WHERE subtotal(x) <> subtotal(z)) = y but i am very clueless about the syntax. I assume you have an inner join there and what you are doing is t1.subtotal + t2.subtotal where t1, t2 are both the same table from inner join, the only thing you need is to add t1.id != t2.id and it won't compare sums of the same record Mind giving me an example? Pretty SQL noob here  So basically you want to find subsets of receipts which values sum up to a given value. Well, this is not a straight forward query, but it is doable. If you can confirm that this is exactly what you want I can show you how to write such query, however I can do it later in the afternoon as I have to go out now and writing this query and explanations for it is not something that will take 5 minutes. Also I will need to check out availability of certain query mechanisms on MS SQL, but I think this will be possible to be done. Yeah that would be it. Wouldn't something like select * from dbo.poslog where subtotal.id1 + subtotal.id2 = '553,90' and subtotal.id1 != subtotal.id2 except with proper syntax work? Trying to figure out how to get id's on columns. I can't alter stuff since this is a prod server.
select t1.*, t2.* from dbo.poslog t1 inner join dbo.poslog t2 on t1.subtotal + t2.subtotal = 553,90 and t1.receiptid != t2.receiptid;
That would be the proper syntax. However, you want that only to work for pairs of receipts? It will identify paired receipts, but this way, you will not be able to find, say three receipts which sum of subtotals would equal to 553,90.
|
Then you will also run into the problem where you have duplicates where one id is in first column and second in second and a duplicate where it is inverted, but that is another mystery to solve
|
Try using a cross join. Cross joins will take two sets of rows and generate every possible combination between them.
SELECT * FROM dbo.poslog t1 CROSS JOIN dbo.poslog t2 WHERE t1.receiptid != t2.receiptid AND t1.subtotal + t2.subtotal = 553,90
If you want three or more possible combinations, you can union them
SELECT t1.receiptid as Receipt1, t2.receiptid as Receipt2, -1 as Receipt3 FROM dbo.poslog t1 CROSS JOIN dbo.poslog t2 WHERE t1.receiptid != t2.receiptid AND t1.subtotal + t2.subtotal = 553,90
UNION
SELECT t1.receiptid as Receipt1, t2.receiptid as Receipt2, t3.receiptid as Receipt3 FROM dbo.poslog t1 CROSS JOIN dbo.poslog t2 CROSS JOIN dbo.poslog t3 WHERE t1.receiptid != t2.receiptid AND t1.receiptid != t3.receiptid AND t2.receiptid != t3.receiptid AND t1.subtotal + t2.subtotal + t3.subtotal = 553,90
... If you need more than three, or the ability to choose how many combinations on demand, you can use temp tables or dynamic sql.
|
On January 10 2014 01:17 artynko wrote: Then you will also run into the problem where you have duplicates where one id is in first column and second in second and a duplicate where it is inverted, but that is another mystery to solve
Right, it will pop each pair twice with interchanged positions. The simplest way to fix it that comes to my mind would be:
with pairs as ( select t1.receiptid id1, t2.receiptid id2 from dbo.poslog t1 inner join dbo.poslog t2 on t1.subtotal + t2.subtotal = 553,90 and t1.receiptid != t2.receiptid ), distinct_pairs as ( select t1.id1, t1.id2 from pairs t1 inner join pairs t2 on t1.id1 = t2.id2 and t1.id2 = t2.id1 and t1.id1 > t1.id2 ) select t2.*, t3.* from distinct_pairs t1 inner join dbo.poslog t2 on t1.id1 = t2.receiptid inner join dbo.poslog t3 on t1.id2 = t3.receiptid;
Google up the WITH clause if you want to find more about it, it is well documented. Basically here I used it as a simple subquery, but it can be used in other ways.
First we find the pairs, we select only ids, as these will uniquely identify our records. (pairs query) Then we know there will be duplicates, so we take only these, where id1 > id2. We know that id1 and id2 will never be equal as we excluded such pairs in the beginning and we take only one side. Finally when we have set of distinct pairs, we join up one poslog table with one of the ids, and second poslog table with second of the ids.
EDIT: Edited in relevant quote.
|
hups sorry, just skimmed through the previous answer, posted a condescending answer
EDIT: As far as I know the easiest way if you only want a sum of TWO records and it will always be combination of TWO is "select * from t1, t2 where t1.id != t2.id and t1.id < t2.id and t1.value + t2.value > 9000"
|
On January 10 2014 03:12 artynko wrote: hups sorry, just skimmed through the previous answer, posted a condescending answer
EDIT: As far as I know the easiest way if you only want a sum of TWO records and it will always be combination of TWO is "select * from t1, t2 where t1.id != t2.id and t1.id < t2.id and t1.value + t2.value > 9000"
Well, apart from the fact that you do not need t1.id != t2.id when you use t1.id < t2.id you are absolutely right. I have no idea why I overcomplicated it so much. Bleh...
|
|
|
|
|
|