how to join muliple tables at a time using linq to sql? - entity-framework

Users:
userid name email
1 venkat v#g.com
2 venu ve#g.com
3 raghu r#g.com
Partners:
id userid partnerid status
1 1 2 1
2 1 3 1
location:
id userid lat lon
1 1 12.00 13.00
2 2 14.00 12.00
3 3 14.00 14.23
Query:
var result = from partner in Partners
join user in Users on partner.UserId equals user.PartnerId
join location in Locations on patner.UserId equals location.PartnerId
where partner.UserId == 1
select new { PartnerId = partner.PartnerId, PartnerName = user.Name, Lat = location.Lat, Lon = location.Lon };
by passing userid=1 as parameter I am getting this result:
partnerid patnername lat lon
2 venkat 14.00 12.00
3 venkat 14.00 14.23
by observation of above result here partnernames are wrong for partnerid = 2 - patname was venu but displaying "venkat"
For partnerid = 3, partnername was raghu but displaying venkat.
How to display the correct partner names?

I believe this JOIN here is wrong:
var result = from partner in Partners
join user in Users on user.UserId equals partner.PartnerId
You're joining a user on his userId to a partner using his PartnerID.
Don't you need to join a user to a partner using PartnerID in both cases? Something like this:
var result = from partner in Partners
join user in Users on user.PartnerId equals partner.PartnerId

Related

Hibernate Native Query Left Join vs Postgresql Left join (Why are they different)

I'd like to start by saying that I'd like to think I am pretty proficient sql (10 years of experience) and have ran into a head scratcher. I have the following data
Group
Id Name
1 Group 1
2 Group 2
User
Id Name
1 Jon Williams
2 Mike Williams
3 Joyce Copper
Product
Id Name
1 Cookies
2 Milk
3 Ice cream
Status
Id Name
1 Untouched
2 Consumed
3 Partly Consumed
HouseholdProduct
Id Product User Status
1 1 1 1
2 2 2 1
3 3 3 1
4 1 1 1
5 2 2 1
6 3 3 1
GroupHousehold
GroupId HouseholdProductId
1 1
1 2
1 3
2 1
2 2
2 3
In Postgres, I wrote the following
select g.id as Group Id, g.name as Group, p.name as Product,
u.name as User
from group g
left join GroupHouse gh on
g.id = gh.groupId
left join HouseHoldProduct hp on
gh.houseHoldProductId = hp.id
left join User u on
hp.userId = u.Id
left join Product p on
hp.product_id = p.id
order by g.id asc
I get the following data which is correct:
ID Group Product User
1 Group 1 Cookies Jon Williams
1 Group 1 Milk Mike Williams
1 Group 1 Ice cream Joyce Copper
2 Group 2 Cookies Jon Williams
2 Group 2 Milk Mike Williams
2 Group 2 Ice cream Joyce Copper
When I take that exact query and paste it in Hibernate as native query, I get the follow results which is wrong.
ID Group Product User
1 Group 1 Cookies Jon Williams
1 Group 1 Milk Jon Williams
1 Group 1 Ice cream Jon Williams
2 Group 2 Cookies Jon Williams
2 Group 2 Milk Jon Williams
2 Group 2 Ice cream Jon Williams
It looks like hibernate is the left joining to the User table incorrectly. Does anyone know why this is happening? Is there a way to fix this?

group by substraction power bi

I have data like this and I want to group on rows with substraction between dates
Customer Date price
Jane 01/01/2018 10
Jane 01/02/2018 14
Joe 01/01/2018 10
Joe 01/02/2018 15
I need to obtain:
Customer price
Jane 4
Joe 5
How to perform this in power Bi ?
Try adding this into a calculated column:
Difference =
var LatestDate = Table[Date]
var LatestValue = Table[Price]
var PreviousDate = Dateadd(Table[Date],-1, day)
var PreviousValue = CALCULATE(FIRSTNONBLANK(Table[Price],1),
FILTER(Table, Table[Date]=PreviousDate))
RETURN IF(CONTAINS(Table,Table[Date],PreviousDate), LatestValue-PreviousValue , 0)

How to write perfect query for join operation on two table?

I have two tables. I wrote this query but don't show for table result
how write perfect query show table result?
Taccess
id userid groupid accesstype
1 2 1 1
2 4 1 1
Tgroup
grugroupid groupbname userid
1 group-1 1
2 group-2 2
3 group-3 3
4 group-4 4
I need this result for userid=1
I want show all groupid then show accesstype userid=1
grugroupid groupbname accesstype
1 group-1 1
2 group-2 null
3 group-3 null
4 group-4 null
select grugroupid ,
userid ,groupbname ,
(select AccessType from Access where UserID=2 ) as dd,
from TLab_Info
union
select id userid groupid accesstype
from TLab_Access
SELECT g.grugroupid,g.groupbname,a.accesstype
FROM Tgroup g LEFT JOIN
Taccess a on g.grugroupid=a.groupid
WHERE (a.accesstype IS NULL) OR (a.accesstype=1)
GROUP BY g.grugroupid,g.groupbname,a.accesstype
Result:
grugroupid groupbname accesstype
----------------------------------
1 group-1 1
2 group-2 (null)
3 group-3 (null)
4 group-4 (null)
See result in SQL Fiddle.
additional variant to already posted by Raging Bull
SELECT DISTINCT
g.grugroupid,
g.groupbname,
a.accesstype
FROM Tgroup g LEFT JOIN
Taccess a ON g.grugroupid = a.groupid
WHERE COALESCE(a.accesstype,1) = 1
test is here SQL Fiddle

entity framework with where orderby and group to get unique values

I have a table like below
Id UserId GroupId
-----------------------
1 4 1
2 4 2
3 5 1
4 5 2
5 6 1
I would like to get UserId not belongs to GroupId with query and bind with dropdown list
var list = _uou.UserInGroups.GetAll().where(u=>u.GroupId!=2).ToList().Select(s => new TextValuePair
{
Text = s.User.UserName,
Value = s.UserId.ToString()
})
.ToArray();
it returns results
Id UserId GroupId
-----------------------
1 4 1
3 5 1
5 6 1
but i want it return only result as below because if a user assign multiple group then don't show again
Id UserId GroupId
-----------------------
1 6 1
what should the query for above result?
Thanks in advance
Is this what you are aiming at?
var list = _uou.UserInGroups.GetAll().where(u=>u.GroupId!=2 && u.UserId == 4).ToList().Select(s => new TextValuePair
{
Text = s.User.UserName,
Value = s.UserId.ToString()
})
.ToArray();

Combine two SQL queries - COUNT and WHERE

I'm trying to combine these two SQL Queries to show the username with the highest count of 1's in the access column that also has an itemID of 2 AND Access of 1.
How would I combine these two statements to produce this result?
Show the account with the highest permissions:
SELECT UserName, COUNT(Access) AS Count
FROM Permissions
GROUP BY UserName, Access
HAVING Access = 1
ORDER BY Count DESC
Result:
Username AccessCount
user1 2
user2 1
user3 2
Show all accounts with Data Manager access:
SELECT UserName FROM Permissions
where itemid = 2 AND Access = 1
Result:
Username
user1
user2
Permissions table sample data:
itemID UserName Access
1 user1 1
2 user1 1
3 user1 1
1 user2 0
2 user2 1
3 user2 0
1 user3 1
2 user3 0
3 user3 1
The result would be user1 as they have access on itemID 2 and the highest count of access.
SQLFiddle demo here
SELECT TOP 1 UserName, COUNT(Access) AS Count
FROM Permissions ps
WHERE Access = 1
AND
userNAme in
(SELECT UserName
FROM Permissions
where itemid = 2
AND Access = 1
)
GROUP BY UserName
ORDER BY Count DESC
SELECT TOP 1 *
FROM
(
SELECT [USERNAME] as un,COUNT(ACCESS) AS COUNT
FROM Permissions
GROUP BY USERNAME,ACCESS
HAVING ACCESS=1
) a
WHERE EXISTS (SELECT [USERNAME] FROM Permissions WHERE [USERNAME]=a.un and itemid=2)
ORDER BY COUNT DESC