Similar to programming languages, is it possible to create a temporary variable and define it a value? e.g.
String date = '12-MAY-12'
Select COUNT (*) FROM TABLEX WHERE TABLE_DATE < DATE;
What you are after can easily be achieved using Oracle PL/SQL. I assume you want this using SQL only. If that's the case, here is one way you could do that:
WITH
variable_table AS
(
SELECT
to_date('12-MAY-2012') v_date
FROM
dual
)
SELECT
COUNT (*)
FROM
TABLEX
WHERE
TABLE_DATE <
(
SELECT
v_date
FROM
variable_table
)
I hope this helps
The answer is, it depends.
It depends on environment you use.
For example, Allround Automations PL/SQL Developer accepts sign & before string for variables (values of them are asked during query execution):
SELECT COUNT (*)
FROM TABLEX
WHERE TABLE_DATE < &DATE;
Related
I need to query orientDB to retrieve data aggregated by day.
E.g.:
SELECT OCCURRENCE, COUNT(1) AS NBR_VALUES FROM
(
SELECT END_PROCESSING.asDate().format('yyyy-MM-dd').asDate().asLong() AS OCCURRENCE
FROM MY_TABLE
WHERE END_PROCESSING >= ? AND END_PROCESSING <= ?
)
GROUP BY OCCURRENCE
However I need to perform this aggregation based on my client timezone (web browser).
Ideally it would be something similar to this:
SELECT END_PROCESSING.asDate('Europe/London')
Is there a way to achieve this or will I need to do this aggregation on the above layer (Java)?
Suggesting per my experience with oracle db.
How about making this a stored procedure? You can return the object with data. Accept the limits as parameters. Get the parameters from upper layer(javascript/java).
I mean something like(Please translate to your db syntax):
FUNCTION get_data(I_time1 TIMESTAMP, I_time2 TIMESTAMP)
RETURNS --your object type here---
IS
BEGIN
SELECT OCCURRENCE, COUNT(1) AS NBR_VALUES INTO --your objects here--
FROM
(
SELECT END_PROCESSING.asDate().format('yyyy-MM-dd').asDate().asLong() AS OCCURRENCE
FROM MY_TABLE
WHERE END_PROCESSING >= I_time1 AND END_PROCESSING <= I_time2
)
GROUP BY OCCURRENCE;
RETURN --your object--
;
END;
/
I'm using Oracle SQL and i need some help with a query.
In the following query i'm selecting some rows with a simple condition (never mind hat kind of). From the output rows, i need to select the row with minimum value of DATE. For that, i'm using ROWNUM.
SELECT *
FROM(
SELECT NAME, DATE
FROM LIST
WHERE NAME = 'BLABLA'
ORDER by DATE)
WHERE ROWNUM = 1;
However, this query must fit to any other SQL languages, and therefore i need to write this query without ROWNUM.
Is there a simple way to write this query without using ROWNUM?
Unfortunately, row limit syntax differs between RDBMS.
The following is portable between SqlServer, Oracle and PostGres:
SELECT *
FROM (
SELECT NAME, DATE, ROW_NUMBER() OVER (ORDER by DATE) AS RowNum
FROM LIST
WHERE NAME = 'BLABLA'
) X
WHERE RowNum = 1;
However, other DB's syntax is different, e.g. MySql's LIMIT
select * from LIST
where Date=(select min(date) from LIST where Name='BLABLA' )
and Name='BLABLA'
I am new to SQL and have been searching for a way to set variables in ANSI SQL. I have this:
select * from table1
where first_date > '2014-01-01'
and where second_date = '2014-01-01'
and where third_date < '2014-01-01'
but I am hoping for something like:
set x = '2010-12-31'
select * from table1
where first_date > x
and where second_date = x
and where third_date < x
I read about stored procedures but it seems like overkill for something so seemingly simple. I'm running on Netezza but I'd like a general solution that can also work on other databases.
Standard (ANSI) SQL does not have variables. But what you can do in standard SQL is use a common table expression to only have the value once.
The following is ANSI SQL:
with data (the_date) as (
values (date '2014-01-01')
)
select *
from table1
where first_date > (select the_date from data)
and second_date = (select the_date from data)
and third_date < (select the_date from data);
The above would work on most DBMS. Not all of them support the values clause like that, but that can usually be worked around using a plain select statement instead.
As I have never used Netezza I have no idea if it supports the row constructor (the values clause) or common table expressions (the with clause)
Also some SQL clients offer the ability to define variables that are replaced before the SQL is actually sent to the database server.
a_horse_with_no_name's solution is the only pure SQL solution I know that doesn't go into procedural SQL extenion territory. There is another solution that is not strictly what you asked for as it is specific to the NZSQL CLI only, but you can use variables there like this.
TESTDB.ADMIN(ADMIN)=> \set x '\'2014-01-01\''
TESTDB.ADMIN(ADMIN)=>
SELECT *
FROM table1
WHERE first_date < :x
AND second_date = :x
AND third_date = :x;
This question already has answers here:
Closed 11 years ago.
Possible Duplicate:
Multiple Select Statements using SQL Server 2005 “WITH” Statement
There's any way to get multiple result-sets from a query using WITH clause?
I'm using MS SQL 2005.
;with temp as
(
SELECT '1' as [Sample]
)
--SELECT COUNT(*) FROM temp
SELECT * FROM temp
This works with each select (count or *), but I need to return both result-sets from the same "temp".
Is that possible?
Use UNION ALL:
;with temp as
(
SELECT '1' as [Sample]
)
SELECT COUNT(*) FROM temp
UNION ALL
SELECT * FROM temp
I'd suggest you need to re-work whatever was meant to consume this pair of result sets so that it doesn't need to know how many results are going to be returned before the results arrive. If that's not the case, I can't think of where else you'd need to retrieve the COUNT of the result set as well as the result set itself.
I have a batch job that I run on a table which I'm sure I could write as a prepared statement. Currently it's all in Java and no doubt less efficient than it could be. For a table like so:
CREATE TABLE thing (
`tag` varchar,
`document` varchar,
`weight` float,
)
I want to create a new table that contains the top N entries for every tag. Currently I do this:
create new table with same schema
select distinct tag
for each tag:
select * limit N insert into the new table
This requires executing a query to get the distinct tags, then selecting the top N items for that tag and inserting them... all very inefficient.
Is there a stored procedure (or even a simple query) that I could use to do this? If dialect is important, I'm using MySQL.
(And yes, I do have my indexes sorted!)
Cheers
Joe
I haven't done this in a while (spoiled by CTE's in SQL Server), and I'm assuming that your data is ordered by weight; try
SELECT tag, document, weight
FROM thing
WHERE (SELECT COUNT(*)
FROM thing as t
WHERE t.tag = thing.tag AND t.weight < thing.weight
) < N;
I think that will do it.
EDIT: corrected error in code; need < N, not <= N.
If you were using SQL Server, I would suggest using the ROW_NUMBER function, grouped by tag, and select where row_number < N. (So in other words, order and number the rows for each tag according to their position in the tag group, then pick the top N rows from each group.) I found an article about simulating the ROW_NUMBER function in MySQL here:
http://www.xaprb.com/blog/2006/12/02/how-to-number-rows-in-mysql/
See if this helps you out!