Count distinct on a varchar field
Hi,
i want to count the unique values for a field, but when i try this in a aggregator i'm getting the following error:
aggrete function COUNT expected a numeric type.
This is the query i want to reproduce in a mapping to update a tablefield.
select material, material_grid, count (distinct season)
from material_season
group by material, material_grid
having count (distinct season)>1
Why isn't this possible in a aggregator?
regards,
Osman
Hi Osman
When do you get the error? Which version of OWB are you using? What SQL do you get generated - look at the intermediate code gen within the mapping.
Cheers
David
Similar Messages
-
Performance problem with more than one COUNT(DISTINCT ...) in a query
Hi,
(I hope this is the good forum).
In the following query, I have 2 Count Distinct on 2 different fields of the same table. Execution time is okay (2 s) with one or the other COUNT(DISCTINCT ...) in the SELECT clause, but is not tolerable (12 s) with both together in the query! I have
a similar case with 3 counts: 4 s each, 36 s when together!
I've looked at the execution plan, and it seems that with two count distinct, SQL server sorts the table twice before joining the results.
I do not have much experience with SQL server optimization, and I don't know what to improve and how. The SQL is generated by Business Objects, I have few possibilities to tune it. The most direct way would be to execute 2 different queries, but I'd like
to avoid it.
Any advice?
SELECT
DIM_MOIS.DATE_DEBUT_MOIS,
DIM_MOIS.NUM_ANNEE_MOIS,
DIM_DEMANDE_SCD.CAT_DEMANDE,
DIM_APPLICATION.LIB_APPLICATION,
DIM_DEMANDE_SCD.CAT_DEMANDE ,
count(distinct FAITS_DEMANDE.NB_DEMANDE_FLUX),
count(distinct FAITS_DEMANDE.NB_DEMANDE_RESOL_NIV1)
FROM
ALIM_SID.DIM_MOIS INNER JOIN ALIM_SID.DIM_JOUR ON (DIM_JOUR.SEQ_MOIS=DIM_MOIS.SEQ_MOIS)
INNER JOIN ALIM_SID.FAITS_DEMANDE ON (FAITS_DEMANDE.SEQ_JOUR=DIM_JOUR.SEQ_JOUR)
INNER JOIN ALIM_SID.DIM_APPLICATION ON (FAITS_DEMANDE.SEQ_APPLICATION=DIM_APPLICATION.SEQ_APPLICATION)
INNER JOIN ALIM_SID.DIM_DEMANDE_SCD ON (FAITS_DEMANDE.SEQ_DEMANDE_SCD=DIM_DEMANDE_SCD.SEQ_DEMANDE_SCD)
WHERE
( ( DIM_MOIS.NUM_ANNEE_MOIS ) >201301
GROUP BY
DIM_MOIS.DATE_DEBUT_MOIS,
DIM_MOIS.NUM_ANNEE_MOIS,
DIM_DEMANDE_SCD.CAT_DEMANDE,
DIM_APPLICATION.LIB_APPLICATIONHere is the script, nothing original. Hope this helps.
-- Fact table :
-- foreign keys begin by FK_,
-- measures to counted (COUNT DISTINCT) begin with NB_
CREATE TABLE [ALIM_SID].[FAITS_DEMANDE](
[SEQ_JOUR] [int] NOT NULL,
[SEQ_DEMANDE] [int] NOT NULL,
[SEQ_DEMANDE_SCD] [int] NOT NULL,
[SEQ_APPLICATION] [int] NOT NULL,
[SEQ_INTERVENANT] [int] NOT NULL,
[SEQ_SERVICE_RESPONSABLE] [int] NOT NULL,
[NB_DEMANDE_FLUX] [int] NULL,
[NB_DEMANDE_STOCK] [int] NULL,
[NB_DEMANDE_RESOLUE] [int] NULL,
[NB_DEMANDE_LIVREE] [int] NULL,
[NB_DEMANDE_MEP] [int] NULL,
[NB_DEMANDE_RESOL_NIV1] [int] NULL,
CONSTRAINT [PK_FAITS_DEMANDE] PRIMARY KEY CLUSTERED
[SEQ_JOUR] ASC,
[SEQ_DEMANDE] ASC,
[SEQ_DEMANDE_SCD] ASC,
[SEQ_APPLICATION] ASC,
[SEQ_INTERVENANT] ASC,
[SEQ_SERVICE_RESPONSABLE] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY],
CONSTRAINT [AK_AK_FAITS_DEMANDE_FAITS_DE] UNIQUE NONCLUSTERED
[SEQ_JOUR] ASC,
[SEQ_DEMANDE] ASC,
[SEQ_DEMANDE_SCD] ASC,
[SEQ_APPLICATION] ASC,
[SEQ_INTERVENANT] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
) ON [PRIMARY]
GO
ALTER TABLE [ALIM_SID].[FAITS_DEMANDE] WITH CHECK ADD CONSTRAINT [FK_FAITS_DEMANDE_DIM_APPLICATION] FOREIGN KEY([SEQ_APPLICATION])
REFERENCES [ALIM_SID].[DIM_APPLICATION] ([SEQ_APPLICATION])
GO
ALTER TABLE [ALIM_SID].[FAITS_DEMANDE] CHECK CONSTRAINT [FK_FAITS_DEMANDE_DIM_APPLICATION]
GO
ALTER TABLE [ALIM_SID].[FAITS_DEMANDE] WITH CHECK ADD CONSTRAINT [FK_FAITS_DEMANDE_DIM_DEMANDE] FOREIGN KEY([SEQ_DEMANDE])
REFERENCES [ALIM_SID].[DIM_DEMANDE] ([SEQ_DEMANDE])
GO
ALTER TABLE [ALIM_SID].[FAITS_DEMANDE] CHECK CONSTRAINT [FK_FAITS_DEMANDE_DIM_DEMANDE]
GO
ALTER TABLE [ALIM_SID].[FAITS_DEMANDE] WITH CHECK ADD CONSTRAINT [FK_FAITS_DEMANDE_DIM_DEMANDE_SCD] FOREIGN KEY([SEQ_DEMANDE_SCD])
REFERENCES [ALIM_SID].[DIM_DEMANDE_SCD] ([SEQ_DEMANDE_SCD])
GO
ALTER TABLE [ALIM_SID].[FAITS_DEMANDE] CHECK CONSTRAINT [FK_FAITS_DEMANDE_DIM_DEMANDE_SCD]
GO
ALTER TABLE [ALIM_SID].[FAITS_DEMANDE] WITH CHECK ADD CONSTRAINT [FK_FAITS_DEMANDE_DIM_INTERVENANT] FOREIGN KEY([SEQ_INTERVENANT])
REFERENCES [ALIM_SID].[DIM_INTERVENANT] ([SEQ_INTERVENANT])
GO
ALTER TABLE [ALIM_SID].[FAITS_DEMANDE] CHECK CONSTRAINT [FK_FAITS_DEMANDE_DIM_INTERVENANT]
GO
ALTER TABLE [ALIM_SID].[FAITS_DEMANDE] WITH CHECK ADD CONSTRAINT [FK_FAITS_DEMANDE_DIM_JOUR] FOREIGN KEY([SEQ_JOUR])
REFERENCES [ALIM_SID].[DIM_JOUR] ([SEQ_JOUR])
GO
ALTER TABLE [ALIM_SID].[FAITS_DEMANDE] CHECK CONSTRAINT [FK_FAITS_DEMANDE_DIM_JOUR]
GO
ALTER TABLE [ALIM_SID].[FAITS_DEMANDE] WITH CHECK ADD CONSTRAINT [FK_FAITS_DEMANDE_DIM_SERVICE_RESPONSABLE] FOREIGN KEY([SEQ_SERVICE_RESPONSABLE])
REFERENCES [ALIM_SID].[DIM_SERVICE] ([SEQ_SERVICE])
GO
ALTER TABLE [ALIM_SID].[FAITS_DEMANDE] CHECK CONSTRAINT [FK_FAITS_DEMANDE_DIM_SERVICE_RESPONSABLE]
GO
-- not shown : extended properties
-- One of the dimension tables (they all have a primary key named SEQ_)
CREATE TABLE [ALIM_SID].[DIM_JOUR](
[SEQ_JOUR] [int] IDENTITY(1,1) NOT NULL,
[SEQ_ANNEE] [int] NOT NULL,
[SEQ_MOIS] [int] NOT NULL,
[DATE_JOUR] [date] NULL,
[CODE_ANNEE] [varchar](25) NULL,
[CODE_MOIS] [varchar](25) NULL,
[CODE_SEMAINE_ISO] [varchar](25) NULL,
[CODE_JOUR_ANNEE] [varchar](25) NULL,
[CODE_ANNEE_JOUR] [varchar](25) NULL,
[LIB_JOUR] [varchar](25) NULL,
[LIB_JOUR_COURT] [varchar](25) NULL,
[JOUR_OUVRE] [tinyint] NULL,
[JOUR_CHOME] [tinyint] NULL,
CONSTRAINT [PK_DIM_JOUR] PRIMARY KEY CLUSTERED
[SEQ_JOUR] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
) ON [PRIMARY]
GO
ALTER TABLE [ALIM_SID].[DIM_JOUR] WITH CHECK ADD CONSTRAINT [FK_DIM_JOUR_DIM_ANNEE] FOREIGN KEY([SEQ_ANNEE])
REFERENCES [ALIM_SID].[DIM_ANNEE] ([SEQ_ANNEE])
GO
ALTER TABLE [ALIM_SID].[DIM_JOUR] CHECK CONSTRAINT [FK_DIM_JOUR_DIM_ANNEE]
GO
ALTER TABLE [ALIM_SID].[DIM_JOUR] WITH CHECK ADD CONSTRAINT [FK_DIM_JOUR_DIM_MOIS] FOREIGN KEY([SEQ_MOIS])
REFERENCES [ALIM_SID].[DIM_MOIS] ([SEQ_MOIS])
GO
ALTER TABLE [ALIM_SID].[DIM_JOUR] CHECK CONSTRAINT [FK_DIM_JOUR_DIM_MOIS]
GO -
OBIEE 10G Total by in answers not correct for count distinct fields. Is this a bug?
For example:
Sales fact has receipt no and line no as key. It has data like:
receipt no, line no, value
1, 1, 30
1, 2, 40
2, 1, 10
2, 2, 10
There is also a transaction field defined as count distinct of receipt no (in BMM)
In answers, I set to show Total.
without any filters:
receipt no, value, transactions
1, 70, 1
2, 20, 1
total: 90, 2
Transactions is 2, which is correct.
If apply filter of transaction value greater than 50.
Then transactions in total will still show 2
1, 70, 1
total: 70, 2
Is this a bug? It looks only SUM works no problem in the total by.I did look at the physical query and saw how it calculated the Total transactions and it didn't take into account of the filter of transaction value greater than 50. Don't know why though. I don't know why you want to count line no. The result would be still 2.
-
Dividing sum of varchar fields?
I'm using SQL Server 2014 and I have the following table containing two varchar fields I need to sum and divide:
CREATE TABLE.[VMs](
[VMID] [int] IDENTITY(1,1) NOT NULL,
[vCenter] [varchar](100) NULL,
[VMName] [varchar](100) NULL,
[Template] [varchar](100) NULL,
[PowerState] [varchar](100) NULL,
[ClusterName] [varchar](100) NULL,
[Disk] [int] NULL,
[DiskPartition] [varchar](70) NULL,
[DiskSizeGB] [decimal](10, 3) NULL,
[Hostname] [varchar](100) NULL,
CONSTRAINT [PK_VMs] PRIMARY KEY CLUSTERED
[VMID] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
) ON [PRIMARY]
I'm trying to get the distinct sum of 'VMName' and 'Hostname' to get the count of each, and then divide the 'VMName' count by 'Hostname' count on a per 'vCenter' basis.
I'm not sure how to perform calculations against the varchar fields and any help would be appreciated.
Thanks in advance
AdamThis is the main query, the top portion was sample data I just put based on your provided data
;With CTE
AS
SELECT
vCenter
,COUNT(DISTINCT VMName) As CountVMName
,COUNT(DISTINCT HostName) As CountHostName
FROM
@myTable
GROUP BY
vCenter
SELECT *, (CASE WHEN CountHostName <> 0 THEN CountVMName / CountHostName ELSE 0 END) As CountPerVCenter
FROM
CTE
Best Wishes, Arbi; Please vote if you find this posting was helpful or Mark it as answered. -
Count Distinct Over Partition Syntax Error
Count(Distinct [field]) over (partition by [field2]) returns a syntax error at the key word distinct
Count(all [field]) over (partition by [field2]) compiles fine
I am writing a query to count the number of clients a sales rep has when the sales rep represents multiple companies (ad agency). I need a count of the clients each company has and a distinct count that each rep has. A client may be assigned to multiple companies.
Is a distinct count supported with partitions?Until we wait for the full implementation of the OVER clause, you can
try something like this:
CREATE TABLE Foo (
fookey INT PRIMARY KEY,
company VARCHAR(30),
sales_rep VARCHAR(30),
client VARCHAR(30));
INSERT INTO Foo VALUES(1, 'ABC Corp.', 'Joe', 'Client1');
INSERT INTO Foo VALUES(2, 'ABC Corp.', 'Joe', 'Client2');
INSERT INTO Foo VALUES(3, 'ABC Corp.', 'Peter', 'Client2');
INSERT INTO Foo VALUES(4, 'DEF Corp.', 'Joe', 'Client1');
INSERT INTO Foo VALUES(5, 'DEF Corp.', 'Joe', 'Client3');
SELECT fookey, company, sales_rep, client,
MAX(rk1) OVER(PARTITION BY sales_rep) AS rep_distinct_client_cnt,
MAX(rk2) OVER(PARTITION BY company) AS company_distinct_client_cnt
FROM (
SELECT fookey, company, sales_rep, client,
DENSE_RANK() OVER(PARTITION BY sales_rep ORDER BY client) As rk1,
DENSE_RANK() OVER(PARTITION BY company ORDER BY client) As rk2
FROM Foo) AS F;
fookey company sales_rep client rep_distinct_client_cnt
company_distinct_client_cnt
1 ABC Corp. Joe Client1 3 2
2 ABC Corp. Joe Client2 3 2
3 ABC Corp. Peter Client2 1 2
4 DEF Corp. Joe Client1 3 2
5 DEF Corp. Joe Client3 3 2
Plamen Ratchev
http://www.SQLStudio.com -
Query to count differents values of a field
I have to do counts to the quantity of entitys in the database i have to obtain the quantity of registries for entities that appear
Buenas, les comento estoy trabajando con plsql y se me presenta un problema, necesito contar la cantidad de registros que devuelve la query por entidad, estoy trabajando con un procedure y evaluando con un cursor, pero no se como hacer trabajar la query para devolver ese valor
ENTITY OVERALL DATE HOUR
====== ===== ==== ====
ENT1 5 20100318 12:00
ENT2 20 20100318 12:00
ENT3 12 20100318 12:00
CURSOR1
SELECT distinct(rp.cod_entidad),
YYYYYYYYY,
to_date(to_char(SYSDATE,'YYYYMMDD'),'YYYY-MM-DD') as fecha_pago,
to_char(sysdate,'hh-mi-ss') as hora_pago
FROM registry rp, product pc
where pc.nro_solicitud = rp.nro_solicitud
and pc.resp_2= 'OK'
and pc.resp_1= 'OK'
Edited by: 862673 on 31/05/2011 10:01I must build a query to tell me how many records per entity is, YYYYYYY is not a field, i dont know that I write
TABLE
====
ENTITY Cliente telefono
====== ======= ========
ENT1 indiana 234 54231
ENT1 jose 566 78954
ENT1 esteban 234 1234
ENT3 juan 23434567
ENT3 jacinto 56745
ENT3 Perez 23467677
ENT2 indiana 678967
ENT1 jaime 234
RESULT
=====
ENTITY OVERALL
====== =======
ENT1 4
ENT2 1
ENT3 3
SELECT cod_entidad
, COUNT (DISTINCT yyyyyyyyy) AS overall
FROM registry rp,
product pc
where pc.nro_solicitud = rp.nro_solicitud
and pc.resp_2 = 'OK'
and pc.resp_1 = 'OK'
GROUP BY cod_entidad
Edited by: 862673 on 31/05/2011 13:41 -
How to use the distinct key in formula field in SAP Crystal Reports
I want to use the distinct key in formula field in SAP Crystal reports.
When i'm using it shows an error.
Please suggest me....Hi,
Use DistinctCount keyword directly for your calculation instead of count(distinct(....
Alternatively, if you want to avoid duplicate records, under "File" > "Report Options" make the 'Select Distinct Records' as True.
Thanks,
Raghavendra -
COUNT(DISTINCT) WITH ORDER BY in an analytic function
-- I create a table with three fields: Name, Amount, and a Trans_Date.
CREATE TABLE TEST
NAME VARCHAR2(19) NULL,
AMOUNT VARCHAR2(8) NULL,
TRANS_DATE DATE NULL
-- I insert a few rows into my table:
INSERT INTO TEST ( TEST.NAME, TEST.AMOUNT, TEST.TRANS_DATE ) VALUES ( 'Anna', '110', TO_DATE('06/01/2005 08:00:00 PM', 'MM/DD/YYYY HH12:MI:SS PM') );
INSERT INTO TEST ( TEST.NAME, TEST.AMOUNT, TEST.TRANS_DATE ) VALUES ( 'Anna', '20', TO_DATE('06/01/2005 08:00:00 PM', 'MM/DD/YYYY HH12:MI:SS PM') );
INSERT INTO TEST ( TEST.NAME, TEST.AMOUNT, TEST.TRANS_DATE ) VALUES ( 'Anna', '110', TO_DATE('06/02/2005 08:00:00 PM', 'MM/DD/YYYY HH12:MI:SS PM') );
INSERT INTO TEST ( TEST.NAME, TEST.AMOUNT, TEST.TRANS_DATE ) VALUES ( 'Anna', '21', TO_DATE('06/03/2005 08:00:00 PM', 'MM/DD/YYYY HH12:MI:SS PM') );
INSERT INTO TEST ( TEST.NAME, TEST.AMOUNT, TEST.TRANS_DATE ) VALUES ( 'Anna', '68', TO_DATE('06/04/2005 08:00:00 PM', 'MM/DD/YYYY HH12:MI:SS PM') );
INSERT INTO TEST ( TEST.NAME, TEST.AMOUNT, TEST.TRANS_DATE ) VALUES ( 'Anna', '110', TO_DATE('06/05/2005 08:00:00 PM', 'MM/DD/YYYY HH12:MI:SS PM') );
INSERT INTO TEST ( TEST.NAME, TEST.AMOUNT, TEST.TRANS_DATE ) VALUES ( 'Anna', '20', TO_DATE('06/06/2005 08:00:00 PM', 'MM/DD/YYYY HH12:MI:SS PM') );
INSERT INTO TEST ( TEST.NAME, TEST.AMOUNT, TEST.TRANS_DATE ) VALUES ( 'Bill', '43', TO_DATE('06/01/2005 08:00:00 PM', 'MM/DD/YYYY HH12:MI:SS PM') );
INSERT INTO TEST ( TEST.NAME, TEST.AMOUNT, TEST.TRANS_DATE ) VALUES ( 'Bill', '77', TO_DATE('06/02/2005 08:00:00 PM', 'MM/DD/YYYY HH12:MI:SS PM') );
INSERT INTO TEST ( TEST.NAME, TEST.AMOUNT, TEST.TRANS_DATE ) VALUES ( 'Bill', '221', TO_DATE('06/03/2005 08:00:00 PM', 'MM/DD/YYYY HH12:MI:SS PM') );
INSERT INTO TEST ( TEST.NAME, TEST.AMOUNT, TEST.TRANS_DATE ) VALUES ( 'Bill', '43', TO_DATE('06/04/2005 08:00:00 PM', 'MM/DD/YYYY HH12:MI:SS PM') );
INSERT INTO TEST ( TEST.NAME, TEST.AMOUNT, TEST.TRANS_DATE ) VALUES ( 'Bill', '73', TO_DATE('06/05/2005 08:00:00 PM', 'MM/DD/YYYY HH12:MI:SS PM') );
commit;
/* I want to retrieve all the distinct count of amount for every row in an analytic function with COUNT(DISTINCT AMOUNT) sorted by name and ordered by trans_date where I get only calculate for the last four trans_date for each row (i.e., for the row "Anna 110 6/5/2005 8:00:00.000 PM," I only want to look at the previous dates from 6/2/2005 to 6/5/2005 and get the distinct count of how many amounts there are different for Anna). Note, I cannot use the DISTINCT keyword in this query because it doesn't work with the ORDER BY */
select NAME, AMOUNT, TRANS_DATE, COUNT(/*DISTINCT*/ AMOUNT) over ( partition by NAME
order by TRANS_DATE range between numtodsinterval(3,'day') preceding and current row ) as COUNT_AMOUNT
from TEST t;
This is the results I get if I just count all the AMOUNT without using distinct:
NAME AMOUNT TRANS_DATE COUNT_AMOUNT
Anna 110 6/1/2005 8:00:00.000 PM 2
Anna 20 6/1/2005 8:00:00.000 PM 2
Anna 110 6/2/2005 8:00:00.000 PM 3
Anna 21 6/3/2005 8:00:00.000 PM 4
Anna 68 6/4/2005 8:00:00.000 PM 5
Anna 110 6/5/2005 8:00:00.000 PM 4
Anna 20 6/6/2005 8:00:00.000 PM 4
Bill 43 6/1/2005 8:00:00.000 PM 1
Bill 77 6/2/2005 8:00:00.000 PM 2
Bill 221 6/3/2005 8:00:00.000 PM 3
Bill 43 6/4/2005 8:00:00.000 PM 4
Bill 73 6/5/2005 8:00:00.000 PM 4
The COUNT_DISTINCT_AMOUNT is the desired output:
NAME AMOUNT TRANS_DATE COUNT_DISTINCT_AMOUNT
Anna 110 6/1/2005 8:00:00.000 PM 1
Anna 20 6/1/2005 8:00:00.000 PM 2
Anna 110 6/2/2005 8:00:00.000 PM 2
Anna 21 6/3/2005 8:00:00.000 PM 3
Anna 68 6/4/2005 8:00:00.000 PM 4
Anna 110 6/5/2005 8:00:00.000 PM 3
Anna 20 6/6/2005 8:00:00.000 PM 4
Bill 43 6/1/2005 8:00:00.000 PM 1
Bill 77 6/2/2005 8:00:00.000 PM 2
Bill 221 6/3/2005 8:00:00.000 PM 3
Bill 43 6/4/2005 8:00:00.000 PM 3
Bill 73 6/5/2005 8:00:00.000 PM 4
Thanks in advance.you can try to write your own udag.
here is a fake example, just to show how it "could" work. I am here using only 1,2,4,8,16,32 as potential values.
create or replace type CountDistinctType as object
bitor_number number,
static function ODCIAggregateInitialize(sctx IN OUT CountDistinctType)
return number,
member function ODCIAggregateIterate(self IN OUT CountDistinctType,
value IN number) return number,
member function ODCIAggregateTerminate(self IN CountDistinctType,
returnValue OUT number, flags IN number) return number,
member function ODCIAggregateMerge(self IN OUT CountDistinctType,
ctx2 IN CountDistinctType) return number
create or replace type body CountDistinctType is
static function ODCIAggregateInitialize(sctx IN OUT CountDistinctType)
return number is
begin
sctx := CountDistinctType('');
return ODCIConst.Success;
end;
member function ODCIAggregateIterate(self IN OUT CountDistinctType, value IN number)
return number is
begin
if (self.bitor_number is null) then
self.bitor_number := value;
else
self.bitor_number := self.bitor_number+value-bitand(self.bitor_number,value);
end if;
return ODCIConst.Success;
end;
member function ODCIAggregateTerminate(self IN CountDistinctType, returnValue OUT
number, flags IN number) return number is
begin
returnValue := 0;
for i in 0..log(2,self.bitor_number) loop
if (bitand(power(2,i),self.bitor_number)!=0) then
returnValue := returnValue+1;
end if;
end loop;
return ODCIConst.Success;
end;
member function ODCIAggregateMerge(self IN OUT CountDistinctType, ctx2 IN
CountDistinctType) return number is
begin
return ODCIConst.Success;
end;
end;
CREATE or REPLACE FUNCTION CountDistinct (n number) RETURN number
PARALLEL_ENABLE AGGREGATE USING CountDistinctType;
drop table t;
create table t as select rownum r, power(2,trunc(dbms_random.value(0,6))) p from all_objects;
SQL> select r,p,countdistinct(p) over (order by r) d from t where rownum<10 order by r;
R P D
1 4 1
2 1 2
3 8 3
4 32 4
5 1 4
6 16 5
7 16 5
8 4 5
9 4 5buy some good book if you want to start at writting your own "distinct" algorythm.
Message was edited by:
Laurent Schneider
a simpler but memory killer algorithm would use a plsql table in an udag and do the count(distinct) over that table to return the value -
Set Aggregation type of Count Distinct to use correct table aggregation in
Hi there,
Currently I use OBIEE 10.1.3.4.1 , and there is a case where a fact table consist of 2 logical table source: detail and aggregate table, which has some measure using count distinct as aggregation type. The problem is everytime I browse the measure with no dimension at all , it always use detail table not aggegation one..
Really appreciate for any suggestion ..
thanks a lotHi,
I don't think it's the same case as mine. Let say I have 2 table : detail and aggegate
Detail Table consists 4 fields:
*) Period
*) Market
*) Region
*) Measure : Customer ID, Sales
Aggregate Table consists 3 fields :
*) Period
*) Region
*) Measure : Customer ID, Sales
in the measure I set aggregation type for each field:
*) Sales >> set as Sum
*) Customer ID >> copy as "Number of Customer" and set as Count Distinct
In each LTS' contents I set the level of aggregation using "Get Levels" feature..
Then I try to browse via Presentation and do some querys belows:
a) only choose single field of measure : Sales, the session shows that the value is taken from aggregation table and just as I expected.
b) choose period and sales, the session shows that the values are taken from aggregation table, and still just as I expected.
c) choose period, sales , and market, the session shows that the values are taken from detail table, just as I expected.
d) only choose single field of measure : "Number of Customer", the session shows that the value is taken from detail table , this is NOT as I expected. It suppose to take the value from aggregation table..
e) choose period and "Number of Customer", the session shows that the value is taken from detail table , this is also NOT as I expected. It suppose to take the value from aggregation table..
I've tried to override the aggregation , but still confuse how to apply in measure "Number of Customer" and did not work at all..
any idea ?
thanks a lot -
Count Distinct Wtih CASE Statement - Does not follow aggregation path
All,
I have a fact table, a day aggregate and a month aggregate. I have a time hierarchy and the month aggregate is set to the month level, the day aggregate is set to the day level within the time hierarchy.
When using any measures and a field from my time dimension .. the appropriate aggregate is chosen, ie month & activity count .. month aggregate is used. Day & activity count .. day aggregate is used.
However - when I use the count distinct aggregate rule .. the request always uses the lowest common denominator. The way I have found to get this to work is to use a logical table source override in the aggregation tab. Once I do this .. it does use the aggregates correctly.
A few questions
1. Is this the correct way to use aggregate navigation for the count distinct aggregation rule (using the source override option)? If yes, why is this necessary for count distinct .. what is special about it?
2. The main problem I have now is that I need to create a simple count measure that has a CASE statement in it. The only way I see to do this is to select the Based on Dimensions checkbox which then allows me to add a CASE statement into my count distinct clause. But now the aggregation issue comes back into play and I can't do the logical table source override when the based on dimensions checkbox is checked .. so I am now stuck .. any help is appreciated.
KOk - I found a workaround (and maybe the preferred solution for my particular issue), which is - Using a CASE Statement with a COUNT DISTINCT aggregation and still havine AGGREGATE AWARENESS
To get all three of the requirements above to work I had to do the following:
- Create the COUNT DISTINCT as normal (counting on a USERID physically mapped column in my case)
- Now I need to map my fact and aggregates to this column. This is where I got the case statement to work. Instead of trying to put the case statement inside of the Aggregate definition by using the checkbox 'Base on Dimension' (which didnt allow for aggregate awareness for some reason) .. I instead specified the case statement in the Column Mapping section of the Fact and Aggregate tables.
- Once all the LTS's (facts and aggregates) are mapped .. you still have to define the Logical Table Source overrides in the aggregate tab of the count distinct definition. Add in all the fact and aggregates.
Now the measure will use my month aggregate when i specify month, the day aggregate when i specify day, etc..
If you are just trying to use a Count Distinct (no CASE satement needed) with Aggregate Awareness, you just need to use the Logical Table Source override on the aggregate tab.
There is still a funky issue when using the COUNT aggregate type. As long as you dont map multiple logical table sources to the COUNT column it works fine and as expected. But, if you try to add in multiple sources and aggregate awareness it randomly starts SUMMING everything .. very weird. The blog in this thread says to check the 'Based on Dimension' checkbox to fix the problem but that did not work for me. Still not sure what to do on this one .. but its not currently causing me a problem so I will ignore for now ;)
Thanks for all the help
K -
No more Count (distinct) : Power View in Excel 2013
Hello,
I used to create a Power View report using this field "Sport" where I choose
Count (Distinct)
Today this option is not available in the drop down menu. What did I miss?
ThanksHi,
The issue may be caused the "Sport filed" is modified or it is converted from a text (non-numeric) field to a measure. It shows the "Count" option, please see the
article. We may try to remove the filed and re-add the filed to the field list.
If you want to know more about "based Power View Reports", we can download a sample with the below link and test the steps.
http://office.microsoft.com/en-us/excel-help/tutorial-create-map-based-power-view-reports-HA104075403.aspx
Regards,
George Zhao
TechNet Community Support -
Efficiency of "Count(Distinct Case" in SQL
Hi,
Could you please let me know if "Count(Distinct Case" statement is efficient for a million rows or is there a better way to do it
For example -this table below contains a set of customers with status flag as 'new' or 'existing'.
CREATE TABLE tableA
( cust_id NUMBER
, status VARCHAR(10)
,txn_id NUMBER
INSERT INTO tableA (cust_id, status,txn_id) VALUES ( 6433, 'New', 11);
INSERT INTO tableA (cust_id, status,txn_id) VALUES ( 6433, 'New', 21);
INSERT INTO tableA (cust_id, status,txn_id) VALUES ( 1234, 'existing', 31);
INSERT INTO tableA (cust_id, status,txn_id) VALUES ( 1234, 'existing', 41);
INSERT INTO tableA (cust_id, status,txn_id) VALUES ( 7654, 'New', 51);
INSERT INTO tableA (cust_id, status,txn_id) VALUES ( 7654, 'New', 61);
INSERT INTO tableA (cust_id, status,txn_id) VALUES ( 9999, 'existing', 71);
INSERT INTO tableA (cust_id, status,txn_id) VALUES ( 8888, 'New', 81);
INSERT INTO tableA (cust_id, status,txn_id) VALUES ( 8888, 'existing', 91);
INSERT INTO tableA (cust_id, status,txn_id) VALUES ( 2121, 'New', 100);
am using the below SQL to calculate the number of distinct customers with status 'New'.
Select
Count(Distinct Case When status = 'New' Then cust_id end) New_Cust_Cnt
from tableA
Regards
-Learnsequelsan wrote:
Hello,
Select
Count(Distinct Case When status = 'New' Then cust_id end) New_Cust_Cnt
from tableA
_Use like this:_
Select
Count(cust_id) New_Cust_Cnt
from tableA
where status='new';And also you can create index on status you will get faster.
Thanks,
SanjeevaAny how you have to use DISTINCT keywork. Otherwise you will not get the correct results for the OP's data. -
Maximum, Count Distinct functions in Template.
Hi,
I have a dataset which I was trying to group for a report.
I need in report in such a way that, I have to list product code in one column and Max(Sub-product Code) in another column product code,Sub Product code are strings and not Numbers.
Suppose my data set has
product-code|sub-product-code
abc|1a
abc|1b
abc|1c
def|2a
def|2b
def|2c
I need in report
abc|1a
def|2b
I grouped it and I was trying to show Max(subproduct code) of current group. But it was giving me numer instead of text. I tried to select "text" for the form field. But it is automatically chaging to Number.
Also, Can you let me know how to write the count(distinct subproduct code) . I also,need to add this column.
I cannot add them in the SQL because, there are tons of templates on this dataset. I have to do it in template.
Any help is greately appreciated.Try using this: <?count(xdoxslt:distinct_values(field_name))?> -- substitute your sub product code field name
Thanks,
Bipuser -
Count distinct aggregation in CWM2
Hi,
Is there any way to make a count distinct aggregation with a CWM2 model?
I mean, for the documentation example (geography, product, channel and time dimensions), I would like to show in a graph the sum of the sales measure and the number of different products, for the different filter selections made.
E.g.:
Page items: geography dim, time dim.
Groups: Measures (sales measure, ¿number of different products?)
Series: channel dim.
I know that the unique possible aggregation in CWM2 model is SUM, so I think it can't be done with an aggregation. Can anyone suggest me a workaround to do this? Is it possible within an analytical workspace?
Thanks in advanced...Hi Nagarajan,
1. have to count distinct records from one internal ta
first of all u have to decide
which FIELD COMBINATION makes a record unique.
2. After that u can use the abap syntax
delete adjacent duplicates (see documention/help)
(before this make sure to SORT the internal table
in the same sequence of FIELD COMBINATION)
3. then u can use
describe table itab.
4. Before doing step 2,
u can copy the whole internal table to another internal table
and do your logic on the second internal table
so that the original is not lost.
u can copylike this.
ITAB2[] = ITAB1[].
Hope it helps.
Regards,
Amit M.
Message was edited by: Amit Mittal -
Hi there,
I have a below query that has a count field, which I want to have count of unique records.
SELECT [SGMT_REV_BY_SITE].GLSeq, Count([SGMT_REV_BY_SITE].SITE_ADDR_ID) AS NUM_SITES, [SGMT_REV_BY_SITE].SGMNT1, [SGMT_REV_BY_SITE].SGMNT2, [SGMT_REV_BY_SITE].LOB$, [SGMT_REV_BY_SITE].PERIOD, [SGMT_REV_BY_SITE].RECUR, [SGMT_REV_BY_SITE].PROMOTED, Sum([SGMT_REV_BY_SITE].REV_GRS)
AS REV_GRS_Sum, Sum([SGMT_REV_BY_SITE].REV_NET) AS REV_NET_Sum, IIf(NUM_SITES >0, REV_GRS_Sum/NUM_SITES, REV_GRS_Sum) AS ARPSg_Sum, IIf(NUM_SITES >0,
REV_NET_Sum/NUM_SITES, REV_NET_Sum) AS ARPSn_Sum
FROM SGMT_REV_BY_SITE
WHERE ([SGMT_REV_BY_SITE].SGMNT1="Small"
Or [SGMT_REV_BY_SITE].SGMNT1="Medium")
And [SGMT_REV_BY_SITE].PERIOD>#12/31/2013#
And [SGMT_REV_BY_SITE].RECUR=Yes
And [SGMT_REV_BY_SITE].PROMOTED=Yes
And [SGMT_REV_BY_SITE].REV_NET<>0
GROUP BY [SGMT_REV_BY_SITE].GLSeq, [SGMT_REV_BY_SITE].LOB$, [SGMT_REV_BY_SITE].SGMNT1, [SGMT_REV_BY_SITE].SGMNT2, [SGMT_REV_BY_SITE].PERIOD, [SGMT_REV_BY_SITE].RECUR, [SGMT_REV_BY_SITE].PROMOTED;
Is there an easy way of accomplishing the task.
Thanks in advance for help.ARSagit,
Unfortunatly access don't support count(distinct fieldname) statement...
Workaround is:
SELECT [NS].glseq,
Count([NS].site_addr_id) AS NUM_SITES,
[NS].sgmnt1,
[NS].sgmnt2,
[NS].lob$,
[NS].period,
[NS].recur,
[NS].promoted,
FROM (
SELECT DISTINCT
[sgmt_rev_by_site].glseq,
[sgmt_rev_by_site].site_addr_id
[sgmt_rev_by_site].sgmnt1,
[sgmt_rev_by_site].sgmnt2,
[sgmt_rev_by_site].lob$,
[sgmt_rev_by_site].period,
[sgmt_rev_by_site].recur,
[sgmt_rev_by_site].promoted,
FROM
sgmt_rev_by_site
WHERE [sgmt_rev_by_site].sgmnt1 in ("small","medium")
AND [sgmt_rev_by_site].period >#12/31/2013#
AND [sgmt_rev_by_site].recur = yes
AND [sgmt_rev_by_site].promoted = yes
AND [sgmt_rev_by_site].rev_net <>0) AS NS
GROUP BY [NS].glseq,
[NS].lob$,
[NS].sgmnt1,
[NS].sgmnt2,
[NS].period,
[NS].recur,
[NS].promoted;
With this quer you can join with main query and show num_sites....
Michał
Maybe you are looking for
-
My thinkpad t420s doesn't last 2 hours
hardware and software: * thinkpad t420s, brand new with standard battery * CPU: i7-2640M CPU @ 2.80GHz, 4 cores. frequency scaling is enabled, they all usually run at 800MHz (ondemand governor) * intel SSD, no HDD * wifi: iwlwifi: Intel(R) Centrino(R
-
Why can't I access my home country App Store while in another country?
I've noticed that since the last iOS update that the new App Store will not let me download updates. The apps were purchased using the Canadian App Store. Some purchases were made while I was physically in Canada. Others were made while traveling. I
-
Drill Down Reports and Drag & Drop capability Tool for Developer2000
I am currently doing my development in Developer2000 rel. 2.0. I have seen many tools for VB which provide the facility for Drill Down reporting and drag & drop option in reporting, but unable to find any tool for developer2000. I wonder if anyone ca
-
Dynamiclly remove node from JTree
hi all, I want to remove nodes from a tree dynamically. I used "treeModel.removeNodeFromParent(node);". The node was removed from that treeModel, but the tree never updated. I tried to use tree.repaint() method. It doesn't work. Any suggestion? thank
-
Fireworks CS3 doesn't work in windows 7
Hi Guys, I have used a version 9.0.0.1188 of Fireworks CS3 for a while in my PCs with OS Windows XP and Vista Home without any problem, but now I have a new PC with windows 7, and I can open Fireworks CS3, but then I can't open old files, and I can't