sql oracle - group by with join - sql

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.

Related

Error 979: not a GROUP BY expression

Getting an error when trying to return the LAST contact date
SELECT CDC.pat_id, MAX(SOC.contact_date),
FROM hb61.cdcsaar_patients CDC
JOIN clarity.social_hx SOC ON CDC.pat_id=SOC.pat_id
LEFT OUTER JOIN clarity.smoking_cess_hx CESS ON CDC.pat_id=CESS.pat_id
LEFT OUTER JOIN clarity.social_hx_alc_use ALC on CDC.pat_id=ALC.pat_id
GROUP BY SOC.contact_date
;
You need to put the selected columns (in this case, CDC.pat_id) on the GROUP BY clause, instead of col you're applying the aggregation function.
SELECT
CDC.pat_id,
MAX(SOC.contact_date)
FROM
hb61.cdcsaar_patients CDC
JOIN clarity.social_hx SOC ON CDC.pat_id = SOC.pat_id
LEFT OUTER JOIN clarity.smoking_cess_hx CESS ON CDC.pat_id = CESS.pat_id
LEFT OUTER JOIN clarity.social_hx_alc_use ALC on CDC.pat_id = ALC.pat_id
GROUP BY
CDC.pat_id;
You need to put any columns that you're not applying an aggregate function to in the GROUP BY clause (in this case CDC.pat_id, rather than SOC.contact_date).

Group by query error in multiple joins

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

PostgreSQL - error in query

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

Query to return SINGLE DISTINCT row

I have the query below working, the thing is I need to only list each unique "VolumeSerialNumber0" once. There's no shortage of questions and approaches to this problem on SO but they suggest using subqueries and group by clause, but when I try to do that I get an error "columnname is invalid in the select list because it is not contained in either an aggregate function or the GROUP BY clause.
I feel like it has to be close I'm just not getting the magical syntax perfectly correct.
SELECT
dbo.v_R_System.Netbios_Name0,
dbo.v_GS_LOGICAL_DISK.TimeStamp,
dbo.v_GS_LOGICAL_DISK.Description0,
dbo.v_GS_LOGICAL_DISK.DeviceID0,
dbo.v_GS_LOGICAL_DISK.DriveType0,
dbo.v_GS_LOGICAL_DISK.Name0,
dbo.v_GS_LOGICAL_DISK.SystemName0,
dbo.v_GS_LOGICAL_DISK.VolumeName0,
dbo.v_GS_LOGICAL_DISK.VolumeSerialNumber0,
dbo.v_GS_PARTITION.Size0,
dbo.v_GS_LOGICAL_DISK.FileSystem0
FROM
dbo.v_R_System
INNER JOIN dbo.v_GS_LOGICAL_DISK
ON dbo.v_R_System.ResourceID = dbo.v_GS_LOGICAL_DISK.ResourceID
INNER JOIN dbo.v_GS_PARTITION
ON dbo.v_GS_LOGICAL_DISK.ResourceID = dbo.v_GS_PARTITION.ResourceID
SELECT
MAX(S.Netbios_Name0),
MAX(L.TimeStamp),
MAX(L.Description0),
MAX(L.DeviceID0),
MAX(L.DriveType0),
MAX(L.Name0),
MAX(L.SystemName0),
MAX(L.VolumeName0),
L.VolumeSerialNumber0,
MAX(P.Size0),
MAX(L.FileSystem0)
FROM
dbo.v_R_System S
INNER JOIN dbo.v_GS_LOGICAL_DISK L
ON S.ResourceID = L.ResourceID
INNER JOIN dbo.v_GS_PARTITION P
ON L.ResourceID = P.ResourceID
GROUP BY
L.VolumeSerialNumber0

GROUP BY statement does not work

Here is my SQL Server query. When I execute this query, Group by was giving me a error
Msg 8120, Level 16, State 1, Line 1
Column 'master_order.order_id' is invalid in the select list because it is not contained in either an aggregate function or the GROUP BY clause.
My tables in relation but he did not find why if cannot use group by its work
SELECT
master_order.order_id, master_order.order_no, article_order.article_name,
size.size_name, transaction_order.quantity, transaction_order.unit_name,
transaction_order.rate, transaction_order.amount, transaction_order.discount,
transaction_order.net_amount, master_order.order_date, buyer.buyer_name,
master_order.shipment_date, bank.bank_name, bank.bank_branch, bank.account_title,
master_order.confirmation_date, payment.payment_terms, agent.agent_name,
agent.company_name, currency.currency_symbol, master_order.half_day,
master_order.half_date, master_order.shipped_board_date, master_order.port_category,
port_info_setup.port_name, port_info_setup.country_name, master_order.revised,
master_order.confirmed, master_order.ex_factory, master_order.comments,
master_order.dis_type
FROM
master_order
INNER JOIN
transaction_order ON master_order.order_id = transaction_order.order_id
INNER JOIN
size ON transaction_order.size_id = size.size_id
INNER JOIN
article_order ON transaction_order.article_id = article_order.article_id
INNER JOIN
buyer ON master_order.buyer_id = buyer.buyer_id
INNER JOIN
port_info_setup ON master_order.port_id = port_info_setup.port_id
INNER JOIN
payment ON master_order.payment_id = payment.payment_id
INNER JOIN
currency ON master_order.currency_id = currency.currency_id
INNER JOIN
bank ON master_order.bank_id = bank.bank_id AND buyer.buyer_id = bank.buyer_id
INNER JOIN
agent ON master_order.agent_id = agent.agent_id
GROUP BY
size.size_name
In the grouped query ether your columns in the select list should be inside an aggregate function or the column without aggregate should mention in the group by statement.
If you want a column with no aggregate and dont want grouping by that column, so use CTE for grouping and use join to have that column in your query results.