Select data from Multiple table and show merged data into view mvc3 - asp.net-mvc-3

I have two tables like
USER_LOGIN
USER_ID USER_NAME PASSWORD
--------------------------------------------
1 User 1 some data
2 User2 somedata
3 User3 Some Data
And
USER_DETAIL
ID USER_ID NAME ADDRESS
-------------------------------
1 2 Name 2 Address
2 3 Name Three
Now how can select data from this two table by user id and show merged data into view??

Do your join operation in SQL Server as a function
Bring the result of the function from database to application using LINQ
Apply your result to the grid datasource

Yes, You can make it through join concept in SQL server
select * from USER_LOGIN A join USER_DETAIL B on A.USER_ID=B.USER_ID
Now you'll get the all data from joining two tables and can pass to a datatable or dataset anything, then you can pass it to controller and to view. Still you can choose you join style it may be "leftjoin","rightjoin" like this.
Please refer these links for clear explanation
http://www.w3schools.com/sql/sql_join.asp
http://beginner-sql-tutorial.com/sql-joins.htm
Hope it helps!!!

Related

Removing duplicate columns in one row while merging corresponding column values

Apologies if this seems simple to some, I am still in the (very) early stages of learning!
Basically I've got a table database that has multiple users (Users_ID), each with a corresponding access name(NAME). The problem is, some Users have multiple access names, meaning when the data is pulled, there is duplicates in the User_ID column.
I need to remove the duplicates in the User column and join their corresponding access names in the NAME column, so it only takes up 1 row and no data is lost.
The current SQL query I'm using is :
select Table1_user_id, Table2.name,
from Table1
inner join Table2
on Table1.role_id = Table2.role_id
An example of what this would return:
USER_ID | NAME
------- --------------
Tim Level_1 Access
John Level 2 Access
Tim Level 2 Access
Mark Level 3 Access
Tim Level 3 Access
Ideally, I would remove the duplicates for Tim and display as following:
USER_ID | NAME
------- ----------------------------------------------
Tim Level_1 Access, Level 2 Access, Level 3 Access
John Level 2 Access
Mark Level 3 Access
Thanks in advance for any help regarding this and sorry if something similar has been asked before!
Use GROUP_CONCAT with SEPARATOR :
SELECT Table1.user_id, GROUP_CONCAT(Table2.name SEPARATOR ',') AS Ename
FROM Table1
INNER JOIN Table2 ON Table1.role_id = Table2.role_id
GROUP BY Table1.user_id
select Table1_user_id, LISTAGG(Table2.name,', ') WITHIN GROUP (ORDER BY Table2.name) as Name
from Table1
inner join Table2
on Table1.role_id = Table2.role_id

Laravel Relation Many to Many

I'm using laravel and sentinel (Sentry) that default using users, groups, and users_groups database table
users
-----------------
id name
1 user
2 user
3 admin
groups
-------------------
id name
1 Users
2 Admins
users_groups
--------------------
user_id group_id
1 1
2 1
3 1
3 2
i define the relation on laravel model and it's work fine when i try to print
$users = Group::find($id)->user;
but how to print just user only?
$users = Group::where(array("name"=>"Users"))->get()->user;
it's still show all data include the admins, i just want to show user only, how to do this?
i know the problem because at users_groups table admin have Users id too..
Please someone help me. It's verry confusing
You can use with to get the data of specified relation as below :
$users = Group::with('user')->find($id);
This will return data of group with user.
If you just want a data of only user table you have to change the query and rather than using model Group you have need to query on User model something like below:
$users = User::where('group_id', $groupId);
http://laravel.com/docs/5.1/eloquent-relationships
http://laravel.com/api/5.1/Illuminate/Database/Eloquent/Model.html#method_with

Need Only Differing column values from two DataTables

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!

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.

What is the best way to fetch Tree/Hierarchical structure data from Oracle

I want to fetch Hierarchical/Tree data something like below from a Table which has following definiton.
Tree Table:
"""""""""""
Id |ParentId
"""""""""""
Work1|null
Work2|Work1
Work3|Work2
...
Required Query result Data (no need to be tabbed)- If I Pick 'Work1' I should complete Ids which are under its root something like below. If I pick 'Work2' then also I should complete Ids above and below its root.
> Work1
----------
> Work2
----------
> Work3
---------
I used below query but it is giving me duplicate records.
select Id from TreeTable start with Id in ('Work1','Work2') connect by nocycle Parentid=prior Id or Id = prior Parentid
Note: I want to get the data within a SINGLE QUERY (optimized) way
Thanks and Regards,
PV.
SELECT id
FROM q
START WITH
id IN ('Work1', 'Work2')
CONNECT BY
parent = PRIOR id
AND id NOT IN ('Work1', 'Work2')
This condition:
AND id NOT IN ('Work1', 'Work2')
will cut off the branches already selected with START WITH.

Resources