COUNT (DISTINCT <; nom_de_colonne >;)
I would like to display the count (DISTINCT < company >) at the end of the report. I use a SQL query in the form of data / the whole model. Thanks in advance.NEW YORK CITY
JANUARY 1, 2008
Company ID DIFF
ABC 1-1
2 2 XYZ
ABC 3-1
FEBRUARY 1, 2008
Company ID DIFF
MNO 1-1
2 2 XYZ
MNO 3-1
Total number of companies: 3
Use this
Tags: Business Intelligence
Similar Questions
-
Count distinct users registered
Hello
I have a table that contains the users and their hours of connection and disconnection. I need to know the maximum number of distinct users connected for a period of 15 minutes for each day. Periods of 15 minutes, start at 00:00 and ends at 23:45
For the above test data, I expect to see:WITH user_data AS (SELECT 'USER1' user_id, TO_DATE('01-DEC-2010 11:57', 'DD-MON-YYYY HH24:MI') login_date, TO_DATE('01-DEC-2010 12:34', 'DD-MON-YYYY HH24:MI') logout_date FROM dual UNION ALL SELECT 'USER1' user_id, TO_DATE('01-DEC-2010 12:18', 'DD-MON-YYYY HH24:MI') login_date, TO_DATE('01-DEC-2010 12:23', 'DD-MON-YYYY HH24:MI') logout_date FROM dual UNION ALL SELECT 'USER1' user_id, TO_DATE('01-DEC-2010 12:41', 'DD-MON-YYYY HH24:MI') login_date, TO_DATE('01-DEC-2010 13:20', 'DD-MON-YYYY HH24:MI') logout_date FROM dual UNION ALL SELECT 'USER1' user_id, TO_DATE('01-DEC-2010 13:22', 'DD-MON-YYYY HH24:MI') login_date, TO_DATE('01-DEC-2010 15:12', 'DD-MON-YYYY HH24:MI') logout_date FROM dual UNION ALL SELECT 'USER1' user_id, TO_DATE('01-DEC-2010 13:25', 'DD-MON-YYYY HH24:MI') login_date, TO_DATE('01-DEC-2010 13:26', 'DD-MON-YYYY HH24:MI') logout_date FROM dual UNION ALL SELECT 'USER2' user_id, TO_DATE('01-DEC-2010 13:27', 'DD-MON-YYYY HH24:MI') login_date, TO_DATE('01-DEC-2010 13:30', 'DD-MON-YYYY HH24:MI') logout_date FROM dual UNION ALL SELECT 'USER1' user_id, TO_DATE('01-DEC-2010 13:34', 'DD-MON-YYYY HH24:MI') login_date, TO_DATE('01-DEC-2010 15:08', 'DD-MON-YYYY HH24:MI') logout_date FROM dual UNION ALL SELECT 'USER1' user_id, TO_DATE('01-DEC-2010 13:53', 'DD-MON-YYYY HH24:MI') login_date, TO_DATE('01-DEC-2010 16:38', 'DD-MON-YYYY HH24:MI') logout_date FROM dual UNION ALL SELECT 'USER3' user_id, TO_DATE('01-DEC-2010 14:00', 'DD-MON-YYYY HH24:MI') login_date, TO_DATE('22-FEB-2011 14:18', 'DD-MON-YYYY HH24:MI') logout_date FROM dual UNION ALL SELECT 'USER1' user_id, TO_DATE('01-DEC-2010 14:14', 'DD-MON-YYYY HH24:MI') login_date, TO_DATE('01-DEC-2010 15:20', 'DD-MON-YYYY HH24:MI') logout_date FROM dual UNION ALL SELECT 'USER1' user_id, TO_DATE('01-DEC-2010 14:15', 'DD-MON-YYYY HH24:MI') login_date, TO_DATE('01-DEC-2010 15:21', 'DD-MON-YYYY HH24:MI') logout_date FROM dual UNION ALL SELECT 'USER1' user_id, TO_DATE('01-DEC-2010 14:23', 'DD-MON-YYYY HH24:MI') login_date, TO_DATE('01-DEC-2010 15:29', 'DD-MON-YYYY HH24:MI') logout_date FROM dual UNION ALL SELECT 'USER1' user_id, TO_DATE('01-DEC-2010 14:30', 'DD-MON-YYYY HH24:MI') login_date, TO_DATE('01-DEC-2010 19:12', 'DD-MON-YYYY HH24:MI') logout_date FROM dual UNION ALL SELECT 'USER1' user_id, TO_DATE('01-DEC-2010 14:36', 'DD-MON-YYYY HH24:MI') login_date, TO_DATE('01-DEC-2010 15:46', 'DD-MON-YYYY HH24:MI') logout_date FROM dual UNION ALL SELECT 'USER1' user_id, TO_DATE('01-DEC-2010 14:39', 'DD-MON-YYYY HH24:MI') login_date, TO_DATE('01-DEC-2010 16:40', 'DD-MON-YYYY HH24:MI') logout_date FROM dual UNION ALL SELECT 'USER1' user_id, TO_DATE('01-DEC-2010 14:44', 'DD-MON-YYYY HH24:MI') login_date, TO_DATE('01-DEC-2010 16:08', 'DD-MON-YYYY HH24:MI') logout_date FROM dual UNION ALL SELECT 'USER1' user_id, TO_DATE('02-DEC-2010 09:10', 'DD-MON-YYYY HH24:MI') login_date, TO_DATE('02-DEC-2010 12:25', 'DD-MON-YYYY HH24:MI') logout_date FROM dual UNION ALL SELECT 'USER1' user_id, TO_DATE('02-DEC-2010 09:52', 'DD-MON-YYYY HH24:MI') login_date, TO_DATE('02-DEC-2010 13:01', 'DD-MON-YYYY HH24:MI') logout_date FROM dual UNION ALL SELECT 'USER1' user_id, TO_DATE('02-DEC-2010 10:03', 'DD-MON-YYYY HH24:MI') login_date, TO_DATE('02-DEC-2010 13:08', 'DD-MON-YYYY HH24:MI') logout_date FROM dual UNION ALL SELECT 'USER1' user_id, TO_DATE('02-DEC-2010 10:37', 'DD-MON-YYYY HH24:MI') login_date, TO_DATE('02-DEC-2010 11:53', 'DD-MON-YYYY HH24:MI') logout_date FROM dual UNION ALL SELECT 'USER2' user_id, TO_DATE('02-DEC-2010 10:40', 'DD-MON-YYYY HH24:MI') login_date, TO_DATE('02-DEC-2010 12:01', 'DD-MON-YYYY HH24:MI') logout_date FROM dual UNION ALL SELECT 'USER1' user_id, TO_DATE('02-DEC-2010 10:54', 'DD-MON-YYYY HH24:MI') login_date, TO_DATE('02-DEC-2010 10:59', 'DD-MON-YYYY HH24:MI') logout_date FROM dual UNION ALL SELECT 'USER1' user_id, TO_DATE('02-DEC-2010 10:55', 'DD-MON-YYYY HH24:MI') login_date, TO_DATE('02-DEC-2010 11:02', 'DD-MON-YYYY HH24:MI') logout_date FROM dual UNION ALL SELECT 'USER1' user_id, TO_DATE('02-DEC-2010 10:59', 'DD-MON-YYYY HH24:MI') login_date, TO_DATE('02-DEC-2010 11:00', 'DD-MON-YYYY HH24:MI') logout_date FROM dual UNION ALL SELECT 'USER2' user_id, TO_DATE('02-DEC-2010 11:00', 'DD-MON-YYYY HH24:MI') login_date, TO_DATE('02-DEC-2010 12:58', 'DD-MON-YYYY HH24:MI') logout_date FROM dual UNION ALL SELECT 'USER1' user_id, TO_DATE('02-DEC-2010 11:20', 'DD-MON-YYYY HH24:MI') login_date, TO_DATE('02-DEC-2010 13:12', 'DD-MON-YYYY HH24:MI') logout_date FROM dual UNION ALL SELECT 'USER1' user_id, TO_DATE('02-DEC-2010 11:45', 'DD-MON-YYYY HH24:MI') login_date, TO_DATE('02-DEC-2010 14:18', 'DD-MON-YYYY HH24:MI') logout_date FROM dual UNION ALL SELECT 'USER1' user_id, TO_DATE('02-DEC-2010 11:53', 'DD-MON-YYYY HH24:MI') login_date, TO_DATE('02-DEC-2010 13:10', 'DD-MON-YYYY HH24:MI') logout_date FROM dual UNION ALL SELECT 'USER1' user_id, TO_DATE('02-DEC-2010 11:57', 'DD-MON-YYYY HH24:MI') login_date, TO_DATE('02-DEC-2010 12:54', 'DD-MON-YYYY HH24:MI') logout_date FROM dual UNION ALL SELECT 'USER1' user_id, TO_DATE('02-DEC-2010 12:01', 'DD-MON-YYYY HH24:MI') login_date, TO_DATE('02-DEC-2010 12:54', 'DD-MON-YYYY HH24:MI') logout_date FROM dual UNION ALL SELECT 'USER1' user_id, TO_DATE('02-DEC-2010 12:09', 'DD-MON-YYYY HH24:MI') login_date, TO_DATE('02-DEC-2010 13:37', 'DD-MON-YYYY HH24:MI') logout_date FROM dual UNION ALL SELECT 'USER3' user_id, TO_DATE('02-DEC-2010 12:12', 'DD-MON-YYYY HH24:MI') login_date, TO_DATE('02-DEC-2010 12:13', 'DD-MON-YYYY HH24:MI') logout_date FROM dual UNION ALL SELECT 'USER1' user_id, TO_DATE('02-DEC-2010 12:54', 'DD-MON-YYYY HH24:MI') login_date, TO_DATE('02-DEC-2010 15:58', 'DD-MON-YYYY HH24:MI') logout_date FROM dual UNION ALL SELECT 'USER1' user_id, TO_DATE('02-DEC-2010 13:12', 'DD-MON-YYYY HH24:MI') login_date, TO_DATE('02-DEC-2010 13:19', 'DD-MON-YYYY HH24:MI') logout_date FROM dual UNION ALL SELECT 'USER1' user_id, TO_DATE('02-DEC-2010 13:13', 'DD-MON-YYYY HH24:MI') login_date, TO_DATE('02-DEC-2010 15:11', 'DD-MON-YYYY HH24:MI') logout_date FROM dual UNION ALL SELECT 'USER1' user_id, TO_DATE('02-DEC-2010 13:22', 'DD-MON-YYYY HH24:MI') login_date, TO_DATE('02-DEC-2010 13:50', 'DD-MON-YYYY HH24:MI') logout_date FROM dual), pivs AS (SELECT 0 piv_num FROM dual UNION ALL SELECT 1 piv_num FROM dual UNION ALL SELECT 2 piv_num FROM dual UNION ALL SELECT 3 piv_num FROM dual UNION ALL SELECT 4 piv_num FROM dual UNION ALL SELECT 5 piv_num FROM dual UNION ALL SELECT 6 piv_num FROM dual UNION ALL SELECT 7 piv_num FROM dual UNION ALL SELECT 8 piv_num FROM dual UNION ALL SELECT 9 piv_num FROM dual UNION ALL SELECT 10 piv_num FROM dual UNION ALL SELECT 11 piv_num FROM dual UNION ALL SELECT 12 piv_num FROM dual UNION ALL SELECT 13 piv_num FROM dual UNION ALL SELECT 14 piv_num FROM dual UNION ALL SELECT 15 piv_num FROM dual UNION ALL SELECT 16 piv_num FROM dual UNION ALL SELECT 17 piv_num FROM dual UNION ALL SELECT 18 piv_num FROM dual UNION ALL SELECT 19 piv_num FROM dual UNION ALL SELECT 20 piv_num FROM dual UNION ALL SELECT 21 piv_num FROM dual UNION ALL SELECT 22 piv_num FROM dual UNION ALL SELECT 23 piv_num FROM dual UNION ALL SELECT 24 piv_num FROM dual UNION ALL SELECT 25 piv_num FROM dual UNION ALL SELECT 26 piv_num FROM dual UNION ALL SELECT 27 piv_num FROM dual UNION ALL SELECT 28 piv_num FROM dual UNION ALL SELECT 29 piv_num FROM dual UNION ALL SELECT 30 piv_num FROM dual UNION ALL SELECT 31 piv_num FROM dual UNION ALL SELECT 32 piv_num FROM dual UNION ALL SELECT 33 piv_num FROM dual UNION ALL SELECT 34 piv_num FROM dual UNION ALL SELECT 35 piv_num FROM dual UNION ALL SELECT 36 piv_num FROM dual UNION ALL SELECT 37 piv_num FROM dual UNION ALL SELECT 38 piv_num FROM dual UNION ALL SELECT 39 piv_num FROM dual UNION ALL SELECT 40 piv_num FROM dual UNION ALL SELECT 41 piv_num FROM dual UNION ALL SELECT 42 piv_num FROM dual UNION ALL SELECT 43 piv_num FROM dual UNION ALL SELECT 44 piv_num FROM dual UNION ALL SELECT 45 piv_num FROM dual UNION ALL SELECT 46 piv_num FROM dual UNION ALL SELECT 47 piv_num FROM dual UNION ALL SELECT 48 piv_num FROM dual UNION ALL SELECT 49 piv_num FROM dual UNION ALL SELECT 50 piv_num FROM dual UNION ALL SELECT 51 piv_num FROM dual UNION ALL SELECT 52 piv_num FROM dual UNION ALL SELECT 53 piv_num FROM dual UNION ALL SELECT 54 piv_num FROM dual UNION ALL SELECT 55 piv_num FROM dual UNION ALL SELECT 56 piv_num FROM dual UNION ALL SELECT 57 piv_num FROM dual UNION ALL SELECT 58 piv_num FROM dual UNION ALL SELECT 59 piv_num FROM dual UNION ALL SELECT 60 piv_num FROM dual UNION ALL SELECT 61 piv_num FROM dual UNION ALL SELECT 62 piv_num FROM dual UNION ALL SELECT 63 piv_num FROM dual UNION ALL SELECT 64 piv_num FROM dual UNION ALL SELECT 65 piv_num FROM dual UNION ALL SELECT 66 piv_num FROM dual UNION ALL SELECT 67 piv_num FROM dual UNION ALL SELECT 68 piv_num FROM dual UNION ALL SELECT 69 piv_num FROM dual UNION ALL SELECT 70 piv_num FROM dual UNION ALL SELECT 71 piv_num FROM dual UNION ALL SELECT 72 piv_num FROM dual UNION ALL SELECT 73 piv_num FROM dual UNION ALL SELECT 74 piv_num FROM dual UNION ALL SELECT 75 piv_num FROM dual UNION ALL SELECT 76 piv_num FROM dual UNION ALL SELECT 77 piv_num FROM dual UNION ALL SELECT 78 piv_num FROM dual UNION ALL SELECT 79 piv_num FROM dual UNION ALL SELECT 80 piv_num FROM dual UNION ALL SELECT 81 piv_num FROM dual UNION ALL SELECT 82 piv_num FROM dual UNION ALL SELECT 83 piv_num FROM dual UNION ALL SELECT 84 piv_num FROM dual UNION ALL SELECT 85 piv_num FROM dual UNION ALL SELECT 86 piv_num FROM dual UNION ALL SELECT 87 piv_num FROM dual UNION ALL SELECT 88 piv_num FROM dual UNION ALL SELECT 89 piv_num FROM dual UNION ALL SELECT 90 piv_num FROM dual UNION ALL SELECT 91 piv_num FROM dual UNION ALL SELECT 92 piv_num FROM dual UNION ALL SELECT 93 piv_num FROM dual UNION ALL SELECT 94 piv_num FROM dual UNION ALL SELECT 95 piv_num FROM dual UNION ALL SELECT 96 piv_num FROM dual UNION ALL SELECT 97 piv_num FROM dual UNION ALL SELECT 98 piv_num FROM dual UNION ALL SELECT 99 piv_num FROM dual) SELECT s2.cdate, MAX(s2.user_count) FROM (SELECT TO_CHAR(t.cdate, 'DD-MON-YYYY') cdate, SUM(LEAST(t.user_count, 1)) user_count FROM (SELECT TO_DATE('01-DEC-2010', 'DD-MON-YYYY') + p1.piv_num cdate, TO_CHAR(TO_DATE((p2.piv_num * 15) * 60, 'SSSSS'), 'HH24:MI') time, SUM(CASE WHEN TRUNC((TO_DATE('01-DEC-2010', 'DD-MON-YYYY') + p1.piv_num) + (p2.piv_num * 15)/1440, 'MI') BETWEEN s.login_date and s.logout_date THEN 1 ELSE 0 END) user_count FROM pivs p1, pivs p2, (SELECT user_id, login_date, logout_date FROM user_data) s WHERE p1.piv_num < 2 -- number of days I want to look at i.e. 01-DEC-2010 and 02-DEC-2010 AND p2.piv_num < 96 -- number of 15 minute periods in a day i.e. 00:00 to 23:45 GROUP BY p1.piv_num, p2.piv_num, s.user_id ORDER BY p1.piv_num, p2.piv_num) t GROUP BY t.cdate, t.time) s2 GROUP BY s2.cdate ORDER BY TO_DATE(s2.cdate) /
The code above works, but I'm sure there must be a better way to do it, I can't see how...Date Users ----------- ----- 01-DEC-2010 2 02-DEC-2010 3
LeeHello
Here's one way:
WITH parameters AS ( SELECT TO_DATE ( '01-Dec-2010' -- Starting time (included in output) , 'DD-Mon-YYYY' ) AS start_dt , TO_DATE ( '03-Dec-2010' -- Ending time (NOT included in output) , 'DD-Mon-YYYY' ) AS end_dt , 24 * 4 AS periods_per_day FROM dual ) , periods AS ( SELECT start_dt + ((LEVEL - 1) / periods_per_day) AS period_start , start_dt + ( LEVEL / periods_per_day) AS period_end FROM parameters CONNECT BY LEVEL <= (end_dt - start_dt) * periods_per_day ) , got_cnt_per_period AS ( SELECT p.period_start , COUNT (distinct user_id) AS cnt , ROW_NUMBER () OVER ( PARTITION BY TRUNC (period_start) ORDER BY COUNT (*) DESC , period_start ) AS rnk FROM periods p JOIN user_data u ON p.period_start <= u.logout_date AND p.period_end > u.login_date GROUP BY p.period_start ) SELECT period_start , cnt FROM got_cnt_per_period WHERE rnk = 1 ORDER BY period_start ;
You will notice that it does not use the pivs table. Generate periods of time with a CONNECT BY query is likely to be more effective, and you don't have to worry about having enough rows in the table pivs.
In this way avoids also all conversions between DATEs and strings (except for the entrance of the start_dt and end_dt parameters), which should take some time.If you are actually hardcode the parameters in the query, the abopve version will be easier to use and maintain, because each parameter only has to register once, in the first auxiliary request.
The output includes the start time of the period who had the highest number on each day. If there are equal (as in this example of data, there were 3 periods December 1, which had a number of 2), then the specified period is that earlier that day. If you want to see all, replace ROW_NUMBER RANK by the last auxiliary request, got_cnt_per_peroid. Of course, you don't have to display the time if you do not want.
-
OVERALL TOTAL incorrect (with COUNT DISTINCT)
Hello
I get incorrect results in a DISTINCT COUNT measure column GRAND TOTAL.
I have 5 separate in Paris and 10 separate clients in New York, I want the grand total for the sum of the two, it is 15.
But OBIEE calculates separate customers for all cities, so if there are customers in Paris and New York, the result is false.
This is the result I get:
City Number_Distinct_Customers
----------------------------------------------------------------------------
Paris 5
NEW YORK CITY 10
GRAND TOTAL 12
12 is the number of all separate clients.
The correct GRANT TOTAL is expected to be 5 + 10 = 15
Thank you
ConcerningI guess that COUNT(DISTINCT...) is regarded as the default aggregation in the Oracle replies.
To come in this, change the State of aggregation of fx (formula column in the criteria of answers tab) to 'SUM '...Let me know if that solves your problem.
-bifacts :-)
http://www.obinotes.com -
How to count distinct exclusion of a value in the business layer?
Hi all
I have a column that has a lot of values. I need to do is a measure with aggregator separate count. But I shouldn't count 0 in the column. How can I do that. If I try to use any way to condition the aggregator option is disable. Help, please
Thank youLook at this example:
I did MDB table is DIRTY as:
Count_Distinct_Prod_Id_Exclude_Prod_Id_144
I'll count distinct PRODUCTS. PROD_ID, but exclude PROD_ID = 144 when counting.
Make this measure like this:
1. new column object/logic
2. go in tab data type and click EDIT the table logic table source
3. now, in the general tab add join of a table (in my case of PRODUCTS)
4. go in the column mapping tab-> see the deleted column mapping5. in the new column (in my case Count_Distinct_Prod_Id_Exclude_Prod_Id_144) write similar code:
CASE WHEN "orcl". » ». "" SH ". "PRODUCTS '." "' PROD_ID ' = 144 THEN ELSE NULL"orcl ". » ». "" SH ". "PRODUCTS '." "' PROD_ID ' END6. click OK and close the source logical table
7. now, in the logical column window go to the tab of the aggregation and choose COUNT DISTINCT.8. move the Count_Distinct_Prod_Id_Exclude_Prod_Id_144 measure for presentation
9 test answers (report cointains columns as follows)
PROD_CATEGORY_ID
Count_Distinct_Prod_Id_Exclude_Prod_Id_144And the result in the NQQuery.log is:
Select T21473. PROD_CATEGORY_ID C1,
Count (distinct from cases when T21473.) PROD_ID is 144, then NULL else T21473. End PROD_ID) C2
Of
PRODUCTS T21473
Group of T21473. PROD_CATEGORY_ID
order of c1Concerning
Goran
http://108obiee.blogspot.com -
What is the difference between the meter and the meter separate in the advanced INTERFACE
Operations-> all settings-> deployed-> Count?
This metric is available in the advanced user interface and the custom user interface. Measures are not read in the Admin UI.
Distinct count is the actual number of virtual machines deployed, while the County is # trend / forecasting of the deployed virtual machines. Usually, you will notice the separate account doesn't have a very clear dynamic threshold, but the County has a dynamic rise. Speaking of which, I would say probably a couple more VMs to meet forecasts of my lab... ha.
-
Hello world
I read an article about a measure of viscosity (http://www.seomoz.org/blog/tracking-browse-rate-a-cool-stickiness-metric). It's basically a way to measure how well you attract people to your Web site.
Viscosity for the last 7 days: (County of different users today) / (number of distinct users for the last 7 days)
Viscosity for the last 30 days: (County of different users today) / (number of distinct users for the last 30 days)
I have a table called WC_WEB_VISITS_F that is in the grain of the user's visit to one of my web sites.
Is there a way I can use OBIEE to calculate values above?
I can get the numerator. I create a logical column called count of separate to everyday users and aggregation set to Distinct Count on USER_ID and then I pivot my report to the Date of visit.
I can't understand how Distinct Count for X days. If there were separate count for this month, I just copy the count of distinct daily users and set the content to the month level, however, it is a floating range, I can't do that.
Does anyone have an idea of Cleaver for this?
Thank you!
-Joe11g is the function PERIODROLLING, which is the answer to all these requirements. The PERIODROLLING function allows you to perform an aggregation on a specified set of periods of grain of query rather than a grain of fixed time series. The most common use is to create rolling averages, as "13-week Rolling average." With 10 all we will get is a feature limited according to solutions...
-
Maximum, Count Distinct functions in the model.
Hello
I have a set of data that I tried to regroup for a report.
I need in the report so that I have product code list in a column and Max (Product Code) in another column product code, product code Sub are strings and not numbers.
Suppose that my data set has
product code:-product-sub-code
____________________________
ABC | 1A
ABC | 1 b
ABC | 1 c
def | 1 B
def | 2B
def | 2 c
...
...
....
...
...
I need to report
ABC | 1A
def | 2B
..
I gathered it and I tried to show the Max (product code) for the current group. But it gave me a number instead of text. I tried to select 'text' to the form field. But it is automatically change the number.
Also, can you let me know how to write the number (code of separate by-product). I must also add that column.
I can't add in SQL because there are tons of models on this data set. I have to do it in the model.
Any help is greately appreciated.Try using this: -substitute your domain under product code name
Thank you
Bipuser -
Distinct count in the instruction box
-------------------------------------------------------------------------------------------------
SELECT A.P_ID,
B.P_NAME,
C.P_DESC,
SUM (CASE
WHEN A.DATE BETWEEN TRUNC (ADD_MONTHS (LAST_DAY (SYSDATE),-4) + 1) AND ADD_MONTHS (LAST_DAY (TO_DATE (SYSDATE)),-1)
AND A.M_ID IS NOT NULL
THEN 1
0 OTHERWISE
END) AS COUNT,
SUM (CASE
WHEN A.DATE BETWEEN TRUNC (ADD_MONTHS (LAST_DAY (SYSDATE),-4) + 1) AND ADD_MONTHS (LAST_DAY (TO_DATE (SYSDATE)),-1)
AND A.M_ID IS NOT NULL
THEN COUNT (DISTINCT A.M_ID)
0 OTHERWISE
END) AS UNIQUE_COUNT, / * is not possible * /.
SUM (CASE
WHEN A.DATE BETWEEN TRUNC(SYSDATE,'YEAR') AND ADD_MONTHS (LAST_DAY (TO_DATE (SYSDATE)), - 1).
THEN A.AMT_1
0 OTHERWISE
END) AS TOTAL_AMT_1,
SUM (CASE
WHEN A.DATE BETWEEN TRUNC(SYSDATE,'YEAR') AND ADD_MONTHS (LAST_DAY (TO_DATE (SYSDATE)), - 1).
THEN A.AMT_2
0 OTHERWISE
END) AS TOTAL_AMT_2
FROM TABLE_A A,.
TABLE_B B,.
C TABLE_C
WHERE A.P_ID = B.P_ID
AND B.PT_ID = C.PT_ID
A.P_ID GROUP,
B.P_NAME,
C.P_DESC
----------------------------------------------------------------------------------------------------------
Hello
It is a simplified version of my request.
I'm doing 4 things here,
1. the County A.M_ID
2 County has separate .M_ID, is where I have a problem.
3 and 4. It is just the sum of 2 diff columns.
Note that the dates for the County and the amt are different and I can't hard-code the.
Can someone help me step of distinct count?
This query runs also a bit slow.
So all suggestions, comments are welcome.
Note: TABLE_A REB 700 million, TABLE_B 4 million and TABLE_c is located only 500 RECS
Thank you!Advantage of the fact that aggregate functions more ignore nulls, you could do something like:
SELECT a.p_id, b.p_name, c.p_desc, COUNT(CASE WHEN a.date BETWEEN TRUNC(ADD_MONTHS(LAST_DAY(sysdate),-4) + 1) AND ADD_MONTHS(LAST_DAY(TO_DATE(sysdate)),-1) AND a.m_id IS NOT NULL THEN m_id END) AS countall, COUNT(DISTINCT CASE WHEN a.date BETWEEN TRUNC(ADD_MONTHS(LAST_DAY(sysdate),-4) + 1) AND ADD_MONTHS(LAST_DAY(TO_DATE(sysdate)),-1) AND a.m_id IS NOT NULL THEN a.m_id END) AS unique_count, /* entirely possible */ SUM(CASE WHEN a.date BETWEEN TRUNC(sysdate,'YEAR') AND ADD_MONTHS(LAST_DAY(TO_DATE(sysdate)),-1) THEN a.amt_1 ELSE 0 END) AS total_amt_1, SUM(CASE WHEN A.DATE BETWEEN TRUNC(sysdate,'YEAR') AND ADD_MONTHS(LAST_DAY(TO_DATE(sysdate)),-1) THEN A.AMT_2 ELSE 0 END) AS TOTAL_AMT_2 FROM table_a a, table_b b, table_c c WHERE a.p_id = b.p_id and b.pt_id = c.pt_id GROUP BY a.p_id, b.p_name, c.p_desc
The two statements inside the COUNTY box return a.m_id or NULL. A simplified test case is:
SQL> WITH t as ( 2 SELECT 1 m_id, 9 dt FROM dual UNION ALL 3 SELECT 1 m_id, 6 dt FROM dual UNION ALL 4 SELECT 2 m_id, 9 dt FROM dual UNION ALL 5 SELECT 2 m_id, 6 dt FROM dual UNION ALL 6 SELECT 1 m_id, 5 dt FROM dual UNION ALL 7 SELECT 2 m_id, 5 dt FROM dual UNION ALL 8 SELECT null m_id, 9 dt FROM dual) 9 SELECT count(CASE WHEN dt BETWEEN 6 and 9 THEN m_id end) cid, 10 count(distinct CASE WHEN dt BETWEEN 6 and 9 THEN m_id end) cdid 11 FROM t; CID CDID ---------- ---------- 4 2
I'm not entirely sure that you really need the IS NOT NULL predicate a.m_id in the instructions BOX, but I left it for more security.
John -
How to count the total number of records with distinction?
Hello guys
I have a report that contains a list of the types of company and other attributes as follows:
The list goes on and... "Business type" appears is not the highest or the lowest... I would like to see what is the total number of different company types we have. By this report, there should be 5, but if I count or count (*) or rcompte, I actually get the total number of lines in the present report, which is not what I want...Shipto Company Type GC Code Measures A cc x 100 y 200 c 120 B YUT I 200 cc U 98 G Maid rt 200 YUT TT 300 JTE TY 400
If someone could help me to find the total number of company types?
Thanks in advanceHave you tried this one?
Sum (Count (distinct ColumnName))
-
Getting Sum, Count, and Distinct Count of a file
Hi all, this is a question of UNIX.
I have a large flat file with millions of records.
col1 | col2. col3
1. a | b
2. c | d
3. e | f
3. g | h
footer *.
I'm supposed to do to calculate the sum of the [= 9] col1 col1 County [= 4] and a separate account of col1 [= c3]
I would like if you avoid external commands like AWK. Also, can we do the same thing by creating a function?
Please keep in mind that the file is huge
Thanks in advanceYes, I suppose that it is very much inappropriate for answering this question here, I just like awk too] :))
where x is the file:
sum(col1) : awk -F\| '{col1+=$1}END{print col1}' x count(col1): wc -l x | awk '{print $1-2}' count(distinct col1): cut -d\| -f1 x|sort -u|wc -l|awk '{print $1-2}'
Perl would be a much more effective tool to use here... try a perl forum :)
-
Oracle 10G version
Hi gurus
I appreciate if someone helps me to solve one of the question.
Examples of data
Reference WITH
AS(
SELECT 1 I_ID, 68 O_ID, '20121' P_NO FROM dual UNION ALL
Select 299, 410, '3993' double UNION ALL
Select 299, 410, '3993' double UNION ALL
Select 299, 410, '3993' double UNION ALL
SELECT 297, 412, '15003030' FROM dual UNION ALL
SELECT 298, 412, '1593939' FROM dual
)
SELECT * REFERENCE
order by 2;The query result
I_ID, O_ID, P_NO
1 68 20121
299 410 3993
299 410 3993
299 410 3993
297 412 15003030
298 412 1593939
Rule
Need to count those P_no who have more than one different I_id against O_ID even...
Output
I_ID, O_ID, P_NO County
297 412 15003030 2
298 412 1593939 2I tried to use the analytical function of the account but no luck, thanks in advance
Concerning
Shu
Hi, Shu,
Here's one way:
WITH got_i_id_count AS
(
SELECT i_id, o_id, p_no
COUNT (DISTINCT i_id) over (PARTITION BY o_id) AS i_id_count
REFERENCE
)
SELECT i_id, o_id, p_no
EARL of (p_no) OVER (PARTITION BY o_id) AS p_no_count
OF got_i_id_count
WHERE i_id_count > 1
ORDER BY o_id
;
Thanks for the display of the data of the sample; It's very useful!
-
number of conditional, exclusion of the counted and other.
Hi guys,.
I have the following table:
MATRIX - ID - Reason_1___Reason_2
001004 7 B - 2014/04471 2
001004 R/2014/000046 8 1
001004 R/2014/000963 7 1
001001 R/2014/000963 7 1
001004 102311427365 7 1
001004 102311428653 8 1
001004 102311429331 7 1
001004 102311429818 7 1
001001 102311429818 7 5
001004 14 - 0027835 7 1
001001 14 - 0124520 7 1
001004 14 - 0124520 2 1
001004 7 1 2014MEL008627
001004 7 1 2014MEL013736
001004 7 1 2014MEL013737
I need a meter that shows me the following:
3 separate ID are displayed for both, matrix 001004 and 001001 (those who are R/2014/000963 102311429818 14-0124520)
9 separate ID appear only for 001004 (3 above must have been excluded).
0 distinct ID appear only for 001001 (the first 3 should have been excluded).
No idea how to solve this problem?
SQL> with t 2 as 3 ( 4 select '001004' matrix, 'B-2014/04471' id, 7 reason_1, 2 reason_2 from dual union all 5 select '001004' matrix, 'R/2014/000046' id, 8 reason_1, 1 reason_2 from dual union all 6 select '001004' matrix, 'R/2014/000963' id, 7 reason_1, 1 reason_2 from dual union all 7 select '001001' matrix, 'R/2014/000963' id, 7 reason_1, 1 reason_2 from dual union all 8 select '001004' matrix, '102311427365' id, 7 reason_1, 1 reason_2 from dual union all 9 select '001004' matrix, '102311428653' id, 8 reason_1, 1 reason_2 from dual union all 10 select '001004' matrix, '102311429331' id, 7 reason_1, 1 reason_2 from dual union all 11 select '001004' matrix, '102311429818' id, 7 reason_1, 1 reason_2 from dual union all 12 select '001001' matrix, '102311429818' id, 7 reason_1, 5 reason_2 from dual union all 13 select '001004' matrix, '14-0027835' id, 7 reason_1, 1 reason_2 from dual union all 14 select '001001' matrix, '14-0124520' id, 7 reason_1, 1 reason_2 from dual union all 15 select '001004' matrix, '14-0124520' id, 2 reason_1, 1 reason_2 from dual union all 16 select '001004' matrix, '2014MEL008627' id, 7 reason_1, 1 reason_2 from dual union all 17 select '001004' matrix, '2014MEL013736' id, 7 reason_1, 1 reason_2 from dual union all 18 select '001004' matrix, '2014MEL013737' id, 7 reason_1, 1 reason_2 from dual 19 ) 20 select matrix 21 , count(case when cnt > 1 then 1 end) common_id_count 22 , count(case when cnt = 1 then 1 end) unique_id_count 23 from ( 24 select matrix 25 , id 26 , count(distinct matrix) over(partition by id) cnt 27 from t 28 ) 29 group 30 by matrix; MATRIX COMMON_ID_COUNT UNIQUE_ID_COUNT ------ --------------- --------------- 001001 3 0 001004 3 9 SQL>
-
Sum (1) vs Count (*) difference - bug?
Hello :-)
This morning I was very shocked after that I discovered that my COUNT (*) wasn't behaving as I supposed it should do.
I managed to create a simple SELECT statement shows the unexpected behavior.
Can someone explain to me what is happening here? Is this a bug? It happens to you too?
My real workaround is to use SUM (1).
WITH T1 AS)
NAME OF SELECT 'ROBERT', 'USA' COUNTRY OF DOUBLE UNION ALL
NAME OF SELECT 'TOM', 'USA' COUNTRY OF DOUBLE UNION ALL
'JOHN' SELECT NAME, 'CANADA' COUNTRY OF DOUBLE),
() AS T2
SELECT SUM (1) SUM1;
COUNT (*) COUNT_STAR,
COUNT (DISTINCT NAME) COUNT_DISTINCT
FROM T1
GROUP OF COUNTRIES
ORDER BY COUNTRY
)
SELECT * FROM T2;
SUM1 COUNT_STAR COUNT_DISTINCT
---------- ---------- --------------
1 1 1
2 1 2
I thought that SUM1 and COUNT_STAR should give the same result, but for the second row COUNT_STAR is 1 instead of 2.
That's not counting the lines.
Please note that if you write this query a little differently then SUM (1) and COUNT (*) give the same result.
For example, WITH the kidnapping:
WITH T1 AS)
NAME OF SELECT 'ROBERT', 'USA' COUNTRY OF DOUBLE UNION ALL
NAME OF SELECT 'TOM', 'USA' COUNTRY OF DOUBLE UNION ALL
'JOHN' SELECT NAME, 'CANADA' COUNTRY OF DOUBLE)
SELECT SUM (1) SUM1;
COUNT (*) COUNT_STAR,
COUNT (DISTINCT NAME) COUNT_DISTINCT
FROM T1
GROUP OF COUNTRIES
ORDER BY COUNTRY
Example Order By deleted
WITH T1 AS)
NAME OF SELECT 'ROBERT', 'USA' COUNTRY OF DOUBLE UNION ALL
NAME OF SELECT 'TOM', 'USA' COUNTRY OF DOUBLE UNION ALL
'JOHN' SELECT NAME, 'CANADA' COUNTRY OF DOUBLE),
() AS T2
SELECT SUM (1) SUM1;
COUNT (*) COUNT_STAR,
COUNT (DISTINCT NAME) COUNT_DISTINCT
FROM T1
GROUP OF COUNTRIES
)
SELECT * FROM T2;
Deleted COUNT DISTINCT example
WITH T1 AS)
NAME OF SELECT 'ROBERT', 'USA' COUNTRY OF DOUBLE UNION ALL
NAME OF SELECT 'TOM', 'USA' COUNTRY OF DOUBLE UNION ALL
'JOHN' SELECT NAME, 'CANADA' COUNTRY OF DOUBLE),
() AS T2
SELECT SUM (1) SUM1;
COUNT (*) COUNT_STAR
FROM T1
GROUP OF COUNTRIES
ORDER BY COUNTRY
)
SELECT * FROM T2;
DB is
Oracle Database 11g Enterprise Edition Release 11.2.0.2.0 - Production on 64-bit Linux.
Good bye
Andrea
I think you hit bug
Bug 9857247 Erroneous for count (*) results with investment group
It affects 11.2.0.1 and 11.2.0.2 version and is set to 11.2.0.3 and 12.1.0.1.
A workaround mentioned in the note of support is to
Set "_optimizer_group_by_placement" = false
-
Count of the number of months with unpaid for the last six months
Hello
I now table to store the daily presence of each employee
ATTENDANCESHEET
EMPID
PAYROLLDATE
UNPAID
1
10/01/2013
6
1
10/02/2013
2
1
11/01/2013
0
1
11/02/2013
0
1
12/01/2013
0
1
12/02/2013
0
1
01/01/2014
0
1
01/02/2014
0
1
02/01/2014
0
1
02/02/2014
0
1
03/01/2014
45
1
03/02/2014
0
2
03/01/2014
0
2
03/02/2014
0
I want to get the number of months used unpaid in the past 6 months (excluding the current).
If the month of payroll, I'm in treatment is 4, 2014, I spend 4 as payrollmonth and 2014 as payroll year
The application must check the latest 6 month data and number of months for which there is any unpaid value
In the case above for month 4, 2014, the power required is
EmpID
County
1
2
2
0
I use oracle 10g
EXAMPLES OF DATA
create the table attendancesheet as
(
Select 1 as empid, to_date (January 10, 2013 ',' DD/MM/YYYY ') as payrolldate, 6 paid by union double all the
Select 1 as empid, to_date (10 February 2013 ',' DD/MM/YYYY ') as payrolldate, 0 paid by union double all the
Select 1 as empid, to_date (January 11, 2013 ',' DD/MM/YYYY ') as payrolldate, 0 paid by union double all the
Select 1 as empid, to_date (February 11, 2013 ',' DD/MM/YYYY ') as payrolldate, 0 paid by union double all the
Select 1 as empid, to_date (12 January 2013 ',' DD/MM/YYYY ') as payrolldate, 0 paid by union double all the
Select 1 as empid, to_date (February 12, 2013 ',' DD/MM/YYYY ') as payrolldate, 0 paid by union double all the
Select 1 as empid, to_date (1 January 2014 ',' DD/MM/YYYY ') as payrolldate, 0 paid by union double all the
Select 1 as empid, to_date (February 1, 2014 ',' DD/MM/YYYY ') as payrolldate, 0 paid by union double all the
Select 1 as empid, to_date (January 2, 2014 ',' DD/MM/YYYY ') as payrolldate, 0 paid by union double all the
Select 1 as empid, to_date (February 2, 2014 ',' DD/MM/YYYY ') as payrolldate, 0 paid by union double all the
Select 1 as empid, to_date (3 January 2014 ',' DD/MM/YYYY ') as payrolldate, 45 paid by union double all the
Select 1 as empid, to_date (3 February 2014 ',' DD/MM/YYYY ') as payrolldate, 0 paid by union double all the
Select 2 as empid, to_date (3 January 2014 ',' DD/MM/YYYY ') as payrolldate, 0 as default all the double union
Select 2 as empid, to_date (3 February 2014 ',' DD/MM/YYYY ') as payrolldate, 0 paid double
);
Help, please
Hello
So, you want to count the distinct months for 6 months before (not included) the given month; is this fair?
Here's one way:
VARIABLE month VARCHAR2 (2)
Year VARIABLE VARCHAR2 (4)
EXEC: months: = '4';
EXEC: year: "2014"; =
WITH got_end_date AS
(
SELECT TO_DATE (: month |) '/' || : year
, ' MM/YYYY ".
) AS end_date
OF the double
)
SELECT a.empid
, COUNT (DISTINCT
CASE
WHEN a.unpaid > 0
THEN TRUNC (a.payrolldate, 'MONTH')
END
), Cnt
Of attendancesheet one
JOIN e-got_end_date WE a.payrolldate > = ADD_MONTHS (e.end_date-6)
AND a.payrolldate< > >
GROUP BY a.empid
ORDER BY a.empid
;
If you use a WHERE clause to exclude the lines with some outstanding = 0 (or unpaid is NULL), then you would get no output for employees like empid = 2 in this example. A WHERE clause ignores an entire line, so I used an expression BOX just a value.
-
Counting of days in a range a group
I use Oracle 11.1, and I have the table and the following data.
CREATE TABLE DELAYS ( DELAY_ID NUMBER (5) NOT NULL, CASE_ID NUMBER (9) NOT NULL, TYPE_ID NUMBER (2) NOT NULL, START_DATE DATE NOT NULL, END_DATE DATE ); ALTER TABLE DELAYS ADD (PRIMARY KEY (DELAY_ID, CASE_ID, TYPE_ID)); INSERT INTO DELAYS VALUES (1, 3770, 1, DATE '2014-08-01', DATE '2014-08-02'); --INSERT INTO DELAYS VALUES (2, 3770, 1, DATE '2014-08-02', DATE '2014-08-02'); --INSERT INTO DELAYS VALUES (3, 3770, 2, DATE '2014-07-01', DATE '2014-07-10'); --INSERT INTO DELAYS VALUES ( 4, 3770, 5, DATE '2014-7-1' , DATE '2014-7-10' ); --INSERT INTO DELAYS VALUES ( 5, 5430, 2, DATE '2014-8-5' , DATE '2014-8-5' ); --INSERT INTO DELAYS VALUES ( 6, 1, 2, DATE '2014-8-1' , DATE '2014-8-5' ); --INSERT INTO DELAYS VALUES ( 7, 2, 2, DATE '2014-8-1' , DATE '2014-9-1' ); --INSERT INTO DELAYS VALUES ( 8, 3, 2, DATE '2014-1-1' , DATE '2014-8-5' ); --INSERT INTO DELAYS VALUES ( 9, 4, 2, DATE '2014-8-1' , DATE '2014-8-5' ); --INSERT INTO DELAYS VALUES (10, 5, 2, DATE '2014-7-1' , DATE '2014-8-5' ); COMMIT;
I need to return the number of days of delays (end_date - START_DATE + 1), grouped by the CASE_ID and TYPE_ID.
Since I have to make sure that the days are not counted several times, I'm expanding including date between start_date and end_date on the lines of the dates using the form:
SELECT D.CASE_ID, D.TYPE_ID, D.START_DATE + LEVEL - 1 DELAY_DAYS FROM DELAYS D CONNECT BY LEVEL <= (D.END_DATE - D.START_DATE + 1);
The following query returns the number of days of delay for each line (every day of delay are in the expression of slider for verification).
But I can't group this request by CASE_ID and TYPE_ID and aggregate the days of delay to remove duplicates.
If I SUMMARY the COUNT_DELAY_DAYS, then duplicate days are counted more than once.
SELECT D.*, CURSOR ( ( SELECT (D.START_DATE + LEVEL - 1) FROM DUAL CONNECT BY LEVEL <= (D.END_DATE - D.START_DATE + 1))) DELAY_DAYS, ( SELECT COUNT (D.START_DATE + LEVEL - 1) FROM DUAL CONNECT BY LEVEL <= (D.END_DATE - D.START_DATE + 1)) COUNT_DELAY_DAYS FROM DELAYS D;
I tried to use this query to the group by the CASE_ID and the TYPE_ID and count the days of delay, but the numbers are bad, from the second row of data is added to the table.
SELECT CASE_ID, TYPE_ID, COUNT (D.START_DATE + LEVEL - 1) COUNT_DELAY_DAYS FROM DELAYS D CONNECT BY LEVEL <= (D.END_DATE - D.START_DATE + 1) GROUP BY CASE_ID, TYPE_ID;
If I change the NUMBER to be COUNT (DISTINCT...), then I get the exact number of days of delay, grouped by the CASE_ID and TYPE_ID, but performance suffers, as most lines of data are added to the table.
This should be good for thousands of CASES and TYPES that cover the delays of days, weeks, months, or even years.
And several users need to be able to run it at the same time if that makes a difference.
Use change beginning of Group-method to combine the intervals overlapping / adjacent:
with t1 as)
Select case_id,
TYPE_ID,
start_date,
End_date,
case sign)
start_date - max (end_date) more)
case_id partition,
TYPE_ID
order of start_date,
End_date
rows between unbounded preceding
and 1 preceding
)
)
When 1 then 1
0 otherwise
end start_of_group
delays
),
T2 as)
Select case_id,
TYPE_ID,
start_date,
End_date,
Sum (start_of_group) more)
case_id partition,
TYPE_ID
order of start_date,
End_date
) grp
from t1
),
T3 like)
Select case_id,
TYPE_ID,
Max (end_date) - min (start_date) + 1 grp_delay_days
the t2
Case_id group,
TYPE_ID,
GRP
)
Select case_id,
TYPE_ID,
Sum (grp_delay_days) delay_days
T3
Case_id group,
TYPE_ID
order of case_id,
TYPE_ID
/
SY.
Maybe you are looking for
-
I've noticed recently that I picked up a small annoying error that, despite all efforts, I am unable to correct. The details of my PC are as follows: HP-TOUCHSMART system nameSystem manufacturer HP-PavilionSystem model KT481AA-ABG IQ505aSystem Type x
-
As Norton causes more problems, which AV program do you use and why do you prefer?
-
Motorcycle G XT1063 Stock on Bootloader
My motorcycle G XT1063 suddenly got in bootloader and with the following errors: bad check failed to start Impossible to check the bad image boot could not validate the boot image Fastboot pattern: Fall-through of the normal startup mode 48-86 (sha -
-
How to connect my laptop seconded to the display of lightning?
How to connect my laptop spare Windows PC to the display of lightning?
-
Amazon notifications on iPhone
Impossible to get notifications from amazon, works on my iPhone. Amazon app says activate Amazon notifications in the settings, but Amazon app is not yet listed? Can anyone help?