I would like to display a concatenation of multiple string built upon when statement when the condition is met. As follow :
select
case
when T1.Field is not null then 'T1,'
when T2.Field is not null then /*last results*/ + 'T2,'
when T3.Field is not null then /*last results*/ + 'T3,'
end
from T1
left outer join T2 on ...
left outer join T3 on ...
Finally found out ... I didn't realize this was possible before :
select
case when T1.Field is not null then 'T1,' else '' end
+ case when T2.Field is not null then 'T2,' else '' end
+ case when T3.Field is not null then 'T3,' else '' end as result
from T1
left outer join T2 on ...
left outer join T3 on ...
Why not just try using ISNULL?
Something like
ISNULL(T1.Field,'') + ISNULL(T2.Field,'') + ... + ISNULL(TN.Field,'')
Related
I have a DB2 Server and I want to select data depending on the condition.
If I have the Value Value in the ColumnX, ColumnY or ColumnZ it should search for it between the TB2.Column1 and TB3.Column1
My statement is a bunch of left outer joins but it looks like that:
SELECT
CASE
WHEN (TB1.ColumnX || TB1.ColumnY || TB1.ColumnZ) = 'Value'
THEN Statement1
ELSE ' '
END AS MyColumn
FROM
TB1 LEFT OUTER JOIN TB2 ON TB1.JOINCOL = TB2.JOINCOL
LEFT OUTER JOIN TB3 ON TB2.JOINCOL2 = TB3.JOINCOL
WHERE TB1.Column1 between TB2.Column1 and TB3.Column1
But it doesn't work, is there a mistake in the syntax?
It is not possible to check for multiple equalities using just a single expression. You may use the following syntax trick:
CASE WHEN 'Value' IN (TB1.ColumnX, TB1.ColumnY, TB1.ColumnZ)
THEN Statement1 ELSE ' ' END AS MyColumn
The alternative to this would be to repeat the full equality check for each column:
SELECT
CASE
WHEN TB1.ColumnX = 'Value' OR
TB1.ColumnY = 'Value' OR
TB1.ColumnZ = 'Value'
THEN Statement1
ELSE ' '
END AS MyColumn
FROM
TB1 LEFT OUTER JOIN TB2
ON TB1.JOINCOL = TB2.JOINCOL
LEFT OUTER JOIN TB3
ON TB2.JOINCOL2 = TB3.JOINCOL
WHERE
TB1.Column1 BETWEEN TB2.Column1 AND TB3.Column1;
If you need to refer to the CASE expression in the WHERE clause, then you have two choices. First, you may subquery your current query, and assign an alias to the CASE expression:
SELECT *
FROM
(
SELECT CASE WHEN ... AS exp
FROM TB1 ...
) t
WHERE exp = ...
Or, you may avoid the subquery and just repeat the entire CASE expression in the WHERE clause.
**The below query is working badly, and I'm planning to replace case stmt in where clause with temp table. Pls suggest to me some option to rewrite this query
Is there a way to simplify this into multiple SQLs
Also explain me how this case stmt works in where clause**
select DISTINCT
ph.order_type_cd,
c.order_num,
ph.created_date,
os.order_status_desc,
ph.facility_name,
PH.vendor_order_num,
l.lic_nm,
(u.last_nm + ', ' + u.first_nm )as requestor_nm,
bl.billing_location_desc,
ph.ship_to_name,
dm.dispatch_method_name_desc as courier,
PH.po_last_updated_dt as last_updated_date,
(u1.last_nm + ', ' + u1.first_nm )as last_updated_by,
ph.right_grp_id,
c.line_id,
a.material_product_id,
a.parent_material_product_id
from avt_po_detail c
inner join avt_po_hdr ph on ph.order_num = c.order_num
left outer join avt_material_product AS a on a.material_product_id=c.material_product_id
INNER JOIN #entity_ids po on po.entity_id = c.order_num --or #select_pos = 0 )
LEFT OUTER JOIN dbo.ert_product AS pc ON pc.product_id = a.product_id
left outer join ert_product_type as pt on pt.product_type_cd = pc.product_type_cd
LEFT OUTER JOIN dbo.ert_product_xref as xref on xref.product_id = a.parent_product_id AND xref.version_id = 1
left outer join avt_order_status AS OS ON OS.order_status_cd = PH.order_status_cd
left outer join avt_licensee l on l.lic_id = ph.lic_id
left outer join avt_po_from pf on pf.material_product_id = a.material_product_id and pf.line_id = c.line_id
left outer join avt_po_audio_detail pad on pad.line_id = c.line_id
left outer join avt_materials_trans_material MTM on mtm.sequence_no = c.video_seq_no
left outer join avt_user u on u.userid = ph.requestor_id
left outer join avt_user u1 on u1.userid = ph.po_last_updated_by
left outer join avt_billing_location bl on bl.billing_location_cd = PH.billing_location_cd
left outer join avt_dispatch_method_name dm on dm.dispatch_method_name_id = PH.dispatch_method_name_id
left outer join avt_right_group arg on arg.right_grp_id = ph.right_grp_id
left outer join #prod product on product.product_id=a.product_id and (product.version_id=c.dist_version_id OR product.version_id = 1)
where (pf.from_desc in ( select from_desc from #from_desc ) or #select_form = 0)
and (case WHEN c.dist_version_id IS NULL THEN c.UCS_prod_no
ELSE
CASE
WHEN isnull(pt.version_control_ind, 'N') = 'Y' THEN xref.rss_prod_no
WHEN
isnull(pt.version_control_ind, 'N') = 'N' AND
xref.rss_prod_no IS NULL AND
xref.rss_film_id IS NULL THEN xref.rss_titleid
WHEN
isnull(pt.version_control_ind, 'N') = 'N' AND
xref.rss_prod_no IS NOT NULL AND
xref.rss_film_id IS NULL THEN substring(pc.glacct, 24, 5) -- ASR 3290687
WHEN isnull(pt.version_control_ind, 'N') = 'N' AND xref.rss_film_id IS NOT NULL THEN isnull(substring(pc.glacct, 24, 5), -- ASR 3290687
(
SELECT substring(dbo.ert_product.glacct, 24, 5) -- ASR 3290687
FROM dbo.ert_product
WHERE dbo.ert_product.product_id = a.parent_product_id
))
END
END in (select prod_no from #prod_no) or #prod_no = 0)
I have a table A, B, C which has many columns. Now I would like to join with some case condition, but the following query failed
SELECT
QTY AS QUANTITY,
IS_UT AS IS_UT,
G_ACC_ID AS G_ACC_ID,
CR_AND_F_ID AS CR_AND_F_ID,
TRNG_ENRMT_ID AS TRNG_ENRMT_ID
TRAINING_COURSE_ID AS TRAINING_COURSE_ID
(CASE WHEN A.CR_AND_F_ID IS NULL OR A.CR_AND_F_ID = 0 THEN
SELECT
A.CR_AND_F_ID AS CR_AND_F_ID,
A.IS_UT AS IS_UT,
A.G_ACC_ID AS G_ACC_ID,
A.CR_AND_F_ID AS CR_AND_F_ID,
A.TRNG_ENRMT_ID AS TRNG_ENRMT_ID,
t4.TRAINING_COURSE_ID AS TRAINING_COURSE_ID
FROM T_E_P_LD
LEFT OUTER JOIN T_TRAINING t2
ON t2.TRAINING_ID = A.TRNG_ENRMT_ID
LEFT OUTER JOIN TR_ENROL t3
ON t2.TR_ID = t3.TR_ID
LEFT OUTER JOIN TR_CLASS t4
ON t3.TR_CLASS_ID = t4.TR_CLASS_ID
ELSE A.CR_AND_F_ID
END) AS TEST
FROM T_E_P_LD
I also tried to write Inline view but that too failed.
Please let me know how to approach.
Thanks.
We have created a report using SAP BO 3.1 SP5 for DB2 9.7 using client 9. Now we have upgraded the DB2 from 9.7 to 10.5. So using client 10 z/OS we have connected the SAP BO, but the generated query differs from what we get while connecting with client 9. Can anyone explain the issue?
Is it not possible to connect DB10.5 with client 10 z/OS? Is it the generated query have difference by this version difference?
Sorry guys, I am a newbie to BO. Maybe this is a silly question, but we have to answer for this.
Thanks in advance.
I have attached the generated query below:
Generated By Client 9:
SELECT
RPT_EMPLOYEE.FIRST_NAME,
RPT_EMPLOYEE.MIDDLE_NAME,
RPT_EMPLOYEE.LAST_NAME,
GENDER.ITEM_VALUE,
RPT_EMPLOYEE.DEPARTMENT,
SUB_DEPARTMENT.ITEM_VALUE,
RPT_EMPLOYEE.JOB_TITLE,
WD_JOB_GROUP.GROUP_NO,
WD_GLBT_LIST.DESCRIPTION,
WD_EEO_LIST.DESCRIPTION,
CASE WHEN RPT_EMPLOYEE.HAS_DISABILITY = -1 THEN ' ' ELSE DISABILITY.ITEM_VALUE END,
RPT_NEW_HIRE.HIRE_DT,
PART_OF_TEN.ITEM_VALUE,
WD_CLUB.LONG_NAME,
CASE WHEN RPT_EMPLOYEE.IS_ADDED <> 0 THEN HIRE_SOURCE.ITEM_VALUE ELSE '' END || '' ||
CASE WHEN RPT_EMPLOYEE.PROMOTED <> 0 THEN CASE WHEN RPT_EMPLOYEE.IS_ADDED <> 0 THEN ',' ELSE '' END || PROMOTED.ITEM_VALUE ELSE '' END || '' ||
CASE WHEN RPT_EMPLOYEE.IS_DEACTIVATED <> 0 THEN CASE WHEN RPT_EMPLOYEE.IS_ADDED <> 0 THEN ',' ELSE '' END || DEACTIVATED.ITEM_VALUE || '-' ELSE '' END || '' ||
CASE WHEN RPT_EMPLOYEE.IS_DEACTIVATED <> 0 THEN DEACTIVATION_REASON.ITEM_VALUE ELSE ' ' END
FROM
WD_KEY_LIST DEACTIVATION_REASON
LEFT OUTER JOIN RPT_DEACTIVATION ON RPT_DEACTIVATION.REASON_CODE=DEACTIVATION_REASON.ITEM_ID AND DEACTIVATION_REASON.KEY_ID=7
RIGHT OUTER JOIN RPT_EMPLOYEE ON RPT_DEACTIVATION.SURVEY_ID=RPT_EMPLOYEE.SURVEY_ID AND RPT_DEACTIVATION.CLUB_ID=RPT_EMPLOYEE.CLUB_ID AND RPT_EMPLOYEE.EMP_ID=RPT_DEACTIVATION.EMP_ID
LEFT OUTER JOIN RPT_NEW_HIRE ON RPT_EMPLOYEE.SURVEY_ID=RPT_NEW_HIRE.SURVEY_ID AND RPT_EMPLOYEE.CLUB_ID=RPT_NEW_HIRE.CLUB_ID AND RPT_EMPLOYEE.EMP_ID=RPT_NEW_HIRE.EMP_ID
LEFT OUTER JOIN WD_CLUB ON RPT_EMPLOYEE.CLUB_ID=WD_CLUB.CLUB_ID AND WD_CLUB.SURVEY_ID=RPT_EMPLOYEE.SURVEY_ID
LEFT OUTER JOIN WD_GLBT_LIST ON RPT_EMPLOYEE.SURVEY_ID=WD_GLBT_LIST.SURVEY_ID AND WD_GLBT_LIST.GLBT_ID=RPT_EMPLOYEE.GLBT_ID
LEFT OUTER JOIN WD_EEO_LIST ON RPT_EMPLOYEE.SURVEY_ID=WD_EEO_LIST.SURVEY_ID AND WD_EEO_LIST.EEO_ID=RPT_EMPLOYEE.EEO_ID
LEFT OUTER JOIN WD_JOB_GROUP ON RPT_EMPLOYEE.SURVEY_ID=WD_JOB_GROUP.SURVEY_ID AND WD_JOB_GROUP.JOB_GROUP_ID=RPT_EMPLOYEE.JOB_GROUP_ID
LEFT OUTER JOIN WD_KEY_LIST GENDER ON GENDER.ITEM_ID=RPT_EMPLOYEE.GENDER_ID AND GENDER.KEY_ID = 1
LEFT OUTER JOIN WD_KEY_LIST PART_OF_TEN ON PART_OF_TEN.ITEM_ID=RPT_EMPLOYEE.IS_TOP_10 AND PART_OF_TEN.KEY_ID=20
LEFT OUTER JOIN WD_KEY_LIST HIRE_SOURCE ON RPT_EMPLOYEE.IS_ADDED=HIRE_SOURCE.ITEM_ID and HIRE_SOURCE.KEY_ID=12
LEFT OUTER JOIN WD_KEY_LIST PROMOTED ON RPT_EMPLOYEE.PROMOTED=PROMOTED.ITEM_ID AND PROMOTED.KEY_ID=13
LEFT OUTER JOIN WD_KEY_LIST DISABILITY ON RPT_EMPLOYEE.HAS_DISABILITY=DISABILITY.ITEM_ID and DISABILITY.KEY_ID=4
LEFT OUTER JOIN WD_KEY_LIST DEACTIVATED ON RPT_EMPLOYEE.IS_DEACTIVATED=DEACTIVATED.ITEM_ID AND DEACTIVATED.KEY_ID=14
LEFT OUTER JOIN WD_SURVEY SURVEY ON RPT_EMPLOYEE.SURVEY_ID=SURVEY.SURVEY_ID
LEFT OUTER JOIN WD_KEY_LIST SUB_DEPARTMENT ON SUB_DEPARTMENT.ITEM_ID=RPT_EMPLOYEE.SUB_DEPARTMENT AND SUB_DEPARTMENT.KEY_ID = 21
WHERE
(
SURVEY.SHORT_NAME IN ('*******')
AND
SUB_DEPARTMENT.ITEM_VALUE = '*****'
)
Generated By Client 10 z/OS:
SELECT
RPT_EMPLOYEE.FIRST_NAME,
RPT_EMPLOYEE.MIDDLE_NAME,
RPT_EMPLOYEE.LAST_NAME,
GENDER.ITEM_VALUE,
RPT_EMPLOYEE.DEPARTMENT,
SUB_DEPARTMENT.ITEM_VALUE,
RPT_EMPLOYEE.JOB_TITLE,
WD_JOB_GROUP.GROUP_NO,
WD_GLBT_LIST.DESCRIPTION,
WD_EEO_LIST.DESCRIPTION,
CASE WHEN RPT_EMPLOYEE.HAS_DISABILITY = -1 THEN ' ' ELSE DISABILITY.ITEM_VALUE END,
RPT_NEW_HIRE.HIRE_DT,
PART_OF_TEN.ITEM_VALUE,
WD_CLUB.LONG_NAME,
CASE WHEN RPT_EMPLOYEE.IS_ADDED <> 0 THEN HIRE_SOURCE.ITEM_VALUE ELSE '' END || '' ||
CASE WHEN RPT_EMPLOYEE.PROMOTED <> 0 THEN CASE WHEN RPT_EMPLOYEE.IS_ADDED <> 0 THEN ',' ELSE '' END || PROMOTED.ITEM_VALUE ELSE '' END || '' ||
CASE WHEN RPT_EMPLOYEE.IS_DEACTIVATED <> 0 THEN CASE WHEN RPT_EMPLOYEE.IS_ADDED <> 0 THEN ',' ELSE '' END || DEACTIVATED.ITEM_VALUE || '-' ELSE '' END || '' ||
CASE WHEN RPT_EMPLOYEE.IS_DEACTIVATED <> 0 THEN DEACTIVATION_REASON.ITEM_VALUE ELSE ' ' END
FROM
WD_KEY_LIST SUB_DEPARTMENT
LEFT OUTER JOIN RPT_EMPLOYEE ON SUB_DEPARTMENT.ITEM_ID=RPT_EMPLOYEE.SUB_DEPARTMENT AND SUB_DEPARTMENT.KEY_ID = 21
RIGHT OUTER JOIN RPT_NEW_HIRE ON RPT_EMPLOYEE.SURVEY_ID=RPT_NEW_HIRE.SURVEY_ID AND RPT_EMPLOYEE.CLUB_ID=RPT_NEW_HIRE.CLUB_ID AND RPT_EMPLOYEE.EMP_ID=RPT_NEW_HIRE.EMP_ID
RIGHT OUTER JOIN WD_CLUB ON RPT_EMPLOYEE.CLUB_ID=WD_CLUB.CLUB_ID AND WD_CLUB.SURVEY_ID=RPT_EMPLOYEE.SURVEY_ID
RIGHT OUTER JOIN WD_GLBT_LIST ON RPT_EMPLOYEE.SURVEY_ID=WD_GLBT_LIST.SURVEY_ID AND WD_GLBT_LIST.GLBT_ID=RPT_EMPLOYEE.GLBT_ID
RIGHT OUTER JOIN WD_EEO_LIST ON RPT_EMPLOYEE.SURVEY_ID=WD_EEO_LIST.SURVEY_ID AND WD_EEO_LIST.EEO_ID=RPT_EMPLOYEE.EEO_ID RIGHT
OUTER JOIN WD_JOB_GROUP ON RPT_EMPLOYEE.SURVEY_ID=WD_JOB_GROUP.SURVEY_ID AND WD_JOB_GROUP.JOB_GROUP_ID=RPT_EMPLOYEE.JOB_GROUP_ID
RIGHT OUTER JOIN WD_KEY_LIST GENDER ON GENDER.ITEM_ID=RPT_EMPLOYEE.GENDER_ID AND GENDER.KEY_ID = 1
RIGHT OUTER JOIN WD_KEY_LIST PART_OF_TEN ON PART_OF_TEN.ITEM_ID=RPT_EMPLOYEE.IS_TOP_10 AND PART_OF_TEN.KEY_ID=20
RIGHT OUTER JOIN WD_KEY_LIST HIRE_SOURCE ON RPT_EMPLOYEE.IS_ADDED=HIRE_SOURCE.ITEM_ID and HIRE_SOURCE.KEY_ID=12
RIGHT OUTER JOIN WD_KEY_LIST PROMOTED ON RPT_EMPLOYEE.PROMOTED=PROMOTED.ITEM_ID AND PROMOTED.KEY_ID=13
RIGHT OUTER JOIN WD_KEY_LIST DISABILITY ON RPT_EMPLOYEE.HAS_DISABILITY=DISABILITY.ITEM_ID and DISABILITY.KEY_ID=4
RIGHT OUTER JOIN RPT_DEACTIVATION ON RPT_DEACTIVATION.SURVEY_ID=RPT_EMPLOYEE.SURVEY_ID AND RPT_DEACTIVATION.CLUB_ID=RPT_EMPLOYEE.CLUB_ID AND RPT_EMPLOYEE.EMP_ID=RPT_DEACTIVATION.EMP_ID
LEFT OUTER JOIN WD_KEY_LIST DEACTIVATION_REASON ON RPT_DEACTIVATION.REASON_CODE=DEACTIVATION_REASON.ITEM_ID AND DEACTIVATION_REASON.KEY_ID=7
RIGHT OUTER JOIN WD_KEY_LIST DEACTIVATED ON RPT_EMPLOYEE.IS_DEACTIVATED=DEACTIVATED.ITEM_ID AND DEACTIVATED.KEY_ID=14
RIGHT OUTER JOIN WD_SURVEY SURVEY ON RPT_EMPLOYEE.SURVEY_ID=SURVEY.SURVEY_ID
WHERE
( SURVEY.CREATED_BY=SURVEY.IS_CREATED )
AND
(
SURVEY.SHORT_NAME IN ('*******')
AND
SUB_DEPARTMENT.ITEM_VALUE = '*******'
)
The differences are instead of LEFT JOIN, RIGHT JOIN have added in client 10 z/OS and the additional condition SURVEY.CREATED_BY=SURVEY.IS_CREATED added in client 10 z/OS.
I have a query that is returning the exchange rate value set up in our system. Not every order will have an exchange rate (currate.currentrate) so it is returning null values.
Can I get it to return 1 instead of null?
Something like an if statement maybe:
if isnull(currate.currentrate) then 1 else currate.currentrate
Here is my query below. I greatly appreciate all your help!
SELECT orderhed.ordernum, orderhed.orderdate, currrate.currencycode, currrate.currentrate
FROM orderhed LEFT OUTER JOIN
currrate ON orderhed.company = currrate.company AND orderhed.orderdate = currrate.effectivedate
You can use a CASE statement.
SELECT
CASE WHEN currate.currentrate IS NULL THEN 1 ELSE currate.currentrate END
FROM ...
You can use COALESCE:
SELECT orderhed.ordernum,
orderhed.orderdate,
currrate.currencycode,
coalesce(currrate.currentrate, 1) as currentrate
FROM orderhed
LEFT OUTER JOIN currrate
ON orderhed.company = currrate.company
AND orderhed.orderdate = currrate.effectivedate
Or even IsNull():
SELECT orderhed.ordernum,
orderhed.orderdate,
currrate.currencycode,
IsNull(currrate.currentrate, 1) as currentrate
FROM orderhed
LEFT OUTER JOIN currrate
ON orderhed.company = currrate.company
AND orderhed.orderdate = currrate.effectivedate
Here is an article to help decide between COALESCE and IsNull:
http://www.mssqltips.com/sqlservertip/2689/deciding-between-coalesce-and-isnull-in-sql-server/
SELECT
ISNULL(currate.currentrate, 1)
FROM ...
is less verbose than the winning answer and does the same thing
https://msdn.microsoft.com/en-us/library/ms184325.aspx
a) If you want 0 when value is null
SELECT isnull(PartNum,0) AS PartNumber, PartID
FROM Part
b) If you want 0 when value is null and otherwise 1
SELECT
(CASE
WHEN PartNum IS NULL THEN 0
ELSE 1
END) AS PartNumber,
PartID
FROM Part
SELECT orderhed.ordernum, orderhed.orderdate, currrate.currencycode,
case(currrate.currentrate) when null then 1 else currrate.currentrate end
FROM orderhed LEFT OUTER JOIN currrate ON orderhed.company = currrate.company AND orderhed.orderdate = currrate.effectivedate
try like below...
CASE
WHEN currate.currentrate is null THEN 1
ELSE currate.currentrate
END as currentrate