I am trying to understand sql recursive statements, but it is really hard for me. For example:
Thats an exampe I am trying to understand and to write down the output.
Can sb pls explain me how this works, step by step?
greetings and thx in advance
maya
Also, see here - http://www.postgresql.org/docs/8.4/static/queries-with.html, where the steps of the recursive query evaluation are described.
The UNION ALL portion gets recursed until it returns no more records. I don't know if you can have multiple UNION ALL portions.
Related
SQL learner here, trying to make a join, but it seems to not work.
I have the following 2 tables:
#device_combined_players
#final_results2
The goal is to have a new table replacing the player_store_id from #final_results with the pseudo_name from #device_combined_players.
I have tried with:
SELECT #final_results2.player_store_id,
#device_combined_players.pseudo_name, #final_results2.genre FROM #device_combined_players INNER JOIN #final_results2 ON #final_results2.player_store_id = #device_combined_players.store_id
but I can't make it work, my output is simply an empty table.
Could you guys, please, give me some light? Thank you!
My expected result would be: as #final_results2 (image 2), but replacing player_store_id column by pseudo_name from #device_combined_players table.
EDIT: a screenshot with more details:
https://i.stack.imgur.com/lSyLb.png
And, I am answering myself, since I had 2 different issues here. Both are rookie mistakes, but I will leave them here so it helps somebody else in the future:
Take a look at the data type of your columns. They can cause conflicts if they are not the same as the columns you are referencing in a JOIN.
Check if your data doesn't have spaces. You can use TRIM and LEN to help you out answering this.
I sorted my problem like that.
So, I'd like to do some random ordering when displaying data the code I have at this point is:
Timsheet.limit(1).offset(RANDOM(Timesheet.count)).first
I know that postgresql's (RANDOM) syntax is different than MYSQL's (RAND()) and Oracles (dbms_random.value) syntax.
I'd just like to confirm that my code is correct, from what I understand it's doing is, it's grabbing the first row, and offsetting the data in a random order?
Please help clear this up, thanks!
I think the following will work with all DBMS's
Timsheet.offset(Random.new.rand(Timsheet.count)).first
I'm new to MDX and I cannot get the ordering correct. I looked at references online and I think i sorted the query correctly, but the result of the query doesn't agree with me. Can anyone shed some light into what I'm not doing.
I have included a hypothetical example that is close to my problem.
The result of the query comes out without being sorted.
Any help is deeply appreciated.
Thanks
The 2nd argument to the Order()-function on your Rows-axis, must be the value or string to sort by. If you want to sort by the names of the SalespersonID-members, do something like this:
Order([Sales].[SalespersonID], [Sales].CURRENTMEMBER.MEMBER_NAME) on Rows
I want to have query like this:
SELECT
sum(data_parts.size) as size_sum,
(size_sum/2.) as half_of_size_sum
FROM
data_parts
WHERE
data_parts.some_id='1';
Of course it won't work, because there's no column named size_sum, so my question is:
Is there a way to use size_sum as a parameter in next select item?
Other than using a subquery containing your current query (see Davide's answer), I don't think there is a way to do that.
But you could always do:
SELECT
sum(data_parts.size) as size_sum,
(sum(data_parts.size)/2.) as half_of_size_sum
FROM
data_parts
WHERE
data_parts.id='1';
Postgres is smart enough to only get that sum once. Only if this query will be greatly expanded with more calculations being done on size_sum would I recommend the subquery approach. Not because it works better, but because it will be easier to read. But if the current calculation is all you need, don't bother with a subquery. It would actually be less easy to read.
yes, a (somewhat) ugly way of making the query run there is...
SELECT
SIZE_SUM,
SIZE_SUM/2 AS HALF_OF_SIZE_SUM
FROM (
SELECT
sum(data_parts.size) as size_sum)
FROM
data_parts
WHERE
data_parts.id='1') X;
But I don't think there is a way on postgre to do operations directly based on the previous select fields
No, and too unnecesary to use subselects for this, simply use SUM(data_parts.size) again.
I'm not sure whether I catch your point.
Do you want the code like this?
SELECT
sum(data_parts.size) as size_sum,
(sum(data_parts.size)/2.) as half_of_size_sum
FROM
data_parts
WHERE
data_parts.some_id='1';
I'm executing a query which I want to randomize query results from a drupal database.
here's what I have done...
$res=db_query("SELECT DISTINCT nid FROM content_type_event ORDER BY RANDOM()");
but this query doesn't work. what's wrong with this query? please help me to solve this?
Thanks a lot...
You probably need to be using ORDER BY RAND() instead.
Also there is a bit you may want to know about ORDER BY RAND()'s performance if you are getting a considerable amount of results that will be randomized:
http://www.titov.net/2005/09/21/do-not-use-order-by-rand-or-how-to-get-random-rows-from-table/
EDIT: Just saw your comment that you are using PostgreSQL, this looks like it could be helpful: http://www.petefreitag.com/item/466.cfm
Sorry I can't be a lot of much help, I don't use PostgreSQL myself
You did not tell us which database engine you are using, but maybe ORDER BY RAND() would be better.
Update: okay, PostgreSQL uses RANDOM() and not RAND() so that's okay. I've found this question which seems to suggest that ORDER BY RANDOM() should work and the error is likely to be elsewhere. Maybe it's DISTINCT that is screwing things up; try your query again without DISTINCT.