GROUP BY statement does not work - sql

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.

Related

Having Asking for Group By clause and not allowing it

I am trying to include a selection for the MAX date range in my where clause. I am using Having to bring it in, but it errors out saying that "Column 'dbo.PURCHLINE.PURCHID' is invalid in the select list because it is not contained in either an aggregate function or the GROUP BY clause." But it won't let me group, throwing other errors. Maybe i'm approaching this wrong. Essentially, I need to get the latest date in in a set of columns for this query:
Select
PURCHLINE.PURCHID as 'PO'
,SUBSTRING(Cast(Max(VENDPACKINGSLIPJOUR.DELIVERYDATE) as varChar), 0, 12) as 'DeliveryDate'
,PURCHLINE.QTYORDERED as 'QtyOrdered'
FROM dbo.PURCHLINE
INNER JOIN dbo.INVENTTABLE
ON dbo.PURCHLINE.ITEMID = dbo.INVENTTABLE.ITEMID
INNER JOIN dbo.INVENTITEMGROUP
ON dbo.INVENTTABLE.ITEMGROUPID = dbo.INVENTITEMGROUP.ITEMGROUPID
INNER JOIN dbo.INVENTPOSTING
ON dbo.INVENTITEMGROUP.ITEMGROUPID = ITEMRELATION
INNER JOIN dbo.PURCHTABLE
ON dbo.PURCHLINE.PURCHID = dbo.PURCHTABLE.PURCHID
INNER JOIN dbo.LEDGERTABLE
ON LEDGERACCOUNTID = ACCOUNTNUM
INNER JOIN VENDPACKINGSLIPJOUR
on VENDPACKINGSLIPJOUR.PURCHID = PURCHLINE.PURCHID
WHERE
dbo.PURCHLINE.PURCHSTATUS = 3 --open Order
AND dbo.PURCHLINE.PURCHASETYPE = 3 --Purchase Order
AND dbo.PURCHLINE.DIMENSION IN (#department)
AND INVENTACCOUNTTYPE = 0 --Purchase Packing Slip
AND PURCHLINE.DIMENSION2_ = #division
HAVING Max(VENDPACKINGSLIPJOUR.DELIVERYDATE) between #start and #end
You have an aggregate, so you'll need to group on the nonaggregate fields:
GROUP BY
PURCHLINE.PURCHID
,PURCHLINE.QTYORDERED
This goes before the HAVING clause.

select list because it is not contained in either an aggregate function or the GROUP BY clause

My query throws this error on execution - how to solve this?
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.
Here is the query
SELECT
master_order.order_id, master_order.order_no,
master_order.program_no, master_order.package_type,
article_production.article_code, article_production.weight,
article_production.gsm, color.color_name, color.color_no,
size.size_name, transaction_order.quantity, transaction_production.avrg,
transaction_production.total_weight, transaction_order.piece_carton,
transaction_order.no_of_carton, transaction_order.unit_name,
transaction_order.packs, transaction_order.kdnr,
master_order.order_date, inlay.inlay_name, yarn.yarn_count,
buyer.buyer_code, master_production.shipment_date,
master_order.confirmation_date, master_order.comments,
master_production.carton_label, carton.carton_size, carton.carton_value,
master_order.special_instruction, master_order.ean_code1,
master_order.ean_code2, master_order.ean_code3, master_order.ean_code4,
article_production.article_name, transaction_order.piece_weight,
article_production.machine_size, article_production.guage,
master_production.image_path, transaction_production.m3,
transaction_order.tabpage_no, transaction_order.serial_sort,
master_order.total_tabpage,
When I add this error shown
sum(distinct transaction_production.m3) as tm3
Continuing with existing code...
from
transaction_order
LEFT JOIN
article_order on transaction_order.article_id = article_order.article_id
LEFT JOIN
size on transaction_order.size_id = size.size_id
LEFT JOIN
color on transaction_order.color_id = color.color_id
INNER JOIN
master_order ON transaction_order.order_id = master_order.order_id
LEFT JOIN
buyer ON master_order.buyer_id = buyer.buyer_id
INNER JOIN
master_production ON master_order.order_id = master_production.order_id
LEFT JOIN
transaction_production ON transaction_order.trans_id = transaction_production.trans_id
LEFT JOIN
article_production on transaction_production.article_id = article_production.article_id
LEFT JOIN
inlay on master_production.inlay_id = inlay.inlay_id
LEFT JOIN
carton ON transaction_production.carton_id = carton.carton_id
LEFT JOIN
yarn ON transaction_production.yarn_id = yarn.yarn_id
WHERE
master_order.program_no = '13-101117'
As soon as you used sum(...), an aggregate function, you need to use a GROUP BY
Furthermore, you have HTML in your SQL code.
...
sum(distinct transaction_production.m3) as tm3 <p>when i add this error shown</p>
...
Is causing your problems

SQL Server 2000 Group By asking for aggregate function when it is supplied

I must be missing something obvious but why does the following
SELECT c.ContractID, max( cs.ContractStatusCreated)
FROM dbo.NMPT_Contract AS c INNER JOIN
dbo.NMPT_ContractStatus AS cs ON c.ContractID = cs.ContractID INNER JOIN
dbo.CMSS_Status AS s ON cs.StatusID = s.StatusID
group by c.ContractID
having cs.ContractStatusCreated = MAX(cs.ContractStatusCreated)
return the following from SQL Server 2000?
Msg 8121, Level 16, State 1, Line 1 Column 'cs.ContractStatusCreated'
is invalid in the HAVING clause because it is not contained in either
an aggregate function or the GROUP BY clause.
Isn't MAX an aggregate function?
Because cs.ContractStatusCreated is not in the group-by clause, therefore it does not know where to get the data from.
And better yet, if you were to group by it you would end up with a HAVING 1=1 condition and your max(cs.ContractStatusCreated) wouldn't do what you want. I think you need a self-join or subquery to find your max value to compare to.
The error:
Msg 8121, Level 16, State 1, Line 1 Column 'cs.ContractStatusCreated' is invalid in the HAVING clause because it is not contained in either an aggregate function or the GROUP BY clause.
MAX is aggregate function
But ContractStatusCreated is NOT in the Group By
Haven't checked this but you could write your 'max' into a subquery something like this:
select ContractID, ContractStatusCreated
FROM dbo.NMPT_Contract AS x INNER JOIN
dbo.NMPT_ContractStatus AS y ON x.ContractID = y.ContractID
where (x.ContractID, y.ContractStatusCreated) = (
SELECT c.ContractID, max( cs.ContractStatusCreated)
FROM dbo.NMPT_Contract AS c INNER JOIN
dbo.NMPT_ContractStatus AS cs ON c.ContractID = cs.ContractID INNER JOIN
dbo.CMSS_Status AS s ON cs.StatusID = s.StatusID
group by c.ContractID
)

Subquery with multiple joins involved

Still trying to get used to writing queries and I've ran into a problem.
Select count(region)
where (regionTable.A=1) in
(
select jxn.id, count(jxn.id) as counts, regionTable.A
from jxn inner join
V on jxn.id = V.id inner join
regionTable on v.regionID = regionTable.regionID
group by jxn.id, regionTable.A
)
The inner query gives an ID number in one column, the amount of times they appear in the table, and then a bit attribute if they are in region A. The outer query works but the error I get is incorrect syntax near the keyword IN. Of the inner query, I would like a number of how many of them are in region A
You must specify table name in query before where
Select count(region)
from table
where (regionTable.A=1) in
And you must choose one of them.
where regionTable.A = 1
or
where regionTable.A in (..)
Your query has several syntax errors. Based on your comments, I think there is no need for a subquery and you want this:
select jxn.id, count(jxn.id) as counts, regionTable.A
from jxn inner join
V on jxn.id = V.id inner join
regionTable on v.regionID = regionTable.regionID
where regionTable.A = 1
group by jxn.id, regionTable.A
which can be further simplified to:
select jxn.id, count(jxn.id) as counts
, 1 as A --- you can even omit this line
from jxn inner join
V on jxn.id = V.id inner join
regionTable on v.regionID = regionTable.regionID
where regionTable.A = 1
group by jxn.id
You are getting the error because of this line:
where (regionTable.A=1)
You cannot specify a condition in a where in clause, it should only be column name
Something like this may be what you want:
SELECT COUNT(*)
FROM
(
select jxn.id, count(jxn.id) as counts, regionTable.A
from
jxn inner join
V on jxn.id = V.id inner join
regionTable on v.regionID = regionTable.regionID
group by jxn.id, regionTable.A
) sq
WHERE sq.a = 1

SQL Query is invalid in the select list

I dont know why this is coming up as invalid and I can not figure it out. I was given a legacy database as my supervisor left and I am in charge until someone comes to replace him. I am trying to run this query...
SELECT tblM.guidRId, SUM(dbo.tblCH.curTotalCost) AS curValue
FROM tblCH INNER JOIN
tblM ON tblCH.guidMId = tblM.guidMId INNER JOIN
ViewLM ON tblM.strNumber = ViewLM.strNumber
WHERE (tblM.guidRId = '4d832bc8-1827-4054-9896-6111844b0f26')
The error I keep getting is...Msg 8120, Level 16, State 1, Line 1
Column 'tblM.guidRId' is invalid in the select list because it is not contained in either an aggregate function or the GROUP BY clause.
Why is this error occuring?
You are forgetting to group guidRId. (you are aggregating the data)
SELECT
tblM.guidRId,
SUM(dbo.tblCH.curTotalCost) AS curValue
FROM
tblCH
INNER JOIN tblM ON tblCH.guidMId = tblM.guidMId
INNER JOIN ViewLM ON tblM.strNumber = ViewLM.strNumber
WHERE
tblM.guidRId = '4d832bc8-1827-4054-9896-6111844b0f26'
GROUP BY tblM.guidRId
Because you need a GROUP BY if you are going to use an aggregate functon like SUM() [or COUNT, AVG(), etc...] with another non-aggregate column:
SELECT tblM.guidRId, SUM(dbo.tblCH.curTotalCost) AS curValue
FROM tblCH
INNER JOIN tblM
ON tblCH.guidMId = tblM.guidMId
INNER JOIN ViewLM
ON tblM.strNumber = ViewLM.strNumber
WHERE tblM.guidRId = '4d832bc8-1827-4054-9896-6111844b0f26'
GROUP BY tblM.guidRId;
Try:
SELECT
tblM.guidRId, SUM(dbo.tblCH.curTotalCost) AS curValue
FROM
tblCH
INNER JOIN tblM
ON tblCH.guidMId = tblM.guidMId
INNER JOIN ViewLM
ON tblM.strNumber = ViewLM.strNumber
WHERE (tblM.guidRId = '4d832bc8-1827-4054-9896-6111844b0f26')
GROUP BY tblM.guidRId