I am attempting to get this particular code to work by breaking it down into smaller bits to see what the issue may be for a SQL query
select tt.tk, tk2, count(*), count(a2.tk) / count(*) as ratio
from ((select distinct a.TK, a2.TK as tk2
from access6 as a inner join
access6 as a2
on (a.DocumentNo = a2.DocumentNo)
where a.TK <> a2.TK
) tt left join
access6 as a
on (a.tk = tt.tk)
) left join
access6 as a2
on (a2.tk = tt.tk2 and a2.DocumentNo = a.DocumentNo)
group by tk, tk2;
If I run it as
(select distinct a.TK, a2.TK as tk2
from access6 as a inner join
access6 as a2
on (a.DocumentNo = a2.DocumentNo)
where a.TK <> a2.TK
) tt left join
access6 as a
on (a.tk = tt.tk)
it says "Syntax error in Union Query"
The first portion of the select works just fine.
first part of sql it must be inner join in from access6 as a **join**
second sql, you can only join tables in a From clause. So
) tt left join
access6 as a
wouldn't work unless the whole part comes under a FROM clause.
Related
I have been struggling with various syntax errors on a query in MS Access that works fine in TSQL.
For example, I learned that MS Access requires parentheses when there are more than one left/inner joins, and also if there is more than one ON condition for any of those joins, thanks to this post and this post.
However, I am now stuck with another "Join expression not supported" error, when I have three ON conditions for an INNER JOIN:
SELECT *
FROM ((
(Table1
INNER JOIN Table2 ON Table2.Col2 = Table1.Col2)
LEFT JOIN Table3
ON (Table1.id = Table3.fid
and Table3.Col2 > SomeNumber)
)
LEFT JOIN Table4
ON (Table4.Col2 = Table3.Col2
and Table4.Col4 = 'SomeValue'
and Table4.fid = Table1.x_id)
)
The above gets the "Join expression not supported" error, but if I remove any one of the three conditions in the last LEFT JOIN ON, the SQL was fine, like this:
SELECT *
FROM ((
(Table1
INNER JOIN Table2 ON Table2.Col2 = Table1.Col2)
LEFT JOIN Table3
ON (Table1.id = Table3.fid
and Table3.Col2 > SomeNumber)
)
LEFT JOIN Table4
ON (Table4.Col2 = Table3.Col2
and Table4.fid = Table1.x_id)
)
Any two of the three conditions was accepted in the above. I have even tried adding additional parentheses like this but it didn't work either:
LEFT JOIN Table4
ON ((Table4.Col2 = Table3.Col2
and Table4.Col4 = 'SomeValue')
and Table4.fid = Table1.x_id)
)
Please help - thanks!
I have written a MYSQL script, that returns incorrect data. I am quite fluent in SQL, but this query is not returning correct results. Can someone have a look and see whats going on. The problem is the noOfBids, and noOfRatedTimes. The values are the same for both columns and are large values too.
select
a.user_name as userName,
coalesce(count(b.sp_user_name),0) as noOfBids,
coalesce(ROUND(AVG(b.a_amount),2),0) as avgAmount,
coalesce(count(d.sp_user_name),0) as noOfRatedTimes,
coalesce(ROUND(AVG(d.user_rate),2),0)
from users a
left join project_imds b
on b.sp_user_name = a.user_name
left join projects c
on b.project_code = c.project_code
left join sp_user_rating d
on d.sp_user_name = b.sp_user_name
where a.user_type = 'SP'
and a.active = 'Y'
group by a.user_name
order by coalesce(ROUND(AVG(d.user_rate),2),0) desc;
I have created a workaround on this, by creating a temp table to get the avg values and joining this to the main query.
Since I don't know the specifics of the data behind your query, this is only a guess. But perhaps you'd rather join "sp_user_rating" directly to "users", changing
left join sp_user_rating d
on d.sp_user_name = b.sp_user_name
to
left join sp_user_rating d
on d.sp_user_name = a.user_name
select
a.user_name as userName,
coalesce(count(b.sp_user_name),0) as noOfBids,
coalesce(ROUND(AVG(b.a_amount),2),0) as avgAmount,
coalesce(count(d.sp_user_name),0) as noOfRatedTimes,
coalesce(ROUND(AVG(d.user_rate),2),0)
from users as a
left join project_imds as b
on b.sp_user_name = a.user_name
left join projects as c
on b.project_code = c.project_code
left join sp_user_rating as d
on d.sp_user_name = b.sp_user_name
where a.user_type = 'SP'
and a.active = 'Y'
group by a.user_name
order by coalesce(ROUND(AVG(d.user_rate),2),0) desc;
I have folowing MYsql Query And Trying to right outer join but unable to understan how to do this
here is query plase any one help
select lp_des.lpname,today.cnt_veh_tdy,todate.cnt_veh_tdate
from
(select distinct registration.lpcode,loadingpoint.lpname
from registration,loadingpoint
where registration.lpcode=loadingpoint.lpcode) lp_des,
(select lpcode,count(vehicleno) cnt_veh_tdate
from registration
where registration.companycode='01'
group by lpcode) todate,
(
select lpcode,count(vehicleno) cnt_veh_tdy
from registration
where registration.companycode='01'
and registration.date=(select max(date) from registration)
group by lpcode) today
right outer join today on lp_des.lpcode = today.lpcode
right outer join todate on lp_des.lpcode = todate.lpcode
I want to make right outer join on this part
where lp_des.lpcode=todate.lpcode
and lp_des.lpcode=today.lpcode
Please help and Thanks in advance
You asked for this:
select
lp_des.lpname,
today.cnt_veh_tdy,
todate.cnt_veh_tdate
from
(select distinct
r.lpcode,
l.lpname
from
registration r
inner join loadingpoint l on l.lpcode = r.lpcode) lp_des
right join
(select
r.lpcode,
count(r.vehicleno) cnt_veh_tdate
from
registration r
where
r.companycode='01'
group by
lpcode) todate on todate.lpcode = lp_des.lpcode
right join
(select
r.lpcode,
count(r.vehicleno) cnt_veh_tdy
from
registration r
where
r.companycode = '01'
and registration.date = (select max(date) from registration)
group by
r.lpcode) today on today.lpcode = lp_des.lpcode
But I think you mean this:
select
r.lpcode,
l.lpname,
count(r.vehicleno) cnt_veh_tdate,
count(case when r.date = md.date then r.vehicleno else null end) cnt_veh_tdy
from
registration r
inner join (select max(rm.date) maxdate from registration rm) md
left join loadingpoint l on l.lpcode = r.lpcode
where
r.companycode = '01'
group by
r.lpcode
and maybe even this:
select
r.lpcode,
l.lpname,
count(r.vehicleno) cnt_veh_tdate,
count(case when r.date = date() then r.vehicleno else null end) cnt_veh_tdy
from
registration r
left join loadingpoint l on l.lpcode = r.lpcode
where
r.companycode = '01'
group by
r.lpcode
If I read it correctly, you want a query that returns the number of vehicles for company 1 assigned to a loading point, overall as well as for today only. And you also want that count for vehicles that do not have loading point assigned yet.
Though it would help if you would have added this description. It will help the ones answering your question, but it will also help you writing the right query in the first place.
The syntax for a right outer join is:
SELECT t1.id, t2.id FROM t1 RIGHT OUTER JOIN t2 ON t1.field1 = t2.field2
If you're joining on the same field you can use USING instead of ON:
SELECT t1.id, t2.id FROM t1 RIGHT OUTER JOIN t2 USING (field)
Somehow am not successful with creating the query that I want.
DB is to do with locations, there are the following tables which are relevant
t_location - list of locations incl. field t_location_zipcode, and t_location_id_location
t_zipcodecity - join table just t_zipcodecity_zipcode t_zipcodecity_id_city
t_city - city list with t_city_id_city
t_citystate - join table, t_citystate_id_city, t_citystate_id_state
t_state - list of states with t_state_id_state
Initially I tried to get a list of states with locations using this query:
SELECT DISTINCT `t_state_id_state`
, `t_state_name_full`
FROM (`t_state`)
LEFT JOIN `t_citystate` ON `t_state_id_state` = `t_citystate_id_state`
LEFT JOIN `t_city` ON `t_citystate_id_state` = `t_city_id_city`
LEFT JOIN `t_zipcodecity` ON `t_city_id_city` = `t_zipcodecity_id_city`
LEFT JOIN `t_location` ON `t_zipcodecity_zipcode` = `t_location_zipcode`
ORDER BY `t_state_name_full` asc
which works fine.
Now what I also need / want which I am failing dismally at is to get the number of locations in each state. I don't know if it can be done in this one query or if i need another, either way I need help!
you can use a count and a group by. Something like this:
SELECT DISTINCT `t_state_id_state`
, `t_state_name_full`
, COUNT(*)
FROM (`t_state`)
LEFT JOIN `t_citystate` ON `t_state_id_state` = `t_citystate_id_state`
LEFT JOIN `t_city` ON `t_citystate_id_state` = `t_city_id_city`
LEFT JOIN `t_zipcodecity` ON `t_city_id_city` = `t_zipcodecity_id_city`
LEFT JOIN `t_location` ON `t_zipcodecity_zipcode` = `t_location_zipcode`
GROUP BY `t_state_id_state` , `t_state_name_full`
ORDER BY `t_state_name_full` asc
SELECT t_state_id_state
, t_state_name_full
, COUNT(DISTINCT t_location_id_location) AS locations_number
FROM
t_state
LEFT JOIN
t_citystate ON t_state_id_state = t_citystate_id_state
LEFT JOIN
t_city ON t_citystate_id_state = t_city_id_city
LEFT JOIN
t_zipcodecity ON t_city_id_city = t_zipcodecity_id_city
LEFT JOIN
t_location ON t_zipcodecity_zipcode = t_location_zipcode
GROUP BY t_state_id_state
ORDER BY t_state_name_full ASC
Ok thats looking good, my bad with the left joins, i was initially trying to get ALL the states which is why i used them. But i change that to use inner joins
SELECT t_state_id_state,
t_state_name_full,
COUNT(DISTINCT t_location_id_location) AS locations_number
FROM t_state
INNER JOIN t_citystate ON t_citystate_id_state = t_state_id_state
INNER JOIN t_city ON t_city_id_city = t_citystate_id_city
INNER JOIN t_zipcodecity ON t_zipcodecity_id_city = t_city_id_city
INNER JOIN t_location ON t_location_zipcode = t_zipcodecity_zipcode
GROUP BY t_state_id_state
ORDER BY t_state_name_full ASC
then i actually end up with a result that looks good !
This is my current query:
$sel = "SELECT
db1t1.userid, db1t1.customer_id, db2t1.customers_id, db2t1.orders_id, db2t2.products_price
FROM
database1.table1 db1t1
LEFT JOIN database2.table1 db2t1 ON
db1t1.customer_id = db2t1.customers_id
LEFT JOIN database2.table2 db2t2 ON
db2t1.orders_id = db2t2.orders_id
WHERE db1t1.userid IN(
SELECT
l.userid, l.username, r.username, r.cus_id
FROM
database1.table3 l
LEFT JOIN database2.table4 r ON
l.username = r.username
WHERE r.cus_id = '1234'
)";
Error message:
Operand should contain 1 column(s)
The error occurred because that you returned a result with multiple columns to an IN clause.
Try this:
SELECT
`db1t1`.`userid`, `db1t1`.`customer_id`, `db2t1`.`customers_id`,
`db2t1`.`orders_id`, `db2t2`.`products_price`
FROM `database1`.`table1` AS `db1t1`
LEFT JOIN `database2`.`table1` AS `db2t1`
USING (`customers_id`)
LEFT JOIN `database2`.`table2` AS `db2t2`
USING (`orders_id`)
WHERE `db1t1`.`userid` IN (
SELECT `l`.`userid`
FROM `database1`.`table3` AS `l`
LEFT JOIN `database2`.`table4` AS `r`
USING (`username`)
WHERE `r`.`cus_id` = 1234
)
What are you trying to achieve ? Maybe we can find a better solution.
Also, I think that you should do an INNER JOIN instead of a LEFT JOIN in the subquery.