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
)
)
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 working on a web app and there are some long winded stored procedures and just trying to figure something out, I have extracted this part of the stored proc, but cant get it to work. The guy who did this is creating alias after alias.. and I just want to get a section to work it out. Its complaining about the ending but all the curly brackets seem to match. Thanks in advance..
FInputs is another stored procedure.. the whole thing is referred to as BASE.. the result of this was being put in a temp table where its all referred to as U. I am trying to break it down into separate sections.
;WITH Base AS
(
SELECT
*
FROM F_Inputs(1,1,100021)
),
U AS
(
SELECT
ISNULL(q.CoverPK,r.CoverPK) AS CoverPK,
OneLine,
InputPK,
ISNULL(q.InputName,r.InputName) AS InputName,
InputOrdinal,
InputType,
ParentPK,
InputTriggerFK,
ISNULL(q.InputString,r.InputString) AS InputString,
PageNo,
r.RatePK,
RateName,
Rate,
Threshold,
ISNULL(q.Excess,r.Excess) AS Excess,
RateLabel,
RateTip,
Refer,
DivBy,
RateOrdinal,
RateBW,
ngRequired,
ISNULL(q.RateValue,r.RateValue) AS RateValue,
ngClass,
ngPattern,
UnitType,
TableChildren,
TableFirstColumn,
parentRatePK,
listRatePK,
NewParentBW,
NewChildBW,
ISNULL(q.SumInsured,0) AS SumInsured,
ISNULL(q.NoItems,0) AS NoItems,
DisplayBW,
ReturnBW,
StringBW,
r.lblSumInsured,
lblNumber,
SubRateHeading,
TrigSubHeadings,
ISNULL(q.RateTypeFK,r.RateTypeFK) AS RateTypeFK,
0 AS ListNo,
0 AS ListOrdinal,
InputSelectedPK,
InputVis,
CASE
WHEN ISNULL(NewChildBW,0) = 0
THEN 1
WHEN q.RatePK is NOT null
THEN 1
ELSE RateVis
END AS RateVis,
RateStatus,
DiscountFirstRate,
DiscountSubsequentRate,
CoverCalcFK,
TradeFilter,
ngDisabled,
RateGroup,
SectionNo
FROM BASE R
LEFT JOIN QuoteInputs Q
ON q.RatePK = r.RatePK
AND q.ListNo = 0
AND q.QuoteId = 100021 )
Well, I explained the issue in the comments section already. I'm doing it here again, so future readers find the answer more easily.
A WITH clause is part of a query. It creates a view on-the-fly, e.g.:
with toys as (select * from products where type = 'toys') select * from toys;
Without the query at the end, the statement is invalid (and would not make much sense anyhow; if one wanted a permanent view for later use, one would use CREATE VIEW instead).
I have a SQL statement on Netezza that uses the following SQL to acquire the currently logged on user ID:
SELECT SESSION_USERNAME FROM _V_SESSION_DETAIL WHERE SESSION_ID=current_sid
This works great when I'm executing the SQL in a database client. However, when I implement the above SQL in a view (along with other SQL) the current_sid is replaced with the session ID I happened to have when I created the view. That SQL will then look something like:
SELECT DEFINITION_SCHEMA."_V_SESSION_DETAIL".SESSION_USERNAME FROM DEFINITION_SCHEMA."_V_SESSION_DETAIL" WHERE (DEFINITION_SCHEMA."_V_SESSION_DETAIL".SESSION_ID = 2434740
Is there a way to define a view that will get the currently logged on user's ID, not the ID that was assigned when the view was created?
Seems like Netezza Metadata functions(ex. current_sid) are not supported in with clause and would advice to remove them from with and to include them in the base query .
CREATE
OR REPLACE VIEW ADMIN.VW_PI_HRCHY_EPH AS
WITH CHAR_MASK(CHAR_MASK_CHAR) AS (
SELECT 'xxx'
FROM _V_SESSION_DETAIL LIMIT 1
)
,NUM_MASK(NUM_MASK_NUM) AS (
SELECT - 1
FROM _V_SESSION_DETAIL LIMIT 1
)
,TS_MASK(TS_MASK_TS) AS (
SELECT '1000-01-01 00:00:00'
FROM _V_SESSION_DETAIL LIMIT 1
)
SELECT CASE
WHEN SECURITY_GRP_CNT.COUNT > 0
THEN PI_HRCHY.HRCHY_LINE_ID
ELSE NUM_MASK.NUM_MASK_NUM
END AS HRCHY_LINE_ID
,CASE
WHEN SECURITY_GRP_CNT.COUNT > 0
THEN PI_HRCHY.LOCALE_CD
ELSE CHAR_MASK.CHAR_MASK_CHAR
END AS LOCALE_CD
,CASE
WHEN SECURITY_GRP_CNT.COUNT > 0
THEN PI_HRCHY.MODIFY_TS
ELSE TS_MASK.TS_MASK_TS
END AS MODIFY_TS
FROM (
SELECT COUNT(*) AS count
FROM _V_USERGROUPS
WHERE USERNAME IN (
SELECT SESSION_USERNAME
FROM _V_SESSION_DETAIL
WHERE SESSION_ID = current_sid
)
AND GROUPNAME = 'GROUP_AUTH2READ'
) SECURITY_GRP_CNT
,ADMIN.PI_HRCHY
,CHAR_MASK
,NUM_MASK
,TS_MASK
WHERE (
(PI_HRCHY.HRCHY_TYP_ID = 11)
AND (PI_HRCHY.ACTV_IND = 'Y'::"NCHAR")
);
The solution NzGuy provided solved my problem. As he stated, apparently placing the current_sid contact in the WITH clause of the SQL causes the constant to be evaluated differently than if it were placed outside the WITH. The common table expression defined outside the WITH clause resolved my problem.
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.
I'm writing a unit test for a method with the following sql
WITH temptab(
i__id , i__name, i__effective, i__expires, i__lefttag, i__righttag,
hier_id, hier_dim_id, parent_item_id, parent_hier_id, parent_dim_id,
ancestor, h__id, h__name, h__level, h__effective, h__expires, rec_lvl)
AS (
SELECT
item.id as i__id,
item.name as i__name,
item.effectivets as i__effective,
item.expirests as i__expires,
item.lefttag as i__lefttag,
item.righttag as i__righttag,
hier_id, hier_dim_id,
parent_item_id,
parent_hier_id,
parent_dim_id, 1 as ancestor,
hier.id as h__id, hier.name as h__name,
hier.level as h__level, hier.effectivets as h__effective,
hier.expirests as h__expires, 1 as rec_lvl FROM metro.item item,
metro.hierarchy hier WHERE item.id = 'DI' AND hier_id = '69' AND hier_dim_id= '36' AND hier.id =item.hier_id
)
SELECT
i__id, i__name, i__effective, i__expires, i__lefttag,
i__righttag, hier_id, hier_dim_id, parent_item_id,
parent_hier_id, parent_dim_id, ancestor,
h__id, h__name, h__level, h__effective, h__expires
FROM temptab
This query returns empty dataset, but I expect 1 row.
The data are correct, as similar simple query without with clause works fine.
I investigated the problem and I've found the
Sub Query with WITH-CLAUSE in H2DB
but that solution did not help.
So, does anyone know how H2 supports with clause?
Thanks in advance for your time.
According to the following :h2 database grammar
Looks like WITH clause is not supported in H2 database, except of experimental support for recursive queries: h2 recursive queries
Its supported now http://www.h2database.com/html/grammar.html
For non-recursive queries also.