I hope someone can help with that tricky question:
How can I "GROUP BY" some rows which have a configurable adjacent distance between the timestamps?
Example table:
ID | Value | When
1 | 5 | 2017-06-30 11:45:55
2 | 9 | 2017-06-30 11:45:56
3 | 0 | 2017-06-30 11:45:59
4 | 2 | 2017-06-30 11:46:02
5 | 7 | 2017-06-30 17:19:22
6 | 7 | 2017-06-30 17:19:22
7 | 3 | 2017-06-30 17:19:22
8 | 6 | 2017-06-30 17:19:22
Desired result:
ID | Value | When
3 | 0 | 2017-06-30 11:45:59
7 | 3 | 2017-06-30 17:19:22
The result shall find adjacent entries (in the example two groups of four rows each) and tell the lowest "Value".
Adjacent distance can be any value like one minute or ten minutes.
I tried to reformat the date to be able to "GROUP BY" without seconds but this won't work for the first result.
My MySQL programming skills are limited but it could be done with following steps:
SELECT and ORDER BY "When"
Go though values and tell difference between current and previous "When" value, if within range, then GROUP, if not output a new row.
Any idea?
Finally I decided to write a C program which uses a view in the DB to obtain raw sorted data. The program then outputs groups if the difference between the timestamps is within the desired limit.
The output is then put back into the database.
Related
Selecting all records but while displaying filter records when the name is same. I want to see all the records occurring once and more than once together and rest default.
I tried -
select * from sale
group by party_name
it not combined data where the name is same and it filtered in ascending order.
THE TABLE IS -
|1 | 2020-05-01 | Ram
|2 | 2020-05-04 | shayam
|3 | 2020-05-03 | Ram
|4 | 2020-05-15 | Mohan
|5 | 2020-05-17 | Shyam
THE OUTPUT I NEED
1 2020-05-01 Ram
3 2020-05-03 Ram
2 2020-05-04 shayam
5 2020-05-17 Shayam
4 2020-05-15 Mohan
6 2020-06-01 Ram
8 2020-06-17 Ram
7 2020-06-15 Mohan
I'm not sure if this is related to your problem, but this query is malformed:
select *
from sale
group by party_name;
You have an aggregation query, but are selecting columns that are not aggregated (assuming the table has more than one column). In almost every database in existence, this returns an error. Sadly some databases -- such as old versions of MySQL -- allow this.
It would explain, though, why you are getting one row per party_name. However, the proper behavior would be an error, which happily almost all databases will provide.
So I am currently writing an SSRS report which I want to calculate lead time based on an arbitrary number deliveries.
I have my query in SSRS but now I would like to filter it to show the last 5 deliveries. Usually I would use a Top N for filtering but SSRS rightly complains that the number 5 is not a date.
How do you filter the last five latest dates without knowing the exact dates of the records you are returning?
Dataset is as follows (lead time is a calculation of order date less delivery date)
Order | Del_Date | Lead_Time
------|------------|-----------
00001 | 2015-05-01 | 20
00002 | 2015-01-08 | 21
00003 | 2015-02-05 | 22
00004 | 2015-03-11 | 26
00005 | 2015-01-21 | 8
00006 | 2015-04-12 | 12
00007 | 2015-03-02 | 12
00008 | 2015-02-01 | 12
The query should return
Order | Del_Date | Lead_Time
------|------------|-----------
00001 | 2015-05-01 | 20
00003 | 2015-02-05 | 22
00004 | 2015-03-11 | 26
00006 | 2015-04-12 | 12
00007 | 2015-03-02 | 12
Thanks,
Can you try this:
SELECT TOP 5
Order,
Del_Date,
Lead_Time
FROM
[TABLE]
...
ORDER BY
Del_Date DESC
You can modify the data set definition as Matteo suggested or apply a filter to the object displaying the data. Based on your question I am assuming you want to use the object displaying the data to filter your results.
To get the top 5 dates change the data object sorting to the date field and order "Z to A". Then under filters select the date field you want in the expression box. Set the operator to "Top N" and set the value to "=5".
If tablix filter complains about top 5, then you can bring row_number (ranking) to tablix and filter it by ranking <=5
How do you get the median of a row in MySQL?
I have a table which gives monthly stock for a series of categories:
cat_id | mar_stk | feb_stk | jan_stk
1 | 5 | 7 | 9
2 | 2 | 1 | 3
3 | 6 | 8 | 10
I need the median, maximum and minimum stock for each category.
Currently have minimum and maximum using:
SELECT
cat_id,
GREATEST(mar_stk, feb_stk, jan_stk) AS max_stk,
LEAST(mar_stk, feb_stk, jan_stk) AS min_stk
FROM example_table
Which leaves me with:
cat_id | max_stk | min_stk
1 | 9 | 5
2 | 3 | 1
3 | 10 | 6
But I can't find any straightforward way to find the median.
By statistics, Median is the middle number in a given out distribution. For instance if in the column cat_id where you have value 1,2,3 etc. Your median is 2 since its the number or value at the middle. Query the middle value and then hurray. Give me a shout if you still need further guide. ..Sectona
Basically, I have a list of records in a mysql db. These records are ordered 1 to 10. The user can re-order these records to whatever order they want. They will press a button to update all the records to their newly, respective order number. For example:
ID | Sort_Index | Name
----------------------
1 | 1 | Jim
2 | 2 | Bob
3 | 3 | Carl
4 | 4 | Bill
5 | 5 | Wendy
The user can change these to this for example:
Note: the changed values are stored into an array before I make the UPDATE calls
ID | Sort_Index | Name
----------------------
1 | 1 | Carl
2 | 2 | Wendy
3 | 3 | Bob
4 | 4 | Jim
5 | 5 | Bill
My question is, how can I make this mysql call with one call, using the new values in my array, instead of one call for each record?
If this is impossible or simply the "wrong way to do it", please feel free to suggest new ideas as I am not fully committed to this idea as of now.
If you have a limited number of rows, you could implement this with an sql CASE statement --
Update users set sort_index = case id when 1 then <newval> when 2 then <newval>...
We just want to make the query for mysql database, in which there are 12 table according to the months(JAN - DEC), with 32 Columns(JAN1, JAN2, JAN3,....JAN31). These database is used for getting the availability for hotel,like if we select a tour for three days (29JAN-1JAN), so the query will check the records for 2 tables, one for JAN and other for FEB. the whole columns stored the values in digit(like, 5,10,2,0,5,etc)its showing Rooms available. We are successfully built a query for single month, but we unable to create a mysql query for 2 months, because we want a value in greater than 1. like we only shows the available rooms only.
$num = mysql_query("SELECT DISTINCT id,table_type,JAN29,room_type FROM JAN Where table_type='disp' AND JAN!=0 ");
above query is working fine for me, we just want this query for 2 tables. and getting the positive value , greater than 0(1).
Please help to solve this problem ..
Thanks
Rod
ID | JAN1 | JAN2 | JAN3 | JAN31|
34 | 5 | 3 | 3 | 4 |
56 | 4 | 3 | 9 | 3 |
28 | 0 | 7 | 0 | 9 |