473,399 Members | 2,478 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,399 software developers and data experts.

Combining two queries on the same table

HI

I am running mysql 5.0.51a and have a table with the following structure

ID, Date, startTime, stopTime, Name

I want to find the last stopTime from yesterday for each name and the first startTime from today for each name.

I have tried using union but end up with a stopTmes for today and yesterday as well as the startTimes for today and yesterday for each name. I also tried using sub queries but get an error as there is more than one result for each query.

How can I create a query to just get the the yesterdays stop time and todays start time only for each name.

Thanks in advance for your help.
Regards
Richard
Jul 14 '08 #1
6 4704
amitpatel66
2,367 Expert 2GB
Could you please post what you have tried so far?
Jul 14 '08 #2
Could you please post what you have tried so far?
Sorry, I tried

SELECT
(SELECT MAX(stopTime) FROM `aaworkingdetailsbreakdown` WHERE `work_date` = (curdate()-1)),
(SELECT MIN(startTime) FROM `aaworkingdetailsbreakdown` WHERE `work_date` = (curdate())),
`name` from `aaworkingdetailsbreakdown` WHERE `customer_id` = 55 group by `name`

this is the closest I got but just shows the max stop and min start. If I try adding the group by name within each select statement
I get the error #1242 - Subquery returns more than 1 row

I also tried using the following union query which showed the data I wanted but doubled up the results as it showed both the start and stop times for each name for each day.

(SELECT MAX(stopTime),MIN(startTime),`name`
FROM `aaworkingdetailsbreakdown`
WHERE `work_date` = (curdate()-1) and `customer_id` = 55
group by `name`)
UNION
(SELECT MAX(stopTime),MIN(startTime),`name`
FROM `aaworkingdetailsbreakdown`
WHERE `work_date` = (curdate()) and `customer_id` = 55
group by `name`)
Jul 14 '08 #3
coolsti
310 100+
You need something like the query technique that I explained in this thread:

http://bytes.com/forum/thread819577.html

Extending this to your problem, I wonder if this would work? I never tried it with a union before.

(SELECT T1.id, T1.stopTime FROM aaworkingdetailsbreakdown T1 left join
(SELECT T2.id, max(T2.stopTime) as maxstoptime
FROM aaworkingdetailsbreakdown T2 where work_date=(curdate()-1) group by T2.id) T3
on (T1.id = T3.id and T1.stopTime = t3.maxstoptime) where T1.work_date=(curdate()-1)
UNION
(SELECT T4.id, T4.startTime FROM aaworkingdetailsbreakdown T4 left join
(SELECT T5.id, min(T5.startTime) as minstarttime
FROM aaworkingdetailsbreakdown T5 where work_date=(curdate()) group by T5.id) T6
on (T4.id = T6.id and T4.startTime = t6.minstarttime) where T4.work_date=(curdate())
Jul 14 '08 #4
Thanks for this and for the other thread

Unfortunately I got an error until I changed (T1.id = T3.id and T1.stopTime = t3.maxstoptime) to (T1.id = T3.id and T1.stopTime = maxstoptime) although I'm hoping that didn't undermine the crux at what you were saying.

This worked but provided similar results to what I was getting previously. With the union it only shows the columns in the first select statement so I only got the stopTimes from yesterday. Im not sure exactly how to add in the startTimes but I think if I did I would then get start and stop times for both days again.

I tried using subqueries rather unions as below, but then got the error #1241 - Operand should contain 1 column(s)

SELECT
(
SELECT T1.id, T1.stopTime, T1.name
FROM aaworkingdetailsbreakdown T1
left join
(

SELECT T2.id, max(T2.stopTime) as maxstoptime
FROM aaworkingdetailsbreakdown T2
where work_date=(curdate()-1)
group by T2.id
)
T3 on (T1.id = T3.id
and T1.stopTime = maxstoptime)
where T1.work_date=(curdate()-1)
)
,
(
SELECT T4.id, T4.startTime, T4.name
FROM aaworkingdetailsbreakdown T4
left join
(
SELECT T5.id, min(T5.startTime) as minstarttime
FROM aaworkingdetailsbreakdown T5
where work_date=(curdate())
group by T5.id
)
T6 on (T4.id = T6.id
and T4.startTime = minstarttime)
where T4.work_date=(curdate())
)
,
name from aaworkingdetailsbreakdown group by name
Jul 14 '08 #5
coolsti
310 100+
Hi!

Yes, you found a typo in the example I made for you. It should have worked if you changed the t3 to T3, because MySql is case sensitive. It also works for you by removing the alias T3 altogether, apparently in this case which table was meant was clear to MySql.

I do not understand why the Union would not work for you. Perhaps you should try the second part of the query (for the start times) by itself without the first part and without the Union, to make sure that you actually do get some selected rows. Perhaps there is something wrong with the data in your table, or with the query (another typo?) that causes the select after the union to return nothing. Try to debug this first, as it should work.

Oh and yes, there is a second typo in my example!!! Make sure you change the t6 to T6 in the second part!!!

Actually, on second thought, try this:

(SELECT T1.id, T1.stopTime as time, 'stoptime' as label FROM aaworkingdetailsbreakdown T1 left join
(SELECT T2.id, max(T2.stopTime) as maxstoptime
FROM aaworkingdetailsbreakdown T2 where work_date=(curdate()-1) group by T2.id) T3
on (T1.id = T3.id and T1.time = T3.maxstoptime) where T1.work_date=(curdate()-1)
UNION
(SELECT T4.id, T4.startTime as time, 'starttime' as label FROM aaworkingdetailsbreakdown T4 left join
(SELECT T5.id, min(T5.startTime) as minstarttime
FROM aaworkingdetailsbreakdown T5 where work_date=(curdate()) group by T5.id) T6
on (T4.id = T6.id and T4.time = T6.minstarttime) where T4.work_date=(curdate())

The changes, besides me fixing the two typos are the following:

I gave the alias of "time" to both stoptime in the first query and starttime in the second query. This is because you are making a UNION of the two queries, and so the column names maybe have to be identical. In order to be able to tell one from the other, I also added a third "column" which is just the string, either "stoptime" or "starttime", and I give this the name "label".

See if that works for you.
Jul 14 '08 #6
Thanks for all your help with this. There was a bracket missing which I added in hopefully in the right place but I still got a mysql error #1064.

However, I managed to resolve the query by creating a view for all the stopTimes from yesterday and then another view for all the startTimes for today. Then it was simply a matter of selecting all from the two "tables" and joining on ID.

Thanks once again for your help.
Regards
Richard
Jul 21 '08 #7

Sign in to post your reply or Sign up for a free account.

Similar topics

7
by: frizzle | last post by:
Hi, I know this might sound strange but i think(/hope) it's quite simple: I'm running 2 queries in a mysql DB, first one returns 20 results. Now how can i echo results from the second query...
1
by: Bruce MacDonald | last post by:
I've got a question/request for the SQL gurus. I'm building a model of bandwidth demand in MS Access and want to get aggregated results for demand at each PCP in each time period. The two...
1
by: ravi | last post by:
I have created the following interest to calculate the interest for the following currency pairs. I have tried to combine them in macros using conditions but the next query that is run in the macro...
3
by: craig.brenizer | last post by:
I have a table of data that has duplicate values in the pagenumber field. How can I combine the data so that the page numbers of duplicate part numbers are on one record for that part number? ...
2
by: BeruthialsCat | last post by:
I have 4 crosstabs which are keyed on a managerID field. I want to combine them to provide the data for a graph. I currently have them combined in a union query to produce 4 rows of data for each...
1
by: ferraro.joseph | last post by:
Hi, I'm querying Salesforce.com via their AJAX toolkit and outputting query results into a table. Currently, their toolkit does not possess the ability to do table joins via their structured...
7
by: isdeveloper | last post by:
Hi All, I have a problem with a table that I want to get nice data out of in a single query. The guys here reckon it can't be done in a single query but I wanted to prove them wrong !!...
2
by: billelev | last post by:
Does anyone know if it is possible to combine two queries that have the same fields? I basically want to combine a number of fields with the totals for those fields into one query (see example...
3
by: Hillary H | last post by:
I would like to take these two queries and combine them into one if possible. This is the first query: SELECT DISTINCT . INTO FROM INNER JOIN NOLDBA_OBLIGATION ON .=NOLDBA_OBLIGATION.ID_CASE...
7
by: tcveltma | last post by:
Hi again, Ok, so I have about 15 crosstab queries. Each crosstab query is an employee's name. Within the query are the weeks as the column heading, the work order numbers as the row heading, and...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.