Count for distinct values ...
Dear Team,
I have following situation at hand...
COLUMN1 COLUMN2 COLUMN3
TECH111 A11111 MATERAL1
TECH111 A11112 MATERAL2
TECH111 A11112 MATERAL3
TECH111 A11113 MATERAL4
TECH111 A11113 MATERAL5
TECH111 A11114 MATERAL6
TECH111 A11115 MATERAL7
TECH111 A11116 MATERAL8
The value in column1 is TECH111
Now i want to count the number of values appearing in column2
i.e.A11111 appears 1 time, A11112 and A11113 appears 2 time and so on..
So the final output i need is shown below..
COLUMN1 COLUMN2 COLUMN3 COLUMN4
TECH111 A11111 MATERAL1 1
TECH111 A11112 MATERAL2 2
TECH111 A11112 MATERAL3 2
TECH111 A11113 MATERAL4 2
TECH111 A11113 MATERAL5 2
TECH111 A11114 MATERAL6 1
TECH111 A11115 MATERAL7 1
TECH111 A11116 MATERAL8 1
kindly give me some tips to achieve this
Regards
nic...
Nic,
Use the formula
=CountAll(<col2>) In (<col1>,<col2>)
Regards,
Harry
Similar Messages
-
Extracting a count of distinct values between two date ranges over months
Hi All,
I am having a bit of difficulty in figuring out the query to build a list of active campaigns over a date range.
i.e. I have a table with campaign IDs and their start and end date details like this
Campaign_id Start_date End_date
10001 1-Jun-09 31-May-11
10002 1-Jun-09 23-Jun-11
30041 21-Aug-09 31-Dec-09
20005 3-Jun-10 31-May-11
90021 21-Nov-09 30-Nov-10
54000 1-Jun-11 1-Dec-12
35600 1-Mar-10 31-Mar-12 What the above data means is, for eg. the campaign 10001 is active from 1-Jun-09 to 31-May-11 i.e. for 24 months (inclusive of the month Jun-09 and May-11)
What I need to figure out is the counts of active campaigns between a date range and display that active count at a month level (for e.g. lets say we want to see all the campaigns that were active
between the date range '01-JUN-2007' and '30-APR-2012' ). So the partial output would be as seen below. The list would continue till december-2012
Month Year Count of active campaigns
Jan 2009 0
Feb 2009 0
Mar 2009 0
Apr 2009 0
May 2009 0
Jun 2009 2
Jul 2009 2
Aug 2009 3
Sep 2009 3
Oct 2009 3
Nov 2009 4
Dec 2009 4
Jan 2010 3
Feb 2010 3
Mar 2010 4
Apr 2010 4
Dec 2012 1 Could anybody please help me with the right query for this.
Thanks a lot for help
Regards
Goldiset pagesize 40
with tab as
select 1 id, sysdate -100 start_date, sysdate end_date from dual
union
select 1 id, sysdate -200 start_date, sysdate -150 end_date from dual
union
select 1 id, sysdate -600 start_date, sysdate - 400 end_date from dual
union
select 1 id, sysdate -300 start_date, sysdate - 150 end_date from dual
union
select 2 id, sysdate -100 start_date, sysdate-50 end_date from dual
year_tab as
select
add_months(min_date, level -1) m
from
select min(trunc(start_date,'YYYY')) min_date, add_months(max(trunc(end_date,'YYYY')), 12) max_date
from tab
connect by level <= months_between(max_date, min_date)
select to_char(m,'YYYY') year_,
to_char(m,'Month') month_,
nvl(act, 0) act
from year_tab,
select m date_,count(*) act
from tab, year_tab
where m between trunc(start_date,'MM') and trunc(end_date,'MM')
group by m
) month_tab
where m = date_(+)
order by m;
YEAR_ MONTH_ ACT
2010 January 0
2010 February 0
2010 March 0
2010 April 0
2010 May 0
2010 June 0
2010 July 0
2010 August 0
2010 September 1
2010 October 1
2010 November 1
2010 December 1
2011 January 1
2011 February 1
2011 March 1
2011 April 0
2011 May 0
2011 June 0
2011 July 1
2011 August 1
2011 September 1
2011 October 2
2011 November 2
2011 December 2
2012 January 2
2012 February 2
2012 March 2
2012 April 1
2012 May 1
2012 June 0
2012 July 0
2012 August 0
2012 September 0
2012 October 0
2012 November 0
2012 December 0
36 rows selected. -
Count distinct values current group
Hi--
Is there a way to count the distinct values within the current group? ie--i've got a PO and want to display all the addresses at the shipment level if there is more than one distinct one, but if they are all the same as the header-level address, then I don't want any of them to show up.
I'm using the following at the header level to tell the header not to show up if there are multiple shipment level addresses, but can't seem to get a similar statement to work when it's sitting at the same level as the group that I want to count.
This is what I use at the header--it seems to work:
<?if:count(xdoxslt:distinct_values(PLL_SHIP_ADDRESS_LINE1))>1?>See Details Below<?end if?><?if:count(xdoxslt:distinct_values(PLL_SHIP_ADDRESS_LINE1))=1?>
POH_SHIP_ADDRESS_LINE1POH_SHIP_ADDRESS_LINE1
POH_SHIP_ADDRESS_LINE2
POH_SHIP_ADDRESS_LINE3
POH_SHIP_ADR_INFO POH_SHIP_COUNTRY<?end if?>
A really simplified version of the structure of the report is below:
<?xml version="1.0" ?>
- <!-- Generated by Oracle Reports version 10.1.2.0.2
-->
- <SMTPOXPRPOP2>
- <LIST_G_INIT_INFO>
- <G_INIT_INFO>
<MANUAL_PO_NUM_TYPE>NUMERIC</MANUAL_PO_NUM_TYPE>
<C_COMPANY>CompanyName</C_COMPANY>
- <LIST_G_HEADERS>
- <G_HEADERS>
<POH_PO_NUM>310001100</POH_PO_NUM>
- <LIST_G_LINES>
- <G_LINES>
<POL_VENDOR_PROD_NUM>12q</POL_VENDOR_PROD_NUM>
<POL_ITEM_DESCRIPTION>sample</POL_ITEM_DESCRIPTION>
<POL_QUANTITY_TO_PRINT>10</POL_QUANTITY_TO_PRINT>
- <LIST_G_SHIPMENTS>
- <G_SHIPMENTS>
<PLL_SHIP_COUNTRY>Canada</PLL_SHIP_COUNTRY>
<PLL_SHIP_ADR_INFO>Calg,AB Zip</PLL_SHIP_ADR_INFO>
<PLL_SHIP_ADDRESS_LINE3 />
<PLL_SHIP_ADDRESS_LINE2 />
<PLL_SHIP_ADDRESS_LINE1>Ad1</PLL_SHIP_ADDRESS_LINE1>
</G_SHIPMENTS>
</G_SHIPMENTS> <PLL_SHIP_COUNTRY>Canada</PLL_SHIP_COUNTRY>
<PLL_SHIP_ADR_INFO>Calg,AB Zip</PLL_SHIP_ADR_INFO>
<PLL_SHIP_ADDRESS_LINE3 />
<PLL_SHIP_ADDRESS_LINE2 />
<PLL_SHIP_ADDRESS_LINE1>Ad1</PLL_SHIP_ADDRESS_LINE1>
</G_SHIPMENTS>
</G_LINES>
<POH_SHIP_ADDRESS_LINE2 />
<POH_SHIP_COUNTRY>Canada</POH_SHIP_COUNTRY>
<POH_SHIP_ADR_INFO>Kanata,ON K2V 0A2</POH_SHIP_ADR_INFO>
<POH_SHIP_ADDRESS_LINE3 />
<POH_SHIP_ADDRESS_LINE1>XXX Palladium Drive</POH_SHIP_ADDRESS_LINE1>
</G_HEADERS>
</LIST_G_HEADERS>
</SMTPOXPRPOP2>
Could anyone help out with this?
Thanks--I'd really appreciate it!
KateHi Vetsrini--
Thanks for getting back to me so quickly! I'd love to email you a copy and the XML if you wouldn't mind taking a look. it'll probably be more clear than me trying to explain.
I can't quite figure out how to do that, though---your profile doesn't list an email. Do I need to click elsewhere?
Thanks!
Kate -
Select records based on first n distinct values of column
I need to write a query in plsql to select records for first 3 distinct values of a single column (below example, ID )and all the rows for next 3 distinct values of the column and so on till the end of count of distinct values of a column.
eg:
ID name age
1 abc 10
1 def 20
2 ghi 10
2 jkl 20
2 mno 60
3 pqr 10
4 rst 10
4 tuv 10
5 vwx 10
6 xyz 10
6 hij 10
7 lmn 10
so on... (till some count)
Result should be
Query 1 should result --->
ID name age
1 abc 10
1 def 20
2 ghi 10
2 jkl 20
2 mno 60
3 pqr 10
query 2 should result -->
4 rst 10
4 tuv 10
5 vwx 10
6 xyz 10
6 hij 10
query 3 should result -->
7 lmn 10
9 .. ..
so on..
How to write a query for this inside a loop.Hi,
So, one group will consist of the lowest id value, the 2nd lowest and the 3rd lowest, reggardless of how many rows are involved. The next group will consist of the 4th lowest id, the 5th lowest and the 6th lowest. To do that, you need to assign numbers 1, 2, 3, 4, 5, 6, ... to the rows in order by id, with all rows having the same id getting the same number, and without skipping any numbers.
That sounds like a job for the analytic DENSE_RANK function:
WITH got_grp_id AS
SELECT id, name, age
, CEIL ( DENSE_RANK () OVER (ORDER BY id)
/ 3
) AS grp_id
FROM table_x
SELECT id, name, age
FROM got_grp_id
WHERE id = 1 -- or whatever number you want
;If you'd care to post CREATE TABLE and INSERT statements for your sample data, then I could test it.
See the forum FAQ {message:id=9360002} -
Rows count for repetitive groups in tablix.
Hi All,
To make the story cut short, i will going to explain my scenario:
i have a tablis contro and i garoup it by a field say "Field1" which contains A and B values,
in my case we divide groups in two parts if it has rows more then 200.
so, my report output and my required is below :
Current Output -------------------------- Required layout
here 200, 20, 30 are number of rows per group.
now my requirement is if a group is repetitive
i need the all rows count for that value like in above case :
Header text of Group A should be like : A(220) i.e 200 +20
please help me on this...
it is very urgent for me
shashankhi Simon,
thanks for reply, sorry, i skipped to mentioned that i have to provide multilevel grouping also, like
Group By: Field 1 -> Field 2 -> field3
so, to achieve my requirement by Sql as solution has no problem if i do not have multilevel grouping, but i have to provide that and in that case the query will be like,
“select distinct [column1],[Column2],[Column3],count(*) as rn from [table] group by [column1],[Column2],[Column3]”
which is problematic in my case.
so for doing this i need a solution apart from sql but by using SSRS Report end.
Thanks again.
Shashank -
Calculating sum of distinct values
Hello,
I have a table with columns
U1,U2,U3 of type VARCHAR2 and other columns as UQ1,UQ2,UQ3 as NUMBER
I need to find out the sum of UQ1,UQ2,UQ3 column for the distinct value in U1,U2,U3 columns.
Can we construct a sql statement to achive this?
CheersMy apology, I actually didn't explained my problem properly. its infact is as following
the table has columns U1,U2,U3 of type VARCHAR2 and other columns as UQ1,UQ2,UQ3 as NUMBER
and i need to obtain the sum UQ1,UQ2,UQ3 for distinct values in column U1,U2,U3
the column UQ1 hold the value for item in column U1
and column UQ2 hold the value for item in column U2
and column UQ3 hold the value for item in column U3
So there could be a instance where columns U1,U2,U3 may contains the same item but difference values in columns UQ1,UQ2,UQ3 i.e.
u1 = 'A' and uq1 = 1
u2 = 'B' and uq2 = 4
u3 = 'A' and uq3 = 6
the result should be
A, 7
B, 4
Can we achive this through simple construct? -
Count number of distinct values for a column for all tables that contains that column
Imagine I have one Column called cdperson. With the query below I know which Tables have column cdperson
select
t.[name]fromsys.schemassinnerjoin
sys.tables
tons.schema_id=t.schema_idinnerjoin
sys.columnscont.object_id=c.object_idinnerjoin
sys.types
donc.user_type_id=d.user_type_idwherec.name ='cdperson'
now I want to know for each table, how many distinct values of cdperson I have and I want the result ordered by the table that has more distinct values (descending)
Table1
cdperson select distinct(cdperson) = 10
cdadress
quant
Table2 with
cdaddress (no column cdperson in this table)
quant
Table3
cdperson select distinct(cdperson) = 100
value
Table 4
cdperson select distinct(cdperson) = 18
sum
I want this result ordered by number of distinct cdperson
table3 100
table4 18
table 10
Thks for your answersI had to add schema name to the above script to make it work in AdventureWorks:
CREATE TABLE #temp(TableName sysname , CNT BIGINT)
DECLARE @QRY NVARCHAR(MAX);
SET @qry=(SELECT
N'INSERT INTO #TEMP SELECT '''+schema_name(t.schema_id)+'.'+T.[name] +''' AS TableName, COUNT (DISTINCT ProductID) DistCount FROM '+
schema_name(t.schema_id)+'.'+t.[name] +';'
FROM sys.schemas s INNER JOIN sys.tables t ON s.schema_id=t.SCHEMA_ID
INNER JOIN sys.columns c ON t.object_id=c.object_id INNER JOIN sys.types d ON c.user_type_id=d.user_type_id
WHERE c.name ='ProductID'
FOR XML PATH(''))
EXEC(@QRY)
SELECT * FROM #temp ORDER BY TableName
DROP TABLE #temp
Production.Product 504
Production.ProductCostHistory 293
Production.ProductDocument 31
Production.ProductInventory 432
Production.ProductListPriceHistory 293
Production.ProductProductPhoto 504
Production.ProductReview 3
Production.TransactionHistory 441
Production.TransactionHistoryArchive 497
Production.WorkOrder 238
Production.WorkOrderRouting 149
Purchasing.ProductVendor 211
Purchasing.PurchaseOrderDetail 211
Sales.SalesOrderDetail 266
Sales.ShoppingCartItem 3
Sales.SpecialOfferProduct 295
Kalman Toth Database & OLAP Architect
SQL Server 2014 Database Design
New Book / Kindle: Beginner Database Design & SQL Programming Using Microsoft SQL Server 2014 -
Count a distinct column value in an interactive report
Hi,
I'm struggling with this and would appreciate any pointers.
I have built a view here:
http://apex.oracle.com/pls/otn/f?p=11189:1
The report can have duplicate column values. I'm tying to find a way using the interactive report functionality (i.e. without modifying the underlying SQL) to count a distinct column value. For example, in the example following the link I'd like to be able to count the unique customer_ids and return 7.
This may be fundametally impossible depending on the way the SQL is generated for the interactive reports. If this is the case can anyone confirm this?
Many thanks
Yog.<B>"Not modifying views is fair enough. But not wrapping a SELECT around them? Really? Why? And as I said, that's imposible, the IR already does it."</B>
This is not a problem. However for the IR's it's a development decision to keep all reported columns ( and analysis on these columns ) as a seperate development process to be controlled centrally by a seperate team. The point to remember is that the app looks at hundereds of views and it was decided that modifying each report to fit each instance would become cumbersome to manage and to put it simply there are not the resources to do it.
again, I'm not here to justify any of that!
<B>SELECT *" ? Every single time? Even when you want to populate a LOV with some names</B>
No just the Reports. It is a very very simple app; a big list an a page for each report.
<B>"I think you should always ask what you want, not less, not more"</B>
I agree, and that should include select * from view.
<B>"Why fetch data you don't want to see"</B>
Some users will want to see some data from the view and other users will want to see other data, Over the whole user base someone somewhere will want to see all of it.
<B> Actually, there is something good in this, in the future, when you will be asked to check and try to improve the load time of some page, here is the first place where you should look</B>
Brilliant! Like it!!
<B>Does it by any chance fetch a bunch of 20 meg LOBS and then discards them</B>
No, Thankfully a few varchar2 columns and a lot of numeric columns, nothing huge. The reports are acutally pretty fast at the moment... -
Oracle UCM distinct values for attribute in GET_SEARCH_RESULTS ResultSet
hi, can anyone, please, tell if there is a way to get a distinct values of resultset of GET_SEARCH_RESULTS?
I mean, is it possible to obtain distinct values with custom where values of attribute through api?
I need to build somewhat like autosuggest feature, but afaik there is no such service.
Alternatives are
- build my own service which can accept query text, pass it to GET_SEARCH_RESULTS, get results, calculate distinct and output it in hda - i dont like this idea, becouse it means getting whole result set into java while it is efficient to handle in DB
- customize CHECK_IN/UPDATE services to update cached distincts (that means no grouping on custom queries)
May be there was some sort of discussion or blog note?
Thanks in advance.If you do not declare "SearchEngineName" as part of the request, whatever value is defined in config.cfg is used. This behavior is by design.
Setting it in your request allows you to override the system setting in order to use another search engine, provided that the rest of the underlying parts are in place for the engine you are trying to use. For example, if your system setting is "DATABASE.METADATA", setting your individual request to "ORACLETEXTSEARCH" will probably fail, since the Oracle full text indexes would not exist. But if you are doing just metadata searching based on a couple of columns, and don't need the full text part for the request, it's a good way to craft a very specific search. Combined with a couple of database indexes on the columns in the where clause, it's also a good performer.
I do "SearchEngineName=DATABASE" usually in conjunction with the parameter "SearchQueryFormat=Native" so I can create some database specific queries in native SQL (and thus avoiding the need to figure out the exact internet or universal syntax.) Your mileage may vary. -
Query or function that returns distinct values and counts
For the following table:
ID number
address varchar(100)
Where the ID is the primary key and addresses might be repeated in other rows, I'd like to write a query that returns distinct addresses and the count for the number of times the address exists in the table. What's the best way to do this? Thank you in advance.Jlokitz,
select address, count(*)
from table
group by address;
HTH
Ghulam -
Count distinct values in report builder
i have a situation where i have to count distinct number of customers.
i have a query which returns the list of values of bill_to_customer_id from ra_customer_trx_all table and i have to display only the number of distinct customers. i cant do this in the query because it has to be grouped and i am doing it in an aging report. i have to list the number of distinct customers in each aging period. can anybody please help me how to achieve this in reports 6i.
thankshow can i count distinct values in reports?
the situation is like this
i have a query which lists customer_id, invoice number, amount due
so what i want is to count the distinct customer_id and display the number of distinct customers. one customer_id can be repeated any number of times but i should count it only once. -
Error: The sort order specified for distinct count records is incorrect
When processing a measure group with a distinct count measure in it, i get the following error:
"The sort order specified for distinct count records is incorrect."
I have no idea what this means - any ideas?I had the same problem and your fix worked. In more detail, the problematic field was contract_no. I added a named calculation to the table in the Data Source View with the formula CHECKSUM(contract_no). Then I created the distinct count measure on that named calculation. And, lo and behold, the errors disappeared!
Thank you to Frank.
- CindyCindy P Hoskey -
Pixel counts for specific RGB values
Can Photoshop CS4 find pixel counts in an
image for specific RGB values? Where would
you input these RGB values and would you see the pixels highlighted and totaled?Hi Zeno:
Thank you very much for your response!
Your directions work very well for a single image. I have an entire series of images and was wondering if I could just enter a specific set RGB values some where and see a total pixel count for each one of them.
When I go to foreground color and enter the RGB values there and then set the color range to zero and press OK, I get a Warning: No pixels were selected message.
Any additional advice that you have would be much appreciated.
Thanks, Bretzfrog -
Select distinct values for ssrs sharepoint parameter
hi,
I need distinct selected values for parameter dropdown in shaepoint ssrs. Everrything will work fine if i select value and label field same field like(Location_Code). But i need value field to be different field (LocationID). like (dropdown id in value field
and text as lable field)
Because i am using value field in record filtering.
Any suggestions?
Thanks Manohara RHi Manohar,
Pls check the lin
http://social.msdn.microsoft.com/Forums/sqlserver/en-US/6495db18-a1c1-480b-8c92-89c74ee47cf5/how-to-get-distinct-values-of-sharepoint-column-using-ssrs?forum=sqlreportingservices
Please remember to click 'Mark as Answer' on the answer if it helps you -
"How to get distinct values of sharepoint column using SSRS"
Hi,
I have integrated sharepoint list data to SQL Server reporting services. I am using the below to query sharepoint list data using sql reporting services.
<Query>
<SoapAction>http://schemas.microsoft.com/sharepoint/soap/GetListItems</SoapAction>
<Method Namespace="http://schemas.microsoft.com/sharepoint/soap/" Name="GetListItems">
<Parameters>
<Parameter Name="listName">
<DefaultValue>{GUID of list}</DefaultValue>
</Parameter>
<Parameter Name="viewName">
<DefaultValue>{GUID of listview}</DefaultValue>
</Parameter>
<Parameter Name="rowLimit">
<DefaultValue>9999</DefaultValue>
</Parameter>
</Parameters>
</Method>
<ElementPath IgnoreNamespaces="True">*</ElementPath>
</Query>
By using this query, I am getting a dataset which includes all the columns of sharepoint list. Among these columns, I wanted to display only 2 columns (i.e Region and Sales type) using chart. I have created a Region parameter but when I click preview, the drop down box is giving me all the repeatative values of region like RG1,RG1,RG1,RG2,RG2,RG2,RG2,RG3.......... I wanted to display only distinct values of Region parameter so that whenever end user select region from the parameter drop down, it will display the respective value of Sales type column.
Also when I select only RG1 parameter, it is giving me a chart including the sales type of all the Regions. (it should display me only the sales type of RG1) How can I link these 2 columns so that they will display the values respectively.
I would really appreciate if anyone can help me out with this.
Thanks,
Sam.Hi Sam,
By code, the CAML language doesn’t have any reserved word (or tag) to set this particular filter to remove duplicate results.
In this case, we could use the custom code to get distinct records.
Here are the detailed steps:
1. Create a hidden parameter that gets all the records in one field.
Note: Please create another dataset that is same of the main dataset. This dataset is used for the parameter.
2. Create a function that used to remove the duplicate records.
Here is the code:
Public Shared Function RemoveDups(ByVal items As String) As String
Dim noDups As New System.Collections.ArrayList()
Dim SpStr
SpStr = Split(items ,",")
For i As Integer=0 To Ubound(Spstr)
If Not noDups.Contains(SpStr(i).Trim()) Then
noDups.Add(SpStr(i).Trim())
End If
Next
Dim uniqueItems As String() = New String(noDups.Count-1){}
noDups.CopyTo(uniqueItems)
Return String.Join(",", uniqueItems)
End Function
3. Create another parameter that will be used for filtering the maindata.
Please set the available value to be =Split(Code.RemoveDups(JOIN(Parameters!ISSUE_STATUS_TEMP.Value, ",")), ",")
And the default value to be the value you what such as the first value:
=Split(Code.RemoveDups(JOIN(Parameters!ISSUE_STATUS_TEMP.Value, ",")), ",").(0)
4. Go to the main dataset. Open the property window of this dataset.
5. In the “Filters” tab, set the filter to be:
Expression: <The field to be filter>
Operator: =
Value: =Parameters!Region.Value
The parameter “Region” should be the parameter we created in the step3.
Now, we should get distinct values of SharePoint columns.
If there is anything unclear, please feel free to ask.
Thanks,
Jin
Jin Chen - MSFT
Maybe you are looking for
-
VGA adapter doesn't work when plug it on data show-
i've tried to use my mbair linked to the datashow, but it doesn't show the content of power point document...How can i solve this problem? What are the possible solutions? In my old PC i used to press "fn...F3" and the images were shown...how do i pr
-
Data missing when Exporting or print preview from ALV report
Hi I have a alv report which is showing fine but when i try to export it to say excel sheet or do print preview some of the data is missing. Can anyone tell me how to fix this. or at least point to some document that discuss this problem. Here is my
-
For the last 5 days, I've been getting a message that says firefox has blocked me from opening a new window. I have to x that to get onto facebook. Once I'm in, everything I try to do brings up this message. My friend's posts don't have the share but
-
How do I CREATE IF NOT EXISTS Temp table in PLSQL?
hello, how do I CREATE IF NOT EXISTS Temp table in PLSQL? The following table is to be created in FIRST call inside a recursive function (which you'll see in QUESTION 2). QUESTION 1: CREATE GLOBAL TEMPORARY TABLE TmpHierarchyMap
-
Transaction Log Truncate not working on Sql Server 2012 High Availability Groups
Hi Everyone Firstly I have tried to search the forum for similar issues but can't seem to find any that match our situation. We have a SQL Server 2012 High Availability Group with 2 Nodes Node 1 = Primary Node 2 = Secondary Backup Schedule as follows