I wonder if someone can help me. I want to replicate the following SQL query using LINQ in VB.Net.I'm a little unclear on how to do subqueries / aggregates.
Thanks
SELECT *
FROM Server S
INNER JOIN ServerHDD H
ON S.Server_ID = H.Server_ID
INNER JOIN (SELECT MAX(ServerHDD_ID) AS ServerHDD_ID
FROM ServerHDD
GROUP BY Server_ID, Letter) Filter
ON H.ServerHDD_ID = Filter.ServerHDD_ID
ORDER BY S.Hostname, H.Letter
Got this as below in C# => need VB.Net Conversion please.
from S in SERVER
join H in SERVERHDD on S.Server_ID equals H.Server_ID
join FILTER in
(from s in SERVERHDD group s
by new {s.Server_ID, s.Letter}
into groupedServerHDD select new
{
SERVERHDD_ID = groupedServer.Sum(gS=>gS.ServerHDD_ID)
}
)
on H.ServerHDD_ID equals FILTER.SERVERHDD_ID
orderby S.Hostname, H.Letter
select S
This is my most favorite page regarding this topic. I love LINQ to SQL (and wish they intended to continue support for it over Entity Framework...) http://msdn.microsoft.com/en-us/vbasic/bb688085.aspx. On this page you will find all the answers to your querying needs. It is hard to format the query here without something to test it against!
Your inner joins go away with the simple join syntax of LtS. You can either say .Max() on your inner select or Max(pseudo functoid here) like this:
From p2 In g _
Where p2.UnitPrice = g.Max(Function(p3) p3.UnitPrice) _
Select p2
There are a couple of LINQ learning tools out there that are pretty cool.
This one is my favourite... LinqPad. But you might also want to check out Linqer.
You should be able to paste your code into one of them apps and it will show you how its converted. Hope they come in handy :)
Related
i really need little bit of help,i am new to MongoDB, is it possible to join collection and to rewrite following query to MongoDB, or if it is not possible, how can i realise this one query? Thanks
SELECT SQL_NO_CACHE
b.id,
b.id_pol AS vrsta_pola,
b.id_dxzavljanstvo AS drzavljanstvo,
o.naziv AS opstina,
bol.naziv AS bolnica,
b.id_odeljenje AS odeljenje_prijema,
b.datum_prijema,
b.id_uputna_dijagnoza AS uputna_dijagnoza,
b.id_osnovni_uzzok_hospitalizacije AS osnovni_uzrok_hospitalizacije,
b.datum_ispisa,
b.bx_dana_lezanja,
b.id_odeljenje_otpust AS odeljenje_otpust,
b.id_vrsta_otpusta,
b.id_obdukovan,
b.id_spoljni_uzzok_povrede AS spoljni_uzzok_povrede
FROM bol_rac_fl b
LEFT JOIN bolnica bol ON b.idbolnica = bol.id_bolnica
LEFT JOIN opstina o ON b.id_opstina = o.id_opstina
WHERE b.id_opstina = 70220
I read that it is not possible to join collection , and it work with lookup, but i don't understand how is possible to get similar result such as MySQL will give back after execution of this query?
I have a SQL query that i would like to write with Magento's collection methods but i don't know how to.
I know that i have to use the getSelect() and joinLeft() methods, but don't know how to put a select inside a left join.
The query is :
SELECT
p.photo_id,
p.photo_name,
s.step_id,
s.step_name
FROM Photo p
LEFT JOIN (
SELECT
photo_id, MAX(step_id) AS max_step_id
FROM photoStep
GROUP BY photo_id
) ps
ON ps.photo_id = p.photo_id
LEFT JOIN Steps s
ON s.step_id = ps.max_step_id
I think that your best option here would be to try to avoid using the subquery. So I would start by reworking the SQL query and then use the functions groupByField & addExpressionFieldToSelect.
->groupByField('photo_id')
->addExpressionFieldToSelect("max_step_id", 'MAX({{step_id}})', 'step_id')
After yeas of lurking on stackoverflow, always finding my answer), I finally need to post my own question.
I new to MySQL and have been using access query designer to help me learn/convert my query to SQL.
This has work fine until I tried to convert a access query that uses another query (sub-query).
Here is the finished Access query that works:
SELECT all_assm_start_stop.*, employee.first_name
FROM all_assm_start_stop LEFT JOIN employee ON all_assm_start_stop.employee_id = employee.employee_id
all_assm_start_stop is based on this access query:
(SELECT assemblies.assemblies_id, assemblies.assembly_name, labor.start_time, labor.stop_time, labor.number_completed, labor.employee_id, assemblies.product_id
FROM labor RIGHT JOIN assemblies ON labor.assemblies_id = assemblies.assemblies_id
WHERE (((assemblies.product_id)=2)))
I've been researching most the day about MySQL sub-queries but can't figure out how to convert this.
Any help is very much appreciated!
As someone who has had to do this too many times, this is the most direct/literal conversion method I have found:
SELECT all_assm_start_stop.*, employee.first_name
FROM ([original query]) AS all_assm_start_stop
LEFT JOIN employee ON all_assm_start_stop.employee_id = employee.employee_id
;
(Note the [original_query] is where you would put the second one you supplied, I posted the answer in this way for brevity and clarity/generality.)
Edit: If that query was used in a lot of other queries, you might want to consider making it a VIEW instead.
SELECT a.assemblies_id
, a.assembly_name
, l.start_time
, l.stop_time
, l.number_completed
, l.employee_id
, a.product_id
, e.first_name
FROM assemblies a
LEFT
JOIN labor l
ON l.assemblies_id = a.assemblies_id
LEFT
JOIN employee e
ON e.employee_id = a.employee_id
WHERE a.product_id = 2
I have a rather big SQL statement that I am working with in MS Access 2010. Here it goes:
SELECT
W.ID AS wid,
W.wpt_ty AS ty,
W.wpt_num AS num,
W.wpt_nxt AS nxt,
W.latdeg AS lat,
W.londeg AS lon,
W.alt AS alt,
W.mission_id AS mid,
W.ctg1 AS ctg1,
W.ctg2 AS ctg2,
W.ctg3 AS ctg3,
W.ctg4 AS ctg4,
W.wpt_index AS indx,
W.vel AS vel,
W.tu AS tu,
R.route_num AS rnum,
R.AC_num AS ac,
R.route_type AS rtype,
R.LastUpdatedOn AS d8,
R.LastUpdatedBy AS auth,
R.flight_wpt_count AS wfcount,
M.mission_name AS msnName,
V.Description AS vstatus,
R.disallowed_reason_id AS did,
CW.wpt_num AS c1num,
CR.matching_route_id AS c1mrid,
CW.wpt_index AS c1indx,
CRU.runway_name AS c1rnwy,
CR.route_num AS c1rnum
FROM Validation AS V
(RIGHT JOIN Runways AS CRU
INNER JOIN (Routes CR
INNER JOIN Waypoints CW ON CR.ID = CW.route_id)
ON Runways.ID = Routes.runway_id
INNER JOIN ((Missions as M
INNER JOIN Routes AS R ON M.ID = R.mission_id)
INNER JOIN Waypoints AS W ON (R.ID = W.route_id)
AND (M.ID = W.mission_id)) ON
V.ID = R.validated
WHERE (((R.matching_route_id)=307543) AND ((R.validated) <> 0 ))
AND (((CW.mission_id)=mid) AND ((CW.wpt_num) = (ctg1))))
If you look at the bottom, you can see am I referencing the values ctg1 and mid on a Right Join while the Inner Joins reference other literal values. Eventually I will want to do the same for ctg2, ctg3, and ctg4
Right now I am running these as 2 separate queries but finding it to be way too slow. If I can join combine the queries (sort of like how I am showing here) it could speed things up greatly. But I am at a loss for how to:
Using select values earlier in said query from the Inner/Left join and push them into values needed on the Right join.
I may be using joins incorrectly, but I thought they had to do with combining data from possible the same tables, just on different pivot points.
How to use the MS Access GUI to help write a query like this.
I know this is for MS Access but I am tagging for MySQL just in case there are similar queries there which can be ported to MS Access?
Have you tried using UNION for this?
It would allow you to execute this query (As two queries, which you mentioned as a possibility), and join the results for your output.
Be warned, it will eat up (only show one of) your duplicates in the results set.
I also suggest reading up on the different types of joins for your own benefit, in the following answer:
MYSQL Joins
I am in the process of building a dashboard and need to extract some data from a somewhat complex schema.
I have a select statement (see below) that I am using to extract information, but need to conduct some filtering on part of the select statement, and I'm struggling.
select distinct j.id 'Job_Id'
, js.outcome 'Outcome'
,(select string from property where parent_sheet_id = ps.id and name= 'Build_Type') as 'Build_Type'
from job j, job_step js, property_sheet ps, property p
where j.id = js.job_id
and ps.entity_id=js.id
and ps.id=p.parent_sheet_id
and ps.entity_type='step'
and p.name = 'Id'
group by j.id
order by j.id desc;
I am sure that there is a better way of doing this query, and I would appreciate any other suggestions, but I am mostly attempting to place a filter on the nested select statement which has an alias of "Build_Type", but when I try it appears not to work. I've read some blogs that this is not possible, so I am a little stuck.
Any help would be much appreciated.
Thanks.
select
ps.id,
Build_Type.string
from
property_sheet as ps
left join
property as Build_Type
on ps.id = Build_Type.parent_sheet_id and Build_Type.name = 'Build_Type'
where Build_Type....