I need help to convert this query from LINQ syntax into SQLServer query syntax:
var t1 = (from p in db.Varors join
op in db.OrderVarors on p.id equals op.IdVara
where op.IdOrder == OrderId
select p).ToList();
Hope this helps:-
SELECT v.* from Varors v
JOIN ordervarors ov on v.id = ov.id
where ov.idOrder == #OrderId
You may want to choose individual columns instead of star but this should work.
select *
from dbo.Varors as P
join dbo.OrderVarors as OP
on p.id equals op.IdVara
where op.IdOrder = OrderId
this is a simple join - try this :
select Varors.*
from Varors
join OrderVarors on Varors.id = OrderVarors.IdVara
where OrderVarors.IdOrder = #OrderId
Related
I have the following query:
SELECT tours_atp.NAME_T, today_atp.TOUR, today_atp.ID1, odds_atp.K1, today_atp.ID2, odds_atp.K2
FROM (players_atp INNER JOIN (players_atp AS players_atp_1 INNER JOIN (today_atp INNER JOIN odds_atp ON (today_atp.TOUR = odds_atp.ID_T_O) AND (today_atp.ID1 = odds_atp.ID1_O) AND (today_atp.ID2 = odds_atp.ID2_O) AND (today_atp.ROUND = odds_atp.ID_R_O)) ON players_atp_1.ID_P = today_atp.ID2) ON players_atp.ID_P = today_atp.ID1) INNER JOIN tours_atp ON today_atp.TOUR = tours_atp.ID_T
WHERE (((tours_atp.RANK_T) Between 1 And 4) AND ((today_atp.RESULT)="") AND ((players_atp.NAME_P) Not Like "*/*") AND ((players_atp_1.NAME_P) Not Like "*/*") AND ((odds_atp.ID_B_O)=2))
ORDER BY tours_atp.NAME_T;
I'd like to add a field to this query that provides me with the sum of a field in another table (FS) with a few criteria applied.
I've been able to build a stand alone query to get the sum of FS by ID_T as follows:
SELECT tbl_Ts_base_atp.ID_T, Sum(tbl_Ts_mkv_atp.FS) AS SumOfFS
FROM tbl_Ts_base_atp INNER JOIN tbl_Ts_mkv_atp ON tbl_Ts_base_atp.ID_Ts = tbl_Ts_mkv_atp.ID_Ts
WHERE (((tbl_Ts_base_atp.DATE_T)>Date()-2000 And (tbl_Ts_base_atp.DATE_T)<Date()))
GROUP BY tbl_Ts_base_atp.ID_T, tbl_Ts_mkv_atp.ID_Ts;
I now want to match up the sum of FS from the second query to the records of the first query by ID_T. I realise I need to do this using a sub query. I'm confident using these when there's only one table but I consistently get 'syntax errors' when there are joins.
I simplified the first query down to remove all the WHERE conditions so it was easier for me to try and error check but no luck. I guess the resulting SQL will also be easier for you guys to follow:
SELECT today_atp.TOUR, (SELECT Sum(tbl_Ts_mkv_atp.FS)
FROM tbl_Ts_mkv_atp INNER JOIN (tbl_Ts_base_atp INNER JOIN today_atp ON tbl_Ts_base_atp.ID_T = today_atp.TOUR) ON tbl_Ts_mkv_atp.ID_Ts = tbl_Ts_base_atp.ID_Ts AS tt
WHERE tt.DATE_T>Date()-2000 And tt.DATE_T<Date() AND tt.TOUR=today_atp.TOUR
ORDER BY tt.DATE_T) AS SumOfFS
FROM today_atp
Can you spot where I'm going wrong? My hunch is that the issue is in the FROM line of the sub query but I'm not sure. Thanks in advance.
It's difficult to advise an appropriate solution without knowledge of how the database tables relate to one another, but assuming that I've correctly understood what you are looking to achieve, you might wish to try the following solution:
select
tours_atp.name_t,
today_atp.tour,
today_atp.id1,
odds_atp.k1,
today_atp.id2,
odds_atp.k2,
subq.sumoffs
from
(
(
(
(
today_atp inner join odds_atp on
today_atp.tour = odds_atp.id_t_o and
today_atp.id1 = odds_atp.id1_o and
today_atp.id2 = odds_atp.id2_o and
today_atp.round = odds_atp.id_r_o
)
inner join players_atp as players_atp_1 on
players_atp_1.id_p = today_atp.id2
)
inner join players_atp on
players_atp.id_p = today_atp.id1
)
inner join tours_atp on
today_atp.tour = tours_atp.id_t
)
inner join
(
select
tbl_ts_base_atp.id_t,
sum(tbl_ts_mkv_atp.fs) as sumoffs
from
tbl_ts_base_atp inner join tbl_ts_mkv_atp on
tbl_ts_base_atp.id_ts = tbl_ts_mkv_atp.id_ts
where
tbl_ts_base_atp.date_t > date()-2000 and tbl_ts_base_atp.date_t < date()
group by
tbl_ts_base_atp.id_t
) subq on
tours_atp.tour = subq.id_t
where
(tours_atp.rank_t between 1 and 4) and
today_atp.result = "" and
players_atp.name_p not like "*/*" and
players_atp_1.name_p not like "*/*" and
odds_atp.id_b_o = 2
order by
tours_atp.name_t;
i need a query that takes in ID_Offerta as input and returns all Cantieri of the Cliente that is linked with the Offerta. these are the relationships:
https://i.gyazo.com/490cd3085c412b7fcc91317d71705ee3.png
this is the query:
SELECT DISTINCTROW Offerte.ID_offerta, Cantieri.ID_cantiere, Clienti.ID_Cliente
FROM Offerte,
Cantieri,
Clienti
WHERE (((Cantieri.ID_Cliente) = (SELECT DISTINCT MAX(Cantieri.ID_Cliente)
FROM Cantieri,
Offerte
WHERE Offerte.ID_Cantiere = Cantieri.ID_Cantiere
AND Offerte.ID_Offerta=[ins])));
and this is what i get:
https://i.gyazo.com/7526cf11681c3828723e25cd24578c8d.png
thanks and sorry about the pizza and mandolino language
I believe you have a cartesian product between Offerte, Cantieri and Clienti
SELECT DISTINCTROW Offerte.ID_offerta, Cantieri.ID_cantiere, Clienti.ID_Cliente
FROM Offerte,
Cantieri,
Clienti
WHERE (((Cantieri.ID_Cliente) = (SELECT DISTINCT MAX(Cantieri.ID_Cliente)
FROM Cantieri,
Offerte
WHERE Offerte.ID_Cantiere = Cantieri.ID_Cantiere
AND Offerte.ID_Offerta=[ins])))
AND Clienti.ID_Cliente = Cantieri.ID_Cliente
AND Offerte.ID_Offerta=[ins]
But from your initial description I'd go with this
SELECT DISTINCT Offerte.ID_offerta, Cantieri.ID_cantiere, Clienti.ID_Cliente
FROM Offerte,
Cantieri,
Clienti
WHERE Offerte.ID_Offerta=[ins]
AND Cantieri.ID_Cantiere = Offerte.ID_Cantiere
AND Clienti.ID_Cliente = Cantieri.ID_Cliente
Use INNER JOIN!!!
SELECT o.ID_offerta, c.ID_cantiere, cl.ID_Cliente
FROM (Offerte as o INNER JOIN
Cantieri as c
ON o.ID_Cantiere = c.ID_Cantiere
) INNER JOIN
Clienti as cl
ON cl.ID_Cliente = c.ID_Cliente
WHERE o.ID_Offerta = [ins];
I removed the DISTINCT, because it is probably not necessary with the correct JOIN. However, if it is still necessary, then include it.
You can probably simplify this query further by removing Clienti; the only column being used is already in Cantiere:
SELECT o.ID_offerta, c.ID_cantiere, c.ID_Cliente
FROM Offerte as o INNER JOIN
Cantieri as c
ON o.ID_Cantiere = c.ID_Cantiere
WHERE o.ID_Offerta = [ins];
I am trying to write the LINQ statement for the following OUTER JOIN with COUNT but can't seem to work it out..
My LINQ skills aren't what they should be yet so any pointer would be greatly appreciated.
The SQL statement in question is:
SELECT b.Id,
b.Text,
b.Active,
COUNT(u.BusinessArea_Id)
FROM dbo.[User] AS u RIGHT OUTER JOIN dbo.BusinessArea AS b ON b.Id = u.BusinessArea_Id
GROUP BY b.Id, b.Text, Active
ORDER BY b.Id
I think you can use a linq like this:
var res = (from ba in businessAreas
let count = users.Count(u => u.BusinessArea_Id == ba.Id)
orderby ba.Id
select new {ba.Id, ba.Text, ba.Active, Count = count}
).ToList();
i have the following SQL DML Update command, but the syntax isn´t correct and the command doesn´t work:
UPDATE hmsg_vehicle_category
SET hmsg_vehicle_category.hmsg_id, hmsg_vehicle_category.vehiclecategories_inputname
SELECT l_p.hmsg_id, tmp_p_vc.inputname
FROM hmsg_him_product AS l_p INNER JOIN ( SELECT p.id, vc.inputname
FROM him_product p INNER JOIN vehicle_category vc
ON p.id = vc.product
ORDER BY p.id, vc.inputname DESC ) AS tmp_p_vc
ON l_p.products_id = tmp_p_vc.id
WHERE l_p.hmsg_id = 171;
How can i execute this SQL command? Where is the mistake in the snytax?
Thanks for help !
Greetz
Marwief
Something like:
update hmsg_vehicle_category set
hmsg_id = l_p.hmsg_id,
vehiclecategories_inputname = tmp_p_vc.inputname
from hmsg_him_product as l_p
inner join him_product as p on p.id = l_p.products_id
inner join vehicle_category as vc on vc.product = p.id
where l_p.hmsg_id = 171
Be warned, this one will update all records in hmsg_vehicle_category table. May be you want to add this into where clause:
update hmsg_vehicle_category as hvc set
vehiclecategories_inputname = tmp_p_vc.inputname
from hmsg_him_product as l_p
inner join him_product as p on p.id = l_p.products_id
inner join vehicle_category as vc on vc.product = p.id
where
l_p.hmsg_id = 171 and hvc.hmsg_id = 171
But I cannot advice something more specific at the moment, because it's unclear from your question.
I have a LEFT OUTER OUTER join in LINQ that is combining with the outer join condition and not providing the desired results. It is basically limiting my LEFT side result with this combination. Here is the LINQ and resulting SQL. What I'd like is for "AND ([t2].[EligEnd] = #p0" in the LINQ query to not bew part of the join condition but rather a subquery to filter results BEFORE the join.
Thanks in advance (samples pulled from LINQPad) -
Doug
(from l in Users
join mr in (from mri in vwMETRemotes where met.EligEnd == Convert.ToDateTime("2009-10-31") select mri) on l.Mahcpid equals mr.Mahcpid into lo
from g in lo.DefaultIfEmpty()
orderby l.LastName, l.FirstName
where l.LastName.StartsWith("smith") && l.DeletedDate == null
select g)
Here is the resulting SQL
-- Region Parameters
DECLARE #p0 DateTime = '2009-10-31 00:00:00.000'
DECLARE #p1 NVarChar(6) = 'smith%'
-- EndRegion
SELECT [t2].[test], [t2].[MAHCPID] AS [Mahcpid], [t2].[FirstName], [t2].[LastName], [t2].[Gender], [t2].[Address1], [t2].[Address2], [t2].[City], [t2].[State] AS [State], [t2].[ZipCode], [t2].[Email], [t2].[EligStart], [t2].[EligEnd], [t2].[Dependent], [t2].[DateOfBirth], [t2].[ID], [t2].[MiddleInit], [t2].[Age], [t2].[SSN] AS [Ssn], [t2].[County], [t2].[HomePhone], [t2].[EmpGroupID], [t2].[PopulationIdentifier]
FROM [dbo].[User] AS [t0]
LEFT OUTER JOIN (
SELECT 1 AS [test], [t1].[MAHCPID], [t1].[FirstName], [t1].[LastName], [t1].[Gender], [t1].[Address1], [t1].[Address2], [t1].[City], [t1].[State], [t1].[ZipCode], [t1].[Email], [t1].[EligStart], [t1].[EligEnd], [t1].[Dependent], [t1].[DateOfBirth], [t1].[ID], [t1].[MiddleInit], [t1].[Age], [t1].[SSN], [t1].[County], [t1].[HomePhone], [t1].[EmpGroupID], [t1].[PopulationIdentifier]
FROM [dbo].[vwMETRemote] AS [t1]
) AS [t2] ON ([t0].[MAHCPID] = [t2].[MAHCPID]) AND ([t2].[EligEnd] = #p0)
WHERE ([t0].[LastName] LIKE #p1) AND ([t0].[DeletedDate] IS NULL)
ORDER BY [t0].[LastName], [t0].[FirstName]
I'm not sure if it will change the result set with "AND ([t2].[EligEnd] = #p0" as part of the subquery rather than the join condition. One thing I like to do with complex queries might help you here. I like to break them into smaller queries before combining them. The deferred execution of LINQ lets us do multiple statements with one eventual call to the database. Something like this:
var elig = from mri in vwMETRemotes
where met.EligEnd == Convert.ToDateTime("2009-10-31")
select mri;
var users = from l in Users
where l.LastName.StartsWith("smith")
where l.DeletedDate == null
var result = from l in users
join mr in elig on l.Mahcpid equals mr.Mahcpid into lo
from g in lo.DefaultIfEmpty()
orderby l.LastName, l.FirstName
select g
Breaking it down like that can make it easier to debug, and perhaps it can tell LINQ better what you intend.
Code ended up looking like this. RecodePopulation and RecordRegistration are just methods to translate values from the query.
var elig = from mri in db.MetRemote
where mri.EligEnd == Convert.ToDateTime(ConfigurationManager.AppSettings["EligibilityDate"])
orderby mri.EligEnd
select mri;
var users = from l in db.Users
where l.LastName.StartsWith(filter)
where l.DeletedDate == null
select l;
var results = (from l in users
join m in elig on l.MahcpId equals m.MAHCPID into lo
from g in lo.DefaultIfEmpty()
orderby l.LastName, l.FirstName
select new UserManage()
{
Username = l.Username,
FirstName = l.FirstName,
LastName = l.LastName,
DateOfBirth = l.DOB,
Gender = l.Gender,
Status = RecodePopulation(g.Population, l.CreatedDate),
UserId = l.Id,
WellAwardsRegistered = RecodeRegistration(l.Id, 1)
}).Distinct().OrderBy(a => a.LastName).ThenBy(n => n.FirstName).Skip((currentPage - 1) * resultsPerPage).Take(resultsPerPage);