Running a UNION query in an access database. I've defined every variable as a int since there was a data type mismatch error prompt. This has not resolved the issue. Each of the variables have values of either 1 or 0 and no nulls. Any ideas?
SELECT
CInt(qryGB.BM∞) AS [BM∞],
CInt(qryGB.PM∞) AS [PM∞],
CInt(qryGB.P∞) AS [P∞],
CInt(qryGB.[RAG_B<0]) AS [RAG_B<0],
CInt(qryGB.[RAG_P<0]) AS [RAG_P<0],
CInt(qryGB.[RAG_C<0]) AS [RAG_C<0],
CInt(qryGB.[B<0]) AS [B<0],
CInt(qryGB.[P<0]) AS [P<0],
CInt(qryGB.[C<0]) AS [C<0],
CInt(qryGB.[P-1]) AS [P-1],
CInt(qryGB.[C-1]) AS [C-1],
CInt(qryGB.P0) AS [P0],
CInt(qryGB.C0) AS [C0],
CInt(qryGB.[P+1]) AS [P+1],
CInt(qryGB.[P+2]) AS [P+2],
CInt(qryGB.[P+3]) AS [P+3]
FROM qryGB
UNION ALL SELECT
CInt(qryTMD.BM∞) AS [BM∞],
CInt(qryTMD.PM∞) AS [PM∞],
CInt(qryTMD.P∞) AS [P∞],
CInt(qryTMD.[RAG_B<0]) AS [RAG_B<0],
CInt(qryTMD.[RAG_P<0]) AS [RAG_P<0],
CInt(qryTMD.[RAG_C<0]) AS [RAG_C<0],
CInt(qryTMD.[B<0]) AS [B<0],
CInt(qryTMD.[P<0]) AS [P<0],
CInt(qryTMD.[C<0]) AS [C<0],
CInt(qryTMD.[P-1]) AS [P-1],
CInt(qryTMD.[C-1]) AS [C-1],
CInt(qryTMD.P0) AS [P0],
CInt(qryTMD.C0) AS [C0],
CInt(qryTMD.[P+1]) AS [P+1],
CInt(qryTMD.[P+2]) AS [P+2],
CInt(qryTMD.[P+3]) AS [P+3]
FROM qryTMD;
Check you don't have any nulls in any of the columns.
Access SQL is a little strange when it comes to nulls (noting that Standard SQL nulls are strange to begin with!). For example you can't cast a null to a data type:
SELECT DISTINCT CINT( NULL ) AS null_cast_to_int FROM AnyPopulatedTable;
errors with "Invalid use of Null".
So all Access SQL nulls are of the same type but what type?:
SELECT DISTINCT TYPENAME ( NULL ) AS type_name FROM AnyPopulatedTable;
does not error and returns 'Null' !!
The one thing I can think of is that doing the CInt() conversion during the UNION may be screwing things up. I'd try doing the conversion in subqueries before doing the UNION. Something like:
SELECT
a.[BM∞],
a.[PM∞],
a.[P∞],
a.[RAG_B<0],
a.[RAG_P<0],
a.[RAG_C<0],
a.[B<0],
a.[P<0],
a.[C<0],
a.[P-1],
a.[C-1],
a.[P0],
a.[C0],
a.[P+1],
a.[P+2],
a.[P+3]
FROM
(SELECT
CInt(qryGB.[BM∞]) AS [BM∞],
CInt(qryGB.[PM∞]) AS [PM∞],
CInt(qryGB.[P∞]) AS [P∞],
CInt(qryGB.[RAG_B<0]) AS [RAG_B<0],
CInt(qryGB.[RAG_P<0]) AS [RAG_P<0],
CInt(qryGB.[RAG_C<0]) AS [RAG_C<0],
CInt(qryGB.[B<0]) AS [B<0],
CInt(qryGB.[P<0]) AS [P<0],
CInt(qryGB.[C<0]) AS [C<0],
CInt(qryGB.[P-1]) AS [P-1],
CInt(qryGB.[C-1]) AS [C-1],
CInt(qryGB.[P0]) AS [P0],
CInt(qryGB.[C0]) AS [C0],
CInt(qryGB.[P+1]) AS [P+1],
CInt(qryGB.[P+2]) AS [P+2],
CInt(qryGB.[P+3]) AS [P+3]
FROM qryGB) as a
UNION ALL SELECT
b.[BM∞],
b.[PM∞],
b.[P∞],
b.[RAG_B<0],
b.[RAG_P<0],
b.[RAG_C<0],
b.[B<0],
b.[P<0],
b.[C<0],
b.[P-1],
b.[C-1],
b.[P0],
b.[C0],
b.[P+1],
b.[P+2],
b.[P+3]
FROM
(SELECT
CInt(qryTMD.[BM∞]) AS [BM∞],
CInt(qryTMD.[PM∞]) AS [PM∞],
CInt(qryTMD.[P∞]) AS [P∞],
CInt(qryTMD.[RAG_B<0]) AS [RAG_B<0],
CInt(qryTMD.[RAG_P<0]) AS [RAG_P<0],
CInt(qryTMD.[RAG_C<0]) AS [RAG_C<0],
CInt(qryTMD.[B<0]) AS [B<0],
CInt(qryTMD.[P<0]) AS [P<0],
CInt(qryTMD.[C<0]) AS [C<0],
CInt(qryTMD.[P-1]) AS [P-1],
CInt(qryTMD.[C-1]) AS [C-1],
CInt(qryTMD.[P0]) AS [P0],
CInt(qryTMD.[C0]) AS [C0],
CInt(qryTMD.[P+1]) AS [P+1],
CInt(qryTMD.[P+2]) AS [P+2],
CInt(qryTMD.[P+3]) AS [P+3]
FROM qryTMD) as b
Related
In the code snippet below I see a column in a where clause that I have never seen or used before. The code works and is not my code. My question is if this is some kind of "customized" TSQL language or if is legitimate to have a column in a where clause and if so what is the scope of it.
For example this clause " WHERE FMTREFl :vall" why the :val1?
SELECT 1
,FMTTRANTYPE
,FMTSTATUS
,FMTTIMEDATE
,FMTREF2
,FMTREF3
,FMTSEQNO
FROM FMTRANS
WHERE FMTREFl :vall
OR FMTHDR ANY (
SELECT FMTHSEQNO
FROM FMTRANSHDR
WHERE FMTHICPNO :val2
)
UNION ALL
SELECT 0
,FMTTRANTYPE
,FMTSTATUS
,FMTTIMEDATE
,FMTREF2
,FMTREF3
,FMTSEQNO
FROM FMTRANSIN
WHERE FMTREFl :val3
OR FMTHDR ANY (
SELECT FMTHSEQNO
FROM FMTRANSHDR
WHERE FMTHICPNO :val4
)
ORDER BY FMTTIMEDATE ASC
I am trying to execute a query in Impala and getting the following error (AnalysisException: operands of type INT and STRING are not comparable: B.COMMENT_TYPE_CD = '100' ) can someone help me fix this:
Query:
SELECT
ORDER_ID,
L1.LONG_TEXT
FROM
DB.ORDER_COMMENT A,
DB.SHORT_TEXT L1
WHERE ACTION_SEQUENCE = (
SELECT MAX(ACTION_SEQUENCE)
FROM DB.ORDER_COMMENT B
WHERE B.COMMENT_TYPE_CD='100'
AND A.ORDER_ID=B.ORDER_ID)
AND COMMENT_TYPE_CD='100'
AND A.LONG_TEXT_ID=L1.LONG_TEXT_ID
AND L1.ACTIVE_IND=1
Thanks
Not knowing the table structure makes this a guess, but... Is it possible that COMMENT_TYPE_CD field is an INT in DB.ORDER_COMMENT table? If so, you should remove the quotes in '100' and let just 100
1st Approach:
COMMENT_TYPE_CD=100
2nd Approach:
cast(COMMENT_TYPE_CD as string)='100'
I have the following SQL statement which returns the desired result in SQL Server 2012:
SELECT
S.ONOMA
, S.DIEY
, S.POLH
, S.TK
, S.IDIOT
, S.KODIKOS
, S.AFM
FROM
SYNERG AS S
INNER JOIN
(SELECT
G.AFM, MIN(KODIKOS) AS KODIKOS
FROM SYNERG AS G
WHERE LEN(ISNULL(AFM, '')) != 0
GROUP BY AFM) AS I ON S.KODIKOS = I.KODIKOS
ORDER BY
S.AFM
but when I run the same SQL statement in MS Access 2007 I get an error:
Circular reference caused by 'KODIKOS' in query definition's SELECT list.
Any help would be appreciated.
As explained in the link by HansUp:
The alias of a calculated field cannot be identical to any of the field names used to calculate the field.
This can be rather annoying (esp. if it is a field that is returned by the query), but there is no way around it.
So you need to change the alias, e.g.:
SELECT
S.ONOMA
, S.DIEY
, S.POLH
, S.TK
, S.IDIOT
, S.KODIKOS
, S.AFM
FROM
SYNERG AS S
INNER JOIN
(SELECT
G.AFM, MIN(KODIKOS) AS MinKODIKOS
FROM SYNERG AS G
WHERE LEN(Nz(AFM, '')) <> 0
GROUP BY AFM) AS I ON S.KODIKOS = I.MinKODIKOS
ORDER BY
S.AFM
Note also that an IsNull() function exists in Access, but has a different meaning (it takes one argument and returns a Boolean). The corresponding function is Nz()
And (thanks #HansUp), the unequal operator is <>, not !=. I always use <> in SQL Server too, no need to make things more complicated than necessary. :)
In Oracle 11g, I came across an error for a query and cannot figure why it is erroring on me. Here is the query:
select
main_data.issue_number,
main_data.transaction_number
from
(
select
p1.payment_date,
p1.media_number,
p1.payment_amount,
p1.issue_number,
p1.advice_na_number,
name.name_address_line_1,
name.name_address_line_2,
name.name_address_line_3,
name.name_address_line_4,
name.name_address_line_5,
name.name_address_line_6,
name.name_address_line_7,
name.name_address_city,
name.state_code,
name.address_country_code,
name.zip_code,
name.tax_id_number,
p1.output_tx_number_prin,
p1.output_tx_number_int,
'' as "transaction_number",
p1header.check_account_number
from
p1
left join name on p1.name_address_number = name.name_address_number
left join p1header on p1.issue_number = p1header.issue_number
UNION ALL
select
check.date_of_payment,
check.media_number,
check.payment_amount,
check.issue_number,
check.payee_na_number,
name.name_address_line_1,
name.name_address_line_2,
name.name_address_line_3,
name.name_address_line_4,
name.name_address_line_5,
name.name_address_line_6,
name.name_address_line_7,
name.name_address_city,
name.state_code,
name.address_country_code,
name.zip_code,
name.tax_id_number,
'' as "output_tx_number_prin",
'' as "output_tx_number_int",
check.transaction_number,
check.dda_number as "check_account_number"
from check
left join name on check.payee_na_number = name.name_address_number
) main_data
Selecting individual fields like above will give me an "invalid identifier error". If I do select * then it gives me back the data without any error. What am I doing wrong here? Thank you.
The old quoted identifier problem... see point 9 in the database object naming documentation, and note that Oracle does not recommend using quoted identifiers.
You've put your column alias as lower case inside double-quotes. That means that any references to it also have to be quoted and exactly match the case. So this would work:
select
main_data.issue_number,
main_data."transaction_number"
from
...
But unless you have a burning need to have that alias like that - and I doubt you do as all the identifier names from the actual table columns are not quoted - it would be simpler to remove the double quotes from the inner selects:
select
main_data.issue_number,
main_data.transaction_number
from
(
select
...
'' as transaction_number,
p1header.check_account_number
...
UNION ALL
select
...
'' as output_tx_number_prin,
'' as output_tx_number_int,
check.transaction_number,
check.dda_number as check_account_number
...
You don't actually need to alias the columns in the second branch of the union; the column identifiers will all be taken from the first branch.
I am getting an error in PCC which doesn't make a lot of sense. I have two statements inside a user defined function that are nearly exactly the same and one runs fine while the other is returning an error:
'INTO': Syntax error
end and start are parameters being passed to the function.
The Error is being thrown on the second INTO statement
SELECT count(*) INTO :divModelTot1
FROM "table1"."info" i
WHERE i.compldate <:end
AND (i.agree is null OR i.agree>:start)
UNION ALL
SELECT count(*) INTO :divModelTot2
FROM "table2"."info" i
WHERE i.compldate <:end
AND (i.agree is null or i.agree>:start);
Any help or suggestions would be appreciate.
Thanks!
SELECT INTO must be the first query in a statement containing a UNION.
SELECT count(*) INTO :divModelTot1
FROM "table1"."info" i
WHERE i.compldate <:end
AND (i.agree is null OR i.agree>:start)
UNION ALL
SELECT count(*)
FROM "table2"."info" i
WHERE i.compldate <:end
AND (i.agree is null or i.agree>:start);