MySql query to get two rows into one with latest data from one row - mysql

Wondering if anyone could please help me with the following query.
http://sqlfiddle.com/#!2/79a49/1
This is the scenario:
A vehicle can be checkedout with a unique ra_no at any time. so a vehicle_id = 70 can be checkedout with ra_no = test with a branch_id and date_created and mileage.
It can then be checked back in with the same ra_no and then branch_id and date_created and mileage.
This query needs to return 1 row per ra_no for a vehicle_id eg. vehicle_id 70 and in this row it needs to display the currect status(latest) as well as the mileage(latest) and then the correct checked out date and location and correct checked in date and location.
You would group by ra_no but i cant seem to get the correct info out.
This needs to be converted to a view so that i can create a model for it in Yii.
Thanks!

Related

Need to retrieve the most recent room type/Rateplan combination (mysql)

I will explain the logic:
I need to retrieve only the most recent room type/rate plan combinations from the rateplan_roomtypeTable.
room type ID and rate plan id are located in separate columns
there are 2 conditions that need to be met: all active room type/rate plan combinations need to be retrieved along with all room type/rate plan combinations that have produced even if they are not active. All these combinations need to be the most recent ones.
The desired results would be like the table I ll share with you:
Your help with the below query will be much appreciated:
select
Id
, RoomTypeId
, RateTypeId
,isactiveRateType
,isactiveRoomType
, RatePlanName
, RoomTypeName
FROM
rateplan_roomtypeTable
where
RateTypeId IN (select RateTypeId from ProductionTable where (cast(bookingdate as date) between date_add('day',-92, current_date) and date_add('day', -2, current_date)))
OR (isactiveRateType = 1 and isactiveRoomType = 1)
GROUP BY
1,2,3,4,5
Thank you

Assistance with complex MySQL query (using LIMIT ?)

I wonder if anyone could help with a MySQL query I am trying to write to return relevant results.
I have a big table of change log data, and I want to retrieve a number of record 'groups'. For example, in this case a group would be where two or more records are entered with the same timestamp.
Here is a sample table.
==============================================
ID DATA TIMESTAMP
==============================================
1 Some text 1379000000
2 Something 1379011111
3 More data 1379011111
3 Interesting data 1379022222
3 Fascinating text 1379033333
If I wanted the first two grouped sets, I could use LIMIT 0,2 but this would miss the third record. The ideal query would return three rows (as two rows have the same timestamp).
==============================================
ID DATA TIMESTAMP
==============================================
1 Some text 1379000000
2 Something 1379011111
3 More data 1379011111
Currently I've been using PHP to process the entire table, which mostly works, but for a table of 1000+ records, this is not very efficient on memory usage!
Many thanks in advance for any help you can give...
Get the timestamps for the filtering using a join. For instance, the following would make sure that the second timestamp is in a completed group:
select t.*
from t join
(select timestamp
from t
order by timestamp
limit 2
) tt
on t.timestamp = tt.timestamp;
The following would get the first three groups, no matter what their size:
select t.*
from t join
(select distinct timestamp
from t
order by timestamp
limit 3
) tt
on t.timestamp = tt.timestamp;

How to add value in SQL?

Good Day Experts,
I want to find out how can update records in a particular table where it will still retain the same value and then add a certain value..
for example
database: product, table stoklist.
id stock
1 150
2 100
my goal is, when I update the the Stock, let say I add 50 on its value, i want to add the figure i enter and then add it to the remaining no. of stock..
I hope you will help me with my problem.
I'm planning to create inventory syste, in PHP MySQL.
UPDATE stoklist SET stock = stock + 50 WHERE id = 1;
This would add 50 to the stock corresponding to the id 1.

number of records per day using grails

I have a grails application with a mysql database as the datasource. The mysql database is created and maintained by a third party. One of the tables 'visitinfo' contains 2 columns consisting of the 'userid' and 'logindatetime'.
userid is of type TEXT and logindatetime is of type 'datetime'.
In order to access the above table 'visitinfo', I have created a domain class 'VisitInfo' and mapped it to the mysql database table by which my grails application can easily store as well as retrieve data from the database.
On one of the pages, I am required to show visitor information for the last 30 days. So basically I am looking out for a solution to get number of visitors per day for the last 30 days. Something like this:
21-Jan-2012 ------ 36
22-Jan-2012 ------ 85
23-Jan-2012 ------ 115
24-Jan-2012 ------ 236
etc.
Also please note, that if a userid 'williamp' has 2 entries on a particular day, it should be counted as 2. So, am not looking out for uniqueness of users.
Any help will be appreciated.
I know nothing at all about grails. The MySQL query to obtain the desired result is as follows:
SELECT DATE_FORMAT(logindatetime,'%d-%m-%Y') dt
, COUNT(*) total
FROM visitinfo
GROUP
BY dt;
You want to use the countBy gorm method.
numLogins=VisitInfo.countByReleaseDateBetween(startOfDay,endOfDay)
This would need to be in a loop that calculates two date objects for each of the last 30 days. startOfDay would need a time value of 00:00:00:00 and endOfDay would need a time value of 23:59:00:00
I suggest following hql query to match your requirement
VisitInfo.executeQuery("select logindatetime, count(*) from VisitInfo group by logindatetime")

Complex MySQL COUNT query

Evening folks,
I have a complex MySQL COUNT query I am trying to perform and am looking for the best way to do it.
In our system, we have References. Each Reference can have many (or no) Income Sources, each of which can be validated or not (status). We have a Reference table and an Income table - each row in the Income table points back to Reference with reference_id
On our 'Awaiting' page (the screen that shows each Income that is yet to be validated), we show it grouped by Reference. So you may, for example, see Mr John Smith has 3 Income Sources.
We want it to show something like "2 of 3 Validated" beside each row
My problem is writing the query that figures this out!
What I have been trying to do is this, using a combination of PHP and MySQL to bridge the gap where SQL (or my knowledge) falls short:
First, select a COUNT of the number of incomes associated with each reference:
SELECT `reference_id`, COUNT(status) AS status_count
FROM (`income`)
WHERE `income`.`status` = 0
GROUP BY `reference_id`
Next, having used PHP to generate a WHERE IN clause, proceed to COUNT the number of confirmed references from these:
SELECT `reference_id`, COUNT(status) AS status_count
FROM (`income`)
WHERE `reference_id` IN ('8469', '78969', '126613', ..... etc
AND status = 1
GROUP BY `reference_id`
However this doesn't work. It returns 0 rows.
Any way to achieve what I'm after?
Thanks!
In MySQL, you can SUM() on a boolean expression to get a count of the rows where that expression is true. You can do this because MySQL treats true as the integer 1 and false as the integer 0.
SELECT `reference_id`,
SUM(`status` = 1) AS `validated_count`,
COUNT(*) AS `total_count`
FROM `income`
GROUP BY `reference_id`