I have this query:
SELECT DISTINCT par.id, par.title, cod.id AS id_codebook, cod.title AS title_codebook
FROM catalog_parameter_codebook cod
JOIN catalog_parameter par ON (par.id=cod.id_parameter)
JOIN catalog_parameter_product_value_parameter_codebook_mm codmm ON (cod.id=codmm.id_parameter_codebook)
JOIN catalog_parameter_product_value pv ON (pv.id=codmm.id_parameter_product_value)
JOIN catalog_product p ON (p.id=pv.id_product)
WHERE p.state=2 AND p.hidden=0 AND par.type=2 AND par.display_in_parameter_search=1 AND par.hidden=0
ORDER BY par.sorting, cod.title
And i get this error:
LINE 8: ORDER BY par.sorting, cod.title
ERROR: for SELECT
DISTINCT, ORDER BY expressions must appear in select list
I am a total beginner with postgreSQL.
Thank you.
as error message states, par.sorting must be in the select list
if you use distinct or group by, all columns you are sorting by, must be in the select column list
SELECT DISTINCT par.id, par.title, cod.id AS id_codebook, cod.title AS title_codebook, par.sorting
FROM catalog_parameter_codebook cod
JOIN catalog_parameter par ON (par.id=cod.id_parameter)
JOIN catalog_parameter_product_value_parameter_codebook_mm codmm ON (cod.id=codmm.id_parameter_codebook)
JOIN catalog_parameter_product_value pv ON (pv.id=codmm.id_parameter_product_value)
JOIN catalog_product p ON (p.id=pv.id_product)
WHERE p.state=2 AND p.hidden=0 AND par.type=2 AND par.display_in_parameter_search=1 AND par.hidden=0
ORDER BY par.sorting, cod.title
Related
from this question, i tried to add join in the query, but i have this error
ORA-00979: not a GROUP BY expression
SELECT ee.UP, max(ee.CODE_DEPT), dpt.LIBELLE_DEPT
FROM ESP_ENSEIGNANT ee
INNER JOIN ESP_DEPT dpt
ON ee.CODE_DEPT = dpt.CODE_DEPT
group by ee.CODE_DEPT;
You need to include the columns which are not in the agg function. i.e ee.UP & dpt.Libelle_Dept as well.
I need to fetch each select item and its count in single query.Here is my query and expecting the output as single array.
select ordetable.order_id, count(ordetable.order_id), order_table.name,
count(order_table.na me), orderitem_table.itemname,
count(orderitem_table.itemname)
from order_table
left join orderitem_table
on order_table.order_id = orderitem_table.order_id
group by ordetable.order_id
Am getting this error:
[Microsoft][ODBC Driver 11 for SQL Server][SQL Server]Column
'orderitem_table.itemname' is invalid in the select list because it is
not contained in either an aggregate function or the GROUP BY clause.
The issue is evident from the error message, isn't it?
Modify your query like below.
select ordetable.order_id, count(ordetable.order_id), order_table.name,
count(order_table.name), orderitem_table.itemname,
count(orderitem_table.itemname)
from order_table
left join orderitem_table
on order_table.order_id = orderitem_table.order_id
group by ordetable.order_id, order_table.name, orderitem_table.itemname
If you not like to group order_table.name and orderitem_table.itemname, then you can use 'min' condition.
select ordetable.order_id, count(ordetable.order_id), min(order_table.name),
count(order_table.name), min(orderitem_table.itemname),
count(orderitem_table.itemname)
from order_table
left join orderitem_table
on order_table.order_id = orderitem_table.order_id
group by ordetable.order_id
Your wording and your query are not in sync. Each item and its count (over all the orders) will be
select orderitem_table.itemname, orderitem_table.itemnumber, count(*)
from order_table
join orderitem_table
on order_table.order_id = orderitem_table.order_id
group by orderitem_table.itemname, orderitem_table.itemnumber
As written in the title: How to write this SQL statement correctly?
select
sl.switch_ip,
sl.switch_name,
count(m.switch_ip) as macentries,
(select arpentries from (select sl1.switch_ip, sl1.switch_name, count(ar.switch_ip) as arpentries
from my_switchlist sl1
left Join my_arptable ar on ar.switch_ip = sl1.switch_ip
group by sl1.switch_ip,sl1.switch_name
order by sl1.switch_ip))
from my_switchlist sl
left Join my_mactable m on m.switch_ip = sl.switch_ip
group by sl.switch_ip,sl.switch_name
order by sl.switch_ip
The select and the sub-select work fine if they are executed separately.
But as soon as I put them together I get the following error:
Error: A subquery has returned not exactly one row.
SQLState: 21000
ErrorCode: -284
Position: 470
Looks like you want both the 'count' aggregates, which should be possible with something like this:
select
macquery.switch_ip,
macquery.switch_name,
macquery.macentries,
arpquery.arpentries
from
(
select
sl.switch_ip as switch_ip,
sl.switch_name as switch_name,
count(m.switch_ip) as macentries
from my_switchlist sl
left outer join my_mactable m
on m.switch_ip = sl.switch_ip
group by
sl.switch_ip,
sl.switch_name
) macquery
join
(
select
sl1.switch_ip as switch_ip,
sl1.switch_name as switch_name,
count(ar.switch_ip) as arpentries
from my_switchlist sl1
left outer join my_arptable ar
on ar.switch_ip = sl1.switch_ip
group by
sl1.switch_ip,
sl1.switch_name
) arpquery
on (macquery.switch_ip = arpquery.switch_ip
and macquery.switch_name = arpquery.switch_name)
Probably there are more than one "sl1.switch_ip,sl1.switch_name" groups in your "my_switchlist, my_arptable" join.
select arpentries from (select sl1.switch_ip, sl1.switch_name, count(ar.switch_ip) as arpentries
from my_switchlist sl1
left Join my_arptable ar on ar.switch_ip = sl1.switch_ip
group by sl1.switch_ip,sl1.switch_name
order by sl1.switch_ip)
The above query should not return more than one result in order you to use its result in your outer query. So probably there is more than one "sl1.switch_ip,sl1.switch_name" group.
I have the following code taken from my previous question here and changed a little.
SELECT *
FROM ES_TOOL
INNER JOIN ES_HARDWARE ON ES_HARDWARE.eshw_ID = ES_TOOL.ESTOOL_HARDWARE
INNER JOIN ES_PAYMENT on ES_payment.espay_id = es_TOOL.estool_payment
LEFT JOIN (
SELECT
tchap.estch_tool, tfacet.estfa_tool,
count(marks.esmrk_value) AmtMarks
FROM ES_MARK marks
left Join ES_TOOL_FACET tfacet ON marks.esmark_tool_facet = tfacet.estfa_id --line added
left Join ES_TOOL_CHAPTER tchap ON marks.esmark_tool_chapter = tchap.estch_id
GROUP BY tchap.estch_tool
) h ON ES_TOOL.estool_id = h.estch_tool
I'm trying to add an additional join in an attempt to get a mark count from "marks" that meet either of the left join "ON" criteria. Without the extra line the query executes, but doesn't count marks that match "facet" criteria. With it I get the following error:
Msg 8120, Level 16, State 1, Line 1
Column 'ES_TOOL_FACET.estfa_tool' is invalid in the select list because it is not contained in either an aggregate function or the GROUP BY clause.
Any help would be appreciated.
The error message means that ES_TOOL_FACET.estfa_tool needs to be included in the Group By.
When you use Group By, all non-aggregated columns must be included in the group by section.
This should be obvious, in your inner query:
SELECT tchap.estch_tool, tfacet.estfa_tool, count(marks.esmrk_value) AmtMarks
FROM ES_MARK marks
left Join ES_TOOL_FACET tfacet ON marks.esmark_tool_facet = tfacet.estfa_id --line added
left Join ES_TOOL_CHAPTER tchap ON marks.esmark_tool_chapter = tchap.estch_id
GROUP BY tchap.estch_tool
you have three selected columns, estch_tool which is in the GROUP BY clause, esmrk_value which is in an aggregate function, and estfa_tool which is neither in the GROUP BY clause nor in an aggregate function.
Your solution should be either:
GROUP BY tchap.estch_tool, tfacet.estfa_tool
AVG(tfacet.estfa_tool) or any aggregate function
There is a syntax error in this query -
SELECT
tchap.estch_tool,
tfacet.estfa_tool,
count(marks.esmrk_value) AmtMarks
FROM ES_MARK marks
left Join ES_TOOL_FACET tfacet ON
marks.esmark_tool_facet = tfacet.estfa_id --line added
left Join ES_TOOL_CHAPTER tchap ON
marks.esmark_tool_chapter = tchap.estch_id
GROUP BY tchap.estch_tool
GROUP BY mandates that any column appearing in SELECT list should either be aggregated or appear in GROUP BY clause.
So put an aggregate function - MIN, MAX, SUM, AVG etc on tfacet.estfa_tool because it does not appear in group by clause or include it there.
My solution comes in two variants. Which one better suits you may depend on which one will yield the better execution plan when tried on your data.
Description of variant #1: (In both cases I am describing only the logic behind the main SELECT's LEFT JOIN subselect, the part that is actually becomes substituted. But the scripts come as complete queries, equivalent to yours):
Pull and UNION ALL the items from both tools tables.
Join the list against the marks table accordingly.
Group the result set by tool items and get the counts.
The query:
SELECT *
FROM ES_TOOL
INNER JOIN ES_HARDWARE ON ES_HARDWARE.eshw_ID = ES_TOOL.ESTOOL_HARDWARE
INNER JOIN ES_PAYMENT on ES_payment.espay_id = es_TOOL.estool_payment
LEFT JOIN (
SELECT
tools.tool,
COUNT(*) AS AmtMarks
FROM (
SELECT 'tchap' AS tbl, estch_id AS id, estch_tool AS tool
FROM ES_TOOL_CHAPTER
UNION ALL
SELECT 'tfacet' AS tbl, estfa_id AS id, estfa_tool AS tool
FROM ES_TOOL_FACET
) tools
INNER JOIN ES_MARK marks
ON tools.tbl = 'tchap' AND tools.id = marks.esmark_tool_chapter
OR tools.tbl = 'tfacet' AND tools.id = marks.esmark_tool_facet
GROUP BY tools.tool
) h ON ES_TOOL.estool_id = h.tool
Variant #2:
Join ES_TOOL_CHAPTER against marks and get all the estch_tool values, including duplicates.
Similarly, join ES_TOOL_FACET against marks and get all the estfa_tool values, with duplicates too.
UNION ALL both sets.
Group the resulting set by tool items and get the counts.
And the query:
SELECT *
FROM ES_TOOL
INNER JOIN ES_HARDWARE ON ES_HARDWARE.eshw_ID = ES_TOOL.ESTOOL_HARDWARE
INNER JOIN ES_PAYMENT on ES_payment.espay_id = es_TOOL.estool_payment
LEFT JOIN (
SELECT
tools.tool,
COUNT(*) AS AmtMarks
FROM (
SELECT estch_tool AS tool
FROM ES_TOOL_CHAPTER tools
INNER JOIN ES_MARK marks ON tools.estch_id = marks.esmark_tool_chapter
UNION ALL
SELECT estfa_tool AS tool
FROM ES_TOOL_FACET tools
INNER JOIN ES_MARK marks ON tools.estfa_id = marks.esmark_tool_facet
) tools
GROUP BY tools.tool
) h ON ES_TOOL.estool_id = h.tool
I have this strange error in SQL Server 2005 where I take a working query, add the UNION keyword below it and then copy the query again. In my opinion, this should always be working, but it is not. I get the message 'Incorrect syntax near the keyword 'union'.
What could create this problem ?
To be more specific, here is the complete query :
select distinct deliveries.id, orders.id, 20 + sum(orders.mass1) as allowed_duration
from features_resources
inner join features on features.id = featureid
inner join orders on orders.id = features_resources.resourceid
inner join orderinformations on orders.id = orderinformations.orderid
inner join deliveries on orderinformations.deliveryid = deliveries.id
where features.name = 'O_FRAIS'
and (deliveries.ID IN
(SELECT ID
FROM dbo.DeliveriesInExportedSchedule))
group by deliveries.id, features.name ,orders.id order by deliveries.id
union
select distinct deliveries.id, orders.id, 20 + sum(orders.mass1) as allowed_duration
from features_resources
inner join features on features.id = featureid
inner join orders on orders.id = features_resources.resourceid
inner join orderinformations on orders.id = orderinformations.orderid
inner join deliveries on orderinformations.deliveryid = deliveries.id
where features.name = 'O_FRAIS'
and (deliveries.ID IN
(SELECT ID
FROM dbo.DeliveriesInExportedSchedule))
group by deliveries.id, features.name ,orders.id order by deliveries.id
I have tried to reproduce the error on a smaller query, by starting from a simple query and adding features one by one (inner join, nested queryes, group by, sum,....) but failed to reproduce the error again.
Any idea ?
It is actually the order by deliveries.id in the top half that causes the problem.
The order by needs to apply to the whole query.
Example Syntax
SELECT v1.number
FROM master.dbo.spt_values v1
WHERE v1.number > 2000
UNION
SELECT v2.number
FROM master.dbo.spt_values v2
WHERE v2.number < 10
ORDER BY v1.number
Try putting the individual SELECTs in parentheses:
(SELECT ... )
UNION
(SELECT ... )
The way you have it now, the second WHERE and GROUP BY clauses are ambiguous - should that apply to the SELECT, or to the UNION? I don't have any way to tell, and neither has your DB server.