simple sql LIMIT statement - mysqli

Can anyone tell me if this is a valid sql statement?..
SELECT * FROM myTable WHERE foo="bar" ORDER BY foo_date DESC LIMIT 0, 5
I've searched online but the difficulty is a) inexperience and b) finding an example that contains the components I'm trying to assemble.
I want the statement to select ALL from the table where condition1 is true, then order the results by descending date, then LIMIT the returned rows to those specified...
many thanks to anyone that can give me a pointer in the right direction.
Scott

SELECT * FROM myTable WHERE foo="bar" ORDER BY foo_date DESC LIMIT 0, 5
...is fine - runs in phpMyAdmin no problem. I should have tested there before posting on SO - apologies.

Related

Error While running query to find count difference

I want to find the difference between 2 counts from same view i.e -
Difference between Total count and Distinct count from the same VIEW view_z_a_base.
FYI - The view structure is -
TABLE z_a is created from the VIEW view_z_a which is created from VIEW view_z_a_base.
Query Being Executed -
SELECT COUNT(*) -(SELECT COUNT(*)FROM (SELECT DISTINCT * FROM
<schema>.vw_z_a_base))AS no_of_duplicates
FROM <schema>.vw_z_a_base;
EXPECTED RESULT -
Difference of the 2 counts -
?column?
0
ACTUAL RESULT -
ERROR MESSAGE _
An error occurred when executing the SQL command:
SELECT COUNT(*) -(SELECT COUNT(*)
FROM (SELECT DISTINCT * FROM <schema>.vw_z_a_base))
FROM <schema>.vw_z_a_base
[Amazon](500310) Invalid operation: Not implemented
Details:
-----------------------------------------------
error: Not implemented
code: 1001
context: 'value' - Ill formed PARAM_EXEC in expression
query: 41412011
location: pg_utils.cpp:1710
process: padbmaster [pid=22726]
-----------------------------------------------;
1 statement failed.
The same query works for Tables but not for views, why is this happening ?
I ran into a similar error and it ended up having to do largely with typecasting in my case. Included below b/c theres literally three google results for this error so hopefully it helps someone. In your case I would suspect that it has to do with the SELECT COUNT(*)FROM (SELECT DISTINCT * FROM <schema>.vw_z_a_base) element. I think you may be taking unnecessary steps here, which could be returning something that SQL is having a hard time performing subtraction with. You should be able to eliminate the nested select here, which also makes things easier to read. Try:
SELECT COUNT(*) - COUNT(DISTINT *) as no_of_duplicates from <schema>.vw_z_a_base
Returning to my case, my code went from:
case when coalesce(allup_charges,0) > 156.04 then 1
to:
case when coalesce(allup_charges::numeric,0)>156.04 then 1
This resolved my issue. I also found that mysqlworkbench was much less fussy with typecasting here than tableau. If you continue to run into the issue just over-typecast and you should be fine?

Combine count and max in postgresql sql

I have a problem to formulate an sql question in postgresql, hoping to get some help here
I have a table called visitor that contains an column called fk_employee_id, fk_employee_id contains different number between 1-10, example:
1,3,4,6,4,6,7,3,2,1,6,7,6
Now I want to find out which value that is the most frequent in this column (in this case 6) I have made an question that seem to solve my question;
SELECT fk_employee_id
FROM visitor
GROUP BY fk_employee_id
ORDER BY COUNT(fk_employee_id) DESC
LIMIT 1
but this question, doesn't get right if it is two values that are the most frequent one. So instead I try to write a question which contains max function but cant figure out how, anyone now how to do this?
We can use RANK here to slightly modify your current query:
WITH cte AS (
SELECT
fk_employee_id,
RANK() OVER (ORDER BY COUNT(*) DESC) rank
FROM visitor
GROUP BY fk_employee_id
)
SELECT fk_employee_id
FROM cte
WHERE rank = 1;
Demo

Selecting a random ID from the 4 lowest ID in the same table

I would be grateful if you could help me.
I would like to write a select statement which would do the following:
"Select a random ID from the 4 lowest ID in the same table"
Note: ID may be for example the score of students from a list of around 100 records in a table. I would like to get the 4 students with the lowest score and finally pick one record randomly.
Thanks lots
I find out about the following code:
SELECT * FROM (select * from tablename ORDER BY status ASC limit 4) q ORDER BY RAND() LIMIT 0,1;
Kindly confirmed whether it is a good answers and proper way to do the select. Any answers would be welcomes
thanks much

Why does this Oracle 10g SQL run slow only when I query a subquery with a where clause?

I can't paste in the entire SQL for various reasons, so consider this example:
select *
from
(select nvl(get_quantity(1), 10) available_qty
from dual)
where available_qty > 30;
get_quantity is a function that makes a calculation based on the ID of a record that's passed through it. If it returns null, I use nvl() to force it to 10.
The query runs very slow when I use the WHERE clause in the parent query. When I comment out the WHERE clause, however, it runs very fast. What I don't get is why it can display the data very fast, but it can't query it just as fast. I am querying the results of a subquery, too. I was under the impression that subqueries return a "rendered" dataset. It's almost as if querying the available_qty identifier is causing it to reference something within the subquery.
This is why I don't think the contents of the get_quantity function are relevant here, so I didn't bother posting it. Instead, I think it's a misunderstanding on my part of how Oracle handles subqueries and whatnot.
Do any of you Oracle gurus have any idea what I am doing wrong?
Afterthought: as I was entering tags for this question, the tag "correlated subquery" came up. In doing some quick research, it seems that this type of subquery somewhat depends on the outer query. Could this be related to my problem?
Let's try an experiment. First we'll run the following query:
select lvl, rnd
from (select level as lvl from dual connect by level <= 5) a,
(select dbms_random.value() rnd from dual) b;
The "a" subquery will return 5 rows with values from 1 to 5. The "b" subquery will return one row with a random value. If the function is run before the two tables are join (by Cartesian), the same random value will be returned for each row. The actual results:
LVL RND
---------- ----------
1 .417932089
2 .963531718
3 .617016889
4 .128395638
5 .069405568
5 rows selected.
Clearly the function was run for each of the joined rows, not for the subquery before the join. This is a result of Oracle's optimizer deciding that the best path for the query is to do things in that order. To prevent this, we have to add something to the second subquery that will make Oracle run the subquery in it's entirety before performing the join. We'll add rownum to the subquery, since Oracle knows rownum will change if it's run after the join. The following query demonstrates this:
select lvl, rnd from (
select level as lvl from dual connect by level <= 5) a,
(select dbms_random.value() rnd, rownum from dual) b;
As you can see from the results, the function was only run once in this case:
LVL RND
---------- ----------
1 .028513902
2 .028513902
3 .028513902
4 .028513902
5 .028513902
5 rows selected.
In your case, it seems likely that the filter provided by the where clause is making the optimizer take a different path, where it's running the function repeatedly, rather than once. By making Oracle run the subquery as written, you should get more consistent run-times.

Is there a way to find TOP X records with grouped data?

I'm working with a Sybase 12.5 server and I have a table defined as such:
CREATE TABLE SomeTable(
[GroupID] [int] NOT NULL,
[DateStamp] [datetime] NOT NULL,
[SomeName] varchar(100),
PRIMARY KEY CLUSTERED (GroupID,DateStamp)
)
I want to be able to list, per [GroupID], only the latest X records by [DateStamp]. The kicker is X > 1, so plain old MAX() won't cut it. I'm assuming there's a wonderfully nasty way to do this with cursors and what-not, but I'm wondering if there is a simpler way without that stuff.
I know I'm missing something blatantly obvious and I'm gonna kick myself for not getting it, but .... I'm not getting it. Please help.
Is there a way to find TOP X records, but with grouped data?
According to the online manual, Sybase 12.5 supports WINDOW functions and ROW_NUMBER(), though their syntax differs from standard SQL slightly.
Try something like this:
SELECT SP.*
FROM (
SELECT *, ROW_NUMBER() OVER (windowA ORDER BY [DateStamp] DESC) AS RowNum
FROM SomeTable
WINDOW windowA AS (PARTITION BY [GroupID])
) AS SP
WHERE SP.RowNum <= 3
ORDER BY RowNum DESC;
I don't have an instance of Sybase, so I haven't tested this. I'm just synthesizing this example from the doc.
I made a mistake. The doc I was looking at was Sybase SQL Anywhere 11. It seems that Sybase ASA does not support the WINDOW clause at all, even in the most recent version.
Here's another query that could accomplish the same thing. You can use a self-join to match each row of SomeTable to all rows with the same GroupID and a later DateStamp. If there are three or fewer later rows, then we've got one of the top three.
SELECT s1.[GroupID], s1.[Foo], s1.[Bar], s1.[Baz]
FROM SomeTable s1
LEFT OUTER JOIN SomeTable s2
ON s1.[GroupID] = s2.[GroupID] AND s1.[DateStamp] < s2.[DateStamp]
GROUP BY s1.[GroupID], s1.[Foo], s1.[Bar], s1.[Baz]
HAVING COUNT(*) < 3
ORDER BY s1.[DateStamp] DESC;
Note that you must list the same columns in the SELECT list as you list in the GROUP BY clause. Basically, all columns from s1 that you want this query to return.
Here's quite an unscalable way!
SELECT GroupID, DateStamp, SomeName
FROM SomeTable ST1
WHERE X <
(SELECT COUNT(*)
FROM SomeTable ST2
WHERE ST1.GroupID=ST2.GroupID AND ST2.DateStamp > ST1.DateStamp)
Edit Bill's solution is vastly preferable though.