Multiple groups in LinqToSql - linq-to-sql

I dont seem to be able to find any evidence of multiple groupby being supported in LinqToSQl on the internet (I should probably buy a book :))
I'd like to be able to do this:
var qry = from s in DBContext.Styles
join artist in DBContext.Artists on s.ID equals artist.StyleID
join track in DBContext.Tracks on s.ID equals track.StyleID
group artist by new { s.ID, s.Name } into a
group track by new { s.ID, s.Name } into t
select new DTO.StyleSummary
{
ID = a.Key.ID,
Name = a.Key.Name,
NumberOfArtists = a.Count(),
NumberOfTracks = t.Count()
};
However the 2nd group statement prevents compilation.. Since this is possible in native sql how can I do this??
Thoughts? Is this even possible?

Its because you don't really even need a groupby in this LINQ statement. You can just do
var qry = from s in DBContext.Styles
join artist in DBContext.Artists on s.ID equals artist.StyleID into a
join track in DBContext.Tracks on s.ID equals track.StyleID into t
select new DTO.StyleSummary
{
ID = s.ID,
Name = s.Name,
NumberOfArtists = a.Count(),
NumberOfTracks = t.Count()
};
Note the into

Related

Two related queries needs to be one

I know this is a basic thing but SQL is a serious weak point of mine...
I have the following query ("query1")
select
SC.statues, SC_some_real_code
from
[corpdb_gs].[dbo].[Simple_Codes] SC
inner join
[corpdb_gs].[dbo].[real_simple_essentials] RSE
on
SC.statues = RSE.se_statutes
AND
SC.some_real_code = RSE.se_statutes_reason
I need to return only the rows in table [db2].[dbo].[statusYo] where
statusYo.code = "query1".SC.statues
AND
statusYo.reason = "query1".SC.some_real_code
Help?
select * from [db2].[dbo].[statusYo] query2
inner join
(select
SC.statues as statues , SC.some_real_code as some_real_code
from
[corpdb_gs].[dbo].[Simple_Codes] SC
inner join
[corpdb_gs].[dbo].[real_simple_essentials] RSE
on
SC.statues = RSE.se_statutes
AND
SC.some_real_code = RSE.se_statutes_reason) query1
on query1.statues =query2.code
and query1.some_real_code=query2.reason
this will work for you......

Selecting data for 1 specific user from multiple tables

So my database is composed of 5 tables with different columns for each one. The only column that keeps them all identified is the id. I'm trying to get the data for a specific user, but I only seem to get all users of the database instead.
This is what I have tried:
SELECT
ControlAccess.UserName,
ControlAccess.Pass,
Users.First_Name,
Users.Last_Name,
UserInfo.Age,
UserInfo.Country,
UserInfo.Address,
UserInfo.ZipCode,
Sessions.Matrix1,
Sessions.Matrix2,
Sessions.Result,
Operations.Operation,
FilePath.LocationFiles
FROM
MatrixUsers.UserInfo
INNER JOIN
MatrixUsers.Users
ON
UserInfo.idUserInfo = Users.idUsers = 1
INNER JOIN
MatrixUsers.ControlAccess
ON
ControlAccess.idControlAccess = UserInfo.idUserInfo = 1
INNER JOIN
MatrixUsers.Sessions
ON
Sessions.idSessions = ControlAccess.idControlAccess = 1
INNER JOIN
MatrixUsers.FilePath
ON
FilePath.idFilePath = Sessions.idSessions = 1
INNER JOIN
MatrixUsers.Operations
ON
Operations.idOperations = FilePath.idFilePath = 1;
I tried putting 1 at the end of each id to see if they matched, but I still get all the users.
I'm new to SQL and I'm only familiar with matching rows, but not choosing specific one.
Here are the columns of each table:
ControlAccess: {idControlAccess, UserName, Pass}
Sessions: {idSessions, Matrix1, Matrix2, Result}
FilePath: {idFilePath, LocationFiles}
Operations: {idOperation, Operation}
UserInfo: {idUserInfo, Age, Country, Address, ZipCode, Phone}
Use WHERE when you want specific user. for example, select user_id from table where user_id=the_specific_user_id . Follow this basic to built you complicate statement.
Just user where after all the joins
WHERE ANY_COLUMN_REFER_TO_USER_ID = YOUR_NEEDED_ID
so your full query would be like :
SELECT
ControlAccess.UserName,
ControlAccess.Pass,
Users.First_Name,
Users.Last_Name,
UserInfo.Age,
UserInfo.Country,
UserInfo.Address,
UserInfo.ZipCode,
Sessions.Matrix1,
Sessions.Matrix2,
Sessions.Result,
Operations.Operation,
FilePath.LocationFiles
FROM MatrixUsers.UserInfo
INNER JOIN MatrixUsers.Users
ON UserInfo.idUserInfo = Users.idUsers
INNER JOIN MatrixUsers.ControlAccess
ON ControlAccess.idControlAccess = UserInfo.idUserInfo
INNER JOIN MatrixUsers.Sessions
ON Sessions.idSessions = ControlAccess.idControlAccess
INNER JOIN MatrixUsers.FilePath
ON FilePath.idFilePath = Sessions.idSessions
INNER JOIN MatrixUsers.Operations
ON Operations.idOperations = FilePath.idFilePath
WHERE UserInfo.idUserInfo = 1

Need Help for formulating Linq query from SQL query

SELECT (count(Competency_Id) * 100 /(Select count(Competency_Id) from [dbo].[QLs]))
as Percentage ,[dbo].[Competencies].Name as CompetencyName
FROM [dbo].[QLs]
INNER JOIN [dbo].[Competencies]
ON [dbo].[QLs].Competency_Id= [dbo].[Competencies].Id
GROUP BY dbo.[QLs].Competency_Id,dbo.[Competencies].Name
I need help to create a Linq from above mentioned SQL query. Please assist. Thanks
var query = (from u in db.QLs
join c in db.Competencies on u.Competency_Id equals c.Id
let total = db.QLs.Count()
group u by new
{
Competency_Id = u.Competency_Id,
Name = c.Name,
total = total
} into g
select new QLbyCompetencyPieChart
{
Percentage = (float)(100 * g.Count()) / ((float)g.Key.total),
Competency= g.Key.Name,
Color=null
}).AsQueryable();

MySQL statement, JOIN

I have these tables :
I don't know how I can write a statement, that takes emails from Table "Firm", that have Location_id = '1' and Category_id = '130';
I know that I should use JOINs, but I'm not sure how to go from there.
SELECT Firm.email
FROM Firm
INNER JOIN FirmID ON Firm.firma_id = FirmID.firma_id
WHERE FirmID.location_id = '1'
AND FirmID.Category_id = '130'
Should be as simple as doing the following:
SELECT email
FROM Firm, FirmID
WHERE Firm.firma_id = FirmID.firma_id
AND FirmID.location_id = 1
AND FirmID.category_id = 130;
It does a join behind the scenes, but can be a bit clearer to understand than using the JOIN keyword.
You could do:
SELECT f.email
FROM Firm f
WHERE f.firma_id =
(
SELECT ff.firma_id
FROM FirmID ff
WHERE ff.location_id = 1
AND ff.category_id = 130
)
Using an inner select.
But using JOINS is in the long term the way to go, what have you tried and what's not working?

Struggling with MySQL query

I have the following model:
I've been at this for a while and still don't know how to tackle it properly. Already looked at joining two aliased subqueries, joining two views, and tried a gynormous and ugly all-in one query, none of which worked.
My question is simple:
How can I select deivce_names.name and match them to a model and manufacturer?
SELECT
name, manufacturer, model
FROM
device_names
JOIN devices ON (device_names.id = name_id)
JOIN devices_generic ON (generic_device_id = devices_generic.id)
JOIN device_manufacturers ON (manufacturer_id = device_manufacturers.id)
JOIN device_models ON (model_id = device_models.id)
select
device_names.name as device_name,
device_manufacturers.name as device_manufacturer_name,
device_models.name as device_model_name
from deivce_names
join device on device.name_id = deivce_names.id
join devices_generic on devices_generic.id = devices.generic_device_id
join device_manufacturers on devices_generic.manufacturer_id = device_manufacturers.id
join device_models on device_models.id = devices_generic.model_id
SELECT dm1.manufacturer, dm2.model from device_manufacturers as dm1 join devices_generic as dg on dg.manufacturer_id = dm1.id join device_models as dm2 on dm2.id = dg.model_id join devices as d2 on d2.generic_device_id = dg.id join device_names as dn on dn.id = d2.name_id where name = 'foo'
In the future, you might want to simply put your data into a fiddle and play with it there.