i have 2 fields in database and i would like to find the difference of them in percentage.
So mathematical the formula should be
ABSOLUTE((field1-field2)/(MAXIMUM(field1, field2)))
the problem is I dont know how to ask for maximum of 2 numbers. Since MAX in sql returns the max of column.
SELECT ABS(field1 - field2) / GREATEST(field1, field2)
FROM mytable
maybe someting like:
if (field1 > field2, (field1 - field2)/field1, (field2 - field1) / field1)
How about simply this
ABS((field1-field2)/(GREATEST(field1, field2)))
Try an IF:
SELECT IF(field1 < field2, field1, field2).
You are looking for the function named GREATEST, since MAX is used elsewhere in SQL.
http://dev.mysql.com/doc/refman/5.0/en/comparison-operators.html#function_greatest
Related
I am using Oracle SQL Developer. I need to find the rows where column1 starts with ‘987’ or ‘I’. Column1 is a String(18). Some sample patterns in this column include: 9(9), 9(12), and others. I am not familiar with the code to see how a column starts with certain values in Oracle SQL. Sample Code is below. Attempt below.
Code
select * from table1
where column1
Attempt Code
SELECT
REGEXP_SUBSTR(column1,
'987') "REGEXP_SUBSTR"
FROM table1;
You can use a regular expression for this:
where regexp_like(column1, '^(987|I)')
You just need to use LIKE.
select *
from table1
where column1 like '987%' or column1 like 'I%';
CREATE TABLE hs(WH VARCHAR2(100));
SELECT
*
FROM
hs
WHERE
REGEXP_LIKE(WH,'^987|^I', 'i')
ORDER BY WH;
I'm trying to create a temporary table with a select into statement in a stored procedure as follows:
SELECT *
INTO #GENEALOGY
FROM
(
SELECT field1, field2,
(SELECT fieldA from tableY WHERE...) as field3,
(SELECT fieldB from tableY WHERE...) as field4
FROM table
WHERE condition
)
Any command I type after this closing bracket fails the syntax check (such as another SELECT statement).
I've tried putting BEGIN and END before and after the whole statement and then starting my next command.
I've tried adding
AS tablename
after the closing bracket and then the next statement but it doesn't like that either
I've tried removing the # but same problem.
I actually need to run a WHILE loop after this and INSERT more records into the same table.
What am i doing wrong?
thanks
Out of curiosity, why are you using a subquery?
SELECT field1, field2,
(SELECT fieldA from tableY WHERE...) as field3,
(SELECT fieldB from tableY WHERE...) as field4
INTO #GENEAOLOGY
FROM table
WHERE condition;
The structure of your query is ok. You should include a semi-colon at the end, if this is in a programming block. I assume table is a real name and that the other parts of the query are syntactically correct.
What does the error mention? If you run this in SSMS more than once, the second time the temp table already exists and could be an error.
You said an alias was not enough, but this is a right sentence:
SELECT *
INTO #GENEALOGY
FROM
(
SELECT field1, field2,
(SELECT fieldA from tableY WHERE...) as field3,
(SELECT fieldB from tableY WHERE...) as field4
FROM table
WHERE condition
) as TableNameAlias
thanks guys for the help
in the end I changed to an INSERT INTO rather than a SELECT INTO because
a) i needed to do 2 different select queries to get the data i need (1 for the first record and a 2nd for all subsequent records) and the second query would have had to be an INSERT anyway
b) and i wasn't sure how long sql server would keep the temp table open if i was calling the procedure from a crystal report
so i created a fixed table, use SELECT INTO and just delete all records as first line of my sp to clear out unwanted data
ta!
I have a table name FooTable in which I have n columns, but I want to search a particular entire column using query. How can I do that?
For ex:
field1 field2 field3 .................... fieldn
hey hi whats
hello bye go
.. .. ..
.. .. ..
n n n
Select *from FooTable
I want to search the entire column 3 items, so how should I make the query so that all the columns 3 items are shown?
Question isn't very clear. You might mean this
SELECT * FROM FooTable
gets all
SELECT field1, field2, field3 FROM FooTable
gets those columns
You have to provide additional WHERE clause with specific value in that row
example SELECT * FROM table WHERE field1 = "Hello";
you have to define a variable
select #var += field3 from footable
I have 10,001 rows in my table, and all of the rows except one start with a number. I need to find this one row that doesn't start with a number, or even that doesn't contain a number.
So this is what I have:
Select col1 from table1 where col1 not like '?%'
Is this even close? I need to find the row that doesn't have a number...
Thanks!!
UPDATE: I am using a sqlite database
Use:
SELECT col1
FROM table1
WHERE SUBSTR(col1, 1, 1) NOT BETWEEN 0 AND 9
Reference:
core functions (incl SUBSTR)
LIKE
On Sql Server,
Select * From table
Where col1 Like '[^0-9]%'
EDIT: Don't know if there is an equivilent on SQLLIte,
but this will work...
Select * From table
Where col1 Not Like '0%'
And col1 Not Like '1%'
...
And col1 Not Like '9%'
There is a post in code project that allow you to use Regex with Ms SQL.
Hope this help.
The easiest way might be to just note that you're not using numbers; you're using strings that happen to have numbers in them. In this case, you can do
select * from table1 where col1 not between '0' and '9:';
(where the colon is the ASCII character after '9'; this way '9999999' won't be found).
It should be a lot less expensive than some of the other suggestions (e.g., checking the value of the first character).
#Dueber got me thinking, couldn't you just do this?
SELECT * FROM table1 WHERE col1 > '9'
Grab the first character, see if it's numeric.
SELECT *
FROM table1
WHERE ISNUMERIC(SUBSTRING(col1,1,1)) = 0
SUBSTRING
ISNUMERIC
I have a very curious question.
We have query to select records from table based on some condition. In general the syntax for the query is as below
SELECT * FROM TABLENAME WHERE COLUMNNAME='VALUE';
Now the question is that will this query will work if we interchange the position of COLUMNNAME and 'VALUE'.
Yes, it will. =)
Why did you not just try?
Yes. The following will work:
SELECT * FROM TABLENAME WHERE 'VALUE' = COLUMNNAME;
In fact, in Oracle at least, you can do some twisted but somewhat useful things like:
select *
from tablename
where 'VALUE' in (field1, field2, field3)
You mean
SELECT * FROM TABLENAME WHERE 'VALUE' = COLUMNNAME;
I tested it, it works on MSSQL Servver 2008
SELECT * FROM TABLENAME WHERE 'VALUE' = COLUMNNAME;
if write something like this.. it'll work for sure..