The following code works in MSQuery but SQL MS rejests rejects the 2nd AS on Line 10 ?
SELECT bcs.BomReference, bcs.DateTimeCosted, bcs.TotalCost
FROM (
SELECT BomReference,
Max(DateTimeCosted) AS MaxDate
FROM NDM_Sage200.dbo.BomCostSession BomCostSession
GROUP BY BomReference
) AS ldc
INNER JOIN
NDM_Sage200.dbo.BomCostSession BomCostSession
as bcs
ON bcs.BomReference = ldc.BomReference AND
bcs.DateTimeCosted = ldc.MaxDate
ORDER BY bcs.BomReference
I am working in SQL MS as I am trying to expand the code as follows cannot locate the MSQuery error message "Could not add the table (" which I could not debug.
SELECT bcs.BomReference, bcs.DateTimeCosted, bcs.TotalCost, StockItem.StockDescription
FROM (
SELECT BomReference,
Max(DateTimeCosted) AS MaxDate
FROM NDM_Sage200.dbo.BomCostSession BomCostSession
GROUP BY BomReference
) AS ldc
INNER JOIN
BomCostSession as bcs
ON bcs.BomReference = ldc.BomReference AND
bcs.DateTimeCosted = ldc.MaxDate
INNER JOIN
StockItem
ON BomCostSession.BomReference = StockItem.ItemID
ORDER BY BomReference
Kristen & JamesK
Thank you. I have corrected as suggested plus found another. I am now getting another error:
"Error converting data type varchar to bigint" on line 5. I have tried various CONVERTS but with no luck. The format used is "2015-07-22 13:36:02.000" which seems to be 121
It confuses me when I get no error messages in MSQuery, but the code fails in SQL MS
The code now reads:
SELECT bcs.BomReference, bcs.DateTimeCosted, bcs.TotalCost, StockItem.Name
FROM (
SELECT BomReference,
Max(DateTimeCosted) AS MaxDate
FROM NDM_Sage200.dbo.BomCostSession BomCostSession
GROUP BY BomReference
) AS ldc
INNER JOIN
NDM_Sage200.dbo.BomCostSession
as bcs
ON bcs.BomReference = ldc.BomReference AND
bcs.DateTimeCosted = ldc.MaxDate
INNER JOIN
NDM_Sage200.dbo.StockItem StockItem
ON bcs.BomReference = StockItem.ItemID
ORDER BY bcs.BomReference
The error message is complaining about VARCHAR to BIGINT. The only places I see where there could be (implicit) conversion is for StockItem.ItemID or bcs.BomReference on the join condition. Are they both numeric?
Also, you mentioned trying to use CONVERT. Which column are you trying to convert? DateTimeCosted? What is its datatype? Regardless of its data type, that should not cause a conversion error.
I don't think that should cause an error. You can use MAX function on VARCHAR. It wouldn't try to convert to BIGINT when trying to compute MAX. Is what you posted the entire code?
Try the following and see if that produces an error.
SELECT MAX(DateTimeCosted) AS MaxDate
FROM NDM_Sage200.dbo.BomCostSession
I am expecting that it would run without any error.
If the query you posted earlier is the entire query, the only place I see where a conversion error of this type can happen is in the final join condition bcs.BomReference = StockItem.ItemID
What are the data types of those two columns? You can find using the following query (or look in the SSMS object explorer by navigating down the tables and opening up the columns node.
SELECT table_name, column_name,data_type
FROM INFORMATION_SCHEMA.COLUMNS
WHERE COLUMN_NAME IN ('BomReference','ItemID');
James
This is the full query which gives the error:: Msg 8114, Level 16, State 5, Line 1 Error converting data type varchar to bigint.
SELECT bcs.BomReference, bcs.DateTimeCosted, bcs.TotalCost, StockItem.Name
FROM (
SELECT BomReference,
Max(DateTimeCosted) AS MaxDate
FROM NDM_Sage200.dbo.BomCostSession BomCostSession
GROUP BY BomReference
) AS ldc
INNER JOIN
NDM_Sage200.dbo.BomCostSession
as bcs
ON bcs.BomReference = ldc.BomReference AND
bcs.DateTimeCosted = ldc.MaxDate
INNER JOIN
NDM_Sage200.dbo.StockItem StockItem
ON bcs.BomReference = StockItem.ItemID
ORDER BY bcs.BomReference
This sub-query works perfectly:
SELECT bcs.BomReference, bcs.DateTimeCosted, bcs.TotalCost
FROM (
SELECT BomReference,
Max(DateTimeCosted) AS MaxDate
FROM NDM_Sage200.dbo.BomCostSession BomCostSession
GROUP BY BomReference
) AS ldc
INNER JOIN
NDM_Sage200.dbo.BomCostSession
as bcs
ON bcs.BomReference = ldc.BomReference AND
bcs.DateTimeCosted = ldc.MaxDate
ORDER BY bcs.BomReference
If I alter the first line of the full query by removing 'StockItem.Name' thus:
I still get the same error.
Looking at the fields:
BomReference & StockItem.Name are alpha-numeric
DateTime Costed is the full field inc. fractional seconds
Total.Cost is numeric
What are the data types of the two columns in the final join? You can find using the following query (or look in the SSMS object explorer by navigating down the tables and opening up the columns node.
SELECT table_name, column_name,data_type
FROM INFORMATION_SCHEMA.COLUMNS
WHERE COLUMN_NAME IN ('BomReference','ItemID');
in the TYPE column will be listed either "table" or "view".
If you are referencing a VIEW it is quite possible that the view itself is making a conversion from, say, CHAR to INT which may include data which is invalid for that conversion.
You need to check all the things in your FROM / JOIN clauses, e.g.
NDM_Sage200.dbo.BomCostSession
NDM_Sage200.dbo.StockIte
etc