I have this SQL query and I need to convert it SqlKata
SELECT VVIAGGIO AS VIAGGIO, ISNULL(AEAN,'') AS EAN
FROM
SALDI V INNER JOIN ARTICOLI A ON VARTI=AARTI
INNER JOIN ORDI OT ON VSTAB=OTSTAB AND VMAGA=OTMAGA AND VAGG=OTRAGG
WHERE VORDI ='21'
AND VHOST ='68'
ORDER BY VPROG, AARTI
I don't know how to structure it because here is ISNULL(), INNER JOIN..
I already checked SqlKata select instruction.
Any suggestions on how to optimize and convert this query in SqlKata?
here is what you need:
var query = new Query("SALDI as V")
.Join("ARTICOLI as A","A.ARTI","V.ARTI")
.Join("ORDI as OT",j => j.On("OT.STAB","V.STAB")
.On("V.MAGA","OT.MAGA")
.On("V.AGG","OT.RAGG")
)
.Where("V.ORDI","21")
.Where("V.HOST","68")
.Select("V.VIAGGIO")
.SelectRaw("ISNULL(V.AEAN,'') as EAN")
.OrderBy("V.PROG", "A.ARTI")
Related
I am working with following query to get required result. But the query takes long time to run in live environment.Query as follows
SELECT a.int_key,main_number,b.local_number,dia.diag_number,a.clm_key
FROM clamp a
INNER JOIN polins b
ON a.ins_id=b.ins_id
LEFT OUTER JOIN
(SELECT int_key, RTRIM(XMLAGG(XMLELEMENT(E,MD.VAL,',').EXTRACT('//text()')
ORDER BY int_key).GetClobVal(),',') diag_number
FROM dia D
INNER JOIN ms_dia MD
ON MD.dia_key=D.dia_code
GROUP BY int_key
) dia ON a.int_key =dia.int_key
WHERE a.int_key NOT IN
(SELECT int_key FROM table1
);
I think subquery in from clause RTRIM(XMLAGG(XMLELEMENT(E,MD.VAL,',').EXTRACT('//text()') ORDER BY int_key).GetClobVal(),',') took long time to run. I already tried with listagg but following error was raise ORA-01489: result of string concatenation is too long. Can anyone rewrite the above query effectively.
This is my SQL query in text format:
SELECT sum(D.Kilogramos) as Kilogramos, C.Categoria , C.Tipo S Tipo, LA.Huerta,TC.TipoCorte, LA.JefeAcopio,LA.IdLote
FROM dbo.MOV_EmpaqueDetalle D
INNER JOIN dbo.Rep_Lote LA on LA.IdLote= D.IdLote
INNER JOIN dbo.MOV_OrdenesCorte MO on MO.IdOrdenCorte = LA.IdOrdenCorte
INNER JOIN dbo.PRO_Productos P on P.IdProducto=D.IdProducto
INNER JOIN dbo.PRO_Categorias C ON C.IdCategoria=P.IdCategoria
INNER JOIN dbo.MOV_Acuerdos A on A.IdAcuerdo=LA.IdAcuerdo
INNER JOIN dbo.MOV_TiposCorte TC on A.IdTipoCorte=TC.IdTipoCorte
WHERE CAST(MO.Fecha AS DATE) BETWEEN '2018-10-23' AND '2018-10-23'
GROUP BY LA.Huerta, LA.IdLote,C.Categoria, C.Tipo, TC.TipoCorte, LA.JefeAcopio,Mo.Fecha
ORDER BY LA.Huerta
The rows in the column kilogramos of the Huerta EL DOS are wrong, the information correct must be the following:
Kilogramos Huerta
13807.00 El DOS
I have got the accumulated row in sum function, the kilograms are wrong.
Its kind of hard to provide an answer with the few information you provided, but I would suggest to check whether your GROUP BY is correct.
If you really just need the sum Kilogramos over the different Huertas your group condition should look like this: GROUP BY LA.Huerta
I'm new here and have some difficult to convert a complex Postgres query to Ruby. Here is the query:
SELECT date_part('year', p.created_at) AS anio,
date_part('month'::text, p.created_at) AS mes,
p.id_pais, pa.nombre AS nombre_pais,
pr.id_region, re.nombre AS nombre_region,
co.id_provincia, pr.nombre AS nombre_provincia,
p.id_comuna, co.nombre AS nombre_comuna,
p.id_tipo_propiedad, p.id_modalidad,
count(*) AS total_propiedades,
sum((p.precio/mo.conversion_dolar)/p.dimension_propiedad) AS suma_precio_m2_dolar,
sum((p.precio/mo.conversion_dolar)/p.dimension_propiedad)/count(*) AS promedio_precio_m2_dolar
FROM propiedad AS p
INNER JOIN monedas AS mo ON (p.id_moneda = mo.id)
INNER JOIN comuna AS co ON (p.id_comuna = co.id)
INNER JOIN provincia AS pr ON (co.id_provincia = pr.id)
INNER JOIN region AS re ON (pr.id_region = re.id)
INNER JOIN pais AS pa ON (p.id_pais = pa.id)
WHERE p.id_modalidad IS NOT NULL
AND p.created_at IS NOT NULL
AND p.precio > 1
AND p.dimension_propiedad > 1
GROUP BY date_part('year',p.created_at), date_part('month'::text, p.created_at), p.id_tipo_propiedad,
p.id_modalidad, p.id_pais, pr.id_region, co.id_provincia, p.id_comuna, pa.nombre, re.nombre, pr.nombre, co.nombre
If you can help me with this i'll appreciate it.
I'd just leave something like that as plain SQL and hand it to select_rows:
connection.select_rows(%q{
-- your big pile of SQL goes here
}).each do |row|
# row will be an array of Strings in here
end
You'll have to unstringify things yourself but that shouldn't be terribly difficult. You could also use execute:
rows = connection.execute(%q{
-- SQL goes here...
})
rows.each do |row|
# row will be a hash
end
Your query is pulling a bunch of data out of the database that doesn't correspond to any particular model so there's not much point in trying to get ActiveRecord to produce that SQL. If the query was producing a model, then you'd use Model.find_by_sql (and still avoid trying to convince ActiveRecord to produce the SQL you're after).
I'm trying to come up with an automated solution for something I do manually now and I only have minimal, bare-bones SQL skill. I usually modify simple queries others have built or will build basic select queries. I have done some reading but don't know how to make it do what I need in this case. I need to come up with something others can use while I am out for a month (and which will save me time when I return).
What I need is to return the fields below where tblThree.EndDate is later than tblFive.ServiceEnd. I have to do a couple of other compares on the dates, but if I get a working query of the first one I can make it work with the others. We use MS SQL Server 2008.
I tried creating sub-queries with aliases and failed miserably at making it work.
These are the table and fields I am working with:
tblOne.ServiceID
tblOne.ServiceYear
tblOne.Status
tblTwo.AccountNbr
tblTwo.AcctName
tblThree.BeginDate (smalldatetime, null)
tblThree.EndDate (smalldatetime, null)
tblFour.ClientID
tblFour.ServiceName
tblFive.ContractID
tblFive.ServiceBegin (smalldatetime, null)
tblFive.ServiceEnd (smalldatetime, null)
This is how the tables are related:
tblOne.ServiceID = tblThree.ServiceID
tblOne.ContractID = tblFive.ContractID
tblOne.ClientID = tblFour.ClientID
tblTwo.AccountNbr = tblFour.Account
I used MS Access 2003 to generate the Join SQL:
SELECT tblOne.ServiceID, tblTwo.AccountNbr,
tblTwo.AcctName, tblFour.ServiceName, tblOne.Status,
tblThree.BeginDate, tblThree.EndDate,
tblOne.ServiceYear, tblFive.ServiceBegin,
tblFive.ServiceEnd
FROM ((tblTwo INNER JOIN tblFour
ON tblTwo.AccountNbr=tblFour.AccountNbr) INNER JOIN (tblThree INNER JOIN tblOne
ON tblThree.ServiceID=tblOne.ServiceID)
ON tblFour.ClientID=tblOne.ClientID) INNER JOIN tblFive
ON tblOne.ContractID=tblFive.ContractID;
Thanks for any help.
Just add a WHERE clause to get started:
SELECT tblOne.ServiceID, tblTwo.AccountNbr,
tblTwo.AcctName, tblFour.ServiceName, tblOne.Status,
tblThree.BeginDate, tblThree.EndDate,
tblOne.ServiceYear, tblFive.ServiceBegin,
tblFive.ServiceEnd
FROM ((tblTwo INNER JOIN tblFour
ON tblTwo.AccountNbr=tblFour.AccountNbr) INNER JOIN (tblThree INNER JOIN tblOne
ON tblThree.ServiceID=tblOne.ServiceID)
ON tblFour.ClientID=tblOne.ClientID) INNER JOIN tblFive
ON tblOne.ContractID=tblFive.ContractID
WHERE tblThree.EndDate > tblFive.ServiceEnd;
SELECT
tblOne.ServiceID,
tblOne.ServiceYear,
tblOne.Status,
tblTwo.AccountNbr,
tblTwo.AcctName,
tblThree.BeginDate,
tblThree.EndDate,
tblFour.ClientID,
tblFour.ServiceName,
tblFive.ContractID,
tblFive.ServiceBegin,
tblFive.ServiceEnd
FROM tblOne
INNER JOIN tblThree
ON tblOne.ServiceID = tblThree.ServiceID
INNER JOIN tblFive
ON tblOne.ContractID = tblFive.ContractID
INNER JOIN tblFour
ON tblOne.ClientID = tblFour.ClientID
INNER JOIN tblTwo
ON tblTwo.AccountNbr = tblFour.Account
WHERE tblThree.EndDate > tblFive.ServiceEnd
I'm Newbi in LinQ, I have problem with group by in linQ.
I wan to query like this:
select
MAX(TCheckpointGrouping.Id) AS CheckpointGroupingId,
MAX(TCheckpointGrouping.MCheckpointId) AS CheckpointId,
MAX(MCheckpoint.Name) AS CheckpointName,
MAX(CAST(MCheckpoint.IsMajor AS VARCHAR)) AS IsMajor,
MAX(TCheckpointGrouping.MIndicatorId) AS IndicatorId,
MAX(MIndicator.Name) AS IndicatorName,
MAX(MCriteria.Id) AS CriteriaId,
MAX(MCriteria.Name) AS CriteriaName,
MAX(MPrinciple.Id) AS PrincipleId,
MAX(MPrinciple.Name) AS PrincipleName,
MAX(TCheckpointGrouping.RelationToCheckPoint) AS RelationToCheckPoint
from TCheckpointGrouping
inner join MCheckpoint on MCheckpoint.Id = TCheckpointGrouping.MCheckpointId
inner join MIndicator on MIndicator.Id = TCheckpointGrouping.MIndicatorId
inner join MCriteria on MCriteria.Id = MIndicator.MCriteriaId
inner join MPrinciple on MPrinciple.Id = MCriteria.MPrincipleId
group by
TCheckpointGrouping.MCheckpointId,
TCheckpointGrouping.MIndicatorId
How can i convert query above into LinQ (VB.NET)
thanks
bestRegards
I'm tempted to convert this SQL query to LINQ for you, but I think that would be a waste of opportunity for you to learn yourself.
There's a great page from Microsoft with lot of VB.NET Linq situations: 101 Linq Samples.
You can even find an example of a Group By using Multiple Columns.
Good learning. :)
I am not sure about this, but you can try it. In select part i have not included all the columns.
var result= from TChkgp in TCheckpointGrouping
join MCpoint in MCheckpoint on TChkgp.Id equals MCpoint.Id
join MIndtor in MIndicator on TChkgp.MIndicatorId equals MIndtor.Id
join MCrteia in MCriteria on MIndtor.Id equals MIndtor.MCriteriaId
join MPrncple in MPrinciple on MCrteia.MPrincipleId equals MPrncple.Id
group TChkgp by new (TChkgp.MCheckpointId,TChkgp.MIndicatorId} into g
select new {
CheckpointGroupingId =TChkgp.Id.Max(),
CheckpointId =TChkgp.MCheckpointId.Max,
....
....
};
you can see one simple example on following link
Group and sum in linq