I believe I'm writing this CTE wrong, I'm a major novice with this so explain as much as you can. Still getting the can not be bound error on almost everything
Error messages:
The multi part identifier could not be bound "PS_Margin.Emp or Vendor ID
The multi part identifier could not be bound "PS_Margin.Project Profit by Person %
Issues/ Goals:
Possible need to add aliases to column names with spaces and special characters although I don't know how to do this
WITH
Profit_Score_CTE ( [Emp or Vendor ID], [Project Profit by Person %] ) AS (
SELECT PS_Margin.[Emp or Vendor ID],
CASE
WHEN ps.[Project Profit by Person %] > .4 THEN 1
WHEN ps.[Project Profit by Person %] > .2 THEN 3
WHEN ps.[Project Profit by Person %] > .1 THEN 5
WHEN ps.[Project Profit by Person %] > .05 THEN 8
ELSE 13 END
AS [Profit Score]
FROM dbo.PS_Margin AS ps
)
SELECT emp.[Employee Name], emp.[USID], [Profit Score]*0.3 AS [Final Score]
FROM dbo.PS_Emp AS emp
left join Profit_Score_CTE
ON emp.USID = Profit_Score_CTE.[Emp or Vendor ID]
"Cannot be bound" usually refers to how you are ID'ing columns with a table alias. Try giving your table an alias and using that for the columns:
WITH
--Section 1: Profit Score
Profit_Score_CTE( [Emp or Vendor ID], [Project Profit by Person %] ) AS (
SELECT PS_Margin.[Emp or Vendor ID],
CASE
WHEN ps.[Project Profit by Person %] > .4 THEN 1
WHEN ps.[Project Profit by Person %] > .2 THEN 3
WHEN ps.[Project Profit by Person %] > .1 THEN 5
WHEN ps.[Project Profit by Person %] > .05 THEN 8
ELSE 13 END
AS [Profit Score]
FROM [dbo].[PS_Margin] ps
)
I'm guessing because you haven't provided enough information to reproduce the error. But I think the problem might be that you define the columns of your CTE here:
Profit_Score_CTE( [Emp or Vendor ID], [Project Profit by Person %] ) AS (
And yet you alias your second column with a different name here:
AS [Profit Score]
You need to decide which column name you want your CTE to expose and use it consistently.
I'll assume your CTE is correctly defined. Your cte is named "Profit_Score_CTE". Your actual select statement refers to objects dbo.PS_Emp and dbo.PS. I'll guess that you should replace dbo.PS with your CTE name. E.g.,
with Profit_Score_CTE as (select ...)
select emp.[Employee Name], ...
from dbo.PS_Emp as emp
left join Profit_Score_CTE
on emp.USID = Profit_Score_CTE.[Emp or Vendor ID]
order by ...
;
The alias PS is visible only inside the definition of the CTE. It cannot be used in the actual select statement that uses your CTE. Notice that I gave your table an alias and used it - a consistent coding style is something you should strive to achieve. Also notice the order by clause. Rows within a resultset have no defined order without one - and usually it is important. And PLEASE use white space to make your code readable and to avoid easy mistakes. You crammed everything together in the formula for computed column [Final Score].
Thanks all for your help!
My newbie ribbon was really showing with this one. The problem had nothing to do with SQL at all. I wasn't pointing to the correct database in sql server.
In the available databases drop down box on the top left hand screen of SQL Server you need to select the database you're working from. This will enable the script to find your database, and begin to solve alot of the errors.
Related
I currently have a very simple SQL script, which provides the sell price for a special customer of ours, who gets Priceline "J". I'd like to add a column of Retail pricing as well for them to reference, which is normally Priceline "R". My query currently looks like this:
SELECT RS_PCL.SKU,
RS_PCL.SKU_DESC,
RS_PCL.PACKAGE,
RS_PCL.PRICE AS [Sell Price],
RS_PCL.STD AS [Start Date],
RS_PCL.END AS [End Date]
FROM RS_PCL
WHERE RS_PCL.PRICELINE = "J"
Would anyone be able to help me figure out how I could add the "R" price line as another column instead of separate rows?
I can add WHERE RS_PCL.PRICELINE IN ("J","R") but it would create a separate row for each Retail price, instead of in the column next to it. I've seen examples of a separate SELECT CASE WHEN, but not sure exactly how the syntax works.
The prices are always going to have the same start date, so it would just need to join where the SKU matches and the Start Date matches.
NOTE : IM sorry I accidentally edited your answer, not my question...
Revised: Running into errors with this code still, saying "Your query does not include the specified expression 'GL Type' as part of an aggregate function" or would say each field that isn't included in the group by clause
SELECT RS_PCL.[GL Type],
RS_PCL.SKU,
RS_PCL.[SKU Desc],
RS_PCL.Supplier,
RS_PCL.[Case UPC],
RS_PCL.[Pack UPC],
RS_PCL.[Unit UPC],
RS_PCL.PDCN,
RS_PCL.Package,
RS_PCL.[Price Start Date],
RS_PCL.[Price End Date],
Iif( RS_PCL.PRICELINE = "J" , RS_PCL.Price , Null) AS [Sell Price],
Iif( RS_PCL.PRICELINE = "R" , RS_PCL.Price , Null) AS [Retail Price],
RS_PCL.Cost,
RS_PCL.Tax,
RS_PCL.Freight
FROM RS_PCL
WHERE (RS_PCL.PRICELINE IN ("J","R"))
Tested the Self Join query listed below, I keep getting a "Syntax Error in FROM Clause" with this query.
SELECT J.*, R.R_PRICE FROM RS_PCL AS J
INNER JOIN SELECT (RS_PCL.SKU, RS_PCL.[Price Start Date], RS_PCL.Price AS R_PRICE FROM RS_PCL WHERE RS_PCL.PRICELINE = "R") AS R
ON J.SKU = R.SKU AND J.[Price Start Date] = R.[Price Start Date]
WHERE RS_PCL.Priceline = "J"
SELECT CASE does not work in Access query. Use IIf() or Switch() or Choose().
Perhaps a CROSSTAB query or emulate CROSSTAB with expressions to create fields. Presuming, as indicated in question title, there is one "J" and one "R" record for each data combination:
SELECT SKU, SKU_DESC, PACKAGE, STD, END,
Max(IIf(PRICELINE = "J", PRICE, Null)) AS J,
Max(IIf(PRICELINE = "R", PRICE, Null)) AS R
FROM RS_PCL
GROUP BY SKU, SKU_DESC, PACKAGE, STD, END
Or a self-join, assuming only fields needed as unique identifier are SKU and STD:
SELECT J.*, R.R_PRICE FROM RS_PCL AS J
INNER JOIN (SELECT SKU, STD, PRICE AS R_PRICE FROM RS_PCL WHERE PRICELINE = "R") AS R
ON J.SKU = R.SKU AND J.STD = R.STD
WHERE PRICELINE = "J";
This would be useful on a report but useless for a data entry form.
Yet another approach would use DLookup() domain aggregate function. Domain aggregate function can cause slow performance in query or form but the dataset would be editable.
I am struggling with the below SQL script relating to the bolded "10 desc" at the end of the script
This field is not in the original tables. It was created separately.
I don't exactly know what it is for but if I remove it it will give me different result. Hope I could have some guidance on this.
SELECT ce.[Agreement ID], m.AGREEMENT_NAME, ce.Max_Deduct_Bond, d.[Product Type], d.Margin, d.Ccy, Left(Right(d.[Margin],12),2) AS Cty, d.Posted_Received_Flag, d.[Market Value], (d.[Market Value]*r.[Rate]) AS [Market Value HKD]
FROM [Step 0103 - CE: CE Source] AS ce,
[201_0002 - Get_Agreement_Treats_Mapping] AS m,
[201_0000 - Get_nCOMS_201_Details] AS d,
SysReportDate AS sd,
SysTOR AS r
WHERE (((ce.[Agreement ID])=[m].[AGREEMENT_ID] And (ce.[Agreement ID])="11") AND ((m.AGREEMENT_NAME)=[d].[Agreement]) AND ((ce.Max_Deduct_Bond)<>0) AND ((d.[Product Type])='Bond') AND ((d.[Reporting Date])=[sd].[Reporting Date]) AND ((sd.Date_Code)='01_CM') AND ((r.[Ccy Code])=[d].[Ccy]))
ORDER BY ce.[Agreement ID], 10 DESC;
10 in order by mean its ordering by 10th column listed in the select statement, which in your query is [Market Value HKD].
which is not recommended to do so , and your order by statement changes depend on the order of columns returned in the select .
always use column names:
... ORDER BY ce.[Agreement ID], [Market Value HKD] DESC;
I am trying to aggregate the Total Sum of [Award] and Count of [Counter] for different [PO Types]. Is there a way to group all the existing PO Types into one value "SRM" within the same query?
The end result I want is one record: SRM | $37,823,372.89 | 1732
Here is the current SQL:
SELECT tblSRMECC.[PO Type], Sum(tblSRMECC.Award) AS SumOfAward, Count(tblSRMECC.[Counter (PO)]) AS
[CountOfCounter (PO)]
FROM tblSRMECC
GROUP BY [PO Type]
HAVING (((tblSRMECC.[PO Type])<>"AutoPO" And (tblSRMECC.[PO Type])<>"Fixed Price Catalog"));
One way of doing this is to amend your existing query to add a UNION that does the subtotal. Something like this as your SQL:
SELECT 0 AS DataSortOrder, tblSRMECC.[PO Type], Sum(tblSRMECC.Award) AS SumOfAward, Count(tblSRMECC.[Counter (PO)]) AS
[CountOfCounter (PO)]
FROM tblSRMECC
GROUP BY [PO Type]
HAVING (((tblSRMECC.[PO Type])<>"AutoPO" And (tblSRMECC.[PO Type])<>"Fixed Price Catalog"))
UNION SELECT 1,"SRM",Sum(tblSRMECC.Award), Count(tblSRMECC.[Counter (PO)])
FROM tblSRMECC
HAVING (((tblSRMECC.[PO Type])<>"AutoPO" And (tblSRMECC.[PO Type])<>"Fixed Price Catalog"))
ORDER BY 1,2
The extra column that I have added, DataSortOrder, is just to make sure that the total appears after the individual totals.
If you only want to show the overall total, then you can just use the part of the query that I added:
SELECT "SRM",Sum(tblSRMECC.Award), Count(tblSRMECC.[Counter (PO)])
FROM tblSRMECC
HAVING (((tblSRMECC.[PO Type])<>"AutoPO" And (tblSRMECC.[PO Type])<>"Fixed Price Catalog"))
Regards,
I have two queries I want to join so I can get the percentages from each department of completed work orders but not sure how to go about it. I know I want a join and a crosstab query so I can display the results in a report.
The first query calculates the numerator,
SELECT
Count(MaximoReport.WorkOrder) AS CountOfWorkOrder,
MaximoReport.[Assigned Owner Group]
FROM MaximoReport
WHERE (
((MaximoReport.WorkType) In ("PMINS","PMOR","PMPDM","PMREG","PMRT"))
AND ((MaximoReport.Status) Like "*COMP")
AND ((MaximoReport.[Target Start])>=DateAdd("h",-1,[Enter the start date])
AND (MaximoReport.[Target Start])<DateAdd("h",23,[Enter the end date]))
AND ((MaximoReport.ActualLaborHours)<>"00:00")
AND ((MaximoReport.ActualStartDate)>=DateAdd("h",-11.8,[Enter the start date])
AND (MaximoReport.ActualStartDate)<DateAdd("h",23,[Enter the end date]))
)
GROUP BY MaximoReport.[Assigned Owner Group];
While the second query calculates the denominator:
SELECT
Count(MaximoReport.WorkOrder) AS CountOfWorkOrder,
MaximoReport.[Assigned Owner Group]
FROM MaximoReport
WHERE (
((MaximoReport.WorkType) In ("PMINS","PMOR","PMPDM","PMREG","PMRT"))
AND ((MaximoReport.Status)<>"CAN")
AND ((MaximoReport.[Target Start])>=DateAdd("h",-11.8,[Enter the start date])
AND (MaximoReport.[Target Start])<DateAdd("h",23,[Enter the end date])))
GROUP BY MaximoReport.[Assigned Owner Group];
Please advise how I can join the two queries to get the percentages of the departments and then do a crosstab query.
If there is a better way of doing this please also let me know.
Below is my mock database that I am using. I have four different tables: Tbl_MainDatabase, Tbl_InsuranceCoverage, Tbl_MatterDetail, and Tbl_PaymentProcessing.
What I want -
I want my form to determine the remaining Retention Limit (i.e., Retention Limit for the applicable policy - sum of invoices for the same Claim Number )
According to the mock database, the required answer should be [ $2500 - (300+700+355)] as highlighted for your convenience
What I tried
I used the help of Graphical representation through the following query:
SELECT [Claim Number], Sum([Net Invoice Amount])
FROM [PaymentProcessing]
GROUP BY [Claim Number]
This method works to show me how much I spent per claim number so far in the form of a graph. However I want to display the remaining amount.
Any Help is appreciated :)
I am one month old at using Access. But I am trying my best to learn
Thank you in advance!
SELECT
IC.[Retention Limit]-SUM([Net Invoice Amt]) AS Remaining, MD.[Claim Number], IC.[Retention Limit], IC.InsuranceID
FROM tbl_InsuranceCoverage IC
INNER JOIN tbl_MatterDetail MD ON ic.InsuranceID = MD.Policy
INNER JOIN tbl_PaymentProcessing PP ON MD.MatterDetailID=pp.MatterDetailID AND MD.[Claim Number]=pp.[Claim Number]
GROUP BY MD.[Claim Number], IC.[Retention Limit], IC.InsuranceID
See if this works. Havent tested it but seems simple. You can remove the extra columns, but this will hlep you understand joins a bit
For All the New users The above code by #Doug Coats works perfectly.
Make Sure All the Foreign Key and Primary Keys is linked in the Relationship Property. ( One of the way To do this in the Query is - Right click on the Query and select Design View --> Right click again on the grey space and Select Show all Tables Now Drag your Primary Key of the table and drop at the foreign Key on the other table --> This will create a relationship between both for the Query Purpose.
This will also Prevent Data from Duplication in the query then use a similar code as described by Doug Coats in the above comment in the SQL View
SELECT [Insurance Coverage].[Retention Unit]-Sum([Net Invoice Amount]) AS Remaining, [Matter Detail].[Claim Number], [Insurance Coverage].[Retention Unit], [Matter Detail].Policy
FROM (([Main Database] INNER JOIN [Matter Detail] ON [Main Database].[Database ID] = [Matter Detail].[Short Name]) INNER JOIN [Payment Processing] ON ([Matter Detail].[Matter Detail ID] = [Payment Processing].[Matter Detail ID]) AND ([Main Database].[Database ID] = [Payment Processing].[Short Name])) INNER JOIN [Insurance Coverage] ON [Matter Detail].Policy = [Insurance Coverage].[Insurance ID]
GROUP BY [Matter Detail].[Claim Number], [Insurance Coverage].[Retention Unit], [Matter Detail].Policy;
You can then display this query in the form - I am still evaluating best way to display this query probably in a Combo Box (Not sure if any other controls has a row source)
Thank you