Need Only Differing column values from two DataTables - asp.net-mvc-3

I have two DataTables in which I need only differing column values in a final DataTable as shown below
DataTable(DT1)
ID Name Salary
1 ABC 2000
2 XYZ 4000
3 Suresh 6000
DataTable(DT2)
ID Name Salary
1 ABC 3000
2 XYZ 5000
3 Suresh 6000
DataTable(DT3)
ID Salary(DT1) Salary(DT2)
1 2000 3000
2 4000 5000
How can I accomplish this using linq queries on Datatables or by any other way? I tried looping and comparing each column values both datatables, but I'm not getting how to store in final DataTables.

First we need to join the tables (assuming the ID's have a PK/FK relationship), Then compare the values and place them in a new table.
var unMatchedRows = from DataRow dt1Row in dt1.Rows
join DataRow dt2Row in dt2.Rows
on dt1Row["id"] equals dt2Row["id"]
where Convert.ToDoubl(dt1Row["salary"]) != Convert.ToDouble(dt2Row["salary"])
select new { dt1Row, dt2Row };
This gives us a collection of DataRow objects joined on the ID where the salary's are different. Now we just need to populate a new DataTable with the rows.
foreach (var m in unMatchedrows)
{
dt3.ImportRow(m);
}
It is worth noting that DT3 will need to have the same schema as the other two tables. And this will only place the unmatched rows in the new table. There are several ways to do this and I am not in front of an IDE right now to test it out, but this should get you going. Hope this helps!

Related

Complex count row etl requirement

I have a requirement related as below
1-If there is employee record then count the number of rows
a-if there are four rows then follow the layout 1,
and populate the column1 and column 2 with values in report and ltrimrtrim
b- if there are three rows, then follow the layout 2,
and hardcode the column 1 and column 2 with NULL
Otherwise, look for the employee record.
Couldn't get the logic, I used the router with as if column 1 and two null the send to layout two else 1. But the requirement is different.
router transformation, if null, layout one else 2
Step 1 - Use SRT>AGG>JNR to calculate count. create new column as count_all and set to COUNT(*). Please group by proper key columns.
Step 2 - Use RTR next to split data based on your condition.
group 1- count_all =4 then follow the layout 1 and...
group 2- count_all =3 then follow the layout 2 and...
group 3 - if count <3 then do employee record.

Append two tables with different sizes

I have table one with 5 columns and table two with 7 columns.
How can I append them into a new table with only the common columns (that is, the columns with common names)?
I tried all sorts of "Append Queries" in the Query Editor but it seems that it only works with exactly identical tables.
In DAX, UNION seems to have the same limitation.
Use Append Queries -> Append as New to create a new table.
The M function, Table.Combine(), will combine the columns where they are named the same and add columns with null values where they only exist in one table
Table.Combine({
Table.FromRecords({[Name = "David", Phone = "01235667886"]}),
Table.FromRecords({[Email= "me#gmail.com", Phone = "01124892522"]})
})
> Name Phone Email
> David 01235667886
> 01124892522 me#gmail.com

How do I update multiple records in Oracle?

I have millions of records in a table and I need to update particular records which have wrong values. How do I do it?
Example:
Si Item_Id
1 T21547856
2 T45200254
3 T54785000
Need to update like:
T21547856 = CS2541
T54785000 = CS5475
This is just an example. I have millions of records and need to update more than half a million.
One approach would be:
Create an index on item_id, then just do the updates. update table set item_id = 'CS2541' where itme_id = 'T21547856'
This works only item_ids are unique in your table.
After this, you may drop the index if you don't need it.
A second approach would be to create another table, B, with values to be updated:
si item_id
1 CS2541
3 CS5475
Then do a merge:
merge into your_table a
using b
on a.si=b.si
when matched then update set a.item_id=b.item_id;

oracle connect by multiple parents

I am facing an issue using connect by.
I have a query through which I retrieve a few columns including these three:
ID
ParentID
ObjectID
Now for the same ID and parentID, there are multiple objects associated e.g.
ID ParentID ObjectID
1 0 112
1 0 113
2 0 111
2 0 112
3 1 111
4 1 112
I am trying to use connect by but I'm unable to get the result in a proper hierarchy. I need it the way it is showed below. Take an ID-parentID combo, display all rows with that ID-parentID and then all the children of this ID i.e. whose parentID=ID
ID ParentID ObjectID
1 0 112
1 0 113
3 1 111
4 1 112
2 0 111
2 0 112
select ID,parent_id, object_id from table start with parent_id=0
connect by prior id=parent_id order by id,parent_id
Above query is not resulting into proper hierarchy that i need.
Well, your problem appears to be that you are using a non-normalized table design. If a given ID always has the same ParentID, that relationship shouldn't be indicated separately in all these rows.
A better design would be to have a single table showing the parent child relationships, with ID as a primary key, and a second table showing the mappings of ID to ObjectID, where I presume both columns together would comprise the primary key. Then you would apply your hierarchical query against the first table, and join the results of that to the other table to get the relevant objects for each row.
You can emulate this with your current table structure ...
with parent_child as (select distinct id, parent_id from table),
tree as (select id, parent_id from parent_child
start with parent_id = 0
connect by prior id = parent_id )
select id, table.parent_id, table.object_id
from tree join table using (id)
Here's a script that runs. Not ideal but will work -
select * from (select distinct test.id,
parent_id,
object_id,
connect_by_root test.id root
from test
start with test.parent_id = 0
connect by prior test.id = parent_id)
order by root,id
First of all Thanks to all who tried helping me.
Finally i changed my approach as applying hierarchy CONNECT BY clause to inner queryw ith multiple joins was not working for me.
I took following approach
Get the hierarchical data from First table i.e. table with ID-ParentID. Select Query table1 using CONNECT BY. It will give the ID in proper sequence.
Join the retrieved List of ID.
Pass the above ID as comma seperated string in select query IN Clause to second table with ID-ObjectID.
select * from table2 where ID in (above Joined string of ID) order by
instr('above Joined string of ID',ID);
ORDER BY INSTR did the magic. It will give me the result ordered by the IN Clause data and IN Clause string is prepared using the hierarchical query. Hence it will obviously be in sequence.
Again Thanks all for the help!
Note: Above approach has one constraint : ID passed as comma separated string in IN Clause. IN Clause has a limit of characters inside it. I guess 1000 chars. Not sure.
But as i am sure on the data of First table that it will not be so much so as to cross limit of 1000 chars. Hence i chose above approach.

Data returned from Oracle Proc in vb.net

I have a VB.Net function which executes a Oracle Stored Proc and returns a dataset.
Below is a list of sample records
OrderID OrderDetail Qty Date Supplier Price
1 Books 10 10-Aug-08 ABC Inc 100.00
1 Pens 20 10-Aug-08 ABC Inc 300.00
2 Keys 1 20-Aug-09 Blue cross 100.00
2 Nots 3 30-Aug-09 Blue Cross 200.00
The above records are returned as a dataset in my function.
Using the above dataset int two different functions how can I return data as shown below.
First function should return only distinct orderID records
Second function should take OrderID as input and return records based on orderID
Any suggestions?
Thanks
For the select distinct, check out Select DISTINCT on DataTable. For the other, use the DataTable.Select method. Assuming that the DataTable is the first in the DataSet ...
var dt = ds.Tables[0];
var rows = dt.Select( "OrderID = 1" );
(Sorry, I don't know any VB :)

Resources