HELLO I WANT COMBINE QUERY.
select * from table where code='?';
select * from table where code='?' or code='?';
select * from table where code='?' or code='?' or code='?';
select * from table where code='?' or code='?' or code='?' or code='?';
i make this 4 query but i want combine them. how i can?
and code has 4 value like 1,2,3,4
please help me!
(i'm using oracle 11, SQL Developer)
you can use union all for example
select * from table where code='?';
union all
select * from table where code='?' or code='?';
union all
select * from table where code='?' or code='?' or code='?';
union all
select * from table where code='?' or code='?' or code='?' or code='?';
or you can use IN operator like
select * from table where code in ('?',?','?','?')
I am not sure from the way question is worded but I think you may be looking for a IN statement?
SELECT * FROM tablename WHERE code IN ('1' ,'2' ,'3' ,'4');
for more on this, visit
https://docs.oracle.com/database/121/SQLRF/conditions014.htm
Related
General except query is like this
(SELECT * FROM name_of_table_one
EXCEPT
SELECT * FROM name_of_table_two);
is there a way to write a query where I pass a list of values and perform except or intersect operation with a specific column of a table and select from that the list I had passed to DB.
Use a VALUES list.
select * from (values (1),(2),(100000001)) as f (aid)
except
select aid from pgbench_accounts
You can achieve that with the IN clause
"except":
SELECT * FROM your_table WHERE your_column NOT IN (list of values)
"intersect":
SELECT * FROM your_table WHERE your_column IN (list of values)
I have been trying to write a subquery based on the SQL Server docs. I am trying to write a SQL Server query to
cast a varchar to an int
select rows where the varchar is null.
My logic is:
outer select+ where clause gets all rows where the new column (q5int) is NULL
create inner select that performs the cast and creates the new column (q5int)
What am I doing wrong? I would appreciate an explanation of whats wrong with my logic, not just how to fix my code.
select *
from
(select
*, cast (NULLIF(q5,'NULL') as int) as q5int
from
ft_merge)
where
q5int = NULL
You can do this sans the sub-query
select *
from ft_merge
WHERE try_convert(int,q5) is null
Let us analyze your query first
select * from (
select *,cast (NULLIF(q5,'NULL') as int) as q5int
from ft_merge
) WHERE q5int=NULL
1.NULLIF(q5,'NULL') should be NULLIF(q5,NULL) not in quotes
2.WHERE q5int=NULL should be replaced with "q5int IS NULL"
Creating test data
Select * into #ft_merge
from
(
Select 1 AS ID,'111' AS q5
union
Select 2, null
union
Select 3,'2222'
union
Select 4,null
)t
/* using your query */
select * from (
select *,cast(q5 as int) as q5int
from #ft_merge where ISNUMERIC(q5) > 0 or q5 is null
)t WHERE q5int is NULL
/* another optimal way of doing this */
select *,cast(q5 as int) as q5int
from #ft_merge where NULLIF(q5,NULL) is null
I have a table with 12 columns.
I need a query for computing COUNT(*) and selecting all the columns.
I mean I want to have these two queries just in one query:
select *
from mytable
where OneOfTheColumns = something;
select COUNT(*)
from mytable
where OneOfTheColumns = something;
Conditions and tables are the same.
Can I do this?
Thanks a million.
You can use a window function for that
select *,
count(*) over () as total_count
from mytable
where OneOfTheFields = something;
Hi I have values like this in my field name called "NumberName"
AB1
CD2
XH506
PQ104
PZ77
I am trying to order this so far I have tried is:
select * from view_name where NumberName='Something'
order by RIGHT('0000' + SUBSTRING(NumberName, ISNULL(NULLIF(PATINDEX('%[0-9]%',NumberName), 0),
LEN(NumberName)+1), LEN(NumberName)), 4)
and
order by LEN(NumberName),NumberName
how to achieve this..?
select * from yourtable
order by LEFT(columnname,1)
Perhaps, I didn't fully understand your question. But ORDER BY NumberName correctly sorts the values.
Query:
DECLARE #temp TABLE(NumberName NVARCHAR(50))
INSERT INTO #temp (NumberName)
VALUES ('AB1'),('CD2'),('XH506'),('PQ104'),('PZ77')
SELECT *
FROM #temp
ORDER BY NumberName
Output:
NumberName
----------
AB1
CD2
PQ104
PZ77
XH506
You didn't explain this well enough, but I recon you'd like to sort the result based on value of rightmost number. Try this. You need to first find first occurence of not-a-number form the right, to know where the last number begins (PATINDEX('%[0-9]% will give you first number, but in "FB3C2" for example numbers are mixed with letters).
select
*
,right(NumberName,PATINDEX('%[A-Z]%',reverse(NumberName))-1)
from (
select 'ABC001' as numbername union all
select 'XQ20001' union all
select 'XQ20002' union all
select 'XQ20003' union all
select 'XQ20004' union all
select 'PM130010' union all
select 'PM130011' union all
select 'PM130012' union all
select 'PM130013' union all
select 'PM130014' union all
select 'PM130015' union all
select 'FB3C2' union all
select 'FB3C2' union all
select 'PM13001' union all
select 'PM13001' union all
select 'PM13002'
) x
order by
cast(right(NumberName,PATINDEX('%[A-Z]%',reverse(NumberName))-1) as int)
Is it possible to access a temporary column that was defined in a query for a Common Table Expression? Say I have
select * from myTable
;with cte as
(
select
*, Salary * 4 as FourTimesSalary
from
Employees
where
Name = #name
and ID >= 100
)
Is there a way to use the temporary column FourTimesSalary when querying cte like so?
select top 2 *
from cte
order by FourTimesSalary, Name
TIA.
Yes you can do that. Example:
with temp as
(
select 1 as id, 2*4 as val
UNION
select 2 as id, 3*4 as val
)
SELECT * FROM temp ORDER BY VAL desc
Your example looks fine, did you get an error when you tried that or something?