I am passing a simple query where I am searching for specific rows where OrderID is an even number
SELECT *
FROM Orders
WHERE mod(OrderID,2) = 0;
Error :
Syntax error (missing operator) in query expression 'mod(OrderID,2) = 0'.
You are not using Oracle, so you should be using the modulus operator:
SELECT * FROM Orders where OrderID % 2 = 0;
The MOD() function exists in Oracle, which is the source of your confusion.
Have a look at this SO question which discusses your problem.
SELECT * FROM Orders where OrderID % 2 = 0;///this is for even numbers
SELECT * FROM Orders where OrderID % 2 != 0;///this is for odd numbers
Sql Server we can use %
select * from orders where ID % 2 = 0;
This can be used in both Mysql and oracle.
It is more affection to use mod function that %.
select * from orders where mod(ID,2) = 0
MOD() function exists in both Oracle and MySQL, but not in SQL Server.
In SQL Server, try this:
SELECT * FROM Orders where OrderID % 2 = 0;
--This is for oracle
SELECT DISTINCT City FROM Station WHERE MOD(Id,2) = 0 ORDER BY City;
Try this:
SELECT DISTINCT city FROM STATION WHERE ID%2=0 ORDER BY CITY;
For SQL Server:
SELECT * FROM Orders where OrderID % 2 = 0; //this is for even numbers
SELECT * FROM Orders where OrderID % 2 != 0; //this is for odd numbers
For Oracle and MySQL, you have to use the MOD function:
select * from orders where mod(ID,2) = 0
SELECT * FROM ( SELECT *, Row_Number()
OVER(ORDER BY country_gid) AS sdfg FROM eka_mst_tcountry ) t
WHERE t.country_gid % 2 = 0
In SQL, all these options can be used for MOD:
SELECT * FROM CITY WHERE MOD(ID,2) = 0 ORDER BY CITY;
OR
SELECT * FROM CITY WHERE ID % 2 = 0 ORDER BY CITY;
OR
SELECT * FROM CITY WHERE ID MOD 2 = 0 ORDER BY CITY;
Hope it helps!
Related
I have an oracle query as follows but when I make changes to pagination the results are different. what should i pass for my code
SELECT *
FROM (
SELECT b.*,
ROWNUM r__
FROM (
select a.KODE_KLAIM,
a.NO_SS,
a.LA,
a.NAMA_TK,
a.KODE_K,
(
select tk.TEM_LAHIR
from KN.VW_KN_TK tk
where tk.KODE_K = a.KODE_K and rownum=1
) TEM_LAHIR,
(
select TO_CHAR(tk.TLAHIR, 'DD/MM/RRRR')
from KN.VW_KTK tk
where tk.KODE_K = a.KODE_K
and rownum=1
) TLAHIR
from PN.KLAIM a
where nvl(a.STATUS_BATAL,'X') = 'T'
and A.NOMOR IS NOT NULL
and A.TIPE_KLAIM = 'JPN01'
)b
)
where 1 = 1
WHERE ROWNUM < ( ( ? * ? ) + 1 )
WHERE r__ >= ( ( ( ? - 1 ) * ? ) + 1 )
but i run this query i have result ORA-00900: invalid SQL statement
You have three WHERE clauses at the end (and no ORDER BY clause). To make it syntactically valid you could change the second and third WHERE clauses to AND.
However, you mention pagination so what you probably want is to use:
SELECT *
FROM (
SELECT b.*,
ROWNUM r__
FROM (
select ...
from ...
ORDER BY something
)b
WHERE ROWNUM < :page_size * :page_number + 1
)
WHERE r__ >= ( :page_number - 1 ) * :page_size + 1
Note: You can replace the named bind variables with anonymous bind variables if you want.
Or, if you are using Oracle 12 or later then you can use the OFFSET x ROWS FETCH FIRST y ROWS ONLY syntax:
select ...
from ...
ORDER BY something
OFFSET (:page_number - 1) * :page_size ROWS
FETCH FIRST :page_size ROWS ONLY;
Additionally, you have several correlated sub-queries such as:
select tk.TEM_LAHIR
from KN.VW_KN_TK tk
where tk.KODE_K = a.KODE_K and rownum=1
This will find the first matching row that the SQL engine happens to read from the datafile and is effectively finding a random row. If you want a specific row then you need an ORDER BY clause and you need to filter using ROWNUM AFTER the ORDER BY clause has been applied.
From Oracle 12, the correlated sub-query would be:
select tk.TEM_LAHIR
from KN.VW_KN_TK tk
where tk.KODE_K = a.KODE_K
ORDER BY something
FETCH FIRST ROW ONLY
I need to get the tour_id where the tour_types_id is 7 AND 1
So in this case (image) i need:
213,215,223,225
I feel is a simple question but i can't figure out :(
Thanks.
[EDIT] Sorry where the tour_types_id is 7 (not 5) AND 1. Pot corrected
EDIT:
Since you want all tour_ids that have tour_types_id 1 AND 7, you can use this:
SELECT DISTINCT tour_id FROM tablename WHERE tour_types_id = 1 OR tour_types_id = 7 GROUP BY tour_id HAVING count(DISTINCT tour_types_id) = 2
PREVIOUS:
Maybe you're thinking about OR? If you want to get all tour_id that have tour_types_id = 5 or tour_types_id = 1, you need an OR. In english, the statement means: Select all the tour_ids from the table tablename where tour_types_id is equal to 1 or equal to 5.
SELECT tour_id FROM tablename WHERE tour_types_id = 1 OR tour_types_id = 5;
Use the query SELECT tour_id FROM tablename WHERE tour_types_id = 5 OR tour_types_id = 1
with tablename the actual name of the table you're using.
Your question is malformed though. You do not provide enough information (tablename?)
From the results you want, it seems you want an OR not an AND.
You can use in clause too after the where like
SELECT tour_id FROM [Table_Name] WHERE tour_type_id IN (1,5)
this can be more flexible for you if you want to add or remove some ids
I found this solution:
SELECT DISTINCT tour_id FROM tablename t1
WHERE EXISTS (SELECT * FROM tablename WHERE tour_types_id = '1' AND tour_id = t1.tour_id)
AND EXISTS (SELECT * FROM tablename WHERE tour_types_id = '7' AND tour_id = t1.tour_id)
Sorry if i wasted your time, i was searching for hours.
Thanks for the help
Use an aggregation query with having:
select tour_id
from t
where tour_types_id in (1, 7)
group by tour_types_id
having count(distinct tour_types_id) = 2;
Try this query:
select tour_id
from xxxxtable
where tour_types_id = 1
OR tour_types_id = 5;
I need to select records that has ID = 10,23,30 so I wrote this SQL
Select * from mytable where position(id in '10,23,30') > 0
But the problem I get additional records where ID = 1 and 2
Any ideas how to select only what I need ?
No need for position, just do IN:
Select * from mytable
where id in (10,23,30)
Use IN operator:
Select * from mytable where id in (10,23,30)
select * from payments where amount = 0 order by id desc
I need to get the values which it has 0.5 part only.Could you tell me how to do that ?
e.g. 12,12.5,12.6,14.5
results data set can be have : 12.5,14.5 values data only
Truncate amount to integer, subtract from original value and compare with 0.5
SELECT *
FROM payments
WHERE amount - CAST(amount AS INT) = 0.5
ORDER BY id DESC
OR:
SELECT *
FROM payments
WHERE amount - FLOOR(amount) = 0.5
ORDER BY id DESC
Hacks way:
SELECT *
FROM payments
WHERE PARSENAME(amount, 1)= 5
ORDER BY id DESC
One more using modulo:
SELECT *
FROM payments
WHERE amount % 1 = 0.5
ORDER BY id DESC
you can find this way also
declare #temp table (val decimal(18,3))
insert into #temp values (0.5),(10.5),(20.8)
select * from #temp
where (val % 1) = 0.5
Same idea as accepted answer, just another thought:
SELECT *
FROM payments
WHERE FLOOR(amount) < CEILING(amount) -- or <>
ORDER BY id DESC
or
WHERE FLOOR(amount) + 1 = CEILING(amount)
how to limit an integer query result to 1. a return of 2 to be 1, a return 1 to be 1, and a return of 0.5 to be 0.5 because it is <= 1. i don't want to modify the tables, i just want to modify the results.
This is my exact query.
select ((select "V01" from sports where "UID" = '1') * 1.0 ) /
(select "V01" from master where "BALL" = 'REQUIREMENT') ;
I'm using postgres.
To limit, you'd do something like this:
select
case
when yourNumber >= 1 then 1
else yourNumber
end
...
Then you just apply this concept to your query.
As noted by Wiseguy, you could also do:
select LEAST(yourNumber, 1)
, since this is postgresql.
The first solution will work with any ANSI SQL compatible database.
Update
Applied to your query, I think (if I understood what you want correctly) it would be like this:
select LEAST(1,
((select "V01" from sports where "UID" = '1') * 1.0 ) /
(select "V01" from master where "BALL" = 'REQUIREMENT')
);
use the LEAST function , docs: http://www.postgresql.org/docs/8.3/static/functions-conditional.html. Also, check out GREATEST too
SELECT LEAST(1, <your value>)
EDIT replaced GREATEST with LEAST
try this:
select CASE
WHEN ((select V01 from sports where UID = '1') * 1.0 ) /
(select V01 from master where BALL = 'REQUIREMENT') >= 1
THEN 1
ELSE ((select V01 from sports where UID = '1') * 1.0 ) /
(select V01 from master where BALL = 'REQUIREMENT')
END;