How to order by two fields ? sql - mysql

i need to run query which do the following:
order the table by x , so the result will be.
then it do a multiple order by for a groups according to the following x range.
so userId_1 will be before userId_2 , because both are in 90-100 x range.
and userId_1 y value > userId_2 y value.
How to implement that ? thanks

You should be able to get the desired effect by dropping the last digit of x for ordering, i.e.
SELECT *
FROM MyTable
ORDER BY x DIV 10, y

Related

MYSQL select rows whose values are positive across all the x

I would like to perform a select similar to the example below. Basically I have three columns name, x, value. I would like to return the name whose values are all positive across a given range of x.
for example, I want to return the names whose values are all positive for x between 1 and 3, this should return y3 and y4. could anyone please help me, thanks.
select name
from your_table
where x between 1 and 3
group by name
having min(value) >= 0

MySQL - Nested where conditions

I have a mysql table, which is as follows:
image_id imagename brandname x y
143 00003.jpg Pirelli 147 265
125 00003.jpg Pirelli 500 259
Through mouse clicks I am generating x and y positions on a html canvas which I want to use to subset the table. For example, if the clicked position of x and y were 510 and 262, I want to retrieve the image id 125 as both the parameters are matching.
Suppose if the x value generated with mouse click is 510, i want to select the line with image_id 125.
I tried the following:
select * from table where imagename='00003.jpg' and 510 < max(x);
I am getting this error:
invalid use of group function.
Max is a group function... You can't use it without having group by in it.
You are comparing X and y value so use them directly, no need for using Max
As #user769889 already mentioned, you can't use aggregate functions like Min, Max etc without using Group By that's why you are getting that error.
I think you need this :
select * from table where imagename='00003.jpg' and x < 510 and y < 262
Max is a grouping function, you cannot use it like that. You can achieve what you need by using the followind nested query:
select * from table where imagename='00003.jpg' and 510 < (select max(x) from table where imagename = '00003.jpg')
This will run the inner query that gives back the maximum value for the x column for the image, then runs the outer query using the result in its where clause!
This is what worked for me:
select * from annotations where imagename='00003.jpg' x <= 510 order by x desc limit 1;

WHERE clause in SSRS expression for max function

I have for example a query with return something as it
route value
1 3
2 2
3 4
4 5
5 1
then I need to put in 2 textbox the max and the min route so in sql this would be
select top 1 route from table where value=(select max(value) from table)
I add a image done in excel, how this would be.
I believe this is so easy but I dont have idea how to get it.
I got using expression, this was extactly expression
="Route "+
Convert.ToString (
Lookup(max(fields!value.Value),fields!value.Value ,fields!route.Value,"mydataset")
)
changing max for min, for the other...
thanks everyone.
I believe the query you're looking for would be:
With Min_Max_CTE as (
Select MIN(value) as Min_Value
, MAX(value) as Max_Value
From Table
)
Select Top 1 'Min' as Type
, T.route
, T.value
From Table T
Inner Join Min_Max_CTE CTE
on T.value = CTE.Min_Value
Union All
Select Top 1 'Max' as Type
, T.route
, T.value
From Table T
Inner Join Min_Max_CTE CTE
on T.value = CTE.Max_Value
Order by Type desc --This will put the Min Route first followed by the Max Route
Then, put that query into a dataset, and then create a tablix and use the Type, route, and value fields to return the minimum route and the maximum route. It should end up being set up just like your excel section with the min and max routes above.
You can do this SSRS by using a couple of separate tables. Your example data:
And two tables in the Designer:
Since the tables only have header rows, only the first row in the table will be displayed.
To make sure we get the MAX and MIN values in the two tables, each table needs to order its Dataset appropriately, i.e. by Value by descending and ascending respectively.
MAX table:
MIN table:
Which gives your expected result:

MySQL - return records for clients unless they have a specific value and only that value

Trying to wrap my head around how to do this query - I want to return a list of client records and need to exclude clients if they had only a specific value and no other values.
For example
c# value
1 X
1 Y
2 X
3 Y
I want all the records for clients 1 and 3, since they had a value other than X. I do not want client 2, because that client had ONLY X.
I for example want returned in this case:
1 X
1 Y
3 Y
Of course, I could have lots of other records with other client id's and values, but I want to eliminate only the ones that have a single "X" value and no other values.
Maybe using a sub-query?
Try this:
SELECT client, value FROM myTable where `client` in
(select distinct(client) from myTable where value !='X');
Returns:
Client Value
1 X
1 Y
3 Y
Something like this
SELECT ABB2.*
FROM
mytable AS ABB2
JOIN
(SELECT c
FROM mytable
WHERE value <> "X"
GROUP BY c) AS ABB1 ON ABB1.c = ABB2.c
GROUP BY ABB2.c, ABB2.value
It's faster than using a WHERE clause to identify the sub query results (as in Mike's answer)

how can I tell if the last x rows of 'state' = 1

I need help with a SQL query.
I have a table with a 'state' column. 0 means closed and 1 means opened.
Different users want to be notified after there have been x consecutive 1 events.
With an SQL query, how can I tell if the last x rows of 'state' = 1?
If, for example, you want to check if the last 5 consecutive rows have a state equals to 1, then here's you could probably do it :
SELECT IF(SUM(x.state) = 5, 1, 0) AS is_consecutive
FROM (
SELECT state
FROM table
WHERE Processor = 3
ORDER BY Status_datetime DESC
LIMIT 5
) as x
If is_consecutive = 1, then, yes, there is 5 last consecutive rows with state = 1.
Edit : As suggested in the comments, you'll have to use ORDER BY in your query, to get the last nth rows.
And for more accuracy, since you have a timestamp column, you should use Status_datetime to order the rows.
You should be able to use something like this (replace the number in the HAVING with the value of x you want to check for):
SELECT Processor, OpenCount FROM
(
SELECT TOP 10 Processor, DateTime, Sum(Status) AS OpenCount
FROM YourTable
WHERE Processor = 3
ORDER BY DateTime DESC
) HAVING OpenCount >= 10