I have the following MYSQL Query - trying to join 3 tables and find unique information(data)
SELECT a.LocationID, a.Model, a.SerialNum,a.Purpose, b.IP, a.Services,a.DeviceID, COUNT(a.Hostname)
FROM RefConnection.Equipment_Info a, RefConnection.Connections b, RefConnection.VM_Info c
JOIN Equipment_Info on b.Hostname = a.Hostname
WHERE a.Hostname = c.Hostname
AND b.status = a.Status
AND a.status = c.Status
GROUP BY a.LocationID, a.Model, a.SerialNum, a.Purpose, b.IP, a.Services, a.DeviceID
ORDER BY COUNT(b.Hostname)DESC;
This works with 2 tables :
SELECT d.locationID, d.Model, d.SerialNum, d.Status, da.IP, COUNT(d.HOSTNAME)
FROM RefConnection.Equipment_Info d, RefConnection.Connections da
WHERE d.Hostname = da.Hostname
AND d.Status = da.Status
Group By d.locationID, d.Model, d.locationID, d.Model, d.SerialNum, d.Status, da.IP
ORDER BY COUNT(da.Hostname) DESC;
Unknown column 'b.Hostname' in 'on clause'
Never use commas in the FROM clause. Always use proper, explicit, standard JOIN syntax.
FROM RefConnection.Equipment_Info a JOIN
RefConnection.Connections b
ON b.Hostname = a.Hostname AND
b.status = a.Status JOIN
RefConnection.VM_Info c
ON a.Hostname = c.Hostname AND
a.status = c.Status
Your problem is the scoping rules for identifiers. This does not work as you expect with commas -- yet another reason to avoid them.
I don't know what Equipment_Info is doing a second time in the query.
I would also strongly advise you to use meaningful table aliases, such as e, c, and i rather than arbitrary characters.
Related
I try to change database from 'MySQL' to 'PostgreSQL' but i have problem some of my query
SELECT
SUM( CASE WHEN b.DISPLAYNAME IS NULL THEN 0 ELSE 1 END ) AS NUMMENUDETAIL,
A.DISPLAYNAME AS GROUPNAME,
b.displayname,
D.NAME,
min( A.ROLEID ) AS ROLEID
FROM
usermenu A
LEFT JOIN usermenu B ON A.ID = B.GROUPID,
userrole C,
position D,
positiondetail E
WHERE
( A.GROUPID IS NULL )
AND D.id = E.position_id
AND A.ROLEID = E.userrole_id
AND A.ROLEID = C.ID
AND B.ROLEID = E.userrole_id
AND B.ROLEID = C.ID
AND b.displayname = 'Transaction Listing Report'
GROUP BY
A.DISPLAYNAME,
b.displayname
LIMIT 10
i use this query into "MySQL" it pass
but when i use in "PostgreSQL" it error, They want my to GROUP BY d.name
then if i GROUP BY it may have different result
How can i fix this ?
This query does not comply with the SQL standard, so it is unsurprising that it does not work on all databases. The standard requires that all columns in the SELECT list that do not appear only inside aggregate functions must be in the GROUP BY clause.
You can have the same effect by using the (again, non-standard) DISTINCT ON clause in PostgreSQL.
I have the next query:
SELECT DISTINCT
bt.name, b.id
FROM
ports po,
cities c,
provinces p,
countries co,
states s,
translations t,
element_types et,
languages l,
boat_models bm,
boat_types bt,
boats b
JOIN
boat_prices bprf ON b.id = bprf.boat_id
AND bprf.checkin_date IS NULL
AND bprf.duration_id IS NULL
WHERE
t.element_translation = 'España'
AND et.name = 'Country'
AND s.name = 'confirmed'
AND s.id = b.state_id
AND l.locale = 'es'
AND t.language_id = l.id
AND t.element_type_id = et.id
AND t.element_id = p.country_id
AND c.province_id = p.id
AND po.city_id = c.id
AND b.port_id = po.id
AND bm.id = b.boat_model_id
AND bt.id = bm.boat_type_id
That is working perfectly and returning 9 rows:
'BOAT_TYPE_CATAMARAN','13707'
'BOAT_TYPE_SAILBOAT','13700'
'BOAT_TYPE_SAILBOAT','13701'
'BOAT_TYPE_SAILBOAT','13702'
'BOAT_TYPE_SAILBOAT','13703'
'BOAT_TYPE_SAILBOAT','13704'
'BOAT_TYPE_SAILBOAT','13705'
'BOAT_TYPE_SAILBOAT','13706'
'BOAT_TYPE_SAILBOAT','13708'
I want to group the results by boat type and get the number of boats per type.
However, when I do:
SELECT DISTINCT
bt.name, COUNT(b.id) AS num_boats
FROM
ports po,
cities c,
provinces p,
countries co,
states s,
translations t,
element_types et,
languages l,
boat_models bm,
boat_types bt,
boats b
JOIN
boat_prices bprf ON b.id = bprf.boat_id
AND bprf.checkin_date IS NULL
AND bprf.duration_id IS NULL
WHERE
t.element_translation = 'España'
AND et.name = 'Country'
AND s.name = 'confirmed'
AND s.id = b.state_id
AND l.locale = 'es'
AND t.language_id = l.id
AND t.element_type_id = et.id
AND t.element_id = p.country_id
AND c.province_id = p.id
AND po.city_id = c.id
AND b.port_id = po.id
AND bm.id = b.boat_model_id
AND bt.id = bm.boat_type_id
GROUP BY bt.name
ORDER BY bt.name
I´m getting:
'BOAT_TYPE_CATAMARAN','241'
'BOAT_TYPE_SAILBOAT','1928'
but according to the first query, I´m expecting
'BOAT_TYPE_CATAMARAN','1'
'BOAT_TYPE_SAILBOAT','8'
What am I missing?
I suspect that you want:
SELECT bt.name, COUNT(DISTINCT b.id) AS num_boats
FROM ...
WHERE ...
GROUP BY bt.name
ORDER BY bt.name
That is: move the DISTINCT within the COUNT() rather than directly in the SELECT.
Generally speaking, DISTINCT and GROUP BY do not go along well together; DISTINCT is already aggregation in essence, so mixing both is usually not relevant.
Note that your syntax uses old-school, implicit joins (with a comma in the FROM clause): you should be using standard joins (with the ON keyword), whose syntax has been state-of-the-art for decades.
You are doing a distinct in your first query so you are 'hiding' a lot if rows that gets doubled because of your join.
select
c.investor_id as investorid,
i.name as investor_name,
inv_typ.name as type_of_investor,
(select count(*) from investor_users where user_id=iu.user_id) as num_of_investors,
(select count(*) from task_logs where campaign_id=c.id) as num_of_task_logs,
c.updated_at as updated_date,
c.call_date as calldate,
a.name as admin_name,
c.id as campaign_id
from
admins a,
investors i,
investor_types inv_typ,
campaigns c,
task_logs tl,
campaign_statuses cs,
investor_users iu
where
c.investor_id=i.id
and i.investor_type_id=inv_typ.id
and i.id=iu.investor_id
and c.campaign_status_id = cs.id
and c.assigned_to_admin_id = a.id
and c.campaign_type_id = 1
and c.campaign_status_id = (select id from campaign_statuses having id=c.campaign_status_id and cs.code ='CLOSED') and c.assigned_to_admin_id = 46
and tl.campaign_id = c.id and max((select task_logs.task_status_id from task_logs having task_logs.campaign_id = c.id)) = 9
order by c.call_date ASC
You have a max() in the where clause:
max((select task_logs.task_status_id
from task_logs
having task_logs.campaign_id = c.id)) = 9
That is an improper use of an aggregation function. Instead:
(select max(task_logs.task_status_id)
from task_logs
where task_logs.campaign_id = c.id) = 9
This glaring error was easy to spot, because it is so audacious. However, your query would benefit from the following:
Never use commas in the FROM clause. Always use proper explicit JOIN syntax. That is the proper way to express joins for two decades, so it is time for everyone to use it.
Use table aliases so queries are easier to write and to read.
Don't confuse the having clause with the where clause. That subquery should have used where.
I have the first query which is producing correct results. What I need is I need to add the sum of values as a last column grouped by surveyid. I can't insert Sum(c.value) into the first query because it is an aggregate function. I have the correct query as my second query below. I know there's pivot functionality but not sure if it can be used here. I do realize that there will be repetition but that's okay.
'first query
SELECT
A.PATIENTID, B.STUDENTNUMBER, c.surveyid,
convert(varchar, A.CreatedDate, 107),
C.QuestionID, C.Value, D.Question
FROM
dbo.Survey A, dbo.Patient B, [dbo].[SurveyQuestionAnswer] C, [dbo].[LookupQuestions] D
WHERE
A.PATIENTID = B.ID
and c.SurveyID = A.ID
and c.QuestionID = d.ID
and c.questionid <> 10
ORDER BY
A.PATIENTID
'second query
select
c.surveyid,SUM(c.value) as scores
from
dbo.SurveyQuestionAnswer c
group by
c.SurveyID
order by
SurveyID '---not important
You can use SUM if you add the OVER clause. In this case:
SELECT
A.PATIENTID, B.STUDENTNUMBER, c.surveyid,
convert(varchar, A.CreatedDate, 107),
C.QuestionID, C.Value, D.Question,
SUM(c.Value) OVER(PARTITION BY c.surveyid) scores
FROM
dbo.Survey A
INNER JOIN dbo.Patient B
ON A.PATIENTID = B.ID
INNER JOIN [dbo].[SurveyQuestionAnswer] C
ON c.SurveyID = A.ID
INNER JOIN [dbo].[LookupQuestions] D
ON c.QuestionID = d.ID
WHERE
c.questionid <> 10
ORDER BY
A.PATIENTID
You could use something like this:
SELECT
s.PATIENTID, p.STUDENTNUMBER, sqa.surveyid,
CONVERT(varchar, s.CreatedDate, 107),
sqa.QuestionID, sqa.Value, lq.Question,
Scores = (SELECT SUM(Value) FROM dbo.SurveyQuestionAnswer s2 WHERE s2.SurveyID = s.ID)
FROM
dbo.Survey s
INNER JOIN
dbo.Patient p ON s.PatientID = p.ID
INNER JOIN
[dbo].[SurveyQuestionAnswer] sqa ON sqa.SurveyID = s.ID
INNER JOIN
[dbo].[LookupQuestions] lq ON sqa.QuestionID = lq.ID
WHERE
sqa.questionid <> 10
ORDER BY
s.PATIENTID
By having a subquery with the SUM(...) you should be able to get that sum as a single value and you don't need to use any grouping function
I have this query, but it takes about 15 seconds to finish.. how can i simplyfy it to get same result in less time? my problem is that i need all of this data at ones.
SELECT * FROM (
SELECT c.client_id, c.client_name, c.client_bpm,
c.client_su_name, c.client_maxbpm, s.bpm,
s.timestamp, m.mesure_id, ms.currentT
FROM tbl_clients c, tbl_meting m, tbl_sensor_meting s,
tbl_magsens_meting ms
WHERE c.client_id = m.client_id
AND (m.mesure_id = s.id_mesure
OR m.mesure_id = ms.id_mesure)
AND m.live =1
ORDER BY s.timestamp DESC
) AS mesure
GROUP BY mesure.client_id
I think the problem may be the OR condition from your WHERE clause? You seem to be trying to join to one table or another, which you can't do. So I've replaced it with a LEFT JOIN, so in the event no related records exist nothing will be returned.
I also took out your GROUP BY, as I don't think it was required.
SELECT c.client_id, c.client_name, c.client_bpm,
c.client_su_name, c.client_maxbpm, s.bpm,
s.timestamp, m.mesure_id, ms.currentT
FROM tbl_clients c
JOIN tbl_meting m ON m.client_id = c.client_id
LEFT JOIN tbl_sensor_meting s ON s.id_mesure = m.mesure_id
LEFT JOIN tbl_magsens_meting ms ON ms.id_mesure = m.mesure_id
WHERE m.live = 1
ORDER BY s.timestamp DESC