Trying to set up this sql as a data connection in excel 2007:
select sv.accountid, sv.companyName, sv.siteid, sv.siteName + '('+sv.siteCity + '-' + sv.siteState + ')' as siteNameWithLocation,
datefromparts(datepart(yyyy,r.logdate),datepart(mm,r.logdate),1) as monthDate,
sum('controllerEstKGal') / 1000 as siteEstimatedTotalKgals
from RuntimeDay r WITH (READUNCOMMITTED)
join controller c on c.id = r.controllerid
join siteView sv on sv.siteid = c.siteid
join (
SELECT r.controllerid, r.logdate, SUM((ISNULL(r.RuntimeSec, 0)/60.0) * ISNULL(s.EsrfGpm, 0)) as 'controllerEstKGal'
FROM RuntimeDay r WITH (READUNCOMMITTED)
JOIN CdcView c WITH (READUNCOMMITTED) ON r.ControllerId = c.ControllerId
JOIN StationFlowConfig s WITH (READUNCOMMITTED)
ON r.ControllerId = s.ControllerId AND r.StationNumber = s.StationId
WHERE c.siteid in (8547, 8299, 8556, 8541, 8292, 8600, 8551, 5487, 8555, 8216, 8342, 8557, 8287, 8542, 8221, 5509, 8218, 8543, 8336, 8343)
AND logDate between '2016-01-01' and '2016-12-31'
AND r.RuntimeSec > 0
AND s.EsrfGpm > 0
group by r.controllerid, r.logdate
) eu on eu.controllerid = r.controllerid and eu.logDate = r.logdate
where sv.siteid in (8547, 8299, 8556, 8541, 8292, 8600, 8551, 5487, 8555, 8216, 8342, 8557, 8287, 8542, 8221, 5509, 8218, 8543, 8336, 8343) and r.logDate between '2016-01-01' and '2016-12-31'
group by sv.accountid, sv.companyName, sv.siteid, sv.siteName + '('+sv.siteCity + '-' + sv.siteState + ')',
datefromparts(datepart(yyyy,r.logdate),datepart(mm,r.logdate),1)
And I get the following error message from Microsoft Query:
No Column name was specified for column 3 of 'eu'.
Statement(s) could not be prepared.
I believe that "column 3" is referring to this specific part:
SUM((ISNULL(r.RuntimeSec, 0)/60.0) * ISNULL(s.EsrfGpm, 0)) as 'controllerEstKGal'
I've found cases where this question was asked before, but the replies say to put the name of column 3 in quotes. However (as you can see) I've done that and I'm still getting this error.
Help me StackExchange, you're my only hope! (and thank you!)
EDIT: To clarify, I get the error both with and without single/double quotes surrounding controllerEstKgal. Backticks result in the following error message:
Incorrect syntax near '`'.
Incorrect syntax near the keyword 'with'. If this statement is a common table expression, an xmlnamespaces clause or a change tracking context clause, the previous statement must be terminated with a semicolon.
Statement(s) could not be prepared.
Related
WITH
TEST_RESULT_CTE AS
(SELECT R.DSDW_RESULT_ID,
R.PARAM_VALUE AS TEST_ID
FROM SLIMS_POC_RESULT_DETAIL R
JOIN SLIMS_POC_PARAMETER P ON P.PARAM_ID=R.PARAM_ID
WHERE P.PARAMETER_NAME='TEST_ID' AND P.CATEGORY = 'Result' )
SELECT * FROM
(
SELECT S.SAMPLE_ID, R.DSDW_RESULT_ID, PARA.PARAMETER_NAME as PNAME, R.PARAM_VALUE as PVALUE
FROM SLIMS_POC_RESULT_DETAIL R
JOIN TEST_RESULT_CTE TR ON TR.DSDW_RESULT_ID = R.DSDW_RESULT_ID
JOIN SLIMS_POC_TEST T ON T.TEST_ID = TR.TEST_ID
JOIN SLIMS_POC_SAMPLE S ON S.SAMPLE_ID = T.SAMPLE_ID --AND S.SAMPLE_ID = to_char(113)
JOIN SLIMS_POC_PARAMETER PARA ON PARA.PARAM_ID=R.PARAM_ID AND PARA.CATEGORY='Result'
)
Result_Data
PIVOT
(
MAX(PVALUE) FOR PNAME IN ( 'TEST_ID', 'RESULT_NAME', 'UNIT', 'RESULT_TEXT', 'VALUE', 'STATUS', 'ENTERED_ON', 'ENTERED_BY', 'RESULT_TYPE' )
) PIVOTED_TAB
WHERE SAMPLE_ID > 111
ORDER BY SAMPLE_ID;
The above sql Query provides an output, without any error.
However if I replace '111' with '112' in WHERE cluase, I get the following error:
ORA-01722: invalid number
01722. 00000 - "invalid number"
*Cause: The specified number was invalid.
*Action: Specify a valid number.
This error is quite strange to me, and that's why tough to fix.
The error is most likely from the data that is being returned from your new sample_id. There is a string being converted to a number that is failing. Check your data for invalid numerical data in varchar columns. Note that it could be an implicit conversion, not necessarily one where you are doing a to_number() call.
SELECT CASE (SELECT Count(1)
FROM wf_item_activity_statuses_v t
WHERE t.activity_label IN ('WAITING_DISB_REQ',
'LOG_DDE',
'LOG_SENDBACK_DDE')
AND t.item_key IN(
SELECT r.i_item_key
FROM wf_t_item_xref r
WHERE r.sz_appl_uniqueid = '20400000988')
)
WHEN 0 THEN
(
delete
from t_col_val_document_uploaded p
WHERE p.sz_application_no = '20400000988'
AND p.sz_collateral_id = 'PROP000000000PRO1701'
AND p.i_item_key = '648197'
AND p.i_document_srno = '27' )
WHEN 1 THEN
(
DELETE
FROM t_col_val_document_uploaded p
WHERE p.sz_application_no = '20400000988'
AND p.sz_collateral_id = 'PROP000000000PRO1701'
AND p.i_document_srno = '28' )
ELSE NULL
END
FROM dual;
You need to recreate your query and make sure to follow the flow of the clauses properly, please check the next two links to get a better understanding :
[ORA-00936: missing expression tips]
How do I address this ORA-00936 error?
Answer: The Oracle oerr utility notes this about the ORA-00936 error:
ORA-00936 missing expression
Cause: A required part of a clause or expression has been omitted. For example, a SELECT statement may have been entered without a list of columns or expressions or with an incomplete expression. This message is also issued in cases where a reserved word is misused, as in SELECT TABLE.
Action: Check the statement syntax and specify the missing component.
The ORA-00936 happens most frequently:
1 - When you forget list of the column names in your SELECT statement.
2. When you omit the FROM clause of the SQL statement.
ora-00936-missing-expression
I hope this can help you.
You cannot use a simple select query like this. You have to use a PL/SQL block like below -
DECLARE NUM_CNT NUMBER := 0;
BEGIN
SELECT Count(1)
INTO NUM_CNT
FROM wf_item_activity_statuses_v t
WHERE t.activity_label IN ('WAITING_DISB_REQ',
'LOG_DDE',
'LOG_SENDBACK_DDE')
AND t.item_key IN(SELECT r.i_item_key
FROM wf_t_item_xref r
WHERE r.sz_appl_uniqueid = '20400000988');
IF NUM_CNT = 0 THEN
delete
from t_col_val_document_uploaded p
WHERE p.sz_application_no = '20400000988'
AND p.sz_collateral_id = 'PROP000000000PRO1701'
AND p.i_item_key = '648197'
AND p.i_document_srno = '27';
ELSIF NUM_CNT = 1 THEN
DELETE
FROM t_col_val_document_uploaded p
WHERE p.sz_application_no = '20400000988'
AND p.sz_collateral_id = 'PROP000000000PRO1701'
AND p.i_document_srno = '28' )
END IF;
END;
I have a SQL query in Excel that will work just fine if I hard code the dates into the query. As soon as I try to change them to ?'s it gives the error. I've used parameterized queries like this in loads of reports, so I'm not sure why this one is suddenly not working.
The full error is [Microsoft][ODBC SQL Server Driver][SQL Server]The multi-part identifier "cancel.arrival_date" could not be bound. which pops up twice.
Here is my query with the ? in it that gives the error:
SELECT cancel.reservation_number, (client.last_name + ', ' + client.first_name) AS 'guest_name',
cancel.cancel_date_time, cancel.arrival_date,
DATEDIFF(DAY, cancel.cancel_date_time, cancel.arrival_date) AS 'Days Out', cancel.cancel_reason,
CASE
WHEN EXISTS (SELECT *
FROM gbfol_head head
LEFT JOIN gbfol_det det ON head.folio_number = det.folio_number
WHERE cancel.reservation_number = head.source_id
AND head.folio_type <> 'b' AND det.posting_code = 'admn') THEN
'ADMN Charged'
ELSE
'No ADMN Fee'
END AS 'ADMN',
cancel.amount, cancel.cancel_clerk_code, cancel.sba_text
FROM canceled cancel
LEFT JOIN reservation res ON cancel.reservation_number = res.reservation_number
LEFT JOIN clients client ON res.home_client_code = client.client_code
WHERE DATEDIFF(DAY, cancel.cancel_date_time, cancel.arrival_date) < 21
AND (cancel.arrival_date BETWEEN ? AND ?)
If I change the last line to AND (cancel.arrival_date BETWEEN '2019-12-01' AND '2019-12-10') it works fine.
I have also tried using AND (cancel.arrival_date >= ? AND cancel.arrival_date <= ?) which didn't work either, same error.
I followed a guide on techonthenet where it says that for a SUM query you need to put the Sum(expr) in the SELECT statement and then GROUP BY every other field you are selecting (psuedo-code; correct me if I'm wrong). Below is my attempt. Why am I getting the error
"An error occurred while executing the query. Incorrect syntax near '('. Error:102; Line 9"
The end result of all this will be a Report, not just a query. In the report, all of the fields below will be shown with the exception that the [Difference] field will be totaled at the bottom. I get the feeling that instead of trying to sum this in the query I should be trying to sum in the report...
EDIT - I removed the [ALL] but now I get the error "Cannot use an aggregate or a subquery in an expression used for the group by list of a GROUP BY clause." It also says that this error is on line 29. I will mark it below.
SELECT
JOB.WHSE,
LEFT(RESSCHD000.RESID, 6) AS Line,
RIGHT(LEFT(JOB000.JSID, 11), 10)+ '-' +RIGHT(LEFT(JOB000.JSID, 27), 2) AS [Job Number],
LEFT(RESSCHD000.STARTDATE, 7)+ ' ' +RIGHT(Convert(Datetime, MIN(RESSCHD000.STARTDATE)), 8) AS [Start],
LEFT(RESSCHD000.ENDDATE, 7)+ ' ' +RIGHT(Convert(Datetime, MAX(RESSCHD000.ENDDATE)),8) AS [End],
RESSCHD000.STATUSCD,
Sum((Dateadd("n",DateDiff("n", MIN(RESSCHD000.STARTDATE),MAX(RESSCHD000.ENDDATE)),'00:00:00'))) AS [SumOfDifference],
RESSCHD000.JOBTAG,
CASE
WHEN Dateadd("n",DateDiff("n", MIN(RESSCHD000.STARTDATE),MAX(RESSCHD000.ENDDATE)),'00:00:00') > '3:59:00'
THEN 'Full Sanitation'
ELSE 'Dry Clean'
END AS [Type of Sanitation],
jrt_sch.Uf_Quoted_Crew_Size AS [Crew Size]
FROM
RESSCHD000 INNER JOIN JOB000 ON RESSCHD000.JOBTAG = JOB000.JOBTAG
INNER JOIN JOB ON JOB.JOB = RIGHT(LEFT(JOB000.JSID, 11), 10) AND JOB.suffix = RIGHT(LEFT(JOB000.JSID, 27), 2)
LEFT OUTER JOIN jrt_sch ON jrt_sch.job = job.job
WHERE
job.job_date = #job_date
GROUP BY
JOB.WHSE,
LEFT(RESSCHD000.RESID, 6),
RIGHT(LEFT(JOB000.JSID, 11), 10)+ '-' +RIGHT(LEFT(JOB000.JSID, 27), 2), --line 29
LEFT(RESSCHD000.STARTDATE, 7)+ ' ' +RIGHT(Convert(Datetime, MIN(RESSCHD000.STARTDATE)), 8),
LEFT(RESSCHD000.ENDDATE, 7)+ ' ' +RIGHT(Convert(Datetime, MAX(RESSCHD000.ENDDATE)),8),
RESSCHD000.STATUSCD,
RESSCHD000.JOBTAG,
CASE
WHEN Dateadd("n",DateDiff("n", MIN(RESSCHD000.STARTDATE),MAX(RESSCHD000.ENDDATE)),'00:00:00') > '3:59:00'
THEN 'Full Sanitation'
ELSE 'Dry Clean'
END,
jrt_sch.Uf_Quoted_Crew_Size
HAVING
RESSCHD000.STATUSCD='s'
The error is due to this line:
Sum([ALL] (Dateadd("n",DateDiff("n", MIN(RESSCHD000.STARTDATE),MAX(RESSCHD000.ENDDATE)),'00:00:00'))) AS [SumOfDifference]
The sum is looking for a field (or formula of some sort) to SUM up. You have a field name followed by a formula. I am guessing you want the sum of the difference in seconds based on the FIELD name (SumOfDifference).
I think you just need to remove the [ALL].
SUM(DATEADD("n",DATEDIFF("n", MIN(RESSCHD000.STARTDATE), MAX(RESSCHD000.ENDDATE)),'00:00:00')) AS [SumOfDifference]
I'm getting an error with a query to get data... The error message is: Msg 248, Level 16, State 1, Line 1: The conversion of the varchar value overflowed an int column...
I cant resolve this problem, so if any one can help me, thanks in advance, Here is My Sql:
This happens when I inserted 3 new joins in the query, I made them BOLD, otherwise it works perfectly...
SELECT DISTINCT
ACR_ART_ID, DES_TEXTS.TEX_TEXT AS CRITERIA_DES_TEXT,
COALESCE(DES_TEXTS2.TEX_TEXT, ACR_VALUE) AS CRITERIA_VALUE_TEXT,
(DES_TEXTS.TEX_TEXT + ': ' + COALESCE(DES_TEXTS2.TEX_TEXT, ACR_VALUE)) AS CEL_OPIS
FROM
Inventory.dbo.ARTICLE_CRITERIA
LEFT JOIN
Inventory.dbo.DESIGNATIONS AS DESIGNATIONS2 ON DESIGNATIONS2.DES_ID = ACR_KV_DES_ID
LEFT JOIN
Inventory.dbo.DES_TEXTS AS DES_TEXTS2 ON DES_TEXTS2.TEX_ID = DESIGNATIONS2.DES_TEX_ID
LEFT JOIN
Inventory.dbo.CRITERIA ON CRI_ID = ACR_CRI_ID
LEFT JOIN
Inventory.dbo.DESIGNATIONS ON DESIGNATIONS.DES_ID = CRI_DES_ID
LEFT JOIN
Inventory.dbo.DES_TEXTS ON DES_TEXTS.TEX_ID = DESIGNATIONS.DES_TEX_ID
INNER JOIN
Inventory.dbo.ART_LOOKUP al ON ARTICLE_CRITERIA.ACR_ART_ID = al.ARL_SEARCH_NUMBER
AND al.ARL_KIND in (1,3)
INNER JOIN
Inventory.dbo.ARTICLES a ON al.ARL_ART_ID = a.ART_ID and (a.ART_SUP_ID=21 or a.ART_SUP_ID=11091)
INNER JOIN
Inventory.dbo.SUPPLIERS ON SUPPLIERS.SUP_ID = ART_SUP_ID**
WHERE
(DESIGNATIONS.DES_LNG_ID IS NULL OR DESIGNATIONS.DES_LNG_ID = 25)
AND (DESIGNATIONS2.DES_LNG_ID IS NULL OR DESIGNATIONS2.DES_LNG_ID = 25);
As Commented by #T I
The varchar col is implicitly converted to an int and is overflowing (i.e. is >larger than 2,147,483,647). To resolve this try casting the columns to bigint
you can cast column value data type as
select cast( max(columnname) AS bigint)+1 from atable ;
But if you will get a varchar than it will fail,
with error
So it is better to change your DB schema.
Use this approach only if you are sure that only numeric string will be present.