How to get count of repeated values in datatable in asp.net - asp.net

I want total count of repeated values in a datable.
as shown in image total count should be : 2 because repeated values are only 1 and 2.
I tried as given below:
DataView dv = new DataView(dtTemp);
int iRowCount = dv.ToTable(true, "Column1").Rows.Count;
but it returns 3 which is incorrect.
Does anyone knows how to do it.
I don't want to use loop becoz sometimes data table contains 4000-5000 rows so if we use loop it will take much more time to get the total count.
Thanks.

Related

Confusing LOD expressions in Tableau

I have the following data structure:
Scope,Metric ID,Item ID,System,Color
TRUE,A1,123,A,Red
FALSE,A1,123,B,Red
FALSE,B1,234,C,Red
TRUE,B1,234,A,Red
FALSE,B1,415,A,Red
I'd like to group by Scope, filter on TRUE and get the unique list of Items, then count these Items and subtract from a total unique count for the Color = Red.
So, in the example above, I have 3 unique items for Color = Red and I have 2 unique items with Scope = TRUE, so the result should say 3 - 2 = 1.
Because of the data structure, simple filtering won't help. I realize I need to use a complex LOD syntax, but after having tried them for a few hours, I find them rather confusing.
Does anyone have an idea how to write an LOD expression to give me the desired count? Thanks!
Did you try using 3 calculated fields like this:
then doing a count distinct on them.
1:
if [Color]='Red' then [Item ID] end
2:
if [Scope]='TRUE' then [Item ID] end
3 :
subtract the 2 calculated fields i,e 2-1
It gives out 1.

Rolling past and future averages from current value

I think I am mostly there, but I can't figure out the remaining piece of how to code this properly.
I begin with a single column of 15 values. I want to create two new columns with the 'previous' containing the average of the previous two values, and the 'future' creating the average of the next two values.
My code is failing because it is INCLUSIVE of the current row's values.
For example, row3 or '30' should have a 'previous' value of 15 ((10+20/2)) and a future value of 45 ((40+50)/2). instead it is returning 25 and 35 because it is including the 30 with the 20 or 40 when making the averages.
I also am stuck on how to just display the previous value.
Anyone mind telling me how to avoid this problem that I am experiencing?
I am using filter but I don't know if there is a better way to do this..
values=data.frame(col=c(10,20,30,40,50,60,70,80,90,100,110,120,130,140,150))
values$previous2 = filter(values$col, rep(1/2,2),sides=1)
values$future2 = filter(values$col, rep(1/2,2),sides=2)
values$last = #should be the previous value - ex 2nd row should be 10
values
For returning the last value try:
values$last = c(NA,values[-nrow(values),1])
or the lag function could be used as well I believe.

JPA/JPQL COUNT question

I have the following JPQL query -
SELECT f.md5
FROM File f, Collection leafCollections, Collection instCollections
WHERE (f.status = com.foo.bar.FileStatus.Happy OR f.status = com.foo.bar.FileStatus.Sad)
AND f.collectionId = leafCollections.collectionId
AND leafCollections.instanceCollectionId = instCollections.collectionId
GROUP BY f.md5, instCollections.collectionId
It basically returns the md5s for files which are organized in a hierarchy (tree) such that if the same MD5 appears in more then one leaf in a particular branch of the hierarchy it will be only shown once (thanks to the GROUP BY).
This works fine. Let's say I get 100 rows back. Each row containing an md5 as a string.
Now I want to get the COUNT of the rows returned. I thought I could simply do:
SELECT COUNT(f.md5)
FROM File f, Collection leafCollections, Collection instCollections
WHERE (f.status = com.foo.bar.FileStatus.Happy OR f.status = com.foo.bar.FileStatus.Sad)
AND f.collectionId = leafCollections.collectionId
AND leafCollections.instanceCollectionId = instCollections.collectionId
GROUP BY f.md5, instCollections.collectionId
However this returns 100 rows, each one containing a long representing the number of times the md5 appeared in a branch. What I wanted was simply to get 1 row back with a long value of 100 being the total count of rows the original query returned. I feel like I am missing something obvious.
Suggestions?
As #doc_180 has commented. Using getSingleResult() will yield the count
Use the following code to get the result.
// Assuming that the Query is strQ
Query q = entityManager.createQuery( strQ );
int count = ( (Integer) q.getSingleResult() ).intValue();

Measure names containing "Total" have strange grand total calculation in cubes

In programmatically building cubes for SQL Server Analysis Services using AMO, I've discovered that when a measure has "Total" in it's title, the grand total in the cube is calculated by a distinct sum instead of just a sum (creating very strange results)
This doesn't occur when building cubes using DSO. Does anyone know of why this could be happening?
Please pardon my use of python:
class MeasureSpec(MeasureSpec):
def create(self, measureGroup, cube, dsv, factTable):
log("creating measure:", self.name)
measure = measureGroup.Measures.Add(self.name)
measure.AggregateFunction = self.aggregateFunction
measure.FormatString = self.format
# Set datatype to integer for counts otherwise this is set to the same
# type as the source column in createDataItem
if self.aggregateFunction in (aggCount, aggDistinctCount):
measure.DataType = MeasureDataType.Integer
measure.Visible = self.isVisible
measure.Source = createDataItem(dsv, factTable, self.column.getColumnName())
Here's what was going on. AMO was tagging the datacolumn as byte for anything with Total in the name. It was cycling to 32 which amazingly was the same number as a distinct sum on the column... Wow.

Randomize numbers in VB.NET without using a range i.e. Min and Max

I was wondering if anyone can point me in the right direction please? Say I am pulling the following table, I would then like to select an ID randomly. I understand how to select a random number using a Randomize() call followed by the relevant syntax but I want to pre-define the range.
i.e. Table Data
ID | Name
4345 Mike
3456 Lee
4567 John
There will be many more names but for this example you could use 3 or 4 etc..
Please help I'm starting to itch :o|
Just to make sure I understand what you want:
Given a table, you want to randomly select one of the ID values from that table.
If so, this should do it:
Dim rand As New Random()
Dim record As Integer = rand.[Next](0, myDataTable.Rows.Count)
Dim randomID As Integer = CInt(myDataTable.Rows(record)("ID"))
We have all the information we need to randomly select a row, and by extension randomly select one of the ID values in the table.
In old Vb you would do
Dim i as integer
i = (Rnd * (maxval-minval)) + minval
Since rnd returns a random number between 0 and 1 you would scale the number to the right range.

Resources