unable to fetch department column value in pl-sql developer - plsql
what to add so that i can fetch department column value
WHERE C1.CODE_ID(+) = ALLRESULT.BANK_CODE
AND C1.CODE_CATEGORY = 'BANK'
AND C2.CODE_CATEGORY(+) = ALLRESULT.DEPT_ID
AND C2.CODE_CATEGORY(+) = 'DEPARTMENT'
AND C3.CODE_ID(+) = ALLRESULT.PAY_CURRENCY
AND C3.CODE_CATEGORY(+) = 'CURRENCY' ORDER BY ALLRESULT.P_ID,ALLRESULT.PAY_CURRENCY
Reformatted your JPG query version to some more readable format:
SELECT
ALLRESULT.P_ID EMP_ID
,ALLRESULT.EMP_NAME
,NVL(C2.CODE_CATEGORY, 'N/A') DEPARTMENT
,ALLRESULT.BANK_CODE
,NVL(C1.CODE_DESC, 'N/A') BANK_NAME
,ALLRESULT.BANK_ACCT
,ALLRESULT.VALUE
,NVL(C3.CODE_DESC, 'N/A') CURRENCY
,ALLRESULT.ID
FROM
HRIS_CODE_SETUP C1
,HRIS_CODE_SETUP C2
,HRIS_CODE_SETUP C3
,(
SELECT
NVL(PAY_EMP.BANK_ACCNAME, 'NA') AS EMP_NAME
,PAY_EMP.DEPARTMENT DEPT_ID
,NVL(PAY_EMP.BANKNAME, 'NA') BANK_CODE
,NVL(PAY_EMP.BANK_ACCT,'NA') BANK_ACCT
,PAY_EMP.ID
,GROUPSUM.*
FROM
PAY_EMP
,(
SELECT
RESULT.P_ID
,RESULT.COM_ID
-- This can be replaced by more readable CASE statement presented below
-- ,SUM(
-- DECODE(PAYTYPE
-- ,0,LTAROUND((RESULT.VALUE*B.RATE),C.PROUND,C.ACCURACY)
-- ,1,LTAROUND((RESULT.VALUE*B.RATE),C.PROUND,C.ACCURACY)
-- ,2,LTAROUND((RESULT.VALUE*B.RATE),C.PROUND,C.ACCURACY)
-- ,3,LTAROUND((RESULT.VALUE*B.RATE),C.PROUND,C.ACCURACY)
-- ,4,LTAROUND((RESULT.VALUE*B.RATE),C.PROUND,C.ACCURACY)
-- ,5,LTAROUND((RESULT.VALUE*B.RATE),C.PROUND,C.ACCURACY)
-- ,6,LTAROUND((RESULT.VALUE*B.RATE),C.PROUND,C.ACCURACY)
-- ,7,LTAROUND((RESULT.VALUE*B.RATE),C.PROUND,C.ACCURACY)
-- ,12,LTAROUND((RESULT.VALUE*B.RATE),C.PROUND,C.ACCURACY)
-- ,8,-1*LTAROUND((RESULT.VALUE*B.RATE),C.PROUND,C.ACCURACY)
-- ,9,-1*LTAROUND((RESULT.VALUE*B.RATE),C.PROUND,C.ACCURACY)
-- ,10,-1*LTAROUND((RESULT.VALUE*B.RATE),C.PROUND,C.ACCURACY)
-- ,11,-1*LTAROUND((RESULT.VALUE*B.RATE),C.PROUND,C.ACCURACY)
-- )
-- ) VALUE
SUM(LTAROUND((RESULT.VALUE*B.RATE),C.PROUND,C.ACCURACY) * CASE WHEN PAYTYPE IN (0,1,2,3,4,5,6,7,12) THEN 1 ELSE -1 END) VALUE
-- the SUM above is way easier to read and maintain, isn't it?
,RESULT.PAY_CURRENCY
FROM
PAY_RESULT RESULT
,PAY_CURRENCY B
,V_APPLYITEM2 C
,EPAY_COMPANY D
WHERE
B.COMID = RESULT.COMID
AND B.BATCH = RESULT.BATCH
AND RESULT.BATCH IN ()
AND B.PAYYEAR=RESULT.PAYYEAR
AND B.PAYMONTH = RESULT.PAYMONTH
AND C.PAYITEM_ID=RESULT.PAYITEM
AND C.COMP_ID = RESULT.COMID
AND RESULT.COMID = D.PEID
AND RESULT.PAY_CURRENCY = B.C1
AND B.C2 = D.PAY_CURRENCY AND (
(
RESULT.PAYTYPE IN (0,1,2,3,4,5,6,7,12)
AND RESULT.PAY_MODE = 1
AND RESULT.PID IS NOT NULL
OR LOWER(RESULT.PAYITEM) IN ('BASICPAY','BACKPAY')
)
OR (RESULT.PAYTYPE IN (8,9,10,11))
)
AND RESULT.COMID = 'P00000001882-001-SG'
AND RESULT.PAYYEAR = 2015
AND RESULT.PAYMONTH = 9
GROUP BY
RESULT.PID
,RESULT.COMID
,RESULT.PAY_CURRENCY
) GROUPSUM
WHERE
PAY_EMP.PAY_METHOD = 1
AND PAY_EMP.P_ID(+) = GROUPSUM.P_ID
AND PAY_EMP.COMPANY(+) = GROUPSUM.COMID
) ALLRESULT
WHERE
C1.CODE_ID(+) = ALLRESULT.BANK_CODE
AND C1.CODE_CATEGORY = 'BANK'
AND C2.CODE_CATEGORY(+) = ALLRESULT.DEPT_ID
AND C2.CODE_CATEGORY(+) = 'DEPARTMENT'
AND C3.CODE_ID(+) = ALLRESULT.PAY_CURRENCY
AND C3.CODE_CATEGORY(+) = 'CURRENCY'
ORDER BY
ALLRESULT.P_ID
,ALLRESULT.PAY_CURRENCY
Now, please answer, what do you mean by what to add so that i can fetch department column value?
If you mean the below:
SELECT
...
,NVL(C2.CODE_CATEGORY, 'N/A') DEPARTMENT
...
FROM
...
WHERE
...
AND C2.CODE_CATEGORY(+) = ALLRESULT.DEPT_ID
AND C2.CODE_CATEGORY(+) = 'DEPARTMENT'
...
then you must've noticed, you've got OUTER JOIN over here, thus ALLRESULT.DEPT_ID does not have to be in C2 table in CODE_CATEGORY column (ALLRESULT has particular DEPT_ID while C2 doesn't).
Related
Create dynamic parent-child hierarchy?
do you know a efficient method to generate a parent-child hierarchy from a table without parent node IDs. The parent child relationship should be switchable. All of this should be done in ABAP. example data: Color producer weight Airplane(key) green CompanyA 330 A350 green CompanyA 222 A320 green CompanyB 222 B450 yellow CompanyA 330 H450 I want generate a child parent relationship based on this rows: producer weight Airplane and ignore color. Then i will change it and use: Color weight Airplane and ignore producer At the end i need a result looks like for "producer weight Airplane" CompanyA CompanyB 330 222 222 A350 A320 B450 H450 in a internal table it should look like this at the end ID attribute value H_level parentID 1 producer CompanyA 1 2 weight 330 2 1 3 airplane A350 3 2 ..... does anyone have a good efficient idea? Thanks a lot.
Here is a simple tree-building algorithm that does what you need. Note that this algorithm is not optimized in terms of performance. If you manage large amounts of data, you may want to revise some aspects, for example use SORTED or HASHED rather than STANDARD tables to improve the lookup READ TABLEs. The algorithm is also not refactored for optimum code style. For example, clean code suggests we may want to extract a couple of methods to improve readability. CLASS hierarchy_builder DEFINITION PUBLIC FINAL CREATE PUBLIC. PUBLIC SECTION. TYPES: BEGIN OF ts_input, color TYPE string, producer TYPE string, weight TYPE string, airplane TYPE string, END OF ts_input. TYPES tt_input TYPE STANDARD TABLE OF ts_input WITH EMPTY KEY. TYPES tt_attributes TYPE string_table. TYPES: BEGIN OF ts_output, id TYPE string, attribute TYPE string, value TYPE string, level TYPE i, parent_id TYPE string, END OF ts_output. TYPES tt_output TYPE STANDARD TABLE OF ts_output WITH EMPTY KEY. CLASS-METHODS build_hierarchy IMPORTING it_data TYPE tt_input it_hierarchy TYPE tt_attributes RETURNING VALUE(rt_result) TYPE tt_output. ENDCLASS. CLASS hierarchy_builder IMPLEMENTATION. METHOD build_hierarchy. DATA(lv_parent_attribute) = ``. DATA(lv_next_id) = 1. LOOP AT it_hierarchy INTO DATA(lv_child_attribute). DATA(lv_level) = sy-tabix. LOOP AT it_data INTO DATA(ls_data). DATA(lv_parent_id) = ``. ASSIGN COMPONENT lv_child_attribute OF STRUCTURE ls_data TO FIELD-SYMBOL(<lv_child_value>). IF lv_parent_attribute IS NOT INITIAL. ASSIGN COMPONENT lv_parent_attribute OF STRUCTURE ls_data TO FIELD-SYMBOL(<lv_parent_value>). READ TABLE rt_result INTO DATA(ls_parent) WITH KEY attribute = lv_parent_attribute value = <lv_parent_value>. lv_parent_id = ls_parent-id. ENDIF. READ TABLE rt_result TRANSPORTING NO FIELDS WITH KEY attribute = lv_child_attribute value = <lv_child_value> parent_id = lv_parent_id. IF sy-subrc <> 0. INSERT VALUE #( id = |{ lv_next_id }| attribute = lv_child_attribute value = <lv_child_value> level = lv_level parent_id = lv_parent_id ) INTO TABLE rt_result. lv_next_id += 1. ENDIF. ENDLOOP. lv_parent_attribute = lv_child_attribute. ENDLOOP. ENDMETHOD. ENDCLASS. Verified with the following unit test. It represents the example you gave: CLASS ltc_unit_tests DEFINITION FOR TESTING RISK LEVEL HARMLESS DURATION SHORT. PRIVATE SECTION. METHODS builds_example FOR TESTING. ENDCLASS. CLASS ltc_unit_tests IMPLEMENTATION. METHOD builds_example. DATA(lt_data) = VALUE hierarchy_builder=>tt_input( ( color = 'green' producer = 'CompanyA' weight = '330' airplane = 'A350' ) ( color = 'green' producer = 'CompanyA' weight = '222' airplane = 'A320' ) ( color = 'green' producer = 'CompanyB' weight = '222' airplane = 'B450' ) ( color = 'yellow' producer = 'CompanyA' weight = '330' airplane = 'H450' ) ). DATA(lt_hierarchy) = VALUE hierarchy_builder=>tt_attributes( ( `PRODUCER` ) ( `WEIGHT` ) ( `AIRPLANE` ) ). DATA(lt_result) = hierarchy_builder=>build_hierarchy( it_data = lt_data it_hierarchy = lt_hierarchy ). DATA(lt_expected) = VALUE hierarchy_builder=>tt_output( ( id = '1' attribute = 'PRODUCER' value = 'CompanyA' level = 1 parent_id = '' ) ( id = '2' attribute = 'PRODUCER' value = 'CompanyB' level = 1 parent_id = '' ) ( id = '3' attribute = 'WEIGHT' value = '330' level = 2 parent_id = '1' ) ( id = '4' attribute = 'WEIGHT' value = '222' level = 2 parent_id = '1' ) ( id = '5' attribute = 'WEIGHT' value = '222' level = 2 parent_id = '2' ) ( id = '6' attribute = 'AIRPLANE' value = 'A350' level = 3 parent_id = '3' ) ( id = '7' attribute = 'AIRPLANE' value = 'A320' level = 3 parent_id = '4' ) ( id = '8' attribute = 'AIRPLANE' value = 'B450' level = 3 parent_id = '4' ) ( id = '9' attribute = 'AIRPLANE' value = 'H450' level = 3 parent_id = '3' ) ). cl_abap_unit_assert=>assert_equals( act = lt_result exp = lt_expected ). ENDMETHOD. ENDCLASS.
Update statement not providing correct result in a BTEQ after Teradata 15 upgradation
An update statement is not working when executed inside BTEQ after Teradata 15 upgrade. The statement gives required output if executed from SQL Assistant manually. The statement is having a simple code. ---Update statement UPDATE MAIN2 FROM PSE_BIX_ABT.TP_ABT_DT_MOB_SBSCR_MAIN2 AS MAIN2, ( SELECT DISTINCT SBSCR_ID, ( SELECT DT FROM PSE_BIX_ABT.TP_ABT_DT_MOB_SBSCR_DT WHERE ID = 1 ) AS SNAP_DT, SRVC_PRVDR_ID AS PORTED_IN_DNR_SP_ID, SRVC_PRVDR_NM AS PORTED_IN_DNR_SP_NM, OREPLACE(OREPLACE(SRVC_PRVDR_DESC,'[Fixed]',''),'[Mobile]','') AS PORTED_IN_DNR_SP_DESC, PORT_STRT_DT AS PORTED_IN_DT, CASE WHEN PORTED_IN_DT = SNAP_DT THEN 1 ELSE 0 END AS PORTED_IN_CNT FROM PSE_BIX_SL_VW.V_F_PORTED_SBSCRS AS PORTED_SBSCR INNER JOIN PSE_BIX_SL_VW.V_SRVC_PRVDR SRVC ON PORTED_SBSCR.DNR_SRVC_PRVDR_ID = SRVC.SRVC_PRVDR_ID WHERE PORT_IN_FL = 1 AND SBSCR_ID <> -1 AND PORT_STRT_DT <= ( SELECT DT FROM PSE_BIX_ABT.TP_ABT_DT_MOB_SBSCR_DT WHERE ID = 1 ) AND (SBSCR_ID,PORT_STRT_DT) IN (SEL SBSCR_ID,MAX(PORT_STRT_DT) FROM PSE_BIX_SL_VW.V_F_PORTED_SBSCRS WHERE PORT_STRT_DT <= ( SELECT DT FROM PSE_BIX_ABT.TP_ABT_DT_MOB_SBSCR_DT WHERE ID = 1 ) GROUP BY 1 ) ) D_PORT_IN SET PORTED_IN_DNR_SP_ID = D_PORT_IN.PORTED_IN_DNR_SP_ID, PORTED_IN_DNR_SP_NM = D_PORT_IN.PORTED_IN_DNR_SP_NM, PORTED_IN_DNR_SP_DESC = D_PORT_IN.PORTED_IN_DNR_SP_DESC, PORTED_IN_DT = D_PORT_IN.PORTED_IN_DT, PORTED_IN_CNT = D_PORT_IN.PORTED_IN_CNT WHERE MAIN2.SBSCR_ID = D_PORT_IN.SBSCR_ID AND MAIN2.SNAP_DT = D_PORT_IN.SNAP_DT; Kindly help on the same. Current Teradata 15.10.04.05 (Upgraded from 14)
i have several errors of PLS-00049
cursor is there to check for eligibility in student. however i cant even compile the script as i keep getting several of error: [Error] PLS-00049 (395: 46): PLS-00049: bad bind variable 'SORLCUR_PIDM'. CURSOR C_ENSP IS SELECT SFRENSP_ESTS_CODE ,STVESTS_DESC FROM STVESTS,SFRENSP WHERE SFRENSP_PIDM = :Sorlcur_PIDM AND SFRENSP_TERM_CODE = :sorlcur_TERM_CODE AND SFRENSP_KEY_SEQNO = :SORLCUR_KEY_SEQNO AND STVESTS_CODE = SFRENSP_ESTS_CODE; CURSOR C_ENSP_SECOND IS SELECT 'Y' FROM SORLCUR, SFRENSP WHERE SORLCUR_PIDM = :Sorlcur_PIDM and SORLCUR_PIDM = SFRENSP_PIDM AND sorlcur_TERM_CODE = :sorlcur_TERM_CODE AND sorlcur_TERM_CODE = SFRENSP_TERM_CODE AND SORLCUR_CURRENT_CDE = 'Y' AND SORLCUR_CACT_CODE = 'ACTIVE' AND SORLCUR_LMOD_CODE = 'LEARNER' AND SORLCUR_PRIORITY_NO = 2 AND SFRENSP_KEY_SEQNO = SORLCUR_KEY_SEQNO AND SFRENSP_ESTS_CODE NOT IN ( 'W', 'X' , 'Z'); v_ENSP_SECOND VARCHAR2(1); CURSOR c_cohort IS SELECT chrt.sgrchrt_chrt_code FROM sgrchrt chrt WHERE sgrchrt_pidm = :sorlcur_pidm AND NVL(chrt.sgrchrt_stsp_key_sequence,99999999) = :sorlcur_key_seqno AND NVL(sgrchrt_term_code_eff,'~') = :stvterm_term_code; CURSOR c_resd IS SELECT sgbstdn_resd_code,stvresd_desc FROM sgbstdn,stvresd WHERE SGBSTDN_PIDM = :KEY_BLOCK.PIDM AND SGBSTDN_TERM_CODE_EFF = :KEY_BLOCK.STVTERM_TERM_CODE AND stvresd_code = sgbstdn_resd_code; cursor c1 is select skricas_cas_number, skricas_cas_status from skricas a where a.SKRICAS_PIDM = :key_block.PIDM and exists (select 'x' from sorlcur where sorlcur_pidm = skricas_pidm and sorlcur_seqno = skricas_lcur_seqno and sorlcur_term_code = :key_block.stvterm_term_code) and skricas_cas_status = 'USED' and skricas_cas_exp_date = (select max(a1.skricas_cas_exp_date) from skricas a1 where a1.skricas_pidm = a.skricas_pidm and a1.skricas_cas_status = a.skricas_cas_status ); -- TT06987 start CURSOR get_JSopt_in IS SELECT szrasop_opt_in FROM szrasop WHERE szrasop_pidm = :sorlcur.Sorlcur_PIDM AND szrasop_term_code = :sorlcur.Sorlcur_TERM_CODE; --- v_elig VARCHAR2(1); CURSOR get_max_szrolps IS SELECT MAX( SZROLPS_SEQ_NO) FROM SATURN.SZROLPS WHERE SZROLPS_PIDM = :sorlcur.Sorlcur_PIDM AND SZROLPS_TERM_CODE = :sorlcur.Sorlcur_TERM_CODE; v_max_stage szrolps.szrolps_seq_no%TYPE; -- TT06987 end BEGIN IF :sorlcur_program IS NOT NULL THEN :SORLCUR_SMRPCMT_TEXT := F_UOG_GET_COMMENT(:sorlcur_program, :sorlcur_term_code, 'PRGTIT'); OPEN C_ENSP; FETCH C_ENSP INTO :SFRENSP_ESTS_CODE, :ESTS_DESCRIPTION; CLOSE C_ENSP; OPEN C_ENSP_SECOND; FETCH C_ENSP_SECOND INTO v_ENSP_SECOND ; IF C_ENSP_SECOND%NOTFOUND THEN :secondary_prog := 'N'; ELSE :secondary_prog := 'Y'; END IF; CLOSE C_ENSP_SECOND; END IF; OPEN c_cohort; FETCH c_cohort INTO :ENTRY_COHORT; CLOSE c_cohort; OPEN c_resd; FETCH c_resd INTO :sgbstdn_resd_code,:stvresd_desc; CLOSE c_resd; SELECT STVATTS_DESC INTO :mode_desc FROM STVATTS WHERE :sorlcur_mode_code = STVATTS.STVATTS_CODE AND STVATTS.STVATTS_CODE LIKE 'M%'; select stvstyp_desc into :sorlcur.styp_desc from stvstyp where :SORLCUR_STYP_CODE = stvstyp_code; :sorlcur.SITE_DESC := gb_stvsite.f_get_description(:SORLCUR_SITE_CODE); get_last_section_completed; -- added by Mary for 5095 open c1; fetch c1 into :cas_number, :cas_status; close c1; -- TT06987 start -- Student eligible?? IF hwzkimag.F_eligible_js (:sorlcur.Sorlcur_PIDM, :sorlcur.sorlcur_TERM_CODE) THEN :optin_elig := 'Y'; END IF; --message(:optin_elig);message(:optin_elig); IF NVL(:optin_elig,'XXX') = 'Y' THEN OPEN get_max_szrolps; FETCH get_max_szrolps INTO v_max_stage; CLOSE get_max_szrolps; -- ELigible and got passed the opt in / opt out stage 7. IF v_max_stage >= 7 THEN OPEN get_JSopt_in; FETCH get_JSopt_in INTO :optin_yes; CLOSE get_JSopt_in; set_item_property('jsopt_message',visible,property_false); -- Eligible but has not passed opt in / opt out stage 7. ELSE :jsopt_message := 'OLR not complete'; set_item_property('jsopt_message',visible,property_true); END IF; END IF; -- TT06987 end
i've solved the problem. There were two errors in the below code. 1st is bad bind variable and 2nd is invalid identifier. CURSOR C_ENSP IS SELECT SFRENSP_ESTS_CODE ,STVESTS_DESC FROM STVESTS,SFRENSP WHERE SFRENSP_PIDM = :Sorlcur_PIDM AND SFRENSP_TERM_CODE = :sorlcur_TERM_CODE AND SFRENSP_KEY_SEQNO = :SORLCUR_KEY_SEQNO AND STVESTS_CODE = SFRENSP_ESTS_CODE; 1st - i took out the colons on the columns name (e.g. :sorlcur_pidm to just sorlcur_pidm) 2nd - the table name sorlcur wasnt defined as i was calling table columns from 3 different tables. -- The correct Syntax is below -- CURSOR C_ENSP IS SELECT SFRENSP_ESTS_CODE ,STVESTS_DESC FROM STVESTS,SFRENSP,SORLCUR WHERE SFRENSP_PIDM = Sorlcur_PIDM AND SFRENSP_TERM_CODE = sorlcur_TERM_CODE AND SFRENSP_KEY_SEQNO = SORLCUR_KEY_SEQNO AND STVESTS_CODE = SFRENSP_ESTS_CODE;
getting total number to specific patients in rdlc report
I have made an rdlc report of total number of patients in a day and it works fine. I have the total number of female male patients, but when the report binds it returns a total equal to the number of rows in my data. For example if I have 20 rows in my report then below when I print the count it returns the count in 20 rows. How can I get it to be in only 1 row? This is the query I'm using: SET ANSI_NULLS ON SET QUOTED_IDENTIFIER ON GO ALTER PROCEDURE [dbo].[Test_test_test]-- '2013/08/02' -- Add the parameters for the stored procedure here -- Add the parameters for the stored procedure here (#date VARCHAR(20)) AS BEGIN SET NOCOUNT ON; SET TRANSACTION ISOLATION LEVEL SERIALIZABLE; SELECT ( CASE WHEN OLD_NEW_PATIENT.OLD_NEW = 'new' THEN PATIENT_REF_MASTER.PAT_ID ELSE NULL END ) AS 'new', ( CASE WHEN OLD_NEW_PATIENT.OLD_NEW = 'old' THEN PATIENT_REF_MASTER.PAT_ID ELSE NULL END ) AS 'old', ---------------------------------------- ( CASE WHEN GENDER_MASTER.NAME1 = 'Female' THEN GENDER_MASTER.NAME1 ELSE NULL END ) AS 'Females', ( CASE WHEN GENDER_MASTER.NAME1 = 'Male' THEN GENDER_MASTER.NAME1 ELSE NULL END ) AS 'Males', ------------------------------------- CONVERT(VARCHAR, PATIENT_REF_MASTER.CREATION_DATE, 105) AS 'creation_Date', PATIENT_REF_MASTER.SR_NO AS 'sr_No', PATIENT_MASTER.PAT_FNAME + ' ' + PATIENT_MASTER.PAT_SNAME AS 'NAME', DEPT_ID AS 'Dept_ID', DEPT_MASTER.DEPT_NAME AS 'Dept_Name', DOC_MASTER.DOC_ID AS 'Doc_Master', DOC_MASTER.DOC_FNAME + ' ' + DOC_MASTER.DOC_SNAME AS 'Doc_Name' , PATIENT_MASTER.PAT_ADDR AS 'addr', GENDER_MASTER.NAME1 AS 'Pat_Sex', PATIENT_MASTER.AGE AS 'age', (SELECT Count(PATIENT_REF_MASTER.SR_NO) FROM PATIENT_REF_MASTER WHERE PATIENT_REF_MASTER.CREATION_DATE = #date) AS 'count', (SELECT Count(PATIENT_MASTER.PAT_SEX) FROM PATIENT_MASTER LEFT JOIN PATIENT_REF_MASTER ON PATIENT_REF_MASTER.PAT_ID = PATIENT_MASTER.PAT_CODE WHERE PATIENT_REF_MASTER.CREATION_DATE = #date AND PATIENT_MASTER.PAT_SEX = 2) AS 'F_count', (SELECT Count(PATIENT_MASTER.PAT_SEX) FROM PATIENT_MASTER LEFT JOIN PATIENT_REF_MASTER ON PATIENT_REF_MASTER.PAT_ID = PATIENT_MASTER.PAT_CODE WHERE PATIENT_REF_MASTER.CREATION_DATE = #date AND PATIENT_MASTER.PAT_SEX = 1) AS 'M_count' FROM PATIENT_REF_MASTER LEFT JOIN DBO.OLD_NEW_PATIENT ON DBO.OLD_NEW_PATIENT.CODE = PATIENT_REF_MASTER.OLD_NEW LEFT JOIN DBO.DEPT_MASTER ON DEPT_MASTER.DEPT_CODE = PATIENT_REF_MASTER.DEPT_ID LEFT JOIN PATIENT_MASTER ON PATIENT_MASTER.PAT_CODE = PATIENT_REF_MASTER.PAT_ID LEFT JOIN DOC_MASTER ON DOC_MASTER.DOC_ID = PATIENT_REF_MASTER.DOC_ID LEFT JOIN GENDER_MASTER ON GENDER_MASTER.CODE = PATIENT_MASTER.PAT_SEX WHERE PATIENT_REF_MASTER.CREATION_DATE = #date --MONTH(Patient_Ref_master.creation_Date)=#month and Dept_ID=#dept ORDER BY PATIENT_REF_MASTER.SR_NO ASC -- select Dept_Master.Dept_Name as 'Dept_Name', -- count(Pat_ID) as 'Pat_ID' -- -- from Patient_Ref_master --left join dbo.Dept_Master on Dept_Master.Dept_code = Patient_Ref_master.Dept_ID --where MONTH(Patient_Ref_master.creation_Date)=#month and Dept_ID=#dept --group by Dept_Master.Dept_Name END
I think you're trying to do to many things at once ;-) The heart of your problem lies here: SELECT Count(PATIENT_MASTER.PAT_SEX) FROM PATIENT_MASTER LEFT JOIN PATIENT_REF_MASTER ON PATIENT_REF_MASTER.PAT_ID = PATIENT_MASTER.PAT_CODE WHERE PATIENT_REF_MASTER.CREATION_DATE = #date AND PATIENT_MASTER.PAT_SEX = 1 By using this query within your query, it will return the total in each and every row. This is also why you can get away with writing the query and not using the GROUP BY clause. I suggest you do all the work in this query with the exception of the count, and then use another query outside of this one for the count. A secondary problem is that in your query you're requesting several details about each row, but you want it to come back in one row. You have to decide what you want ;). In order to get just the counts in one row, try something like this: SET ANSI_NULLS ON SET QUOTED_IDENTIFIER ON GO ALTER PROCEDURE [dbo].[Test_test_test]-- '2013/08/02' -- Add the parameters for the stored procedure here -- Add the parameters for the stored procedure here (#date VARCHAR(20)) AS BEGIN SET NOCOUNT ON; SET TRANSACTION ISOLATION LEVEL SERIALIZABLE; SELECT Count(*) count, Sum(CASE WHEN FEMALES IS NOT NULL THEN 1 ELSE 0 END) F_Count, Sum(CASE WHEN MALES IS NOT NULL THEN 1 ELSE 0 END) M_Count FROM (SELECT ( CASE WHEN OLD_NEW_PATIENT.OLD_NEW = 'new' THEN PATIENT_REF_MASTER.PAT_ID ELSE NULL END ) AS 'new', ( CASE WHEN OLD_NEW_PATIENT.OLD_NEW = 'old' THEN PATIENT_REF_MASTER.PAT_ID ELSE NULL END ) AS 'old', ---------------------------------------- ( CASE WHEN GENDER_MASTER.NAME1 = 'Female' THEN GENDER_MASTER.NAME1 ELSE NULL END ) AS 'Females', ( CASE WHEN GENDER_MASTER.NAME1 = 'Male' THEN GENDER_MASTER.NAME1 ELSE NULL END ) AS 'Males', ------------------------------------- CONVERT(VARCHAR, PATIENT_REF_MASTER.CREATION_DATE, 105) AS 'creation_Date', PATIENT_REF_MASTER.SR_NO AS 'sr_No', PATIENT_MASTER.PAT_FNAME + ' ' + PATIENT_MASTER.PAT_SNAME AS 'NAME' , DEPT_ID AS 'Dept_ID', DEPT_MASTER.DEPT_NAME AS 'Dept_Name', DOC_MASTER.DOC_ID AS 'Doc_Master', DOC_MASTER.DOC_FNAME + ' ' + DOC_MASTER.DOC_SNAME AS 'Doc_Name', PATIENT_MASTER.PAT_ADDR AS 'addr' , GENDER_MASTER.NAME1 AS 'Pat_Sex', PATIENT_MASTER.AGE AS 'age' FROM PATIENT_REF_MASTER LEFT JOIN DBO.OLD_NEW_PATIENT ON DBO.OLD_NEW_PATIENT.CODE = PATIENT_REF_MASTER.OLD_NEW LEFT JOIN DBO.DEPT_MASTER ON DEPT_MASTER.DEPT_CODE = PATIENT_REF_MASTER.DEPT_ID LEFT JOIN PATIENT_MASTER ON PATIENT_MASTER.PAT_CODE = PATIENT_REF_MASTER.PAT_ID LEFT JOIN DOC_MASTER ON DOC_MASTER.DOC_ID = PATIENT_REF_MASTER.DOC_ID LEFT JOIN GENDER_MASTER ON GENDER_MASTER.CODE = PATIENT_MASTER.PAT_SEX WHERE PATIENT_REF_MASTER.CREATION_DATE = #date --MONTH(Patient_Ref_master.creation_Date)=#month and Dept_ID=#dept )T ORDER BY PATIENT_REF_MASTER.SR_NO ASC END I hope this helps, let me know if you need any more info. Edit Found a small mistake in the query I posted, the field names should have been FEMALES not FEMALE and MALES not MALE. I also set up a scaled down example in SQL Fiddle. Take a look and tell me what you think.
PL/SQL: Conditional Where
I have the following scenario: CREATE OR REPLACE PROCEDURE GETINBOX ( inHasAttachments IN int ) AS BEGIN SELECT M.MailId, M.SenderId, E.Emp_Name As "Sender", MI.RecipientId, M.Subject FROM MAIL M INNER JOIN MAILINBOX MI ON M.MailId = MI.MailId WHERE MI.RecipientId = '547' AND M.NotificationSelected = 'Y' IF inHasAttachments = '1' THEN AND M.Attachments = 'Y' END IF; END GETINBOX; Is it possible to add conditions to the where clause based on the value of a parameter? WHERE MI.RecipientId = '547' AND M.NotificationSelected = 'Y' IF inHasAttachments = '1' THEN AND M.Attachments = 'Y' END IF; Obviously this is not allowed but is it possible to do this in some way in PL/SQL? I know one way to do it is to duplicate the query and execute a different query based on the value of the parameter but I don't want to duplicate my code.
As I understand your requirements: if the value of parameter inHasAttachments is 1 then you want to filter further by M.Attachments = 'Y', and if its value isn't 1 then you don't care about M.Attachments. This is in addition to the condition MI.RecipientId = '547' AND M.NotificationSelected = 'Y'. You can do it like this: SELECT M.MailId, M.SenderId, E.Emp_Name As "Sender", MI.RecipientId, M.Subject FROM MAIL M INNER JOIN MAILINBOX MI ON M.MailId = MI.MailId WHERE MI.RecipientId = '547' AND M.NotificationSelected = 'Y' AND (inHasAttachments <> '1' OR M.Attachments = 'Y')