select tblProductMaster.*,AVG(tblReviewMaster.Rating) from tblProductMaster
left join tblReviewMaster
on tblProductMaster.ProductID = tblReviewMaster.ProductID
Group By tblProductMaster.ProductID
This query is returning an error:
Column 'tblReviewMaster.ReviewID' is invalid in the select list because it is not contained in either an aggregate function or the
GROUP BY clause.
It is not allowing me to have any column with AVG function... if I write
select AVG(tblReviewMaster.Rating) from tblProductMaster ...
then it works fine
So what to do to get product details from tblProductMaster also?
Error message says "Column 'tblReviewMaster.ReviewID' is invalid in the select list because it is not contained in either an aggregate function or the GROUP BY clause."
This means all the columns other than column in aggregate functions(AVG) should be a part of Group By Clause. So if * means (Column 1, Column 2....etc) so all of these columns must be present in GroupBy clause.
So your query would be
select tblProductMaster.*,AVG(tblReviewMaster.Rating) from tblProductMaster
left join tblReviewMaster
on tblProductMaster.ProductID = tblReviewMaster.ProductID
Group By tblProductMaster.*
By *, I mean write all columns that represent *. * won't work here.
Please find more details on MSDN
Related
What I have tried:
SELECT requestformtbl.employee_name, requestformtbl.request_type, requestformtbl.total_day,
requestformtbl.request_status, requestformtbl.admin_remark, requestformtbl.confirmed_by, requestformtbl.date_confirmed, requesttbl.max_allotment,
(requesttbl.max_allotment - sum(requestformtbl.total_day)) as Available from requestformtbl inner join requesttbl on
requestformtbl.request_type = requesttbl.request_type;
error: Column 'requestformtbl.employee_name' is invalid in the select list because it is not contained in either an aggregate function or the GROUP BY clause.
If requestttbl.request_type="Vacation Leave" has requesttbl.max_allotment=20,
when a new entry in requestformtbl is inserted with requestformtbl.request_type="Vacation Leave" and requestformtbl.total_day=5
I want to get the remaining available leave
You can get the results you want without using a GROUP BY by using a correlated subquery to get the sum of the employees used days:
SELECT rft.employee_name,
rft.request_type,
rft.total_day,
rft.request_status,
rft.admin_remark,
rft.confirmed_by,
rft.date_confirmed,
rt.max_allotment,
rt.max_allotment - (select sum(total_day)
from requestformtbl rft2
where rft2.employee_name = rft.employee_name) as Available
from requestformtbl rft
inner join requesttbl rt on rft.request_type = rt.request_type;
Note I've used table aliases to make the query more readable.
I keep getting the same error: But as far as I can tell it is included in the Query:
SELECT TOP 1 tblOutlets.OutletName, tblOutlets.Parish, Count([Query OOSActions].OODate) AS CountOfOODate1
FROM tblOutlets INNER JOIN [Query OOSActions] ON tblOutlets.OutletID = [Query OOSActions].OutletLookup
WHERE ((([Query OOSActions].OODate) Between [Forms]![Settings]![StartDate] And [Forms]![Settings]![EndDate]) AND (([Query OOSActions].Classification)="Biscuit"))
GROUP BY tblOutlets.OutletName, tblOutlets.Parish
HAVING (((tblOutlets.Parish)="St. Mary"))
ORDER BY Count([Query OOSActions].OODate) DESC;
UNION
SELECT TOP 1 tblOutlets.OutletName, tblOutlets.Parish, Count([Query OOSActions].OODate) AS CountOfOODate1
FROM tblOutlets INNER JOIN [Query OOSActions] ON tblOutlets.OutletID = [Query OOSActions].OutletLookup
WHERE ((([Query OOSActions].OODate) Between [Forms]![Settings]![StartDate] And [Forms]![Settings]![EndDate]) AND (([Query OOSActions].Classification)="Biscuit"))
GROUP BY tblOutlets.OutletName, tblOutlets.Parish
HAVING (((tblOutlets.Parish)="St. Catherine"))
ORDER BY Count([Query OOSActions].OODate) DESC;
I solved it by including ORDER BY only in the first query and then removing all ending semicolons. Thanks for that bit #Uueerdo
When using "TOP" and "ORDER BY" in the second SQL statement of a UNION query, I have experienced that it runs the TOP instruction first and then the "ORDER BY", so it makes no sense to order by anything here. With a simpler SQL example:
SELECT TOP 1 Ingresos.ID_Fra, Ingresos.Tipo, Ingresos.Numero, Ingresos.Fecha, "First Query" AS Tbl
FROM Ingresos
ORDER BY Ingresos.Fecha DESC
UNION
SELECT TOP 5 IngresosAnulados.ID_Fra, IngresosAnulados.Tipo, IngresosAnulados.Numero, IngresosAnulados.Fecha, "DEL" AS Tbl
FROM IngresosAnulados
ORDER BY Fecha DESC;
Source table: IngresosAnulados:
Assuming that the data in "IngresosAnulados" are these 10 records, the defined SQL statement shows the following result:
As you can see, it has taken five records from the second table and has ordered them by the field "Fecha", as specified, but we do not know what the criteria are for having selected the five records that it has chosen.
By the way, it is necessary to indicate the name of a field selected in the first query as the order of any of the union queries (except the first one). That's just what the error message says (The ORDER BY expression includes fields that are not selected by the query).
So you will need two independent queries (at least, an independent query for the second SQL statement) to get the record(s) that interests you in each of them and then join them in a UNION query if you really want the x top most of each UNION query to be displayed.
Each page in my system has multiple page_objects.
I need to return the last_changed records of my page_objects per page.
To decrease DB-impact, I have a SELECT ... IN query to return each last-edited page_object per page:
SELECT object, f_page_id, page_object_id, last_change
FROM page_objects
WHERE f_page_id IN (page_id1, page_id2, page_id3, etc...) GROUP BY f_page_id
ORDER BY last_change ASC;
Of course this does not work, because GROUP BY is applied before ORDER BY, so I changed the query:
SELECT object, f_page_id, page_object_id, max(UNIX_TIMESTAMP(last_change))
FROM page_objects
WHERE f_page_id IN (page_id1, page_id2, page_id3, etc...) GROUP BY f_page_id
But this still does not return the last-edited page_object per page_id.
What am I doing wrong?
Your query does not specify to get the record for the latest last_change. Merely that it gets the latest value of last_change. The other non aggregate values (ie, not the result of an aggregate function like MAX or MIN) that are not mentioned in the GROUP BY clause can come from any row for the grouped values.
As such you use a sub query to get the latest value for each page, and then join that back against your main table to get the matching rows
Something like this:-
SELECT page_objects.object,
page_objects.f_page_id,
page_objects.page_object_id,
page_objects.last_change
FROM page_objects
INNER JOIN
(
SELECT f_page_id, MAX(last_change) AS latest_last_change
FROM page_objects
GROUP BY f_page_id
) sub0
ON page_objects.f_page_id = sub0.f_page_id
AND page_objects.last_change = sub0.latest_last_change
WHERE page_objects.f_page_id IN (page_id1, page_id2, page_id3, etc...)
ORDER BY last_change DESC
Note that MySQL is quite unusual at allowing you to have non aggregate columns that are not mentioned in the GROUP BY clause (as it is against SQL standards, except under very particular circumstances). Most flavours of SQL will issue an error if you try this, and MySQL has a configuration parameter which will similar cause it to reject such queries.
Trying to convert below query into SQL, query works fine on MySQL. Problem seems to be the GROUP BY area. Even when I use just 1 GROUP BY field I get same error. Using query in InformaticaCloud.
ERROR
"the FROM Config_21Cent WHERE resp_ind = 'Insurance' GROUP BY
resp_Ind;;] is empty in JDBC connection:
[jdbc:informatica:sqlserver://cbo-aps-inrpt03:1433;DatabaseName=SalesForce]."
SELECT sum(Cast(Resp_Ins_Open_dol AS decimal(10,2))) as baltotal,
carrier_code,
carrier_name,
carrier_grouping,
collector_name,
dataset_loaded,
docnum,
envoy_payer_id,
loc,
market,
master_payor_grouping,
plan_class,
plan_name,
resp_ins,
resp_ind,
resp_payor_grouping,
Resp_Plan_Type,
rspphone,
state
FROM Config_21Cent
WHERE resp_ind = 'Insurance'
GROUP BY
(resp_ins + resp_payor_grouping +
carrier_code + state + Collector_Name);
Your entire query isn't going to work. The group by statement contains a single expression, the summation of a bunch of fields. The select statement contains zillions of columns without aggregates. Perhaps you intend for something like this:
select resp_ins, resp_payor_grouping, carrier_code, state, Collector_Name,
sum(Cast(Resp_Ins_Open_dol AS decimal(10,2))) as baltotal
from Config_21Cent
WHERE resp_ind = 'Insurance'
GROUP BY resp_ins, resp_payor_grouping, carrier_code, state, Collector_Name;
THis will work in both databases.
The columns in SELECT statement must be a subset (not proper subset but subset) of columns in 'GROUP BY' statement. There is no such restriction on aggregates in SELECT statement though. There could be any number of aggregates; aggregates even on columns not in GROUP BY statement can be included.
I need to get a title from table 2, table 2 has title and id column.
Table 1 has some data and three of these columns concatenated together makeup the id that can be found in table 1.
I used CONCAT_WS() function and gave this column an alias name and need to use the Alias for the on argument(At least this is what I understood I needed to do)
I thought this could be a simple left join, yet it is not working for me.
This is my query
SELECT
table_openers.mail,
table_openers.f_name,
table_openers.l_name,
table_openers.Quality,
CONCAT_WS('-',
table_openers.esp,
table_openers.acc,
table_openers.group) as 't1aid',
table_groups.aid,
table_groups.group_name
FROM
lance_mailstats.table_openers
LEFT JOIN
lance_mailstats.table_groups ON table_groups.aid = t1aid;
I get results for mail, f_name, l_name, Quality and t1aid, but the aid and group_name columns of the second table return null.
I feel like you can't use an alias in the ON clause.
Try doing
LEFT JOIN
lance_mailstats.table_groups ON table_groups.aid = CONCAT_WS('-',
table_openers.esp,
table_openers.acc,
table_openers.group);
"You can use the alias in GROUP BY, ORDER BY, or HAVING clauses to refer to the column" (from dev.mysql.com/doc/refman/5.0/en/problems-with-alias.html).
And "The conditional_expr used with ON is any conditional expression of the form that can be used in a WHERE clause" (from dev.mysql.com/doc/refman/5.1/en/join.html).
So as a logical inference you're not allowed to use aliases in ON clauses.
try to use a subquery..
it goes like this.........
ex.
SELECT
tbl1.mail, tbl1.f_name, tbl1.l_name,tbl1.Quality, tbl1.t1aid,table_groups.aid,
table_groups.group_name
FROM
(SELECT
table_openers.mail,
table_openers.f_name,
table_openers.l_name,
table_openers.Quality,
CONCAT_WS('-',
table_openers.esp,
table_openers.acc,
table_openers.group) as 't1aid',
FROM
lance_mailstats.table_openers )tbl1
LEFT JOIN
lance_mailstats.table_groups ON table_groups.aid = tbl1.t1aid;