Retrieving Distinct Records using Query Expression in Microsoft CRM 2011 - distinct

I have a doubt in retrieving records in CRM 2011 using C# RetrieveMultiple method in a web service.
I need to retrieve distinct records from an entity based on a attribute value(primary key). I am able to achieve this by using the code below
QueryExpression query = new QueryExpression("entityname");
query.ColumnSet.AddColumns("primarycolumn", "column1");
query.Distinct = true;
EntityCollection result1 = serviceProxy.RetrieveMultiple(query);
Output:
primarycolumn column1
xyz 1
lmn 2
This displays distinct records. But if I add few more columns to the columnset, the results are not distinct. This is shown in the code below
QueryExpression query = new QueryExpression("entityname");
query.ColumnSet.AddColumns("primarycolumn", "column1", "column2");
query.Distinct = true;
EntityCollection result1 = serviceProxy.RetrieveMultiple(query);
Output:
primarycolumn column1 column2
xyz 1 a
xyz 1 b
lmn 2 a
I need the result to be distinct only based on the primary column.
Please help me in how to achieve this.
More information on the above question.
Actually, the code is in C#. The table below shows the table which I am querying
**Primary
column Column1 Column2 Column3<br/>**
Xyz Value1 Value1 Value1 <br/>
Xyz Value2 Value2 Value2<br/>
Lmn Value1 Value1 Value1<br/>
Lmn Value2 Value2 Value2<br/>
Xyz Value1 Value1 Value1<br/>
Lmn Value1 Value1 Value1<br/>
The query result should be like below. Where only the primary columns distinct values must be considered and all other columns can be either distinct or not distinct. Any one row of the distinct value of the primary column should only be displayed.
(This is the output I am trying to achieve)<br/>
**Prmrycolumn Column1 Column2 Column3<br/>**
Xyz Value1 Value1 Value1<br/>
Lmn Value1 Value1 Value1<br/>
The below code gives the following output
QueryExpression query = new QueryExpression("entityname");
query.ColumnSet.AddColumns("primarycolumn", "column1");
query.Distinct = true;
EntityCollection result1 = serviceProxy.RetrieveMultiple(query);
**Primary column Column1**<br/>
Xyz Value1<br/>
Lmn Value1<br/>
But as I add more columns to the columnset, the output is considering distinct values of other columns also
QueryExpression query = new QueryExpression("entityname");
query.ColumnSet.AddColumns("primarycolumn", "column1", "column2");
query.Distinct = true;
EntityCollection result1 = serviceProxy.RetrieveMultiple(query);
**Primary
column Column1 Column2 Column3<br/>**
Xyz Value1 Value1 Value1<br/>
Xyz Value2 Value2 Value2<br/>
Lmn Value1 Value1 Value1<br/>
Lmn Value2 Value2 Value2<br/>
ctually, the code is in C#. The table below shows the table which I am querying
**Primary
column Column1 Column2 Column3<br/>**
Xyz Value1 Value1 Value1 <br/>
Xyz Value2 Value2 Value2<br/>
Lmn Value1 Value1 Value1<br/>
Lmn Value2 Value2 Value2<br/>
Xyz Value1 Value1 Value1<br/>
Lmn Value1 Value1 Value1<br/>
The query result should be like below. Where only the primary columns distinct values must be considered and all other columns can be either distinct or not distinct. Any one row of the distinct value of the primary column should only be displayed.
(This is the output I am trying to achieve)<br/>
**Prmrycolumn Column1 Column2 Column3<br/>**
Xyz Value1 Value1 Value1<br/>
Lmn Value1 Value1 Value1<br/>
The below code gives the following output
QueryExpression query = new QueryExpression("entityname");
query.ColumnSet.AddColumns("primarycolumn", "column1");
query.Distinct = true;
EntityCollection result1 = serviceProxy.RetrieveMultiple(query);
**Primary column Column1**<br/>
Xyz Value1<br/>
Lmn Value1<br/>
But as I add more columns to the columnset, the output is considering distinct values of other columns also
QueryExpression query = new QueryExpression("entityname");
query.ColumnSet.AddColumns("primarycolumn", "column1", "column2");
query.Distinct = true;
EntityCollection result1 = serviceProxy.RetrieveMultiple(query);
**Primary
column Column1 Column2 Column3<br/>**
Xyz Value1 Value1 Value1<br/>
Xyz Value2 Value2 Value2<br/>
Lmn Value1 Value1 Value1<br/>
Lmn Value2 Value2 Value2<br/>

Editted Answer:
I need the result to be distinct only based on the primary column.
The above requirement is not possible using the RetrieveMultiple service call. What you are essentially asking for is a way to retrieve the first record for each record with a unique primary column. This might be possible using generated Early Bound entities but it is not possible using the standard service methods alone.
To achieve what you are after, you will need to retrieve all the records, which is what your current query does:
ueryExpression query = new QueryExpression("entityname");
query.ColumnSet.AddColumns("primarycolumn", "column1", "column2");
query.Distinct = true; // this distinct will apply over all columns
EntityCollection result1 = serviceProxy.RetrieveMultiple(query);
Once this data has been received, you will need to filter out all the records you want to ignore. In your case, you only want the first record found. You can achieve this using C# code as follows:
// this code filters out all records except the
// first for each unique primary column
var unique = result1.Entities.GroupBy(item => item.GetAttributeValue<Guid>("primarycolumn"))
.Select(item => item.First());

Related

Extract character before space from UK postcode

In my data Postcode records are as below
1.'ABC XYZ'
2.' EFG PQR'
Requirements is to get all character before space
So for first record ,I am getting expected result if I am putting
select NVL(substr(postcode,0,instr(postcode,' ')-1), postcode)
But for second record I am getting whole postcode value . Because in second record ' '(space is at very beginning).
I tried multiple query but not getting the results .
I want single expression which handles both scenarios.
Try this:
NVL(substr(ltrim(postcode),0,instr(ltrim(postcode),' ')-1), postcode)
A simple option uses regular expressions:
Sample data:
SQL> with test (id, postcode) as
2 (select 1, 'ABC XYZ' from dual union all
3 select 2, ' EFG PQR' from dual
4 )
Query:
5 select id, postcode,
6 regexp_substr(postcode, '\w+') result
7 from test;
ID POSTCODE RESULT
---------- -------- --------
1 ABC XYZ ABC
2 EFG PQR EFG
SQL>

Traverse records into columns for huge tables +800 million records

How can I traverse records (actually a subset of columns) into one-record columns - up to 99 columns -- for a huge table?
I mean, I have a table with following sample structure/data :
TABLE_ORI
COLUMN1 COLUMN2 COLUMN3 CODE VALUE
------- ------- ------- ---- ------------
C1 C2 C3 1 Value1
C1 C2 C3 2 Value2
C1 C2 C3 3 Value3
C100 C39 C21 1 Value40
C100 C39 C21 2 Value41
I want to convert this data into:
TABLE_NEW
COLUMN1 COLUMN2 COLUMN3 VALUE1 VALUE2 VALUE3 VALUE4 VALUE5 ... VALUE99
------- ------- ------- ------ ------ ------- ------ ------ -------
C1 C2 C3 Value1 Value2 Value3
C100 C39 C21 Value40 Value41
Please consider this is a big table and result table can have up to 99 value columns. I tried a PL/SQL with nested loop besides bulk collect cursor but the process takes days and never ends.
Thanks a lot!
This would probably be the fastest way:
create table table_new as
select /*+ parallel */ column1, column2, column3,
max(case when code = 1 then value else null end) value1,
max(case when code = 2 then value else null end) value2,
max(case when code = 3 then value else null end) value3,
max(case when code = 4 then value else null end) value4,
max(case when code = 5 then value else null end) value5,
--...
max(case when code = 99 then value else null end) value99
from table_ori
group by column1, column2, column3;
That assumes you have Enterprise Edition, a database that is configured to use parallelism properly, a large amount of tablespace to sort all the data at one time, etc.
It would also help performance to use the option NOLOGGING when creating the table. That would avoid generating a lot of REDO and UNDO although at the expense of the table not being recoverable.
For large processes like this, Real-Time SQL Monitoring is the perfect way to diagnose problems. If the above SQL is taking a long time, run a statement like this to monitor the SQL and see what operations and events are taking up the most time:
select dbms_sqltune.report_sql_monitor('$the_real_sql_id') from dual;

Reordering data table rows based on value in one column

I have a data table with one column called "position". There can be no duplicates. I want to be able to reorder the rows if the value of this column is changed. For example,
**Name Column2 Position**
Name1 value1 1
Name2 Value2 2
Name3 Value3 3
Name4 Value4 4
Name5 Value5 5
If I change the position number 3 to 1, then the table would look like:
**Name Column2 Position**
Name3 Value3 1
Name1 value1 2
Name2 Value2 3
Name4 Value4 4
Name5 Value5 5
If I change the position number 3 to 5, in original data table, then:
**Name Column2 Position**
Name1 value1 1
Name2 Value2 2
Name4 Value4 3
Name5 Value5 4
Name3 Value3 5
(kind of like NetFlix, when you reorder your movie list)
The question is long ago but maybe this could help someone who has the same problem.
//Create a DataView from your table.
DataView view = new DataView(YourTable);
// Sort by Position column
view.Sort = "Position ASC";
//and get your new table
newTable = view.ToTable;
// or you just use the DataView for diplaying your values

change the value of a query when inserting

tab1
column1 =4
column2 = (empty column )
column3 = also empty
what I want is when I insert data into column2
column2 = test
I want column 2 to have the data of column1 , I want to have such result
column 2= 4test
I know I can achieve that with a trigger or procedure , I am asking you if there is another way when I insert it
Try
update tab1 set column2 = column1 || 'test' where condition;

oracle update each distinct row with new value

value group
-----------------
value2a, value2
value2b, value2
value2c, value2
value3a, value3
value3b, value3
value3c, value3
value3d, value3
value4a, value4
value4b, value4
value4c, value4
value4d, value4
value5a, value5
value5b, value5
value5c, value5
How can I UPDATE table1 so that my script basically runs through every line and changes 'group' to something like
group-1
group-2
group-3
group-4
group=5
instead of
value1
value2
value3
value4
value5
If I can get the distinct values with this simple qquery
SELECT DISTINCT group FROM table1;
I must then be able to do some sort of
UPDATE table1 SET group = CONCAT('group-',rownum) WHERE group = ....
Can you just do this:
UPDATE table1 SET group = REPLACE(t.group, 'value', 'group-')
Reference:
REPLACE

Resources