mySQL group by? - mysql

Say I have 5 rows in the database, three of them have an ID of 2, two of them have an ID of 1.
The three with ID of 2 have dates, lets say for example 1st June, 2nd June, 3rd June.
The two with ID of 1 have dates, lets say for example, 1st July, 2nd July.
How do I go about selecting only 1 of the latest values, for each ID?
When currently using GROUP BY id it is returning the first row for each, not the latest added.
I want the latest one added... any idea?
Thank you.

Same Kyle R from SitePoint?
Every column in your SELECT list must either appear in your GROUP BY clause or be an aggregate function, otherwise the return value is undefined by specification and MySQL is free to give you anything it wants.
Think about what grouping means. Take some set of rows, and collapse them down into a single row representing the group. In doing so, you must tell MySQL from which row each column in the representative row should come. If you want the greatest value from the group, you use MAX. If you want the smallest, you use MIN, etc.

maybe something like:
select id, max(date)
from thetable
group by id

Related

SSRS 2016 - How to calculate a percentage difference between two values in a group?

I'm creating a report that shows the sales of item before a date range and after a date range.
The part I ran into trouble with is the percentage difference between the total sales on Date 1 and Date 2.
Items can have no sales for a certain week.
The user can select multiple item ID's in the item ID parameter.
I can update the question to post my SQL query if needed.
What I've tried
Since I put a group on item ID I thought the First and Last functions would work.
Here's my expression on the column PCT.
=(Last(Fields!total_sales1.Value, "Date1")- First(Fields!total_sales1.Value, "Date1")) / First(Fields!total_sales1.Value, "Date1") * 100
But when I run the report I get the following results.
I need an expression on PCT column that will give me a percentage difference for each item pairs.
It looks like your scope is incorrect. Check what the rowgroup is called where you group by item id (let's say the row group is called "yourItemRowGroupName").
Then change you expression to use that scope rather than "Date1".
In fact you may not need the scope at all as it should work within the scope that expression sits (in your case, within your ItemID group.).
So try
=(Last(Fields!total_sales1.Value)- First(Fields!total_sales1.Value)) / First(Fields!total_sales1.Value) * 100
Or..
=(Last(Fields!total_sales1.Value, "yourItemRowGroupName")- First(Fields!total_sales1.Value, "yourItemRowGroupName")) / First(Fields!total_sales1.Value, "yourItemRowGroupName") * 100
You may have to handle divide by zero but try this to start with before you add any more complications.

SSRS - Sum Last 12 items

I have a Total column and am summing all the values in the row with:
=Sum(Fields!MyField.Value)
How can I sum only the last 12 items?
This is valid:
=Last(Fields!MyField.Value)
I need something like the below...
=Sum( LastX(Fields!MyField.Value, 12) )
You could achieve this result by creating a subquery in your main dataset query. You will just have to have a value that you want to sort on in descending order.
Say you have a table sorted by the price in ASC order. You want to sum the last 12. You just need to flip the sort in the subquery and sum the top 12.
SELECT OtherFields, (SELECT TOP 12 Sum(S1.MyField) FROM MyTable S1 ORDER BY S1.MyField DESC) AS BottomTwelveSum
FROM MyMainQueryTable
Yes this value will repeat in your dataset, however if you place it correctly in the Table you should be fine, just dont show the value in the details section. This wont add additional rows. Another solution would be using a subreport, however this is much quicker on render time.

MySQL - SUM() old entries

I've a table with lots of entries consisting of dates and a number.
For instance:
07.02.2016 - 12
06.02.2016 - 48
05.02.2015 - 24
...and so on.
Now I need to sum all of the values older than 2 months. For instance the 3rd entry (05.02.2015) will be added to the second (06.02.2016) and the second one should get the value 72 and the 3rd one should be deleted.
I'd like to know if there is some way to do this in mysql only?
Instead of writing the code for you, I'd like to merely give you some hints:
Identify which rows are older than 2 month and sum them up.
select sum(number) from table where date > curdate() + interval 2 months
or sth. similar will do.
Select the max. date of the entries that are smaller or equal to "now+2months".
Update that row with the value from step 1.
Delete the rows from step 1.
See here for details on date functions in MySQL.
This can be done in 2 statements (one for steps 1-3, one for the deletion).

Using the LIMIT function to display the 1st, 2nd and 3rd row, with the MAX customer rating using MySQL

I need to display the 1st, 2nd and 3rd names in the table with the highest customer rating. I created done the following query:
This query display the 1st, 2nd and 3rd names in the table,
SELECT *
FROM donuts
WHERE customer_rating
LIMIT 1,3;
I tried to use the MAX function to get the 3 highest customer ratings but I get a blank table.
SELECT MAX(customer_rating), donutName
FROM donuts
WHERE customer_rating
LIMIT 1,3;
Any suggestions,
Use ORDER BY
SELECT *
FROM donuts
ORDER BY customer_rating DESC
LIMIT 0, 3
The reason you got an empty result is because LIMIT 1, 3 prints the 2nd, 3rd, and 4th entries, not the 1st, 2nd, and 3rd entries, because LIMIT is zero-based (unlike just about everything else in SQL). But when you use an aggregate function like MAX() you only get one row of results (all the results are combined into a single maximum), so there is no 2nd entry.

sum of last field

I have a trouble to sum fields.
This is what I have: I have list of employees and two rows of values for each, I also have calculations in SQL of running total for the 8 and 16 weeks for each employee for each of two rows using windowing function. I have to group employees by branches they work and calculate sum of running totals for the last 8 and 16 weeks for each row and then device row 1 by row 2. I need to use Last function, because I only need the running total for the last 8 and 16 weeks. The challenge is to go around and have something like: Sum(Last(Fields!Last116WeekSilk.Value) . this one obviously gives me an error. I have tried to add calculated field to the dataset with both Sum and Last functions, doesn’t work, tried RunningValue, doesn’t work. What else can I do to have the sum of last running totals?
Many thanks in advance
I would add the SQL ROW_NUMBER function to the query and derive the row number (e.g. 1 or 2) within each group. If you do this in descending sequence the row number 1 will always be the last row within the group.
Then in SSRS you can use an Iif to only show/calculate on row number 1.