An aggregate may not appear in the WHERE clause (MSSM studio) - sql

I am trying to create a trigger for insert on Advertisement table. When trying to write this cursor
declare Users cursor for
Select "User".IDUser, Sum(Price)
from "User"
inner join Purchase as pu on "User".IDUser = pu.IDUser
inner join PurchaseProduct as pp on pu.IDPurchase = pp.IDPurchase
inner join Product as pr on pp.IDProduct = pr.IDProduct
inner join inserted on pr.IDProduct = inserted.IDProduct
where pr.ProductType = (select ProductType
from Product
inner join Advertisement on Product.IDProduct = Advertisement.IDProduct
inner join inserted on Advertisement.IDProduct = inserted.IDProduct
where Advertisement.IDAdvertisement = inserted.IDAdvertisement)
and Sum(Price) > 50;
I get this error
Msg 147, Level 15, State 1, Procedure AutomaticUserAdvertisementScoreCalculating, Line 15 [Batch Start Line 113]
An aggregate may not appear in the WHERE clause unless it is in a subquery contained in a HAVING clause or a select list, and the column being aggregated is an outer reference.
What might be the error here? Below you can see my DB structure
Thanks in advance

You could use HAVING to filter rows after aggregation:
declare Users cursor for
Select "User".IDUser, Sum(Price) from "User"
join Purchase as pu on "User".IDUser = pu.IDUser
join PurchaseProduct as pp on pu.IDPurchase = pp.IDPurchase
join Product as pr on pp.IDProduct = pr.IDProduct
join inserted on pr.IDProduct = inserted.IDProduct
where pr.ProductType = (select ProductType from Product
join Advertisement on Product.IDProduct = Advertisement.IDProduct
join inserted on Advertisement.IDProduct = inserted.IDProduct
where Advertisement.IDAdvertisement = inserted.IDAdvertisement)
GROUP BY "User".IDUser
HAVING Sum(Price) > 50;

Related

First record in SQL statement

I need to link and additional table to filter by the plant in doing so create multiple records for each entry. I am looking to just get the first entry for one.
SELECT
OrderHead.CreditOverride,
OrderHead.OpenOrder, Customer.CreditHold,
OrderHead.OrderNum, Customer.Name,
OrderHead.EntryPerson, OrderHead.OrderDate,
Customer.TermsCode, OrderHead.ShipToCustNum, OrderRel.Plant
FROM
Customer
INNER JOIN
OrderHead ON Customer.Company = OrderHead.Company
AND Customer.CustNum = OrderHead.BTCustNum
INNER JOIN
OrderRel ON OrderHead.OrderNum = OrderRel.OrderNum
WHERE
(OrderHead.CreditOverride = 0)
AND (OrderHead.OpenOrder = 1)
AND (Customer.CreditHold = 1)
AND (OrderRel.Plant = 'mfgsys')
Trying to grab the first unique record from orderhead.
Use window functions. You can do this entirely in the FROM clause:
FROM Customer c INNER JOIN
(SELECT oh.*,
ROW_NUMBER() OVER (PARTITION BY Company, BTCustNum ORDER BY OrderDate ASC) as seqnum
FROM OrderHead oh
) oh
ON c.Company = oh.Company AND
c.CustNum = oh.BTCustNum AND
oh.seqnum = 1 INNER JOIN
OrderRel orr
ON oh.OrderNum = orr.OrderNum
Note that I replaced the table names with simpler table aliases, which you should repeat in the rest of the query.

Oracle (Netsuite) SQL one join limit results

I have an oracle SQL query and a slight problem. I need to check if an item has a PO# that it has at least 1 line item. The query below works however it returns a result for each line of transaction_lines and I need only une result. PS I tried DISTINCT but get an ODBC error.
SELECT ITEMS.NAME, INVENTORY_NUMBER.INVENTORY_NUMBER, INVENTORY_NUMBER.ON_HAND_COUNT, ITEMS.SALESDESCRIPTION, CONDITION.LIST_ITEM_NAME,
BRAND_PARTNER.LIST_ITEM_NAME, PPROGRAM.LIST_ITEM_NAME, ENTITY.NAME, PO.TRANSACTION_NUMBER, INVENTORY_NUMBER.RECEIVED_COST, ITEMS.SALESPRICE, IR.TRANSACTION_NUMBER,
INVENTORY_SOURCE.LIST_ITEM_NAME, LOCATIONS.NAME, INVENTORY_NUMBER.RECEIPT_DATE, PO.INTERNAL_MEMO, INVENTORY_NUMBER.REFERENCE_, TEST_RESULTS.LIST_ITEM_NAME,
INVENTORY_NUMBER.TEST_FILE_LINK, INVENTORY_NUMBER.CONNECT_TRADE_ID, INVENTORY_NUMBER.SOLD_DATE, INVENTORY_NUMBER.SOLD_PRICE, INVENTORY_NUMBER.MEMO, ITEMS.UPC_CODE, ITEMS.MPN,
ITEMS.ITEM_ID, INVENTORY_NUMBER.CLEI, INVENTORY_NUMBER.CERTIFICATION_REF_ID
FROM INVENTORY_NUMBER
INNER JOIN ITEMS ON INVENTORY_NUMBER.ITEM_ID = ITEMS.ITEM_ID
INNER JOIN TRANSACTIONS AS PO ON INVENTORY_NUMBER.PURCHASE_ORDER_ID = PO.TRANSACTION_ID
INNER JOIN TRANSACTIONS AS IR ON INVENTORY_NUMBER.ITEM_RECEIPT_ID = IR.TRANSACTION_ID
INNER JOIN TRANSACTION_LINES ON PO.TRANSACTION_ID = TRANSACTION_LINES.TRANSACTION_ID
INNER JOIN ENTITY ON TRANSACTIONS.ENTITY_ID = ENTITY.ENTITY_ID
INNER JOIN CONDITION ON INVENTORY_NUMBER.CONDITION_ID = CONDITION.LIST_ID
INNER JOIN BRAND_PARTNER ON INVENTORY_NUMBER.BRAND_PARTNER_ID = BRAND_PARTNER.LIST_ID
INNER JOIN PPROGRAM ON INVENTORY_NUMBER.PROGRAM_ID = PPROGRAM.LIST_ID
INNER JOIN INVENTORY_SOURCE ON INVENTORY_NUMBER.INVENTORY_SOURCE_ID = INVENTORY_SOURCE.LIST_ID
INNER JOIN LOCATIONS ON INVENTORY_NUMBER.LOCATION_ID = LOCATIONS.LOCATION_ID
INNER JOIN TEST_RESULTS ON INVENTORY_NUMBER.TEST_RESULTS_ID = TEST_RESULTS.LIST_ID
WHERE INVENTORY_NUMBER.ON_HAND_COUNT IS NOT NULL AND ((INVENTORY_NUMBER.PURCHASE_ORDER_ID IS NULL) OR (INVENTORY_NUMBER.PURCHASE_ORDER_ID IS NOT NULL AND TRANSACTION_LINES.TRANSACTION_LINE_ID IS NOT NULL))
you could also remove the join to the transaction_lines and instead of tl.TRANSACTION_LINE_ID IS NOT NULL use an exists clause
and exists (select 1 from transaction lines tl
where tl.transaction_id = po.transaction_id)
I would suggest using a GROUP BY to help limit your results. You could also if you are interacting with transactions in your query you must always remember that without limiting results based on the "main line" you will receive the header record and then a record for each individual line item.
If you were doing this with a saved search you could put the criteria as "main line = true". Since I don't understand your query entirely I can't advise where to put this limitation in.

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

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.

SQL Query to retrieve single record per filter

I have the following query:
SELECT min(salesorder.SOM_SalesOrderID) AS salesorder,
Item.IMA_ItemID,
Item.IMA_ItemName,
Customer.CUS_CorpName,
WK.WKO_WorkOrderID,
min(WK.WKO_OrigRequiredDate),
WK.WKO_WorkOrderTypeCode,
min(WK.WKO_RequiredDate),
max(WK.WKO_LastWorkDate),
min(wk.WKO_RequiredQty),
wk.WKO_MatlIssueDate,
min(SalesOrderDelivery.SOD_RequiredQty),
Item.IMA_ItemTypeCode,
Item.IMA_OnHandQty,
min(SalesOrderDelivery.SOD_PromiseDate),
min(WO.woo_operationseqID) AS seqid
FROM SalesOrder
INNER JOIN SalesOrderLine ON SalesOrder.SOM_RecordID = SalesOrderLine.SOI_SOM_RecordID
INNER JOIN SalesOrderDelivery ON SalesOrderLine.SOI_RecordID = SalesOrderDelivery.SOD_SOI_RecordID,
WO.
INNER JOIN Item ON SalesOrderLine.SOI_IMA_RecordID = Item.IMA_RecordID
INNER JOIN WKO wk ON Item.IMA_ItemID = WK.WKO_ItemID
INNER JOIN Customer ON SalesOrder.SOM_CUS_RecordID = Customer.CUS_RecordID
INNER JOIN woo WO ON WO.WOO_WorkOrderID = WK.WKO_WorkOrderID
WHERE wk.WKO_StatusCode = 'Released'
AND WO.WOO_StatusCode IS NULL
AND SalesOrderDelivery.SOD_ShipComplete = 'false'
GROUP BY WK.WKO_WorkOrderID,
Item.IMA_ItemID,
Item.IMA_ItemName,
Customer.CUS_CorpName,
WK.WKO_WorkOrderTypeCode,
wk.WKO_MatlIssueDate,
Item.IMA_ItemTypeCode,
Item.IMA_OnHandQty
I need 1 record returned for each wk.wko_workorderid. There is a field that is not included that I'm not sure how to get. I need to retrieve the woo.woo_workcenterid that corresponds to min(WO.woo_operationseqID)as seqid. I cannot include it in the general query since there are multiple workcenterids in the table and I only want the specific one that is part of the min operation sequence record.
Any help would be appreciated.