SQL syntax error with joins - sql

Can someone point our what is wrong with below query? I have 3 tables.
1. Order Header - Order_header_id
2. Order Pricelist - order_header_id(fk),pricelist_id(fk)
3. Pricelist - pricelist_id,Name
I am trying to get the pricelist name based on the order header using below query and it throws "missing right parenthesis" error. Not sure if I am in the right direction.
Select pricelist.Name from ORDER_HEADER
left outer join
(select order_pricelist.pricelist_id from order_pricelist on order_header.order_header_id = order_pricelist.order_header_id
left outer join
(select pricelist.name from pricelist) pricelist on order_pricelist.pricelist_id = pricelist.pricelist_id)
Thanks

Select pricelist.Name from ORDER_HEADER
left outer join
(select pricelist_id, order_header_id from order_pricelist) op
ON order_header.order_header_id = op.order_header_id
left outer join
(select name,pricelist_id from pricelist) pr
ON op.pricelist_id = pr.pricelist_id
But actually, you don't need subqueries here, so it can be like this:
Select pricelist.Name from ORDER_HEADER
left outer join order_pricelist op on order_header.order_header_id = op.order_header_id
left outer join pricelist pr on op.pricelist_id = pr.pricelist_id

Related

POSTGRESQL - UNNEST function not work in LINUX [duplicate]

When i run this query in window system behave correctly UNNSET
but when i run this query Linux behave different.unnset duplicate record list on different row
SELECT DISTINCT
"billing_billmanagement"."creation_date",
"billing_billmanagement"."bill_number",
unnest(array_agg(DISTINCT "inventory_product"."product_name")) AS "product",
unnest(array_agg(DISTINCT "services_service"."name")) AS "service"
FROM "billing_billmanagement"
INNER JOIN "users_staffuser" ON ("billing_billmanagement"."staff_id" = "users_staffuser"."id")
INNER JOIN "auth_user" ON ("users_staffuser"."user_id" = "auth_user"."id")
LEFT OUTER JOIN "billing_customerproductbill" ON ("billing_billmanagement"."id" = "billing_customerproductbill"."bill_id")
LEFT OUTER JOIN "inventory_product" ON ("billing_customerproductbill"."product_id" = "inventory_product"."id")
LEFT OUTER JOIN "billing_customerservicebill" ON ("billing_billmanagement"."id" = "billing_customerservicebill"."bill_id")
LEFT OUTER JOIN "services_service" ON ("billing_customerservicebill"."service_id" = "services_service"."id")
WHERE "billing_billmanagement"."creation_date" BETWEEN '2017-12-04' AND '2017-12-06'
GROUP BY billing_billmanagement.creation_date,
billing_billmanagement.bill_number
ORDER BY "billing_billmanagement"."creation_date" ASC
If getting duplicate rows is the problem, try this
SELECT billing_billmanagement.creation_date,
billing_billmanagement.bill_number,
inventory_product.product_name AS product,
services_service.name AS service
FROM billing_billmanagement
INNER JOIN users_staffuser ON (billing_billmanagement.staff_id = users_staffuser.id)
INNER JOIN auth_user ON (users_staffuser.user_id = auth_user.id)
LEFT OUTER JOIN billing_customerproductbill ON (billing_billmanagement.id = billing_customerproductbill.bill_id)
LEFT OUTER JOIN inventory_product ON (billing_customerproductbill.product_id = inventory_product.id)
LEFT OUTER JOIN billing_customerservicebill ON (billing_billmanagement.id = billing_customerservicebill.bill_id)
LEFT OUTER JOIN services_service ON (billing_customerservicebill.service_id = services_service.id)
WHERE billing_billmanagement.creation_date BETWEEN '2017-12-04' AND '2017-12-06'
GROUP BY 1,
2,
3,
4
ORDER BY 1 ASC;

sum of particular column getting error [duplicate]

This question already has answers here:
Reason for Column is invalid in the select list because it is not contained in either an aggregate function or the GROUP BY clause [duplicate]
(4 answers)
Closed 4 years ago.
I have a query like this:
SELECT DISTINCT
dbo.T_Order_Header.F_Exhibitor,
dbo.T_Order_Header.F_Exhibition,
dbo.T_Exhibition.F_Exhibition_Name,
dbo.T_Exhibitor.F_Exhibitor_Name,
dbo.T_Order_Detail.F_ItemCode,
dbo.T_L2Category.F_L2Cat_Name,
SUM(dbo.T_Order_Detail.F_Qty-dbo.T_Order_Detail.F_CNQty) AS F_Qty,
dbo.T_L1Category.F_L1Cat_Name,
dbo.T_Order_Header.F_Stand,
dbo.T_Category.F_Cat_name,
dbo.T_ExStand.F_Bld_Code,
dbo.T_ExBuilding.F_Bld_name
FROM dbo.T_Order_Header
LEFT OUTER JOIN dbo.T_OrderAttachment
ON dbo.T_OrderAttachment.F_OrderNumber = dbo.T_Order_Header.F_OrderNumber
LEFT OUTER JOIN dbo.T_Order_Detail
ON dbo.T_Order_Detail.[Header_ID] = dbo.T_Order_Header.[ID]
LEFT OUTER JOIN dbo.T_L2Category
ON dbo.T_Order_Detail.F_ItemCode = dbo.T_L2Category.F_ItemCode
LEFT OUTER JOIN dbo.T_L1Category
ON dbo.T_L1Category.F_L1Cat_Code = dbo.T_L2Category.F_L1Cat_Code
LEFT OUTER JOIN dbo.T_Category
ON dbo.T_Category.F_Cat_Code = dbo.T_L2Category.F_Cat_Code
LEFT OUTER JOIN dbo.T_ExStand
ON dbo.T_ExStand.F_Stand_Code = dbo.T_Order_Header.F_Stand
LEFT OUTER JOIN dbo.T_ExBuilding
ON dbo.T_ExStand.F_Bld_Code = dbo.T_ExBuilding.F_Bld_Code
LEFT OUTER JOIN dbo.T_Exhibition
ON dbo.T_Order_Header.F_Exhibition = dbo.T_Exhibition.F_Exhibition_Code
LEFT OUTER JOIN dbo.T_Exhibitor
ON dbo.T_Order_Header.F_Exhibitor = dbo.T_Exhibitor.F_Exhibitor_Code
WHERE
F_Stand IN(
SELECT F_Stand_Code
FROM T_ExStand
WHERE
F_Site_Code ='DWTC'
AND F_Bld_Code = 'HALL1-4 & CONCOURSE'
)
AND T_Order_Header.F_Exhibition = '12004'
AND T_Order_Header.F_IsActive = 1
AND F_Exhibitor='2467'
I want to show sum of F_qty but I keep getting this error:
Column 'dbo.T_Order_Header.F_Exhibitor' is invalid in the select list
because it is not contained in either an aggregate function or the
GROUP BY clause
You need to group by with all the columns which you're selecting (other than the one which is in the aggregate function), something like below query:
SELECT
dbo.T_Order_Header.F_Exhibitor,
dbo.T_Order_Header.F_Exhibition,
dbo.T_Exhibition.F_Exhibition_Name,
dbo.T_Exhibitor.F_Exhibitor_Name,
dbo.T_Order_Detail.F_ItemCode,
dbo.T_L2Category.F_L2Cat_Name,
SUM(dbo.T_Order_Detail.F_Qty-dbo.T_Order_Detail.F_CNQty) AS F_Qty,
dbo.T_L1Category.F_L1Cat_Name,
dbo.T_Order_Header.F_Stand,
dbo.T_Category.F_Cat_name,
dbo.T_ExStand.F_Bld_Code,
dbo.T_ExBuilding.F_Bld_name
FROM dbo.T_Order_Header
LEFT OUTER JOIN dbo.T_OrderAttachment
ON dbo.T_OrderAttachment.F_OrderNumber = dbo.T_Order_Header.F_OrderNumber
LEFT OUTER JOIN dbo.T_Order_Detail
ON dbo.T_Order_Detail.[Header_ID] = dbo.T_Order_Header.[ID]
LEFT OUTER JOIN dbo.T_L2Category
ON dbo.T_Order_Detail.F_ItemCode = dbo.T_L2Category.F_ItemCode
LEFT OUTER JOIN dbo.T_L1Category
ON dbo.T_L1Category.F_L1Cat_Code = dbo.T_L2Category.F_L1Cat_Code
LEFT OUTER JOIN dbo.T_Category
ON dbo.T_Category.F_Cat_Code = dbo.T_L2Category.F_Cat_Code
LEFT OUTER JOIN dbo.T_ExStand
ON dbo.T_ExStand.F_Stand_Code = dbo.T_Order_Header.F_Stand
LEFT OUTER JOIN dbo.T_ExBuilding
ON dbo.T_ExStand.F_Bld_Code = dbo.T_ExBuilding.F_Bld_Code
LEFT OUTER JOIN dbo.T_Exhibition
ON dbo.T_Order_Header.F_Exhibition = dbo.T_Exhibition.F_Exhibition_Code
LEFT OUTER JOIN dbo.T_Exhibitor
ON dbo.T_Order_Header.F_Exhibitor = dbo.T_Exhibitor.F_Exhibitor_Code
WHERE
F_Stand IN(
SELECT F_Stand_Code
FROM T_ExStand
WHERE
F_Site_Code ='DWTC'
AND F_Bld_Code = 'HALL1-4 & CONCOURSE'
)
AND T_Order_Header.F_Exhibition = '12004'
AND T_Order_Header.F_IsActive = 1
AND F_Exhibitor='2467'
group by dbo.T_Order_Header.F_Exhibitor,
dbo.T_Order_Header.F_Exhibition,
dbo.T_Exhibition.F_Exhibition_Name,
dbo.T_Exhibitor.F_Exhibitor_Name,
dbo.T_Order_Detail.F_ItemCode,
dbo.T_L2Category.F_L2Cat_Name,
dbo.T_L1Category.F_L1Cat_Name,
dbo.T_Order_Header.F_Stand,
dbo.T_Category.F_Cat_name,
dbo.T_ExStand.F_Bld_Code,
dbo.T_ExBuilding.F_Bld_name
Use below query for your requirement.
SELECT DISTINCT
dbo.T_Order_Header.F_Exhibitor,
dbo.T_Order_Header.F_Exhibition,
dbo.T_Exhibition.F_Exhibition_Name,
dbo.T_Exhibitor.F_Exhibitor_Name,
dbo.T_Order.F_ItemCode,
dbo.T_L2Category.F_L2Cat_Name,
T_Order.F_Qty F_Qty,
dbo.T_L1Category.F_L1Cat_Name,
dbo.T_Order_Header.F_Stand,
dbo.T_Category.F_Cat_name,
dbo.T_ExStand.F_Bld_Code,
dbo.T_ExBuilding.F_Bld_name
FROM dbo.T_Order_Header
LEFT OUTER JOIN dbo.T_OrderAttachment
ON dbo.T_OrderAttachment.F_OrderNumber = dbo.T_Order_Header.F_OrderNumber
LEFT OUTER JOIN
(select SUM(dbo.T_Order_Detail.F_Qty)-sum(dbo.T_Order_Detail.F_CNQty) AS F_Qty,
[Header_ID],F_ItemCode from
dbo.T_Order_Detail
group by
[Header_ID]) T_Order ON dbo.T_Order.[Header_ID] = dbo.T_Order_Header.[ID]
LEFT OUTER JOIN dbo.T_L2Category
ON dbo.F_ItemCode.F_ItemCode = dbo.T_L2Category.F_ItemCode
LEFT OUTER JOIN dbo.T_L1Category
ON dbo.T_L1Category.F_L1Cat_Code = dbo.T_L2Category.F_L1Cat_Code
LEFT OUTER JOIN dbo.T_Category
ON dbo.T_Category.F_Cat_Code = dbo.T_L2Category.F_Cat_Code
LEFT OUTER JOIN dbo.T_ExStand
ON dbo.T_ExStand.F_Stand_Code = dbo.T_Order_Header.F_Stand
LEFT OUTER JOIN dbo.T_ExBuilding
ON dbo.T_ExStand.F_Bld_Code = dbo.T_ExBuilding.F_Bld_Code
LEFT OUTER JOIN dbo.T_Exhibition
ON dbo.T_Order_Header.F_Exhibition = dbo.T_Exhibition.F_Exhibition_Code
LEFT OUTER JOIN dbo.T_Exhibitor
ON dbo.T_Order_Header.F_Exhibitor = dbo.T_Exhibitor.F_Exhibitor_Code
WHERE
F_Stand IN(
SELECT F_Stand_Code
FROM T_ExStand
WHERE
F_Site_Code ='DWTC'
AND F_Bld_Code = 'HALL1-4 & CONCOURSE'
)
AND T_Order_Header.F_Exhibition = '12004'
AND T_Order_Header.F_IsActive = 1
AND F_Exhibitor='2467'
You are using SQL group function. In this case you have to add GROUP BY clause at the end of the sql-statement. Please add all generic columns to GROUP BY clause except F_Qty. And remove DISTINCT from your select statement.

SQL - Multiplying columns

I cannot get this to work
SELECT PM.PMNUM, (COUNT(ROUTE_STOP.LOCATION) * JOBPLAN.JPDURATION)
FROM PM
LEFT OUTER JOIN ROUTE_STOP ON ROUTE_STOP.ROUTE = PM.ROUTE
LEFT OUTER JOIN JOBPLAN ON JOBPLAN.JPNUM = PM.JPNUM
GROUP BY PM.PMNUM
As I see your minimum missing a group by.
SELECT PM.PMNUM, (COUNT(ROUTE_STOP.LOCATION) * JOBPLAN.JPDURATION)
FROM PM
LEFT OUTER JOIN ROUTE_STOP ON ROUTE_STOP.ROUTE = PM.ROUTE
LEFT OUTER JOIN JOBPLAN ON JOBPLAN.JPNUM = PM.JPNUM
GROUP BY PM.PMNUM, JOBPLAN.JPDURATION
If this won't help you need to give further feedback what is not working?

SQL: Cross join query

SELECT * FROM training.dbo.[PERSON] P
LEFT JOIN training.dbo.PERSON_CAREER_HISTORY PC ON (P.PERSON_ID=PC.PERSON_ID)
CROSS JOIN (SELECT DISTINCT PC2.POSITION training.dbo.PERSON_CAREER_HISTORY) PC2
WHERE PC.POSITION IS NULL
the cross join part is not working giving the error
"Incorrect syntax near '.'."
I can't fix it, and been fixing it for about an hour. Please tell me my error
You missed the FROM in the CROSS JOIN.
SELECT * FROM training.dbo.[PERSON] P
LEFT JOIN training.dbo.PERSON_CAREER_HISTORY PC ON (P.PERSON_ID=PC.PERSON_ID)
CROSS JOIN (SELECT DISTINCT PC2.POSITION FROM training.dbo.PERSON_CAREER_HISTORY) PC2
WHERE PC.POSITION IS NULL
You have missed out the FROM keyword from the sub query. Try this:
SELECT
*
FROM
training.dbo.[PERSON] P
LEFT JOIN training.dbo.PERSON_CAREER_HISTORY PC
ON (P.PERSON_ID=PC.PERSON_ID)
CROSS JOIN (
SELECT DISTINCT
PC2.POSITION
FROM
training.dbo.PERSON_CAREER_HISTORY) PC2
WHERE
PC.POSITION IS NULL
If you are looking for the positions that people do not have, then this is the query that you want:
SELECT *
FROM training.dbo.[PERSON] P CROSS JOIN
(SELECT DISTINCT PC2.POSITION FROM training.dbo.PERSON_CAREER_HISTORY
) pp LEFT JOIN
training.dbo.PERSON_CAREER_HISTORY PCH
ON P.PERSON_ID = PC.PERSON_ID AND pp.POSITION = PC.POSITION
WHERE PC.POSITION IS NULL;
The joins are not correct in your version (as well as the problem with the subquery). Otherwise, I cannot figure out the purpose of your original query.

Sql Query Issue 'BatchRelease.BatchReleaseNo' is invalid in the select list

In the below query when i execute it throws error "Column 'BatchRelease.BatchReleaseNo' is invalid in the select list because it is not contained in either an aggregate function or the GROUP BY clause." Please me to solve the issue.This error occurs when i sum the values.
SELECT DISTINCT BR.BatchReleaseNo,BR.CreatedOn,P.ProductName,
BRD.BatchReleaseQuantity,P.ProductCode,BOED.NoOfEmployee,
BOED.TimeInHours,EM.EmployeeType,BRPD.ProcessLoss,
MP.UnitProcessTime,MM.RequiredCostPerHour,
MM.MachineryType,
(BRD.BatchReleaseQuantity*BRD.UnitPrice) AS BatchCompletionValue,
ISNULL(
SUM((BOED.NoOfEmployee
*(BOED.TimeInHours/60 )/60)
*(EM.CostPerDay/EM.WorkingHours))
,0.0
)
FROM BatchReleaseDetails BRD
LEFT OUTER JOIN BatchRelease BR ON BR.BatchReleaseID=BRD.BatchReleaseID
LEFT OUTER JOIN Product P ON P.ProductID=BRD.ProductID
LEFT OUTER JOIN BatchOrderEmployeeDetails BOED ON BOED.BatchReleaseID=BR.BatchReleaseID
LEFT OUTER JOIN EmployeeMaster EM ON EM.EmployeeTypeID=BOED.EmployeeTypeID
LEFT OUTER JOIN BatchReleasedProcessDetails BRPD ON BRPD.BatchReleaseID=BR.BatchReleaseID
--LEFT OUTER JOIN Process PR ON PR.ProcessID=BRPD.ProcessID
LEFT OUTER JOIN MachineProcess MP ON MP.ProcessID=BRPD.ProcessID
LEFT OUTER JOIN MachineMaster MM ON MM.MachineID=MP.MachineID
GRoup BY BR.BatchReleaseNo