I want to compare two columns. I want select only column where value difference between two columns > 0,10.
Example:
Select ColumnA from Table1 where CoLumnA <> ColumnB > 0,10
SELECT ColumnA
FROM Table1
WHERE ColumnA <> ColumnB
AND ABS(ColumnA - ColumnB) BETWEEN 0 AND 10
Details on ABS() and BETWEEN can be found here:
ABS()
BETWEEN
Related
I have the following select in my mysql database:
select t.col1, t.THE_DATE, (select ?? as PREVIOUS_DATE)
from DUMMY_TABLE t
order by date
What I am trying to achieve is have the 'PREVIOUS_DATE' contain the value of the previous row's 'THE_DATE' column if there is one.
So if DUMMY_TABLE has the data :
col1 THE_DATE
x 10-01-2010
x 10-01-2012
x 10-01-2009
my select should return
col1 THE_DATE PREVIOUS_DATE
x 10-01-2009
x 10-01-2010 10-01-2009
x 10-01-2012 10-01-2010
You need order by clause in subquery with limit clause :
select t.col1, t.the_date,
( select t1.the_date
from dummy_table t1
where t1.col = t.col and
t1.the_date < t.the_date
order by t1.the_date desc
limit 1
) as PREVIOUS_DATE
from dummy_table t
order by the_date;
I have a query that has something like this:
;with Dataset1 as (SELECT columnA, columnB, SUM(columnC) as columnC FROM Table1 GROUP BY columnA, columnB )
SELECT
(SELECT columnC FROM Dataset1 WHERE columnA = '1' and columnB='2') as Result1,
(SELECT columnC FROM Dataset1 WHERE columnA = '2' and columnB='3') as Result2,
(SELECT columnC FROM Dataset1 WHERE columnA = '3' and columnB='4') as Result3,
...
What I'm wondering is does Dataset1 get selected in-memory and reused with every one of those select clauses, or does SQL fetch it each time? If that's the case, that seems really wasteful, and what should I do to optimize this ?
I would like to get the difference between 2 consecutive rows in the MySql. I am trying to resolve, but no luck. Here is the data in the image
I need a difference between rows of "Data2" column and results into "Diff" column.
Thanks for your kind attention and much appreciated for your help.
-Ram
If the table have an auto incremental column 'id', We can order by id and identify the next row value and subtract it
SELECT t1.*, t1.Data2-(SELECT t2.Data2 FROM `table_name` t2 WHERE t2.id > t1.id LIMIT 1 ) AS difference
FROM `table_name` t1
ORDER BY t1.id
to subtract from next row value
SELECT t1.*, t1.Data2-(SELECT t2.Data2 FROM `table_name` t2 WHERE t2.id < t1.id ORDER BY id DESC LIMIT 1 ) AS difference
FROM `table_name` t1
ORDER BY t1.id
Since you don't have a unique column in your table, you can achieve this by including a bind variable [#rn & #rn1] which adds a unique number sequentially to every row in the table.
Try this:
SELECT tab1.application_id, tab1.fiscal_year, tab1.data1, coalesce(cast(tab1.data2 as signed) -
cast(tab2.data2 as signed), tab1.data2) as diff
FROM
(SELECT b.application_id, #rn1:=#rn1+1 AS rank, b.fiscal_year, b.data1, b.data2
FROM your_table b, (SELECT #rn1:=0) t1) as tab1,
(SELECT a.application_id, #rn:=#rn+1 AS rank, a.fiscal_year, a.data1, a.data2
FROM your_table a, (SELECT #rn:=0) t2) as tab2
WHERE tab1.rank = tab2.rank + 1;
(I am trying to use TimeDiff to find out the duration between columnA and columnB in Hours.)
SELECT tp.awb_number,
tp.courier_code,
tph.created,
tph.status_date,
MINUTE(TIMEDIFF(tph.status_date, tph.created))
FROM shiptrack.tracking_package tp
INNER JOIN shiptrack.tracking_package_history tph ON tph.tracking_package_id= tp.id
WHERE tph.status_date BETWEEN '2016-01-07 12:00:01' AND '2016-01-07 20:59:59'
AND tp.courier_code IN ('ESSKAY')
AND tph.new_status='UDL'
AND tph.last_status <> tph.new_status
AND MINUTE(TIMEDIFF(tph.status_date,tph.created))> 30
LIMIT 10;
I wanted to set the number rows returned per query say 5. How can I set this in the sql query.
Can any one please help me
Highly dependent on what RDBMS you are using.
For Oracle
SELECT * FROM the_table WHERE ROWNUM < 6
(since 12c there is another option, too).
For Postgresql
SELECT * FROM the_table LIMIT 5
According to the MySQL manual you can do this by adding LIMIT statement to your query:
SELECT * FROM tbl_name
LIMIT offset, row_numbers
or
SELECT * FROM tbl_name
LIMIT row_numbers OFFSET offset
offset option is very useful in case of pagination.
SELECT TOP 5 *
FROM dbo.MyTable
As someone suggest zou can use:
select top X from table_name
where X is the numer of rows that you want
or you can use row_number
With cte AS
( SELECT *,
ROW_NUMBER() OVER (order by table_column) as RowNumber
FROM table_name)
select *
from cte
Where RowNumber <= 5
or even:
With cte AS
( SELECT *,
ROW_NUMBER() OVER (order by table_column) as RowNumber
FROM table_name)
select *
from cte
Where RowNumber between 5 and 10