Count of unique values in 7 columns
Hey there…
I have a Lookup table with 8 columns
ID = unique ID
U = Sunday,
M = Monday
T = Tues
W = Weds
R = Thur
F = Fri
S = Sat
Each of the ‘day’ columns can have a value between 1 and 1150, looks like this
ID U M T W R F S
1 15 15 16 15 17 345 17
What I am trying to find is a count of the deviation of each row… or how many of the days have different values
So for the above example
ID U M T W R F S COUNT
1 15 15 16 15 17 345 17 4
As there are 4 unique values in the week..
15,16,17,345
Any ideas on how that could be done in oracle? Stumped!
Yep, pre-11g:
CREATE TABLE temp (id INTEGER, col_1 INTEGER, col_2 INTEGER, col_3 INTEGER, col_4 INTEGER, col_5 INTEGER, col_6 INTEGER, col_7 INTEGER);
INSERT INTO temp VALUES (1000, 1, 2, 3, 4, 5, 6, 7);
INSERT INTO temp VALUES (1001, 7, 2, 3, 4, 5, 6, 7);
INSERT INTO temp VALUES (1002, 1, 1, 1, 1, 1, 1, 1);
SELECT * FROM temp
ORDER BY id;
SELECT id, COUNT (DISTINCT col)
FROM (
SELECT id, col_1 col FROM temp UNION
SELECT id, col_2 FROM temp UNION
SELECT id, col_3 FROM temp UNION
SELECT id, col_4 FROM temp UNION
SELECT id, col_5 FROM temp UNION
SELECT id, col_6 FROM temp UNION
SELECT id, col_7 FROM temp
GROUP by id
ORDER BY 1;
Table dropped.
Table created.
1 row created.
1 row created.
1 row created.
ID COL_1 COL_2 COL_3 COL_4 COL_5 COL_6 COL_7
1000 1 2 3 4 5 6 7
1001 7 2 3 4 5 6 7
1002 1 1 1 1 1 1 1
ID COUNT(DISTINCTCOL)
1000 7
1001 6
1002 1
Similar Messages
-
How do I count the unique value pairs in two columns of a table?
I have a table (Table 2) that is populated with data from an imported .csv file. On table 1 I need to count the unique value pairs in two columns of Table 2.
Here's what I have:
Date Person
7/10/2011 A
7/12/2011 W
7/12/2011 X
7/12/2011 X
7/12/2011 X
7/12/2011 Z
7/14/2011 Z
7/15/2011 X
7/16/2011 Z
I'm focusing on person "X" and can easily count how many days that person shows up but what I want is to see on how many unique days that person shows up.
Here's the result I'm looking for (Person "X" shows up on 2 different days - 3 times on 7/12/2011 and once on 7/15/2011):
X 2
I can't seem to find a function that allows me to do that. I also am not allowed to modify Table 2 so that leaves me to come up with a solution on Table 1.
Any ideas would be greatly appreciated.Hi John,
Not being allowed to modify Table 2 is a minor inconvenience. Just copy (using a formula) the necessary two columns onto Table 1.
Yellow columns may be hidden. The procedure progresses from left to right. All formulas are entered into row 2 then filled down that column to the end of the table. The table must be as long as the list in column A of Table 2.
A2: =Table 2::A
Fill right to column B.
Fill both columns down as far as needed.
I've used actual Date and Time values in column A, formatted to show only the Date part, but the technique will work with text in these cells, provided all cells representing the same 'date' have exactly the same content.
C2: =A&B
This concatenates the contents of each row of columns A and B into a single text string.
D2: =COUNTIF($C$2:C2,C)
This counts the number of occurrences of the Date&Name string on the current row from the first regular cell in column C (C2) to the current cell.
E2: =IF(COUNTIF($B$2:B2,B)=1,MAX($E$1:E1)+1,"")
This constructs the index of first occurrences of each name, in the order they first occur. The index is used by LOOKUP in column F.
F2: =IF(ROW()-1>MAX(E),"",LOOKUP(ROW()-1,$E,$B))
This uses the index value created in E as a search-for value to extract a single copy of the names in column B. The result is a list of all distinct names in the list. Note that spelling differences will be counted as distinct names.The IF statement stops the listing when the last distinct name is extracted.
G2: =IF(LEN(F)>0,COUNTIFS($B,"="&F,$D,"=1"),"")
This counts the number of 'first occurrences of distinct Date & Name strings for each name on the list (ie. the number of distinct dates on which each name appears in the original list).
All of the functions used are described, with at least one example for each, in the iWork formulas and Functions User Guide. You can download the guide, and the Numbers '09 User Guide, via the Help menu in numbers.
Regards,
Barry -
How to get count of unique values of column B for each unique value of column A in Excel 2010?
I have an excel sheet containing 72,000 rows in Excel 2010, column A is "site name", column B is category. I want a result that shows number of sites for each category.
Column A Column B
site 1 Category 1
Site 1 Category 1
Site 2 Category 1
site 3 Category 2
site 3 Category 2
site 4 Category 2
site 5 Category 2
I want this result:
Category 1 2
Category 2 3
I tried some formula in a 3rd column and created a pivot table on that column, but it did not give me the result i want.
Any one has an idea ? what is the easiest way to achieve this?
Appreciate any pointerI tried both formula referenced in these two post (I think they are a little simpler than the one above):
http://stackoverflow.com/questions/11876238/simple-pivot-table-to-count-unique-values
http://stackoverflow.com/questions/18898138/pivot-table-count-unique-values-excel-2010
They both worked! and I got the result for my 72,000 rows!
Thanks for your help! -
Select Statement to Get Unique values in a Column
I am trying to figure out how to get all the unique values from a
table column?
select code from atable sort unique?
I am running 8.1.7
Does anyone know how to do this?
Thanks,Hi,
Here is the code for that.
Select empno from emp
group by empno
having count(empno) = 1
Regards,
Ganesh R -
Display all columns but only unique values in one column
My table looks like this;
ApprovalKey (PK)
ApprovalCCN (Links to main table)
ApprovalName
ApprovalDate
ApprovalMsg
ApprovalResult
Typical data would look like;
Key CCN ApprovalName ApprovalDate ApprovalMsg ApprovalResult
1 1 John Smith 12/12/2014 16:20:22 Testing the message False
2 1 Andy Brown 12/12/2014 16:20:27 testing the decline False
3 1 John Smith 12/12/2014 16:20:46 More testing True
4 1 Andy Brown 12/12/2014 16:25:13 testing True
5 2 Andy Brown 12/12/2014 16:25:26 testing the accept True
6 2 John Smith 12/12/2014 16:34:04 testing, 1 2 True
When I view data from linked table (matched via ApprovalCCN) It should display data from this table but only the latest unique entry from ApprovalName.
eg, table from above has 4 entries for ApprovalCCN=1 but should only show the following;
Key CCN ApprovalName ApprovalDate ApprovalMsg ApprovalResult
3 1 John Smith 12/12/2014 16:20:46 More testing True
4 1 Andy Brown 12/12/2014 16:25:13 testing True
Distinct ApprovalName doesn't return all values and I can't seem to get my head around Group By to view all columns
Any help would be appreciatedHere is my test data;
7 30 John Smith 12/12/2014 16:20:22 Testing the message False
8 30 Andy Brown 12/12/2014 16:20:27 testing the decline False
9 30 John Smith 12/12/2014 16:20:46 More testing True
10 30 Andy Brown 12/12/2014 16:25:13 testing True
20 30 David Jones 12/12/2014 16:25:26 testing False
21 30 David Jones 12/12/2014 16:34:04 Testing the message True
25 30 Me 12/12/2014 16:35:05 testing the decline False
26 28 You 12/12/2014 16:36:05 testing the decline False
27 28 Me 12/12/2014 16:37:05 testing True
28 28 John Smith 12/12/2014 16:38:05 testing the decline False
29 28 David Jones 12/12/2014 16:39:05 testing the decline False
30 30 David Jones 14/12/2014 17:39:44 True
31 29 David Sawyer 14/12/2014 18:51:06 declining the offer False
32 29 David Sawyer 14/12/2014 18:51:37 My message False
33 29 David Sawyer 14/12/2014 18:52:01 another message False
34 29 David Sawyer 14/12/2014 18:52:16 True
35 29 David Sawyer 14/12/2014 18:52:25 True
36 29 David Sawyer 14/12/2014 18:52:34 True
If ApprovalCCN=30 I get the following;
10 30 Andy Brown 2014-12-12 16:25:13.000 testing 1 1
30 30 David Jones 2014-12-14 17:39:44.000 1 2
9 30 John Smith 2014-12-12 16:20:46.000 More testing 1 1
25 30 Me 2014-12-12 16:35:05.000 testing the decline 0 1
All I need is 3 Approved, 1 Declined
Apologies if I wasn't clear -
Query Designer - how to count distinct characteristic values in a column
Hello Experts,
I need to count the number of distinct occurences in a column and use it later for a percentage calculation. I'll give you a concrete example:
Promotion_ID Product Promoted
A ****************** P1 ******** 1
A ****************** P2 ******** 0
A ****************** P3 ******** 1
B ****************** P1 ******** 0
B ****************** P3 ******** 1
C
D
A promotion ID can have more than 1 product associated with it. A product can be promoted (1) or not. A promotion_ID can be also empty (no products C and D)
I need to calculate for each product the percentage of promotions where the product is promoted out of ALL promotions (4) or
P1 25 %
P2 0 %
P3 50%
How can I model this in the Query Designer ? How can I count the distinct Promotion_IDs ?
Any ideas will be appreciated.
Thanks
RadoHi,
Please take a look at the docs below.
https://websmp102.sap-ag.de/~sapdownload/011000358700002762432003E/HowToCount.pdf
https://www.sdn.sap.com/irj/sdn/go/portal/prtroot/docs/library/uuid/7e58e690-0201-0010-fd85-a2f29a41c7af
Regards,
® -
Counting true boolean values in a column
Post Author: scott0_1
CA Forum: General
Hi Everyone,
I have a report with four columns that show boolean (Y or N) values. In the report footer I am trying to total the columns, specifically the "Y" values.
Thanks,
ScottPost Author: quafto
CA Forum: General
You will get a better response if you post this to the Crystal Reports forum. -
Getting unique values from a column within a hierarchy
I'm sort of new to working with databases and oracle so this is probably a beginner-ish question. I'm using oracle 9i on this.
I have a database that contains a table with an organizational hierarchy (dept_id, dept_name, parent_id), and another that contains a list of items for each department (like dept_id, item_id). Item_id joins with another table for the actual description of the items. Each department may have many items, and the departments may overlap with the items included in each.
I only want to contain that item list for leaf nodes in the hierarchy and for any departments that are not at the bottom, have a query pull a list of all unique items contained by its children, without the overlap. I know I can use START WITH and CONNECT BY to traverse the hierarchy but I'm not sure how I'd put all these pieces together into one query to get what I want, so I'd really appreciate help.can you pls post a sample data and the output you are looking for.
-
Want to query a set of unique values for concat columns
Hi,
I have Table1 with unique constraint on col2 and col3.
Have to optimize the following query as it does a FTS
select * from table1
where col2||col3 in (col1val1 || col2val1, col1val2 || col2val2, ....)
Please advise, Thanks In Advance.select * from table1
where col2||col3 in (col1val1 || col2val1, col1val2
|| col2val2, ....)How about
select * from table1
where col2, col3 in (select col1val1, col2val1 from dual union all
select col1val2, col2val2 from dual union all...
) -
Extracting unique values from (non-category) columns for chart
Hello:
I've created a worksheet to keep inventory of my Intellivision games. It has the following columns:
Publisher
Class
Network
Title
Quantity
(misc...)
The "Class" represents whether the game is "complete in box" or "loose cartridge." The "Network" represents the general genre or game cateogory. The quantity is how much I have of each.
I have set the first three columns as categories: Publisher, Class, and Network. I also created a bar chart based on the Title and Quantity columns, to show how many I have of each.
The problem I have is that, although it looks real cool and helps me keep the games organized, since a title can appear in more than one "Class" (e.g., I can have one in box, and two loose), the chart includes duplicates, and they are not grouped together.
Is there a way that I can create a graph (or a secondary table) that exctracts only the UNIQUE values from a column that may contain duplicates?
Note that I don't want to put the "Title" column in a category. I want to group by the three major categories and list the games on each.
-dZ.From the Numbers Help Menu, download the Numbers User Guide. Read the first three or four chapters to get a feel for the app. It's well written and won't take long to read that much.
Then use the Table of Contents and the Search tool to get additional specific directions.
First, delete all unneeded Rows and Columns from your data table. If you have patches of data in a larger table, Cut the patches and Paste them to a blank Sheet area to create separate dedicated tables for your various needs. These small special purpose tables are like Named Ranges in Excel. Name them in the Sheets Pane.
This is how Numbers was intended to be used. The User Guide will describe how to reference cells in one table from expressions in another table. If you use the point and click method of creating references from within the equation editor it won't matter a bit that the tables are separate.
Come back here for specific help on anything you are having trouble with.
Jerry -
Compare floating point values in a column
I have a column defined as NUMBER. The values are say 1.2345 and 1.23443. Visually i can say they are almost the same but if i do select count(distinct colA) from table it gives two values, which is perfectly right. What i want to do is say if they are less than 0.0001 then count them as same value.
You could argue if i use ROUND or TRUNCATE to the 'n' decimal point but i see this as an issue even even to two decimal places. If the 3rd digit after the decimal is greater than 5 it would round the 2nd digit. If i use TRUNCATE to 'n' places then the last digit might be smaller than the previous value so again the same issue.
However, my problem is how do i do if its in 2 or 3 different rows for the same column. If they are in two columns, i am able to do (COLA - COLB) and if the value is < 0.00001 then ignore or filter from the query result.
Example:
ColA , ColB
Variable1, 1.23453876
Variable1, 1.2342736
Variable1, 1.23420087
Variable2, 10.250
Variable2, 15.775
Variable3, -1.93815994262695
Variable3, -1.93815803527832
Output: Count of unique values for each variable in columnA
Variable1, 1
Variable2, 2
Variable3, 1
OR
Output: Display on those variables whose values is more than 0.0001 (4 digits after the decimal)
Variable2, 2
I am using oracle 10.2 Enterprise Edition and accessing data via TOAD
ThanksPeter,
Thank you for the sql, i had not tried lag function before so it was new to me. I modified it slightly to lag by 'partition query'. The issue i see is it does delta between only two consecutive values so if there are 5 values in 5 rows then they can be off by 0.0001 (some small number) and we will never catch those variables because the case statement would evaluate to '0' based on the if condition.
However, for my case it would work perfectly fine since i wanted to separate small values from the real obvious big deltas. As in the case below where the delta was 5.XX which narrows down to Variable2.
with
tab as
(select 'Variable1' cola, 1.23453876 colb from dual union all
select 'Variable3', -1.93815994262695 from dual union all
select 'Variable1', 1.2342736 from dual union all
select 'Variable2', 10.250 from dual union all
select 'Variable2', 15.775 from dual union all
select 'Variable1', 1.23420087 from dual union all
select 'Variable3', -1.93815803527832 from dual union all
select 'Variable4', 47.41209 from dual union all
select 'Variable4', 47.41207 from dual
select *
from
select
cola,colb,lag(colb) over (partition by cola order by colb)prev_value, round(abs(colb - lag(colb) over (partition by cola order by colb)),4)prev_delta
,case when colb - nvl(lag(colb) over (partition by cola order by colb ), colb) < 0.0001 then 0
else 1 end dif_count
from
tab
--order by cola
--group by cola
COLA COLB PREV_VALUE PREV_DELTA DIF_COUNT
Variable1 1.23420087 0
Variable1 1.2342736 1.23420087 0.0001 0
Variable1 1.23453876 1.2342736 0.0003 1
Variable2 10.25 0
Variable2 15.775 10.25 5.525 1
Variable3 -1.938159943 0
Variable3 -1.938158035 -1.938159943 0 0
Variable4 47.41207 0
Variable4 47.41209 47.41207 0 0
You guys rock! thank you. -
Creating a list of unique values within a range.
I have a huge data set I am working with and need to do two things.
First I need to get rid of any repeat entires, which there are many.
Second, and more importantly, I need to generate a list of unique values within a column for which I have no idea what the number or sort of values might be. For instance, say the column contained flavors of ice cream, I am looking for a formula that can return an array of the flavors listed.
Can either of these two things be done in numbers?CJ Eder wrote:
I have a huge data set I am working with and need to do two things.
First I need to get rid of any repeat entires, which there are many.
If entries are in column B starting from B2
In C2 enter the formula:
=IF(COUNTIF($B$1:$B1,B)=0,B,"")
and apply fill down.
You will get a single copy of existing entries.
Select the column C
Copy to Clipboard
Paste in column C
Sort on column C
delete the rows whose cell of column C is blank.
If I understand well, the same protocol apply to your second request.
Yvan KOENIG (from FRANCE mercredi 15 juillet 2009 21:32:30) -
LOV's auto refresh and having unique values.
Hi, I'm having LOVs connected with few attributes of a table, and those fields are used in view criteria of the table.
This view criteria is used in the af:query component as search fields.
To avoid the duplicates in the LOV, i created a separate view for each of the attribute, using the distinct key , and connected them with the attribute via view accessors. So that the af:query component , search field drop downs can give unique value of the column.
There is an another requirement, to refresh the LOVs of the search field, when there is a change in the database. To accomplish this auto refresh property of the view's query, i added the PK of the table with each view. By doing this i can see the latest values of the database in the UI . ie, the LOVs of af:query getting the changes of database.
But this one causing, the duplicate values to be in LOV, and the distinct key word in the view's query will not work because of the PK of table is added to query.
I tried different ways to query , like groupby . But no success.
I need both auto refresh as well as unique values in the LOVs of the af:query . Could some one point me a reference to solve this. Thanks .
Edited by: user642477 on Oct 7, 2010 10:52 AMuser642477 wrote:
Hi, I'm having LOVs connected with few attributes of a table, and those fields are used in view criteria of the table.
This view criteria is used in the af:query component as search fields.
To avoid the duplicates in the LOV, i created a separate view for each of the attribute, using the distinct key , and connected them with the attribute via view accessors. So that the af:query component , search field drop downs can give unique value of the column.
why you have done that what is your reason?!!! it seems so wierd to me? you could easily add distinct on query of the viewobject! -
Filtering unique values in dataset
Hi, I have nice little dataset here, populated from xml. It
contains articles, categories.....
What I need is to initialize my custom menu class with unique
values from categories column. So how do I query my dataset to
return me only unique categories?
I could do it in my xml parser (extract uniques in array),
but that would be poor design.
So please help! Need those uniques out of dataset fast!Could you please tell me how i will Filter duplicate
valus inside the objectI'm sorry, but I can't read the future. -
How to ensure unique values of all records in the form?
Hi all,
I need to do some personalization in this Oracle EBS Form - Purchasing Super User -> Receiving -> Receipts -> Lot Entry
I must ensure that evry record entered on that form has a unique value in the column Lot Number.
Can anybody advise me please?
Thank you, WillyHi there.
One way to do this is to do a SPQuery and convert it to a datatable using the GetDataTable method and then to a View. The View then has the ability to get unique values using the ToTable method, something like this:
SPList list = SPContext.Current.Web.Lists["MyList"];
SPQuery query = new SPQuery();
query.Query = "<OrderBy><FieldRef Name='Title' /></OrderBy>";
DataTable table = list.GetItems(query).GetDataTable();
DataView v = new DataView(table);
string[] columns = {"Title"};
DataTable tbl = v.ToTable(true, columns);
Hope this helps.
Regards,
Magnus
My blog: InsomniacGeek.com
Maybe you are looking for
-
SideBySide Errors w R&R 4.30
I just removed XP and installed W7 Pro 32-bit on a T61. I installed numerous of the Lenovo applications, including Power Manager, Access Connections 6.01, System Update and R&R 4.30.0027. I noticed many SideBySide Event ID 33 Errors. The machine h
-
I recently upgraded my OS to Lion 10.7.4. I also just upgraded from a 1st Generation Time Capsule to a 4th Generation Time Capsule. I have an Airport Express (Version A1264) that I formerly used to extend my wireless network. Airport Express no lo
-
Hi, I am getting the following error in my Oracle AS 10g R3 on Solaris . Error in resource adapter cleanup.. javax.resource.ResourceException: java.sql.SQLException: Closed Connection in log file:apps/oracle/oc4j_home/j2ee/homeMYAPP/log/homeMYAPP_def
-
Photoshop Elements 4.0 on iMac
Hello, my parents bought me Photoshop Elements 4.0 about 3 years ago for my MacBook. I've just bought an iMac and successfully migrated all my files from there onto the new one (I couldn't find the installation disc/serial number). I think I've got t
-
how do I move my iTunes from my eMac's hard drive to an external hard rive and or vis versa. Thanks