weird where clasue in TSQL - sql

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

Related

What is the syntax problem here using this subquery inside where clause

SELECT p.pnum, p.pname
FROM professor p, class c
WHERE p.pnum = c.pnum AND c.cnum = CS245 AND (SELECT COUNT(*) FROM (SELECT MAX(m.grade), MAX(m.grade) - MIN(m.grade) AS diff
FROM mark m WHERE m.cnum = c.cnum AND m.term = c.term AND m.section = c.section AND diff <= 20)) = 3
Incorrect syntax near ')'. Expecting AS, FOR_PATH, ID, or QUOTED_ID.
Consider the following example:
SELECT COUNT(1)
FROM SYSCAT.TABLES T
WHERE
(
-- SELECT COUNT(1)
-- FROM
-- (
SELECT COUNT(1)
FROM SYSCAT.COLUMNS C
WHERE C.TABSCHEMA=T.TABSCHEMA AND C.TABNAME=T.TABNAME
-- )
) > 50;
The query above works as is. But the problem is, that if you uncomment the commented out lines, you get the following error message: "T.TABNAME" is an undefined name. and least in Db2 for Linux, Unix and Windows.
You can't push external to the sub-select column references too deeply.
So, your query is incorrect.
It's hard to correct it, until you provide the task description with data sample and the result expected.
I can see a potential syntax errors: It seems that CS245 refers to a value c.cnum may take and not a column name. If that is the case, it should be enclosed in single quotes.

UNION data type mismatch

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

multiplying modified colums to form new column

I want a query like:
select tp.package_rate, sum(sell) as sold_quantity , select(tp.package_rate * sold_quantity ) as sell_amount from tbl_ticket_package tp where tp.event_id=1001
here the system firing error while doing the multiplication as
sold_quantity is invalid column
another problem is that in multiplication I want to use package_rate which got by select query from tp.package_rate but it multiplying with all package_rate of the table but I want only specific package_rate which was output of select query
What would you suggest? I want to bind this query in gridview . is there any way to do it using ASP.net gridview?
Your problem is that you are referring to sold_quantity here :
select(tp.package_rate * sold_quantity )
The alias is not recognized at this point.You will have to replace it with sum(sales). You will also have to group by tp.package_rate.
Your query should ideally be like :
select tp.package_rate, sum(sell) as sold_quantity ,
(tp.package_rate * sum(sell) ) as sell_amount from tbl_ticket_package tp
where tp.event_id=1001 group by tp.package_rate;
I am guessing that tp.package_rate is unique for a given event_id, from latter part of your question. If that's not the case, the sql you have written makes no sense.

Error in select statement, with union all in a subquery

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.

Conditional Clause in Where Access SQL

I'm looking to add a conditional IIf or CASE to a WHERE clause in Access SQL to add an either/or condition based upon a passed value. I've seen a couple of examples on the site, but they were a little different and I have struggled to get the code to work in my case. The code:
SELECT * FROM incHC
WHERE
incHC.repdte=(SELECT Max(repdte) AS maxDt FROM bYrs) AND
incHC.asset>0 AND
incHC.eq2<>0 AND
(
CASE WHEN recType="inst" THEN
incHC.orphan=0
ELSE
incHC.orphan<=1
END
)
Any help is much appreciated.
Unless I am missing something with your query, you should be able to do this without a CASE:
SELECT *
FROM incHC
WHERE incHC.repdte=(SELECT Max(repdte) AS maxDt FROM bYrs)
AND incHC.asset>0
AND incHC.eq2<>0
AND
(
(
recType="inst"
AND incHC.orphan=0
)
OR
(
recType<>"inst"
AND incHC.orphan<=1
)
)