The query performance is very low + correlated subquery - oracle11g

The query's aim is to get reserved rooms status is 3 or going to reserve in 30 to 45 minutes status in 2 or unreserved status in 1. reservation rooms are in RESEENH table and each reservation is in ORD_NOARCHIVE table which has begintime and endtime of reservation. So for each reservation room this query checks whether reservation is available at current time also its checks the meeting room parents and children. if children is reserved then parents are are blocked.
it takes 10 secs to fetch first 50 records.
with cte as
(
SELECT DISTINCT R.syscode,
R.behcode,
R.syscode AS FK_RESERVATIONUNIT, (
CASE
WHEN R.TYPE = 3 THEN '1'
WHEN R.TYPE = 1 THEN '2'
ELSE NULL
END ) AS LOCATION_TYPE,
R.sysobjalg AS FK_PROPERTY,
MP.syscode AS FK_MEASUREMENTPOINT,
MP.fk_plc_occupancy_state AS FK_PLC_OCCUPANCY_STATE,
F.syscode AS FK_FLOOR,
R.transitiontime,
r.type,
r.is_compoundreservationunit,
r.is_archived,
MP.fk_person,
os.transitionperiod
FROM reseenh R
--left outer join ordtrantbl RSS
--ON RSS.reservationunisyscode = R.syscode
left outer join objond F
ON F.syscode = R.fk_floor
left outer join pln_measurementpoint MP
ON MP.fk_reservationunit = R.syscode
AND MP.is_primary_measurement_point = 'T',
pln_ordersetting os
)
select cte.syscode,cte.behcode,cte.FK_RESERVATIONUNIT,
(CASE
WHEN O.begindatetime_user IS NULL THEN '1' --GREEN
WHEN O.begindatetime_user - (Nvl(cte.transitiontime, ( cte.transitionperiod ))/1440 ) > current_date THEN '2' -- ORANGE
WHEN O.begindatetime_user + (Nvl(cte.transitiontime, ( cte.transitionperiod )) /1440 ) > current_date THEN '3' -- RED
ELSE '3'
END ) AS LOCAVAILABILITY_STATUS_CODE,
cte.LOCATION_TYPE,
cte.FK_PROPERTY,
Coalesce(O.sysmelder, cte.fk_person) AS FK_PERSON,
O.syscode AS FK_ORDER,
O.ref_bostate_userdefined AS FK_ORDER_STATE_USER,
O.fk_bostate AS FK_ORDER_STATE_SYSTEM,
FK_MEASUREMENTPOINT,FK_PLC_OCCUPANCY_STATE,FK_FLOOR
from cte left outer join ord_noarchive O on O.syscode in
( SELECT MAX(ord.syscode) KEEP (DENSE_RANK FIRST ORDER BY ord.begindatetime_user) OVER (PARTITION BY ord.sysreseenh )
FROM ord_noarchive ORD
WHERE ( ( (
current_date >= ( ORD.begindatetime_user - ( Nvl(cte.transitiontime, ( cte.transitionperiod ))/1440) )
AND (
current_date - ( Nvl(cte.transitiontime, (cte.transitionperiod )) / 1440 ) ) <=ORD.enddatetime_user )
OR ( (
current_date + ( (
CASE
WHEN (
cte.TYPE = 1 ) THEN 30
ELSE 45
END ) / 1440 ) ) >= ( ORD.begindatetime_user - (Nvl(cte.transitiontime, ( cte.transitionperiod))/1440 ) )
AND (
current_date - ( Nvl(cte.transitiontime, ( cte.transitionperiod )) / 1440 ) ) < ORD.enddatetime_user ) )
AND ORD.sysreseenh IN
(
SELECT fk_reservationunit_parent
FROM pln_reservationunit_rut
WHERE fk_reservationunit_child IN
(
SELECT fk_reservationunit_child
FROM pln_reservationunit_rut
WHERE cte.is_compoundreservationunit = 'T'
AND fk_reservationunit_parent = cte.syscode)
UNION
SELECT cte.syscode
FROM dual
UNION
SELECT
CASE
WHEN cte.is_compoundreservationunit = 'T' THEN fk_reservationunit_child
ELSE fk_reservationunit_parent
END
FROM pln_reservationunit_rut
WHERE (
cte.is_compoundreservationunit = 'T'
AND fk_reservationunit_parent = cte.syscode )
OR (
cte.is_compoundreservationunit = 'F'
AND fk_reservationunit_child = cte.syscode ))
AND ORD.fk_bostate IN
(
SELECT syscode
FROM pln_bostate
WHERE pnname IN ( 'Requested',
'Made',
'AdministrativelyCompleted' )
AND fk_bodefinition = ref_bodefinition)
AND ORD.sysreseenh = O.sysreseenh
))
WHERE cte.is_archived = 'F'
AND cte.TYPE IN ( 1,
3 )
AND cte.fk_floor=495

No time to analyze the details of a very complex query, but my overall sense is that you are trying to do too many things at once. Separate the tasks. Check on room availability in one query, and check on the children/parent thing in a separate query.
Also, you could analyze the execution plan for the query and see what it bogging it down. My suspicion (again without time to really try to understand your query) is that at some point the mixing of tasks is translating into a many-to-many relationship where you have an intermediate result that is a cross product of rows between some of the tables in your query.

Related

Can I create a date variable in R for dbGetQuery?

This is my code:
cohort_query <- dbGetQuery(con,'select u.unique_id customer_id,
o.user_id user_hash,
"min"(o.deliveryconfirmeddate) first_order,
(
CASE
WHEN (
"min"(o.deliveryconfirmeddate) >= (date(\'2021-11-01\') - INTERVAL \'1\' YEAR)
) THEN 0
WHEN (
"min"(o.deliveryconfirmeddate) >= (date(\'2021-11-01\') - INTERVAL \'2\' YEAR)
) THEN 1
WHEN (
"min"(o.deliveryconfirmeddate) >= (date(\'2021-11-01\') - INTERVAL \'3\' YEAR)
) THEN 2
WHEN (
"min"(o.deliveryconfirmeddate) >= (date(\'2021-11-01\') - INTERVAL \'4\' YEAR)
) THEN 3
WHEN (
"min"(o.deliveryconfirmeddate) >= (date(\'2021-11-01\') - INTERVAL \'5\' YEAR)
) THEN 4
ELSE 5
END
) "cohort_year",
min(o.id) as first_order_id
FROM (
"bj-analytics"."mysql_bj_orders" o
INNER JOIN "bj-analytics"."mysql_bj_users" u ON (u.user_id = o.user_id)
)
WHERE (
(
(o.connectedorder = false)
AND (o.status <> \'cancelled\')
)
AND (o.status <> \'Cancelled\')
)
GROUP BY 1,
2
HAVING (
"min"(o.deliveryconfirmeddate) >= (date(\'2021-11-01\') - INTERVAL \'6\' YEAR)
)
and "min"(o.deliveryconfirmeddate) < date(\'2021-11-01\')')
This all runs fine. Basically, I want to make the 2021-11-01 date a variable, so that I only have to enter it once.
Someone recommended glue_sql but I couldn't make it work. I think it's an issue with the double quotation marks and the single marks. I tried to change these to all "" or to all '' but then the query just doesn't run!
I also found that I have to include \'s - again without them doesn't seem to run.
I'm new to R so not too sure how to get around this!
Basically can anyone please help with turning that date into a variable?
Any help is much appreciated - thanks!
Never use paste or sprintf to put "data" into a query, for many reasons (query optimization/caching and accidental sql-injection being the top two), instead use parameter-binding:
DBI::dbGetQuery(con, "
select (case
when (5 < ?) then 5
when (3 < ?) then 3
when (1 < ?) then 1
else 0 end) as quux",
params = replicate(n=3, expr=3, simplify = FALSE))
# quux
# 1 1
DBI::dbGetQuery(con, "
select (case
when (5 < ?) then 5
when (3 < ?) then 3
when (1 < ?) then 1
else 0 end) as quux",
params = replicate(n=3, expr=10, simplify = FALSE))
# quux
# 1 5
The replicate(3,10,F) is just a programmatic way to do list(10,10,10), they are equivalent here. My use of static comparisons (5 < ?) is solely for placeholders, your min(.) should work fine.
(FYI, one more benefit of parameter-binding: no need to deal with additional quotes in the query.)
See https://db.rstudio.com/best-practices/run-queries-safely/#parameterized-queries for more discussions on safely using data in the query.
I think that would make your query this:
cohort_query <- dbGetQuery(con, 'select u.unique_id customer_id,
o.user_id user_hash,
"min"(o.deliveryconfirmeddate) first_order,
(
CASE
WHEN (
"min"(o.deliveryconfirmeddate) >= (date(?) - INTERVAL \'1\' YEAR)
) THEN 0
WHEN (
"min"(o.deliveryconfirmeddate) >= (date(?) - INTERVAL \'2\' YEAR)
) THEN 1
WHEN (
"min"(o.deliveryconfirmeddate) >= (date(?) - INTERVAL \'3\' YEAR)
) THEN 2
WHEN (
"min"(o.deliveryconfirmeddate) >= (date(?) - INTERVAL \'4\' YEAR)
) THEN 3
WHEN (
"min"(o.deliveryconfirmeddate) >= (date(?) - INTERVAL \'5\' YEAR)
) THEN 4
ELSE 5
END
) "cohort_year",
min(o.id) as first_order_id
FROM (
"bj-analytics"."mysql_bj_orders" o
INNER JOIN "bj-analytics"."mysql_bj_users" u ON (u.user_id = o.user_id)
)
WHERE (
(
(o.connectedorder = false)
AND (o.status <> \'cancelled\')
)
AND (o.status <> \'Cancelled\')
)
GROUP BY 1,
2
HAVING (
"min"(o.deliveryconfirmeddate) >= (date(?) - INTERVAL \'6\' YEAR)
)
and "min"(o.deliveryconfirmeddate) < date(?)'),
params = replicate(n=7, expr='2021-11-01', simplify=FALSE)

How can I set query_band for Block Level Compression using Aster's load_to_teradata function,

When you are loading a teradata table in bteq you can set the queryband for block level compression. This even works when you are using querygrid and inserting from a foreign server.
SET QUERY_BAND = 'BlockCompression=Yes;' UPDATE FOR SESSION;
My issue is that I am creating a table on an Aster system and then using the load to teradata function. I suspect there is a syntax call where I can set the query band as part of the load_to_teradata call but after searching the internet and through a reem of teradata documentation I haven't found anything yet.
-- Load Agg data for the YYYYMM to Teradta
SELECT SUM(loaded_row_count),SUM(error_row_count)
FROM load_to_teradata (
ON ( select
Cust_id
, cast(lst_cnf_actvy_dt_tm as date) as lst_cnf_actvy_dt
, cast(sum(str_cnt ) as INTEGER) as acct_open_brnch_use_cnt
, cast(sum(phone_cnt ) as INTEGER) as acct_open_phn_use_cnt
, cast(sum(mail_cnt ) as INTEGER) as acct_open_mail_use_cnt
, cast(sum(onlnchnl_cnt) as INTEGER) as acct_open_onln_use_cnt
, cast(sum(mblbnk_cnt ) as INTEGER) as acct_open_mbl_dvc_use_cnt
, cast(sum(acctopen_cnt) as INTEGER) as acct_open_trck_chnl_cnt
from <someDB>.<someTBL>
where acctopen_cnt > 0
and lower(lst_cnf_actvy_typ_cd) = 'acctopen'
and cast(lst_cnf_actvy_dt_tm as date) between
cast(substring('${YYYYMM}' from 1 for 4) || '-' || substring('${YYYYMM}' from 5 for 2) || '-01' as date) and
cast((cast(substring('${YYYYMM}' from 1 for 4) || '-' || substring('${YYYYMM}' from 5 for 2) || '-01' as date) + interval '1 month') - interval '1 day' as date)
and (str_cnt > 0 or phone_cnt > 0 or mail_cnt > 0 or onlnchnl_cnt > 0 or mblbnk_cnt > 0)
group by 1,2 )
TDPID('TD_RDBMS_C2T') USERNAME('${c2tUID}') PASSWORD('${c2tPWD}') ${LDAP_IND_AST_C2T}
TARGET_TABLE ( 'C2T.t_yyyymm_agg' ) LOG_TABLE ('C2T.t_yyyymm_aggLOG')
MAX_SESSIONS(120));
Was able to get the syntax for the load_to_teradata options. You can see the query_band_sess_info argument after max_sessions and before query_timeout...
load_to_teradata(
ON (source query)
TDPID('tdpid')
TARGET_TABLE('fully-qualified table name')
[ERROR_TABLES('error table'[, 'unique constraint violation table'])]
[LOG_TABLE('table name')]
Teradata QueryGrid: Aster-Teradata Connector
Loading Data From Aster Database to Teradata
Aster Database User Guide for Aster Appliances 301
[USERNAME('username')]
[PASSWORD('password')]
[LOGON_MECHANISM('TD2' | 'LDAP' | 'KRB5')]
[LOGON_DATA('mechanism-specific logon data')]
[ACCOUNT_ID('account-id')]
[TRACE_LEVEL('trace-level')]
[NUM_INSTANCES('instance-count')]
[START_INSTANCE('start-instance')]
[MAX_SESSIONS('max-sessions-number')]
[QUERY_BAND_SESS_INFO('key1=value1;key2=value2;...')]
[QUERY_TIMEOUT('timeout-in-seconds')]
[AUTO_TUNE_INSTANCES('yes'|'no')]
[WORKINGDATABASE(‘dbname’)]
[DIAGNOSTIC_MODE('All'|['GetCOPEntries','CheckConnectivity',
'CheckAuthentication','GetTPTSessions',
'TargetTableOrQuerySchema'])])
);

Optimizing Query - I'm doing something very wrong here. Alternatives?

So basically what this query does is SUM some value, and fetch some data from another table (with 1.8 million records - EDTF007 - the one in the WITH). EDTF011 (main table) has around 800k records.
So what's the problem?
Well... the field that I fetch from EDTF007 has some rules (seen in the CASE clause). I fetch the field CCONTA by making different "INNER JOINS" (as seen in the CASE).
The database cries for 1 hour to run this query. I've ran statistics, I've created indexes (rebuild them), tried access directly to the partitions. And it takes around 1 hour to run this query. Not acceptable at this moment.
The thing is... I'm out of ideas... and the problem is the structure of the query because... the WAIT events that I get... are all related to this bad query structure... :(
Any ideas how could I turn this around? An update on the table is also too slow I think...
WITH IMP AS (SELECT CCONTA, CREFERENCIA FROM EDTF007_IMPARIDADE PARTITION (P_IMPARIDADE_201703))
SELECT SUM(GC.MAVALIAA),
V.ID, NVL(GC.CKNUMCTA, '00000000000'), NVL(GC.CKBALCAO, '0000'), GC.CKPRODUT, GC.CKSUBPRO, GC.ZDEPOSIT, GC.MSLDACT, SUM(GC.MAVALIAA), GC.TIPGARBL, GC.CGARANT, NVL(G.TIPO_GAR, 'SEM GAR'), G.TIPO_GAR2, GC.CREFERENCIA_IMP,
CASE
WHEN (SUBSTR(GC.CKPRODUT, 1, 3) ) IN ('096' , '097' , '020' , '021' , '024') AND GC.CKPRODUT != 'MOR' THEN
(SELECT DISTINCT CCONTA FROM IMP WHERE (GC.CKBALCAO || GC.CKNUMCTA) = CCONTA)
WHEN (SUBSTR(GC.CKPRODUT, 1, 3) ) IN ('035') AND GC.CKPRODUT != 'MOR' THEN
(SELECT DISTINCT CCONTA FROM IMP WHERE GC.ZDEPOSIT = CREFERENCIA)
WHEN NVL(GC.CKBALCAO ||GC.CKNUMCTA, '000000000000000') IN (SELECT IMP.CCONTA FROM RE_ED.EDTF007_IMPARIDADE IMP WHERE (IMP.PRODUTO ) = ( '000' ) AND (IMP.SEGMENTO ) IN ('IE' , 'IP' , 'IA' , 'GI')) AND GC.CKPRODUT != 'MOR' THEN
(SELECT DISTINCT CCONTA FROM IMP WHERE (GC.CKBALCAO || GC.CKNUMCTA) = CCONTA)
WHEN SUBSTR(GC.CKPRODUT, 1, 3) NOT IN ('096' , '097' , '020' , '021' , '024' , '035' ) AND GC.CKPRODUT != 'MOR' THEN
(SELECT DISTINCT CCONTA FROM IMP WHERE (GC.CKBALCAO || GC.CKNUMCTA || GC.CREFERENCIA_IMP) = CCONTA || CREFERENCIA)
WHEN GC.CKPRODUT = 'MOR' THEN
(SELECT DISTINCT CCONTA FROM IMP WHERE (GC.CKBALCAO || GC.CKNUMCTA || GC.CREFERENCIA_IMP) = CCONTA || CREFERENCIA)
ELSE '000000000000000'
END CCONTA_IMP,
GC.CREATED_BY, GC.CREATED_DATE
FROM RE_ED.EDTD011_GARANTIAS_CONTRATO GC,
(SELECT MAX(IDVERSAO) AS ID FROM RE_CD.CDTD009_VERSOES WHERE (TABELA) = ('RE_CD.CDTD015_GARANTIAS_CONTRATO')) V,
(SELECT DISTINCT TIPO_GAR, TIPO_GAR2, CODIGO FROM RE_CD.CDTD011_COD_GARANTIAS WHERE FLAG_ATIVO = 1 AND DTBEGIN_VER <= TO_TIMESTAMP(TRUNC(TO_DATE('2017-03-01', 'YYYY-MM-DD'), 'MM')) AND DTEND_VER >= TO_TIMESTAMP(LAST_DAY(TO_DATE('2017-03-01', 'YYYY-MM-DD')))) G
WHERE (GC.ANO) = ('2017')
AND (GC.MES) = ('03')
AND (GC.CGARANT) = (G.CODIGO)
GROUP BY V.ID, GC.CKNUMCTA, GC.CKBALCAO, GC.CKPRODUT, GC.CKSUBPRO, GC.ZDEPOSIT, GC.MSLDACT, GC.TIPGARBL, GC.CGARANT, G.TIPO_GAR, G.TIPO_GAR2, GC.CREFERENCIA_IMP, GC.CREATED_BY, GC.CREATED_DATE;
EDIT: After some conversation with a collegue he sugested to separate the query universe and instead of 1 query, I should try with 5 insert queries for each case. I'm going forward with this approach. Lets see how it goes. The main goal is a Insert into Select by the way.

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.

Mdx calculate count distinct

I want to write a mdx script that displays the count rows I have for a member.
This is my initial script:
SELECT NON EMPTY { [Measures].[I_OPC_ATTEINT]
and 6 measures } ON COLUMNS
, NON EMPTY { ([Axe_Temps].[MOIS_ANNEE].[MOIS_ANNEE].ALLMEMBERS
* [Axe_ORGANISATION].[Structure].[EQUIPE].ALLMEMBERS
* [Axe_OPC].[TYPE_REGROUPEMENT].[TYPE_REGROUPEMENT].ALLMEMBERS
* [Axe_OPC].[COMPOSITION].[COMPOSITION].ALLMEMBERS
* [Axe_OPC].[OPC].[OPC].ALLMEMBERS ) } DIMENSION PROPERTIES MEMBER_CAPTION, MEMBER_UNIQUE_NAME ON ROWS
FROM ( SELECT ( STRTOMEMBER('[Axe_ORGANISATION].[CODE_EQUIPE].&[E_1001]') ) ON COLUMNS
FROM ( SELECT ( STRTOMEMBER('[Axe_ORGANISATION].[CODE_PLATEAU].&[D_1000]') ) ON COLUMNS
FROM ( SELECT ( STRTOMEMBER('[Axe_ORGANISATION].[CODE_UNITE].&[U_107864]') ) ON COLUMNS
FROM ( SELECT ( STRTOMEMBER('[Axe_ORGANISATION].[CODE_CANAL].&[AVSC]') ) ON COLUMNS
FROM ( SELECT ( STRTOMEMBER('[Axe_Temps].[MOIS_ANNEE].&[201306]') ) ON COLUMNS
FROM [PVC_Reporting])))))
I want to display 2 calculated measures:
The count of rows of my result ( count distinct ( [Axe_OPC].[COMPOSITION].[COMPOSITION].ALLMEMBERS )
The count of rows where [Measures].[I_OPC_ATTEINT] <> 0
Thank you.
I would create a measure of type 'distinct count' within the cube and create a simple dimension (oui|non) for I_OPC_ATTEINT.

Resources