I added Project_Contract.ContractorName but query got error.
Msg 156, Level 15, State 1, Line 18 Incorrect syntax near the keyword 'GROUP'.
select top (100) percent dbo.Project_Profile.Project_ID
,dbo.Project_Contract.ContractorName
,dbo.Schemes.Scheme_Name as [Name of Project]
,dbo.Program_Ctrl.Program_Title as [Contract Status]
,dbo.District_Ctrl.District_Name as District
,M.Module_Title as Module
,(
select MAX(Progress_Percentage) as Expr1
from dbo.Progress_Reporting as PR
where (M.ModuleID = Module_Id)
and (dbo.Milestone_Ctrl.Milestone_ID = Project_Milestone_ID)
and (Progress_Type = 'A')
) as Milestone_Percent_Share
,dbo.Milestone_Ctrl.Milestone_Title
from dbo.District_Ctrl
inner join dbo.Project_Profile
inner join dbo.Project_Profile
inner join dbo.Project_Contract
on dbo.Project_Profile.Project_ID = dbo.Project_Contract.ProjectId
inner join dbo.Schemes
on dbo.Project_Profile.Scheme_id = dbo.Schemes.Scheme_id
inner join dbo.Program_Ctrl
on dbo.Project_Profile.Program_ID = dbo.Program_Ctrl.Program_ID
inner join dbo.Project_Modules
on dbo.Project_Profile.Project_ID = dbo.Project_Modules.ProjectId
on dbo.District_Ctrl.District_ID = dbo.Schemes.District_ID inner join dbo.Modules_Ctrl as M
on M.ModuleID = dbo.Project_Modules.ModuleID
inner join dbo.Milestone_Ctrl
inner join dbo.Project_Milestones
on dbo.Milestone_Ctrl.Milestone_ID = dbo.Project_Milestones.Project_Milestone_ID
on M.ModuleID = dbo.Project_Milestones.ModuleId
group by dbo.Project_Profile.Project_ID
,dbo.Project_Contract.ContractorName
,dbo.Schemes.Scheme_Name
,dbo.Program_Ctrl.Program_Title
,M.Module_Title
,dbo.Project_Modules.ProjectModuleId
,dbo.Project_Milestones.Project_Milestone_ID
,dbo.District_Ctrl.District_Name
,dbo.Project_Milestones.Milestone_Percent_Share
,dbo.Project_Milestones.MileStoneId
,dbo.Milestone_Ctrl.Milestone_ID
,dbo.Milestone_Ctrl.Milestone_Title
,M.ModuleID
order by dbo.Project_Profile.Project_ID
You have two join with Project_Profile Table and have only one ON for this join you must add another ON for solve your problem or remove dbo.Project_Profile INNER JOIN part command.
Here:
dbo.Project_Profile INNER JOIN
dbo.Project_Profile INNER JOIN
Have duplicate Project_Profile without alias
Related
Here is my code for inserting values from AdventureWorks to custom database.
I needed to transfer identifiers from AdventureWorks too but copy of identifiers are not allowed. So I thought using distinct with identifier would be enough, but I still get error about inserting duplicates.
If you could comment and tell me what I am doing wrong I would be very grateful.
Thank you!
Here is my full attempt: https://pastebin.com/ZPniteh1
set identity_insert proizvodi ON
insert into proizvodi (ProizvodID, Sifra, Naziv, Kategorija, Podkategorija, Boja, Cijena, StanjeZaliha)
select distinct
p.ProductID, p.ProductNumber,p.[Name], pc.[Name],psc.[Name],
p.Color, p.ListPrice, pii.Quantity
from
AdventureWorks2017.Production.Product as p
join
AdventureWorks2017.Production.ProductSubcategory as psc on psc.ProductSubcategoryID = p.ProductSubcategoryID
join
AdventureWorks2017.Production.ProductCategory as pc on pc.ProductCategoryID = psc.ProductCategoryID
inner join
AdventureWorks2017.Production.ProductInventory as pii on pii.ProductID = p.ProductID
inner join
AdventureWorks2017.Sales.SalesOrderDetail as sod on sod.ProductID = p.ProductID
inner join
AdventureWorks2017.Sales.SalesOrderHeader as soh on soh.SalesOrderID = sod.SalesOrderID
inner join
AdventureWorks2017.Sales.SalesTerritory as st on st.TerritoryID = soh.TerritoryID
inner join
AdventureWorks2017.Production.[Location] as l on l.LocationID = pii.LocationID
where
st.[Group] ='Europe'
order by
p.ProductID
set identity_insert proizvodi OFF
I'm trying to create an input parameter procedure
but i'm having error in it
create procedure CaseSummary (#client_id int='')
as
begin
if exists(select Client_ID from [Client] where Client_ID= #client_id)
begin
SELECT CaseRegister.Client_ID,CaseRegister.Case_ID, CaseAssign.Lawyer_ID , Document.Doc_ID, CaseToCourt.Court_ID, CaseToJudge.J_ID,
CaseDetail.Result, Clauses.Clause_ID, SubClauses.SC_ID
FROM((((((((( CaseAssign
INNER JOIN Lawyer ON CaseAssign.Lawyer_ID = Lawyer.Lawyer_ID)
Inner Join CaseRegister ON CaseAssign.CR_ID = CaseRegister.CR_ID)
Inner Join Client ON Client.Client_ID = CaseRegister.Client_ID)
Inner Join Document On Document.Client_ID= Client.Client_ID)
Inner JOIn CaseToCourt On CaseToCourt.CR_ID= CaseRegister.CR_ID )
Inner Join CaseToJudge On CaseToJudge.CR_ID = CaseRegister.CR_ID )
Inner Join CaseDetail on CaseDetail.CJ_ID = CaseToJudge.CJ_ID )
Inner join Clauses on Clauses.CD_ID = CaseDetail.CD_ID)
Inner Join SubClauses on SubClauses.Clause_ID = Clauses.Clause_ID);
END
error:
Msg 102, Level 15, State 1, Procedure CaseSummary, Line 18
Incorrect syntax near 'end'.
Try this;
create procedure CaseSummary (#client_id int='')
as
if exists(select Client_ID from [Client] where Client_ID= #client_id)
begin
SELECT CaseRegister.Client_ID,CaseRegister.Case_ID, CaseAssign.Lawyer_ID , Document.Doc_ID, CaseToCourt.Court_ID, CaseToJudge.J_ID,
CaseDetail.Result, Clauses.Clause_ID, SubClauses.SC_ID
FROM CaseAssign
INNER JOIN Lawyer ON CaseAssign.Lawyer_ID = Lawyer.Lawyer_ID
Inner Join CaseRegister ON CaseAssign.CR_ID = CaseRegister.CR_ID
Inner Join Client ON Client.Client_ID = CaseRegister.Client_ID
Inner Join Document On Document.Client_ID= Client.Client_ID
Inner JOIn CaseToCourt On CaseToCourt.CR_ID= CaseRegister.CR_ID
Inner Join CaseToJudge On CaseToJudge.CR_ID = CaseRegister.CR_ID
Inner Join CaseDetail on CaseDetail.CJ_ID = CaseToJudge.CJ_ID
Inner join Clauses on Clauses.CD_ID = CaseDetail.CD_ID
Inner Join SubClauses on SubClauses.Clause_ID = Clauses.Clause_ID
END
The 'begin' at the start either needs an 'end' at the end OR just don't specify it
I have the following query:
SELECT
a.Name,
ISNULL(CAST(sum((b.qty * b.unit_rate)* b.Eng_RPQ )/100 AS DECIMAL(8,1)),0) AS [EngHours],
SUM(BR.BlendedRate)
FROM
Activity_Details b
INNER JOIN
Activity c on b.activity_id = c.id
INNER JOIN
Project p on p.id = c.project_id
RIGHT OUTER JOIN
Discipline a on c.discipline_id = a.id
INNER JOIN
(SELECT
a.Name, c.id,
CAST(f.POH * (d.HourlyRate * (1-(r.Discount/100))/100) AS DECIMAL(8,2)) AS BlendedRate
FROM
Activity_Details b
INNER JOIN
Activity c on b.activity_id = c.id
INNER JOIN
Team f on f.activity_id = c.id
INNER JOIN
SOF_Details d on d.id = f.sof_detail_id
INNER JOIN
Project p on p.id = c.project_id
INNER JOIN
Rate r on r.projectid = p.id
INNER JOIN
Teammate_Type tt on tt.id = f.team_type_id
RIGHT OUTER JOIN
Discipline a on c.discipline_id = a.id
GROUP BY
a.Name, c.id, f.POH, d.HourlyRate, r.Discount) AS BR ON BR.id = c.id
GROUP BY
a.Name
ORDER BY
a.Name
Which yields:
Name EngHours BlendedRate
Architechtural 80.8 38.48
Architechtural 80.8 55.33
Architechtural 80.8 55.40
I want to SUM this BlendedRate and ROUND it but if i try SUM(BR.BlendedRate) to the SELECT and remove the BR.BlendedRate in the GROUP BY
I get:
Name EngHours BlendedRate
Architechtural 242.3 895.26
I was expecting BlendedRate to equal 149.21
Any idea what i am doing wrong?
unable to comment due to reputation. It is a crude solution, but your code is returning duplicated (seemingly 6) records. The code should be fixed elsewhere, but without sample data it is difficult. In the mean time a crude solution would be to add a distinct clause to the sum function
SUM( DISTINCT BR.BlendedRate)
I am trying to compile a subquery in MSSQL 2012 to extract container volumes in import shipments using the following
SELECT TOP (100) PERCENT JobShipment_1.JS_UniqueConsignRef, JobContainer_1.JC_ContainerNum,
(SELECT dbo.JobPackLines.JL_ActualVolume
FROM dbo.JobPackLines INNER JOIN
dbo.JobShipment ON dbo.JobPackLines.JL_JS = dbo.JobShipment.JS_PK INNER JOIN
dbo.JobContainerPackPivot ON dbo.JobPackLines.JL_PK = dbo.JobContainerPackPivot.J6_JL INNER JOIN
dbo.JobContainer ON dbo.JobContainerPackPivot.J6_JC = dbo.JobContainer.JC_PK) AS Expr1
FROM dbo.JobConsol INNER JOIN
dbo.JobConShipLink ON dbo.JobConsol.JK_PK = dbo.JobConShipLink.JN_JK INNER JOIN
dbo.JobShipment AS JobShipment_1 ON dbo.JobConShipLink.JN_JS = JobShipment_1.JS_PK INNER JOIN
dbo.cvw_JobShipmentOrgs ON JobShipment_1.JS_PK = dbo.cvw_JobShipmentOrgs.JS_PK INNER JOIN
dbo.JobContainer AS JobContainer_1 ON dbo.JobConsol.JK_PK = JobContainer_1.JC_JK INNER JOIN
dbo.JobDeclaration ON JobShipment_1.JS_PK = dbo.JobDeclaration.JE_JS
But get the following error
"Error Message: Subquery returned more than 1 value. This is not permitted when the subquery follows =, !=, <, <=, >, >= or when the subquery is used as an expression."
I have looked ad nauseum on the web, but cannot find an answer, can anyone help?
Many thanks
Try this query
SELECT TOP (100) PERCENT JobShipment_1.JS_UniqueConsignRef, JobContainer_1.JC_ContainerNum,
(SELECT a.JL_ActualVolume
FROM dbo.JobPackLines a
INNER JOIN dbo.JobShipment b ONa.JL_JS = b.JS_PK
INNER JOIN dbo.JobContainerPackPivot c ON a.JL_PK = c.J6_JL
INNER JOIN dbo.JobContainer d ON c.J6_JC = d.JC_PK
where b.JS_PK=JobShipment_1.JS_PDK and d.JC_PK=JobContainer_1.JC_PK
) AS Expr1
FROM dbo.JobConsol
INNER JOIN dbo.JobConShipLink ON dbo.JobConsol.JK_PK = dbo.JobConShipLink.JN_JK
INNER JOIN dbo.JobShipment AS JobShipment_1 ON dbo.JobConShipLink.JN_JS = JobShipment_1.JS_PK
INNER JOIN dbo.cvw_JobShipmentOrgs ON JobShipment_1.JS_PK = dbo.cvw_JobShipmentOrgs.JS_PK
INNER JOIN dbo.JobContainer AS JobContainer_1 ON dbo.JobConsol.JK_PK = JobContainer_1.JC_JK
INNER JOIN dbo.JobDeclaration ON JobShipment_1.JS_PK = dbo.JobDeclaration.JE_JS
Please try below:
SELECT TOP (100) PERCENT JobShipment_1.JS_UniqueConsignRef, JobContainer_1.JC_ContainerNum,
(SELECT JPL.JL_ActualVolume
FROM dbo.JobPackLines AS JPL
INNER JOIN dbo.JobShipment AS JS ON JS.JS_PK = JPL.JL_JS
INNER JOIN dbo.JobContainerPackPivot AS JCP ON JCP.J6_JL = JPL.JL_PK
INNER JOIN dbo.JobContainer AS JC ON JC.JC_PK = JPL.J6_JC) AS Expr1
FROM dbo.JobConsol
INNER JOIN dbo.JobConShipLink ON dbo.JobConsol.JK_PK = dbo.JobConShipLink.JN_JK
INNER JOIN dbo.JobShipment AS JobShipment_1 ON dbo.JobConShipLink.JN_JS = JobShipment_1.JS_PK
INNER JOIN dbo.cvw_JobShipmentOrgs ON JobShipment_1.JS_PK = dbo.cvw_JobShipmentOrgs.JS_PK
INNER JOIN dbo.JobContainer AS JobContainer_1 ON dbo.JobConsol.JK_PK = JobContainer_1.JC_JK
INNER JOIN dbo.JobDeclaration ON JobShipment_1.JS_PK = dbo.JobDeclaration.JE_JS
The following is my CTE:
;WITH CTE AS
(SELECT O.*, E.Num, E.Amount
FROM OData O
INNER JOIN Equip E
ON O.Name = E.Name)
SELECT * FROM CTE -- gives results I want to join to
The following is the query that I want to SELECT from (and only use this SELECT statement for my query results:
SELECT
MU.Type
,MU.Num
,MU.MTBUR
,MF.MTBF
,MU.Hours
,MF.Hours
FROM
MUType_Stage MU
INNER JOIN
MFType_Stage MF
ON
MU.Type = MF.Type
AND
MU.Num = MF.Num
-- Need do JOIN to CTE right here
INNER JOIN
Status_STAGE S
ON
MU.Nu = S.Part
LEFT OUTER JOIN
RCN N
ON
N.Name = R.Part
LEFT OUTER JOIN
Repair RR
ON
R.ACSS_Name = RR.Name
So basically I need to JOIN to the CTE inside the SELECT query in which I want the results.
OR ALTERNATIVELY Uses this select statement to join to the CTE but only what the selected columns from the second select statement
Try this syntax
WITH CTE
AS (SELECT O.*,
E.Num,
E.Amount
FROM OData O
INNER JOIN Equip E
ON O.Name = E.Name)
SELECT MU.Type,
MU.Num,
MU.MTBUR,
MF.MTBF,
MU.Hours,
MF.Hours
FROM MUType_Stage MU
INNER JOIN MFByACType_Stage MF
ON MU.Type = MF.Type
AND MU.Num = MF.Num
INNER JOIN CTE C --- JOIN HERE as like other tables
ON C.Num = MF.Num
INNER JOIN Status_STAGE S
ON MU.Nu = S.Part
LEFT OUTER JOIN RCN N
ON N.Name = R.Part
LEFT OUTER JOIN Repair RR
ON R.ACSS_Name = RR.Name