Need Help for formulating Linq query from SQL query - linq-to-sql

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();

Related

How to join many tables without duplicate result?

Hello all StackOverFlow families.
I need your help about sql query in mysql. I join four tables but result is duplicate row.
I have tried by using GROUP BY but not work.
Here is my query:
SELECT `tbl_leave`.`id`, `tbl_leave`.`staff_id`, `tbl_leave`.`type_id`, `tbl_leave`.`start_date`, `tbl_leave`.`end_date`,
`tbl_leave`.`total_days`, `tbl_leave`.`reason`, `tbl_leave_type`.`type`, `tbl_employment`.`com_id` as `comid`, `tbl_staff`.`name`
FROM `tbl_leave` JOIN
`tbl_leave_type`
ON `tbl_leave_type`.`id` = `tbl_leave`.`type_id` JOIN
`tbl_employment`
ON `tbl_employment`.`staff_id` = `tbl_leave`.`staff_id` JOIN
`tbl_staff`
ON `tbl_staff`.`id` = `tbl_leave`.`staff_id`
You can look as picture: https://imgur.com/gallery/1Ewku
And this is relationship table: https://imgur.com/gallery/ziKq3
The result I want like this : https://imgur.com/gallery/6NJpR
Thank for your valuable times for this question.
Please try
SELECT distinct `tbl_leave`.`id`, `tbl_leave`.`staff_id`,
`tbl_leave`.`type_id`, `tbl_leave`.`start_date`, `tbl_leave`.`end_date`,
`tbl_leave`.`total_days`, `tbl_leave`.`reason`, `tbl_leave_type`.`type`,
`tbl_employment`.`com_id` as `comid`, `tbl_staff`.`name`
FROM `tbl_leave` JOIN
`tbl_leave_type`
ON `tbl_leave_type`.`id` = `tbl_leave`.`type_id` JOIN
`tbl_employment`
ON `tbl_employment`.`staff_id` = `tbl_leave`.`staff_id` JOIN
`tbl_staff`
ON `tbl_staff`.`id` = `tbl_leave`.`staff_id`;
Please try this solution and let me know if is there any problem occur:
SELECT [Columns] From tbl_staff as staff
JOIN tbl_employment as emp on staff.id = = emp.staff_id
JOIN tbl_leave as leave on staff.id = = leave.staff_id
JOIN tbl_leave_type as ltype on leave.type_id = = ltype.id

SQL Query into Laravel Query Builder

I have this SQL Query and at the moment I don't know how to build this with the larval query builder
SELECT h1.Name, h2.Torvorlage from
(SELECT Name, Player_ID FROM `Spieler` WHERE Vereins_ID = 1) h1
left JOIN
(SELECT Torvorlage, Player_ID FROM `SpielerStats` left join Spielplan on (SpielerStats.Spielplan_ID = Spielplan.Spielplan_ID) WHERE Spielplan.Spielplan_ID = 1 and Spielplan.Spieltag = 2) h2
on h1.Player_ID = h2.Player_ID
Can anyone give me a example how to build this?
Try this.
DB::table('Spieler')->where('Vereins_ID','=',1)
->join('SpielerStats','SpielerStats.Player_ID','=','Spieler.Player_ID','left')
->select('SpielerStats.Torvorlage','Spieler.Name')
->join('Spielplan','SpielerStats.Spielplan_ID','=','Spielplan.Spielplan_ID','left')
->where('Spielplan.Spielplan_ID','=',1)
->where('Spielplan.Spieltag','=',2)->get();
Or
$d = DB::raw('(SELECT Torvorlage, Player_ID FROM `SpielerStats` left join Spielplan on (SpielerStats.Spielplan_ID = Spielplan.Spielplan_ID) WHERE Spielplan.Spielplan_ID = 1 and Spielplan.Spieltag = 2) as h2')
DB::table('Spieler')
->select('Spieler.Name','h2.Torvorlage')
->leftJoin($d, 'h2.Player_ID', '=', 'Spieler.Player_ID')
->where('Vereins_ID','=',1)
->get();
In order to get a better answer, you should explain what your query is doing. What is the relationship with these tables? In my experience, when I had a complicated SQL query to convert to Eloquent, I found that defining the relationships on the models solved the problem.
I solved this with DB:Select
$spielerstats = DB::Select('SELECT h1.Name, h2.Torvorlage from
(SELECT Name, Player_ID FROM `Spieler` WHERE Vereins_ID = 1) h1
left JOIN
(SELECT Torvorlage, Player_ID FROM `SpielerStats` left join Spielplan on (SpielerStats.Spielplan_ID = Spielplan.Spielplan_ID) WHERE Spielplan.Spielplan_ID = 1 and Spielplan.Spieltag = 1) h2
on h1.Player_ID = h2.Player_ID');
I'm not sure if this is the best way but it works.

how to optimize mysql query with inner join

select id from customer_details where store_client_id = 2
And
id NOT IN (select customer_detail_id from orders
where store_client_id = 2 and total_spent > 100 GROUP BY customer_detail_id )
Or
id IN (select tcd.id from property_details as pd, customer_details as tcd
where pd.store_client_id = 2 and pd.customer_detail_id = tcd.customer_id and pd.property_key = 'Accepts Marketing'
and pd.property_value = 'no')
And
id IN (select customer_detail_id from orders
where store_client_id = 2 GROUP BY customer_detail_id HAVING count(customer_detail_id) > 0 )
Or
id IN (select tor.customer_detail_id from ordered_products as top, orders as tor
where tor.id = top.order_id and tor.store_client_id = 2
GROUP BY tor.customer_detail_id having sum(top.price) = 1)`
I have this mysql query with inner join so when it run in mysql server it slow down what is the issue cant find.
But after 4-5 minutes it return 15 000 records. This records is not an issue may be.
In some tutorial suggest to use Inner join, Left join,...
But I don't know how to convert this query in Join clause.
Any help will be appreciated. Thanks in advance.
First of all please read relational model and optimizing select statements.

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......

Multiple groups in LinqToSql

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