PowerBi Desktop: Distinct Measure Missing values - count

I have a problem with a measure / calculation producing inconsistent data for which I would need help.
I'm importing data from Active Directoy which is working fine. I use the AccountLockoutState to determine if a computer is active or not. Besides that I have a conditional column to aggregate OS in a simplified version - all working fine.
I the have 3 different measures to count the distinct number of devices with the followin attributes:
All Active clients:
AD Total =
CALCULATE(
DISTINCTCOUNT('AD Data'[computer.cn]),
FILTER(
'AD Data',
'AD Data'[userAccountControl]= VALUE("4096")
|| 'AD Data'[userAccountControl] = Value("4128")
|| 'AD Data'[userAccountControl] = Value("69632")
)
)
All Active Clients with OS Win7 or Win 10
AD In Scope =
CALCULATE(
DISTINCTCOUNT('AD Data'[computer.cn]),
FILTER(
'AD Data',
'AD Data'[OS] = "Win 7"
|| 'AD Data'[OS] = "Win 10"
),
FILTER(
'AD Data',
'AD Data'[userAccountControl]= VALUE("4096")
|| 'AD Data'[userAccountControl] = Value("4128")
|| 'AD Data'[userAccountControl] = Value("69632")
)
)
All Clients not with Win 7 or Win 10
AD Out Scope =
CALCULATE(
DISTINCTCOUNT('AD Data'[computer.cn]),
FILTER(
'AD Data','AD Data'[OS] <> "Win 7"
&& 'AD Data'[OS] <> "Win 10"
),
FILTER(
'AD Data',
'AD Data'[userAccountControl]= VALUE("4096")
|| 'AD Data'[userAccountControl] = Value("4128")
|| 'AD Data'[userAccountControl] = Value("69632")
)
)
Normally I would expect that Measure 2 + Measure 3 = Measure 1
However in my case I have a difference of 4 Clients that are simply not showing up...
Missing clients
Does anyone have an idea why this is happening or how to identify the "missing" clients?
Thx & BR
Daniel

Related

conditionalPanel condition contained in a list

So, I have a checkboxgroupButtons where the user can select one or more options
checkboxGroupButtons("id","select options",
choices = c( "A","B","C" )
,selected = "A"
, checkIcon = list(yes = icon("ok", lib = "glyphicon"))
, direction = "vertical"
)
Then I want a conditional panel, but I want it such that the action depends if user selects "A" or (inclusive) "B". Already tried &&. Tried || but it doesn't logically accepts the inclusion. also tried
input.id.values, but it triggers even if I select "A" and "C".
conditionalPanel( condition = "input.id=='A' || input.id=='B'" , helpText("something") )
How can I set a condition is : "input.id" contained in the list ("A","B")
Thank you
Not sure, but could you try
input.id.every(i => i === 'A' || i === 'B')
Or, if the browser you use does not support =>:
input.id.every(function(i){return i==='A' || i==='B';})

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.

Active record query issues in Rails 3.2

I have category model and category has many postings.
Problem: Sometimes postings are not visible under category in web even records are present in database
I investigated the query for the action in production enviroment by enabling config.log_level = :debug and restarted the
nginx passenger server. Now I can see the records under the category. I was unable to reproduce the same issue again and it occurs rarely.
Note:
I didn't change any code in the project. The same code behave differently.
Rails is 3.2.22. Nginx passenger(5.1.1)
Model are as follows
class Category < ActiveRecord::Base
has_many :postings, conditions: ['paid = ? AND start_date <= ? AND end_date >= ?', true, Date.current, Date.current]
end
class Posting < ActiveRecord::Base
searchkick
belongs_to :category
class << self
def payed
where paid: true
end
def activated
where :code => ""
end
def starts_on(date)
where "start_date <= ?", date
end
def ends_after(date)
where "end_date >= ?", date
end
def in_location(state,city)
where(stateid: state.id, cityid: city.id)
end
def not_deleted
where "active != false"
end
end
Postings controller
def index
#category = Category.find(params[:category_id])
postings = #category.postings.payed.activated.not_deleted.starts_on(Date.current).ends_after(Date.current).order(:created_at)
#postings = postings.in_location(current_state, current_city).page(params[:page])
end
From production.log, when accessing postings page /postings?category_id=25
Category Load (0.2ms) SELECT categories.* FROM categories
WHERE categories.id = 25 LIMIT 1
(0.4ms) SELECT COUNT(*) FROM postings WHERE
postings.category_id = 25 AND postings.paid = 1 AND
postings.code = '' AND postings.stateid = 44 AND
postings.cityid = 14823 AND (active != false) AND (paid = 1 AND
listing_start_date <= '2017-03-13' AND listing_end_date >=
'2017-03-13') AND (listing_start_date <= '2017-03-13') AND
(listing_end_date >= '2017-03-13')
CACHE (0. SELECT COUNT(*) FROM postings WHERE
postings.category_id = 25 AND postings.paid = 1 AND
postings.code = '' AND postings.stateid = 44 AND
postings.cityid = 14823 AND (active != false) AND (paid = 1 AND
listing_start_date <= '2017-03-13' AND listing_end_date >=
'2017-03-13') AND (listing_start_date <= '2017-03-13') AND
(listing_end_date >= '2017-03-13')
Posting Load (0.4ms) SELECT postings.* FROM postings WHERE
postings.category_id = 25 AND postings.paid = 1 AND
postings.code = '' AND postings.stateid = 44 AND
postings.cityid = 14823 AND (active != false) AND (paid = 1 AND
listing_start_date <= '2017-03-13' AND listing_end_date >=
'2017-03-13') AND (listing_start_date <= '2017-03-13') AND
(listing_end_date >= '2017-03-13') ORDER BY created_at LIMIT 10 OFFSET
0
The above set of queries did not pick any records; and after enabling debug mode and restart/touch the nginx server the same query fetched available records
Is the problem caused by active record query/ Nginx/ cache?
Please help me to resolve this issue.
Fixed the problem using Proc for association condition like
has_many :postings, conditions: proc { "payed = 1 AND start_date <= '#{Date.current.to_s(:db)}' AND end_date >= '#{Date.current.to_s(:db)}'"}
If you would have done association with dynamic condition like has_many :postings, conditions: ['paid = ? AND start_date <= ? AND end_date >= ?', true, Date.current, Date.current], there will be cases when the results you’ll get are not expected since the condition will have the day you started the Rails application and Date.current won’t be called again.
Thanks to Jose M.Gilgado. Reference: http://josemdev.com/articles/dynamic-conditions-associations-rails-3/

The query performance is very low + correlated subquery

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.

Resources