How to join IN SELECT with LIKE from many tables in? - select

I'm trying to create a query that searches out all records that share a similar attribute like so:
select * from table_A
where fullname like in (select firstname from employees where X)
Only thing is that this is just my pseudo code, I actually am sorting through many tables so the real query I have currently looks something like:
select * from devices
where devicename like in (select X from X1 where T in (select T from T1 where Y in (select Y from Y1 where Z in (select Z from Z1 where AreaName = '74';
I'm trying to use the join command but very confused on how to apply it to this situation.

Try:
SELECT d.*
FROM devices d
INNER JOIN X1 ON d.devicename like '%'||x1.X||'%'
INNER JOIN T1 ON x1.T = T1.T
INNER JOIN Y1 ON T1.Y = Y1.Y
INNER JOIN Z1 ON Y1.Z = Z1.Z
WHERE Z1.AreaName = '74'

Here you go ....
SELECT D.*
from Devices D
inner join X1 X on D.devicename like X.X
inner join T1 T on T.T = X.T
inner join Y1 Y on Y.Y = T.Y
inner join Z1 Z on Y.Z = Z1.Z AND Z.Areaname = '74';

Okay, i give you the second query with Join syntax (well, i try to):
select * from Z1 Z1_1
JOIN Y1 Y_1 ON Y_1.Z=Z1_1.Z
JOIN T1 ON T1.Y=Y_1.Y
JOIN X1 X1_2 ON X1_2.T=T1.T
JOIN devices d ON d.devicename=X1_2.X
where Z1_1.AreaName = '74';

Try this:
SELECT d.*
FROM devices d
INNER JOIN X1 ON d.`like` = x1.X
INNER JOIN T1 ON x1.T = T1.T
INNER JOIN Y1 ON T1.Y = Y1.Y
INNER JOIN Z1 ON Y1.Z = Z1.Z
WHERE Z1.AreaName = '74'

The "join" command runs a cross product of the two tables and then selects only the tuples/rows where the common column names have the same values.
So, for your query:
select * from table_A
where fullname like in (select firstname from employees where X)
would be:
select * from table_A
left join employees
on table_A.fullname = employees.firstname
where X

Related

SSMS Query slow join

Tried to join three tables: car, car_and_engine, and engine. The second table, car_and_engine, connects the cars and their engines. A car type has up to three possible engine types. The query is significantly slower than expected (based on experience with similar operations in other languages). Is there anything terribly inefficient about this code?
select engine_type, AVG(horsepower) into #horsepower_by_engine_type
from TRANSPORT.dbo.engine
group by engine_type
go
with temp as(select * from TRANSPORT.dbo.car left join TRANSPORT.dbo.car_and_engine on TRANSPORT.dbo.car_and_engine.car_type_y = TRANSPORT.dbo.car.car_type_x)
select * from temp left join #horsepower_by_engine_type as e1 on temp.engine_type_1 = e1.engine_type
left join #horsepower_by_engine_type as e2 on temp.engine_type_2 = e2.engine_type
left join #horsepower_by_engine_type as e3 on temp.engine_type_3 = e3.engine_type
You don't really need a temp table (except when you are doing some diagnostics). You could replace your temp table syntax with an inline-view.
with temp as(select * from TRANSPORT.dbo.car left join TRANSPORT.dbo.car_and_engine on TRANSPORT.dbo.car_and_engine.car_type = TRANSPORT.dbo.car.car_type)
select * from temp left join
(select engine_type, AVG(horsepower)
from TRANSPORT.dbo.engine
group by engine_type) as e1 on temp.engine_type_1 = e1.engine_type
left join
(select engine_type, AVG(horsepower)
from TRANSPORT.dbo.engine
group by engine_type) as e2 on temp.engine_type_2 = e2.engine_type
left join
(select engine_type, AVG(horsepower)
from TRANSPORT.dbo.engine
group by engine_type) as e3 on temp.engine_type_3 = e3.engine_type
Better still, you could put your summary into your CTE
with temp as (select * from TRANSPORT.dbo.car left join TRANSPORT.dbo.car_and_engine on TRANSPORT.dbo.car_and_engine.car_type = TRANSPORT.dbo.car.car_type),
avgHP as (select engine_type, AVG(horsepower) from TRANSPORT.dbo.engine group by engine_type)
select * from temp left join avgHP as e1 on temp.engine_type_1 = e1.engine_type
left join avgHP as e2 on temp.engine_type_2 = e2.engine_type
left join avgHP as e3 on temp.engine_type_3 = e3.engine_type

How to select a column from nested query

SELECT x, createddate, count_ FROM
(SELECT *, count(*)
OVER
(PARTITION BY
x
) AS count_
FROM machineslocation_loc AS ml
JOIN m AS ma ON ma.mid= ml.mid
JOIN sh AS sh ON sh.shid = ma.shid
JOIN ph AS ph ON ph.phid = sh.phid
JOIN ar AS ar ON ar.arid = ph.arid
JOIN pn AS pa ON pa.pnid = ar.pnid
AND x LIKE '%705N%') tableWithCount
The table machineslocation_loc has createddate column in it. Whenever I call it says "missing FROM-clause entry for table "machineslocation_loc" but x column is coming from that table. I tried ml.createddate and machineslocation_loc.createddate. There are createddate column in other tables too
Try calling using the alias of your subquery, and please avoid * , call the name by his name
SELECT x, tableWithCount.createddate, count_ FROM
(SELECT x, ml.*,ma.*,sh.*,ph.*,ar.*,pa.*, count(*)
OVER
(PARTITION BY
x
) AS count_
FROM machineslocation_loc AS ml
JOIN m AS ma ON ma.mid= ml.mid
JOIN sh AS sh ON sh.shid = ma.shid
JOIN ph AS ph ON ph.phid = sh.phid
JOIN ar AS ar ON ar.arid = ph.arid
JOIN pn AS pa ON pa.pnid = ar.pnid
AND x LIKE '%705N%') as tableWithCount
make sure that not exist another column named createddate from the other tables inside the subquery
UPDATE:but you do not use the rest of the columns on the main query, so remove the * and only put the columns that you need, for example:
SELECT x, tableWithCount.mlcreateddate, count_ FROM
(SELECT x,ml.createddate mlcreateddate, count(*)
OVER
(PARTITION BY
x
) AS count_
FROM machineslocation_loc AS ml
JOIN m AS ma ON ma.mid= ml.mid
JOIN sh AS sh ON sh.shid = ma.shid
JOIN ph AS ph ON ph.phid = sh.phid
JOIN ar AS ar ON ar.arid = ph.arid
JOIN pn AS pa ON pa.pnid = ar.pnid
AND x LIKE '%705N%') as tableWithCount

how to solve this complicated sql query

these are the five given tables
http://i58.tinypic.com/53wcxe.jpg
this is the recomanded result
http://i58.tinypic.com/2vsrts7.jpg
please help how can i write a query to have this result.
no idea how!!!!
SELECT K.* , COUNT (A.Au_ID) AS AnzahlAuftr
FROM Kunde K
LEFT JOIN Auftrag A ON K.Kd_ID = A.Au_Kd_ID
GROUP BY K.Kd_ID,K.Kd_Firma,K.Kd_Strasse,K.Kd_PLZ,K.Kd_Ort
ORDER BY K.Kd_PLZ DESC;
SELECT COUNT (F.F_ID) AS AnzahlFahrt
FROM Fahrten F
RIGHT JOIN Auftrag A ON A.Au_ID = F.F_Au_ID
SELECT SUM (T.Ts_Strecke) AS SumStrecke
FROM Teilstrecke T
LEFT JOIN Fahrten F ON F.F_ID = T.Ts_F_ID
how to join these 3 in one?
Grouping on Strasse etc. is not necessary and can be quite expensive. What about this approach:
SELECT K.*, ISNULL(Au.AnzahlAuftr,0) AS AnzahlAuftr, ISNULL(Au.AnzahlFahrt,0) AS AnzahlFahrt, ISNULL(Au.SumStrecke,0) AS SumStrecke
FROM Kunde K
LEFT OUTER JOIN
(SELECT A.Au_Kd_ID, COUNT(*) AS AnzahlAuftr, SUM(Fa.AnzahlFahrt1) AS AnzahlFahrt, SUM(Fa.SumStrecke2) AS SumStrecke
FROM Auftrag A LEFT OUTER JOIN
(SELECT F.F_Au_ID, COUNT(*) AS AnzahlFahrt1, SUM(Ts.SumStrecke1) AS SumStrecke2
FROM Fahrten F LEFT OUTER JOIN
(SELECT T.Ts_F_ID, SUM(T.Ts_Strecke) AS SumStrecke1
FROM Teilstrecke T
GROUP BY T.Ts_F_ID) AS Ts
ON Ts.Ts_F_ID = F.F_ID
GROUP BY F.F_Au_ID) AS Fa
ON Fa.F_Au_ID = A.Au_ID
GROUP BY A.Au_Kd_ID) AS Au
ON Au.Au_Kd_ID = K.Kd_ID

How to aggregate calculation in SQL Server?

I have a following script to get the total unit but it gives me an error
Cannot perform an aggregate function on an expression containing an aggregate or a subquery.
Do I need to calculate SUM(ta.Qty) outside the main table?
SELECT
ta.ProductName
, SUM(ta.Total)
, SUM(SUM(ta.Qty) * ta.Unit)
FROM
tableA tA
INNER JOIN
tableB tB on tA.ID = tb.TableAID
INNER JOIN
tableC tc on ta.ID = tc.TableAID
INNER JOIN
tableD td on td.ID = tb.TableBID
GROUP BY
ta.ProductName
Here is a query in the AdventureWorks database that produces the same error (but might make some sense):
SELECT v.Name AS Vendor, SUM(SUM(p.ListPrice*d.OrderQty)+h.Freight)
FROM Production.Product p
INNER JOIN Purchasing.PurchaseOrderDetail d ON p.ProductID = d.ProductID
INNER JOIN Purchasing.PurchaseOrderHeader h ON h.PurchaseOrderID = d.PurchaseOrderID
INNER JOIN Purchasing.Vendor v ON v.BusinessEntityID = h.VendorID
GROUP BY v.Name
And here are two ways that I could rewrite that query to avoid the error:
SELECT v.Name AS Vendor, SUM(x.TotalAmount+h.Freight)
FROM (
SELECT PurchaseOrderID, SUM(p.ListPrice*d.OrderQty) AS TotalAmount
FROM Production.Product p
INNER JOIN Purchasing.PurchaseOrderDetail d ON p.ProductID = d.ProductID
GROUP BY PurchaseOrderID
) x
INNER JOIN Purchasing.PurchaseOrderHeader h ON h.PurchaseOrderID = x.PurchaseOrderID
INNER JOIN Purchasing.Vendor v ON v.BusinessEntityID = h.VendorID
GROUP BY v.Name
SELECT v.Name AS Vendor, SUM(x.TotalAmount+h.Freight)
FROM Purchasing.PurchaseOrderHeader h
INNER JOIN Purchasing.Vendor v ON v.BusinessEntityID = h.VendorID
CROSS APPLY (
SELECT SUM(p.ListPrice*d.OrderQty) AS TotalAmount
FROM Production.Product p
INNER JOIN Purchasing.PurchaseOrderDetail d ON p.ProductID = d.ProductID
WHERE d.PurchaseOrderID=h.PurchaseOrderID
) x
GROUP BY v.Name
The first query uses derived tables and the second one uses CROSS APPLY.

I receive conversion varchar to float error when insert **set rowcount=1** in first line of code

I receive an error
Coversion of varchar to float
when I write
set rowcount = 1
on first line of my code.
My script is:
select
vh.VchNum, ct.Val, r.RoutSheetNo, vi.FinalQty,
rh.RequestNo, vh.VchDate,
p.PartCode, d.Title, co.val
from
inv.InvVchHdr vh
join
acc.DL d on d.AccNum = vh.DLREF
join
inv.InvVchItm vi on vi.VchHdrRef = vh.VchHdrID
join
inv.InvVchItmCtrl ct on ct.VchItmRef = vi.VchItmID
join
QCS.QcsCertificateOfAnalysis q on q.Number = ct.Val
join
USR.kalaf_info_p kp on kp.Id = q.QcsCertificateOfAnalysisId
join
USR.coil_trace co on co.id = kp.coil_id
join
inv.Part p on p.Serial = vi.PartRef
join
inv.InvRqstItm rq on rq.RqstItmID = vi.RefNum
join
inv.InvRqstHdr rh on rh.RqstHdrID = rq.HdrRef
join
PRD.vwPrdOrderItemPlan pl on rh.OrdPlnBase = pl.OrdPlnId
join
prd.prdroutsheet r on r.OrdPlnRef = pl.OrdPlnId
where
pl.pPartRef not in (select pipe_code from usr.pipe_kalaf)
and pl.pPartRef not in (select Serial from inv.Part where PartName like '%لاف%')
and vi.VchType = 57
union
select vh.VchNum,ct.Val,pl.OrdPlnNo
,vi.FinalQty,
rh.RequestNo,vh.VchDate,
p.PartCode,d.Title,co.val from
inv.InvVchHdr vh
join acc.DL d
on d.AccNum=vh.DLREF
join
inv.InvVchItm vi
on vi.VchHdrRef=vh.VchHdrID
join inv.InvVchItmCtrl ct
on ct.VchItmRef=vi.VchItmID
join QCS.QcsCertificateOfAnalysis q
on q.Number=ct.Val
join USR.kalaf_info_p kp
on kp.Id=q.QcsCertificateOfAnalysisId
join USR.coil_trace co
on co.id=kp.coil_id
join
inv.Part p
on p.Serial=vi.PartRef
join inv.InvRqstItm rq
on rq.RqstItmID=vi.RefNum
join inv.InvRqstHdr rh
on rh.RqstHdrID=rq.HdrRef
join PRD.vwPrdOrderItemPlan pl
on rh.OrdPlnBase=pl.OrdPlnId
where pl.pPartRef in (select pipe_code from usr.pipe_kalaf) and vi.VchType=57
SET ROWCOUNT 0
i use sql server 2000 and when I remove setrowcount statement, problem resolve.
please help me
Remove the =(equal) sign
from
set rowcount = 1
To
SET ROWCOUNT 1