I need help to solve my issue.
I have a table like this one,
id | Desc | Min Range | Max Range
-----------------------------------
1 | A | 0 | 10
2 | B | 11 | 20
3 | C | 21 | 30
How to get the second record when I put parameter 20.
My solution now is like this:
select * from table where maxRange <= 20
and in java, I get last of result list.
My problem is I don't have parameter to compare when I use between. eg.
select * from table where maxRange between ? and ?
select * from table where ? between minrange and maxrange
You need to add the parameter to you command when you query.
Create a Parameter called #Parameter with value 20 - this is dependant on what technology you are using. Then:
SELECT * FROM table WHERE #Parameter >= MinRange AND #Parmeter <=
MaxRange
Related
I need to make a visualization using only SQL. I would like to make both a column chart and a histogram
How about using bar function?
presto:default> select * from test order by 1;
name | value
------+-------
a | 10
b | 20
c | 5
d | 15
e | 25
presto:default> select name, bar(value, value) from test order by 1;
https://prestosql.io/docs/current/functions/color.html#bar
I am trying to create a report that pulls the date from a previous row, does some calculation and then displays the answer on the row below that row. The column in question is "Time Spent".
E.g. I have 3 rows.
+=====+===============+============+====+
|name | DatCompleted | Time Spent | idx|
+=====+===============+============+====+
| A | 1/1/17 | NULL | 0 |
+-----+---------------+------------+----+
| B | 11/1/17 | 10 days | 1 |
+-----+---------------+------------+----+
| C | 20/1/17 | 9 days | 2 |
+=====+===============+============+====+
Time Spent C = DatCompleted of C - DateCompleted of B
Apart from using a crazy loop and using row x row instead of set I can't see how I would complete this. Has anyone ever used this logic before in SQL? If how did you go about this?
Thanks in advance!
Most databases support the ANSI standard LAG() function. Date functions differ depending on the database, but something like this:
select t.*,
(DateCompleted - lag(DateCompleted) over (order by DateCompleted)) as TimeSpent
from t;
In SQL Server, you would use datediff():
select t.*,
datediff(day,
lag(DateCompleted) over (order by DateCompleted),
DateCompleted
) as TimeSpent
from t;
You can do this by using ROW number syntax is
ROW_NUMBER ( ) OVER ( [ PARTITION BY value_expression , ... [ n ] ] order_by_clause)
For reference you can use ROW_NUMBER
You have an index already (similar to rownumber above). Join to itself.
Select table1.*
,TimeSpent=DateDiff("d",table1.DateCompleted,copy.DateCompleted)
from table1
join table1 copy on table.idx=copy.idx-1
I want to find the minimum value of a column in a certain date range of a table.
so lets say I have a table like the following,
Date | Value
---------------
01-26 | 2
01-26 | 1
01-27 | 2
01-27 | 4
01-28 | 3
01-28 | 5
How can I apply the MIN() function to the subgroup of the Value column so that the result might be
Date | MIN(Value)
---------------
01-26 | 1
01-27 | 2
01-28 | 3
I thought about GROUP BY .. or such but couldn't figure out how to get the results into a table.
Using UNION and JOIN isn't quite scalable because the query could be using a date range of a month
Group by should work:
Select date, min( value )
From table1
Group by date
Maybe too simple, but seems like this would work
Select Min(col1), datecol from yourtable group by datecol;
HTH
So let's say I have this table with these rows in it
Table name: MYTABLE
ID | NUMBER | FK_ID
1 | 0 | 26
2 | 0 | 26
3 | 1 | 26
4 | 0 | 27
5 | 1 | 27
Now I want to filter out only the rows that that go under the same FK_ID and have two or more NUMBER 0's in them.
So for instance if I would apply this filter here, I would only see one row which corresponds to the FK_ID 26 because it has two NUMBER 0s in it's MYTABLE data.
Is this even possible to do or should I just handle the whole data in my programming language not filter it like that from DB.
SELECT FK_ID ,
COUNT(DECODE(NUMBER ,0,1))
FROM TEST_DATA
GROUP BY FK_ID
HAVING COUNT(DECODE(NUMBER ,0,1)) >= 2
Fiddle here : http://sqlfiddle.com/#!4/44d70/4
Does this query work for you?
SELECT
FK_ID
FROM MYTABLE
WHERE NUMBER = 0
GROUP BY FK_ID
HAVING COUNT(*) >= 2;
Also, consider renaming the NUMBER column, as NUMBER is a reserved word in Oracle.
i've two columns in mysql db table votes : accept and reject
i want to query only the top result after the accept-reject column values
table : votes
=================
accept | reject
=================
7 | 9
5 | 1
2 | 15
5 | 1
i want just the positive and top value, here 5-1 = 4
actually, i've lot of other columns along with accept and reject..i just want to ORDER the results by the top value of difference(positive only) and LIMIT it to 1 so that i can get the one row i want..was i clear? :)
how to write query for this?
thanks..
Use:
SELECT t.accept,
t.reject,
t.accept - t.reject AS difference
FROM VOTES t
WHERE t.accept - t.reject > 0
ORDER BY difference DESC
LIMIT 1
Alternate using subquery:
SELECT v.accept,
v.reject
FROM VOTES v
WHERE v.accept - v.reject > 0
JOIN (SELECT MAX(t.accept - t.reject) AS difference
FROM VOTES t
WHERE t.accept - t.reject > 0) x ON x.difference = (v.accept - v.reject)
SELECT (accept - reject) as top_value FROM table WHERE (accept - reject) > 0
So if you have values like this:
=============================
accept | reject | top_value
=============================
5 | 1 | 4
7 | 9 | -2
2 | 15 | -13
5 | 5 | 0
the query will select only row 1.
I'm assuming those two columns are just an extract from your table, and you want the entire row where (accept-reject) is maximum. Then you can do it like this:
SELECT * FROM votes
WHERE accept-reject = (SELECT MAX(accept-reject) FROM votes)
LIMIT 1