Understanding random ordering in Rails + postgresql - sql

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

Related

SQL column name with function

I want to create 3 new columns with their names reffering to some date varibales and this is not possible to write them like this. So the first column name should be YEAR2022, 2nd column YEAR2021 and 3rd column YEAR2020.
Can you please give me an idea how to write this?
select column1*2 as CONCAT('YEAR',YEAR(DATEADD(YY,0,GETDATE()))),
column1*3 as CONCAT('YEAR',YEAR(DATEADD(YY,-1,GETDATE()))),
column1*4 as CONCAT('YEAR',YEAR(DATEADD(YY,-2,GETDATE()))) FROM table1
The error that I get is:
Incorrect syntax near 'YEAR'.
As I mentioned in my comment, an alias cannot be an expression, it has to be a literal. As such the expressions you have tried to use are not allowed and generate syntax errors.
Normally, this sort requirement is the sign of a design flaw, or that you're trying to do something that should be in the presentation in the SQL layer. I'm going to assume the latter here. As a result, instead you should use static names for the columns, and then in your presentation layer, control the name of the columns there, so that when they are presented to the end user they have the names you want (for today that would be YEAR2022, YEAR2021 and YEAR2020). Then your query would just look like this:
select column1*2 AS ThisYear,
column1*3 AS YearPrior,
column1*4 AS Year2Prior
FROM dbo.table1;
How you change the names of the columns in your presentation layer is a completely different question (we don't even know what language you are using to write your application). If you want to ask about that, I suggest asking a new question (only if after you've adequately researched the problem and failed to find a solution), so that we can help you there.
Note that Though you can achieve a solution via dynamic SQL, I would strongly suggest it is the wrong solution here, and thus why I haven't given an answer providing a demonstration.

Order not working correctly in MDX

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

Negative query issue in Lucene

Take a look at this query :
-HOSTNAME:ram AND SEVERITY:information
The above query isn't giving me the intended results. I know we need to append a * : * to a query that it is totally negative, also I found that the above case could be solved by appending a * : * to the HOSTNAME search criteria, but it might be a serious performance crunch, since my application gets search queries from the end user directly.
How do I go about this and help me arrive at an optimal solution please!
Your query should work OK as written, However, including only negative terms does not work:
-HOSTNAME:ram AND -SEVERITY:ERROR
as there is nothing to subtract from (at least, that is the way I think of it).

SQL - Return Columns with a specific value

Before I ask my question will layout what I'm trying to do.
I have this table such as below
Columns - PID, Choice1, Choice2,......Choice10
Rows - 1,X, O, X, O.........
Ive been searching on the net for quite some time and need a little push in the right direction if what I'm trying to do is possible. While getting the coding will help me with the small project I'm doing, it doesn't really help me learn more about SQl.
Is it possible to do a search on the table and return only the columns that have a value of X where PID = some value??
My gut instinct is saying no and I might have to restructure my database to accomplish what I'm doing. As i said a point in the right direction where I can read up on what I'm trying to do is great, getting the coding for it.. really doesn't help me learn it for future reference.
It does sound like you should restructure your database, but you can use PIVOT and UNPIVOT to transpose and restructure the output table. Columns are normally fixed with a variable number of rows depending on the WHERE clause. Using PIVOT can swap columns for rows, giving you what you need.

How to get multi row data of one column to one row of one Column

I need to get data in multiple row of one column.
For example data from that format
ID Interest
Sports
Cooking
Movie
Reading
to that format
ID Interest
Sports,Cooking
Movie,Reading
I wonder that we can do that in MS Access sql. If anybody knows that, please help me on that.
Take a look at Allen Browne's approach: Concatenate values from related records
As for the normalization argument, I'm not suggesting you store concatenated values. But if you want to join them together for display purposes (like a report or form), I don't think you're violating the rules of normalization.
This is called de-normalizing data. It may be acceptable for final reporting. Apparently some experts believe it's good for something, as seen here.
(Mind you, kevchadder's question is right on.)
Have you looked into the SQL Pivot operation?
Take a look at this link:
http://technet.microsoft.com/en-us/library/ms177410.aspx
Just noticed you're using access. Take a look at this article:
http://www.blueclaw-db.com/accessquerysql/pivot_query.htm
This is nothing you should do in SQL and it's most likely not possible at all.
Merging the rows in your application code shouldn't be too hard.