|
not sure about looping in MySQL, believe its tricky, so I'd take care of that in by building a recordset in your program code, first your gonna need each distinct ServiceID...
SELECT DISTINCT serviceid from service
put the results of that into an array..
then your gonna have to loop through each one with something like...
SELECT *
FROM `service`
WHERE ID
IN (
SELECT id
FROM service
WHERE serviceid = frompreviousstatement
)
ORDER BY Qos ASC
LIMIT 0 , 1
and put each result from that into another array...
|
|
|
|
|
thank you for your reply, but I need a resultset object, It's hard to write it step by step like your solution.
I mean I need a single query statement
my result of goal 1 should be:
2000 2 Jacob 2011/1/1 2
2001 4 Jacob 2011/1/1 2
2002 5 Jacob 2011/2/3 1
2003 6 Tyler 2011/1/4 1
2004 9 Jacob 2011/2/4 1
2005 10 Jacob 2011/2/5 1
2006 11 Jacob 2011/2/4 2
2007 12 Jacob 2011/1/8 1
2008 13 Tyler 2011/2/6 1
2009 14 Dyla 2011/2/8 1
2010 15 Dyla 2011/2/9 1
2011 19 Andrew 2011/2/2 1
2012 18 Andrew 2011/2/19 1
2013 17 Andrew 2011/1/1 1
2014 16 Andrew 2011/1/1 1
2015 20 Andrew 2011/2/1 1
2016 21 Andrew 2011/1/19 1
2017 22 Jacob 2011/1/1 1
2018 23 Dyla 2011/2/3 1
2019 24 Dyla 2011/1/9 1
2020 25 Dyla 2011/1/1 1
2021 26 Andrew 2011/1/3 1
2022 28 Jacob 2011/1/9 1
2023 29 Tyler 2011/1/19 1
2024 30 Andrew 2011/2/1 1
2025 31 Dyla 2011/2/3 1
2026 32 Jacob 2011/2/4 1
2027 33 Tyler 2011/2/9 1
2028 34 Daniel 2011/1/6 1
2029 35 Daniel 2011/2/1 1
|
|
|
|
|
SELECT MIN(Qos), serviceid, id, TCID, EndTime from service
GROUP BY serviceid
maybe this then...
|
|
|
|
|
Thank you. It worked just like what I want. I cant believe it because your answer is more simple than another answher that I have got few hours ago:
SELECT DISTINCT serviceid,tcid,endtime,qos FROM (SELECT * FROM service ORDER BY serviceid, qos, id) AS base GROUP BY serviceid
thank you again genius, and plz help me with aim2-4.
|
|
|
|
|
Have a look at this query:
WITH id AS (
SELECT serviceid,tcid,Min(EndTime) endtime,Min(Qos) qos
FROM service
GROUP BY serviceid,tcid
)
SELECT s.ServiceID,s.ID,s.TCID,s.EndTime,s.Qos
FROM service s,id
WHERE s.serviceid = id.serviceid
AND s.tcid = id.tcid
AND s.endtime = id.endtime
AND s.qos = id.qos
|
|
|
|
|
hi,Jörgen Andersson
thank you for your help. the statement you posted seemed like a sqlsvr statement, I ran it on mysql and got a error:You have an error in your SQL syntax near "with.....".
I should run it on mysql.
in any case, I have got the correct answer of aim 1( I have write it in the top/first post), and plz help me with aim 2-4( It's still in trouble).
|
|
|
|
|
SELECT * FROM (SELECT COUNT(TCID) as C, TCID FROM service WHERE Qos = 1
and MONTH(EndTime) = 1
GROUP BY TCID) AS T
ORDER BY C DESC
This'll rank em for a month, I'll leave you to learn how to increment that month value to loop through for every one...
SELECT * FROM (SELECT COUNT(TCID) as count, TCID, Month FROM (SELECT month(endtime) as Month, tcid, serviceid FROM service
WHERE qos = 1
group by month(endTime),tcid, serviceid) AS T
GROUP BY month, tcid) AS S
order by month, count DESC
alternatly, this'll give you all of the months in the statement, I'll leave you to actually learn some SQL to refine it to get exactly what you want...
|
|
|
|
|
Thanks a lot!
I add filter statement( aim 1, without it the result will not be correct),like:
SELECT * FROM (SELECT COUNT(TCID) as count, TCID, Month FROM (
SELECT month(endtime) as Month, tcid, serviceid
FROM (SELECT MIN(Qos) qos, serviceid, id, TCID, EndTime from service
GROUP BY serviceid
)T1
WHERE qos = 1
group by month(endTime),tcid, serviceid
)As T
GROUP BY month, tcid) AS S
order by month, count DESC
and It got correct data:
count TCID Month
4 Andrew 1
3 Jacob 1
2 Tyler 1
2 Dylan 1
1 Daniel 1
4 Dylan 2
4 Jacob 2
4 Andrew 2
2 Tyler 2
1 Daniel 2
I think It is half of aim 2, thank you. please go on ,I think Top-2-in-January(rank) is Andrew/Jacob, Top-2-in-February(rank) is Andrew/Dylan/Jacob . So the result of aim2 should be 2 reocrders: Andrew/Jacob
modified 19-Apr-12 4:11am.
|
|
|
|
|
|
I have table(Tbl_A) in Server1 whenever DML(Insert/Update/Delete) operation happens in Tbl_A
it has to be automatically insert/Update/Delete into Table B,C(Tbl_B,Tbl_C) in different sever2.
'Whenever Table B,C Update/Delete/Insert record automatically record in Table A(Tbl_A).
I tried trigger but it does not help work for me.
Please advice me how to deal this scenario.
Thanks
Rao
Thanks & Regards
Rao
|
|
|
|
|
|
Thanks..
It won't do automatically only sync occasionally / manually.
I am looking for whenver DML operation happens.
Thanks & Regards
Rao
|
|
|
|
|
|
I have no better idea about relevant of database but your problem will be solve at w3school site because of there is well explain about database...I also daily visit that site for getting new information about html...
|
|
|
|
|
Have been working with and studying Microsoft SQL Server, SSRS and SSIS. I am reading about six books at the same time plus using them at work. I am close to interviewing for a Mid level job. I wish to hire a good T-SQL developer to put together study sheets which will include snipets of real world code in the major areas of TSQL such as stored procedures, funtions, loops, CASE and much more. I need to go in strong on the coding side but don't get much experience coding at work. Reading the books helps but its not like the real world.
ANY Suggestions.
|
|
|
|
|
It is an interesting approach, not sure what the response will be like! The way I always learn is to build, if there is an area where I feel deficient and work does not supply an opportunity to expand into that area I will build a project on my own time to explore the area.
While you are trying to short cut that process I don't think it can be done, someone can't feed you experience.
Never underestimate the power of human stupidity
RAH
|
|
|
|
|
As I ststed I am already reading 5 books and studying as hard as I can. I have no free time. I am only trying to get study sheets from a professional not take short cuts. I guess you suggest I stay in this underpaid paid possition that I can't even pay my rent with for another five years while I set up projects on my own to explore.
|
|
|
|
|
|
I was not denigrating the fact that you are trying, just the fact that experience is just that, experience and cannot be transferred, you can however learn from the experienced which is what you are trying to do.
Your problem is finding an experienced person with the spare time to teach and transfer that knowledge.
Never underestimate the power of human stupidity
RAH
|
|
|
|
|
Get about ten years experience. Then you will know how to deal with the types of situations you will likely encounter.
|
|
|
|
|
hi ,i want to select name from all around tables
& i have near 25 tables such as : select name from pharmacy , hospital , theater ,etc...
all of them is linked with place table by id
this picture show sample of tables :
http://www.mediafire.com/?20rsb4q25kyj14w[^]
|
|
|
|
|
If there can be several names for a specific place :
SELECT
'Pharmarcy' AS Entity
,Pharmacy.Name AS Name
FROM
Pharmacy
INNER JOIN Place ON Place.Id = Pharmacy.PlaceId
UNION
SELECT
'Hospital' AS Entity
,Hospital.Name AS Name
FROM
Hospital
INNER JOIN Place ON Place.Id = Hospital.PlaceId
and so on...
If there can be only one name for a specific place:
SELECT
Pharmacy.Name AS PharmacyName
,Hospital.Name AS HospitalName
FROM
Place
INNER JOIN Pharmacy ON Place.Id = Pharmacy.PlaceId
INNER JOIN Hospital ON Place.Id = Hospital.PlaceId
No memory stick has been harmed during establishment of this signature.
|
|
|
|
|
i have used union
select mall.name from mall,places where mall.place_id=places.id UNION select hospital.name from hospital,places where hospital.place_id=places.id UNION select clinic.name from clinic,places where clinic.place_id=places.id UNION select shop.name from shop,places where shop.place_id=places.id
but it's very long i should select name from 25 table
can you use nested query i think it will solve the problem
|
|
|
|
|
Having a long query doesn't count as a problem. Why is it classified as such? Does it not work?
Bastard Programmer from Hell
|
|
|
|
|
when there is much commonality in the fields of a number of tables, it tells me the data should have been structured differently, using just one or two tables and one more field. You now have to pay for the bad decisions made earlier, by adding unproductive code everywhere.
|
|
|
|