Simple SELECT query problem - sql

I have this MySql select query which seems to have a bug but I am quite "green" so I simply cannot see, so maybe you could help?
Here is the query:
SELECT node_id
FROM rate
WHERE node_id='".$cat_node_id_string."'
LIMIT ".$node_count_star.",".$node_count_end."
ORDER BY SUM(amount)
GROUP BY node_id
Thanks for help in advance...
UPDATE:
I will post a mysql error to make it clearer...
You have an error in your SQL syntax;
check the manual that corresponds to
your MySQL server version for the
right syntax to use near 'GROUP BY
node_id LIMIT 1,20' at line 5

try this
SELECT node_id
FROM rate
WHERE node_id='".$cat_node_id_string."'
ORDER BY SUM(amount)
GROUP BY node_id
LIMIT ".$node_count_star.",".$node_count_end."
Be aware though, that the result will be a single record containing whatever $cat_node_id_string resolves to!
With WHERE node_id='".$cat_node_id_string."' you tell MySQL to only return those records where node_id matches an exact string.
With GROUP BY node_id you tell MySQL to group all records into one

Your error:
GROUP BY node_id LIMIT ,'
That comma there suggests that your limit variables $node_count_star and $node_count_end are empty.
Cheers & good luck.

Related

How to debug "ORA-00911: invalid character" in this SQL Query?

I tried to run the following query on Toad for Oracle. And it gives the error "ORA-00911: invalid character". Can someone please help me to figure out the issue?
DELETE FROM `CC_AUDIT_TRAIL`
WHERE SEQ_NO NOT IN (
SELECT SEQ_NO
FROM (
SELECT SEQ_NO
FROM `CC_AUDIT_TRAIL`
ORDER BY SEQ_NO DESC
LIMIT 1000
) foo
);
You have written a MySQL query. Perhaps you want this:
DELETE FROM CC_AUDIT_TRAIL
WHERE SEQ_NO NOT IN (SELECT SEQ_NO
FROM (SELECT SEQ_NO
FROM CC_AUDIT_TRAIL
ORDER BY SEQ_NO DESC
) foo
WHERE rownum <= 1000
);
Couple of observations:
To Aleksej's point, quotes around the table name are not necessary
You are sending direct query to Oracle, so semi-colon (;) is probably not needed
If above two point don't fix your issue, try retyping your code in Notepad editor and use it. This will eliminate any whitespace inconsistencies that may be present in your code and not visible to eyes (This is especially the case if you copied your code from another source)
P.S. Let the community know which one solved it for future reference.

SQL Apache Derby - Select last value in column

I am using Apache Derby and am trying to select the last value in a column.
Currently I have the following:
SELECT id FROM hotels ORDER BY id DESC WHERE ROWNUM <=1;
However this is resulting in a syntax error:
Syntax error: Encountered "WHERE" at line 1, column 44.
Would anyone know the proper way to write this query?
Thank you.
The order by clause goes after the where. Perhaps you intend:
SELECT MAX(id)
FROM hotels;

SQLite Query _ Select only different Varchar and add them to one

this might sound kind of strange, but what I want to do is this:
SQLite table # SQL Fiddle
I want as result:
peter
when I query ID<=5
and
alvinpeter
if ID is <=10 (names added to one in alphabetical order)
Anyone know how to do this with SQLite only?
Thanks in advance, best regards!
Following will do:
SELECT GROUP_CONCAT(name,'')
FROM (
SELECT DISTINCT name
FROM testtable
WHERE ID<=10
ORDER BY name
);
(in SQL Fiddle)

Why won't this statement work? SQL

I am using SQLFire. I am doing an assignment and when beginning the assignment I came across that I couldn't return a particular value. See this SQL Fiddle, it contains the EXACT data in the table that I am using in my assignment and it contains the SQL Statement that i have tried.
FIDDLE: http://sqlfiddle.com/#!2/e761ac/1
What i want to be outputted is:
Rate | RentalCode
-----------------
350 | WL
I am getting this error when I type my code into SQL Fire.
I Have been told NOT to use the ORDER BY clause and I have not learnt 'LIMIT'
Thank you
You need to have GROUP BY clause since you have non-aggregated column in the SELECT clause
SELECT MIN(Rate), Rentalcode
FROM RentalRates
GROUP BY Rentalcode
UPDATE
Since you want to get the lowest rate, I think this is the better way than using ORDER BY - LIMIT since it supports multiple records having the lowest rate.
SELECT *
FROM RentalRates
WHERE rate = (SELECT MIN(rate) FROM rentalrates)
It's not clear what you want to get with this query. I guess following will work:
SELECT Rate, Rentalcode
FROM RentalRates
order by Rate
LIMIT 1
SQLFiddle demo
As you see, even in SQLfiddle your result are quite strange - you're getting RentalCode from one record and Rate from another.
Select aggregate from table without grouping is MySQL syntax and not ANSI. If you want to get record with minimum Rate, here's a query:
select * from RentalRates order by Rate limit 1
sql fiddle demo

TSQL Distinct, and OrderBy and WHERE

I have the following SQL statement sample:
SELECT DISTINCT [CommentNdx]
,[CommentsText]
,[DateTimeAdded]
FROM [dbo].[CommentTable]
ORDER BY [dbo].[CommentTable].DateTimeStart DESC
WHERE [CommentsText] = 'Hello World'
I keep getting the error Incorrect syntax near the keyword 'WHERE'. I know the syntax is incorrect but I'm not sure how this should be formatted. Any help is appreciated.
UPDATE:
My mistake, I meant date time start should be datetimeadded. Corrected syntax.
SELECT DISTINCT [TestCommentNdx]
,[TestID]
,[CommentsText]
,[DateTimeAdded]
,[OperatorNdx]
FROM [PTDB].[dbo].[TestsComments]
WHERE [TestID] = 1174411854
ORDER BY [PTDB].[dbo].[TestsComments].[DateTimeAdded] DESC
UPDATE 2:
Thanks much everyone, one last thing, would it make a difference if there were joins in the select statement? I have a really long query with joins and when I try to use DISTINCT, I get ORDER BY items must appear in the select list if SELECT DISTINCT is specified.
The WHERE needs to come before the ORDER BY. Also, you won't be able to sort by DateTimeStart unless it's included in the SELECT statement.
SELECT DISTINCT [CommentNdx]
,[CommentsText]
,[DateTimeAdded]
FROM [dbo].[CommentTable]
WHERE [CommentsText] = 'Hello World'
ORDER BY [dbo].[CommentTable].DateTimeStart DESC -- you can't do this
ORDER BY follows the WHERE clause.
EDIT: As per #LukeGirvin post, you can't sort by a column that isn't included in the SELECT clause.