I have the following query:
(
SELECT * FROM salon_promo tt
INNER JOIN
(SELECT salon_id, MAX(promo_id) AS MaxDateTime
FROM salon_promo
GROUP BY salon_id)
groupedtt ON tt.salon_id = groupedtt.salon_id
AND tt.promo_id = groupedtt.MaxDateTime)
INNER JOIN salons USING (salon_id)
And recive the following error message :
error : You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near 'INNER JOIN salons USING (salon_id)' at line 1
When I use the following query:
SELECT * from salon_promo INNER JOIN salons USING (salon_id)
Everything works fine. And query from the ():
(
SELECT * FROM salon_promo tt
INNER JOIN
(SELECT salon_id, MAX(promo_id) AS MaxDateTime
FROM salon_promo
GROUP BY salon_id)
groupedtt ON tt.salon_id = groupedtt.salon_id
AND tt.promo_id = groupedtt.MaxDateTime
)
Works fine too. What can cause the issue?
Your query is wrong
This
groupedtt.MaxDateTime)
^^ )should remove
should be
groupedtt.MaxDateTime
So final query is
SELECT *
FROM salon_promo tt
INNER JOIN (
SELECT salon_id, MAX(promo_id) AS MaxDateTime
FROM salon_promo
GROUP BY salon_id)
groupedtt ON tt.salon_id = groupedtt.salon_id
AND tt.promo_id = groupedtt.MaxDateTime
INNER JOIN salons USING (salon_id)
Related
Actually, i would like to reproduce with Doctrine this regular SQL Query :
SELECT SUM(b.nb_places * (SELECT pricing FROM param p WHERE t.date BETWEEN p.from_date AND p.to_date)) as gains from booking b INNER JOIN tour t ON b.tour_id = t.id;
That compute total gains according prices between two dates.
I wrote this DQL in a Repository :
public function allBooking() {
$query = $this->manager->createQuery(
'SELECT
SUM(b.nbPlaces * SELECT p.pricing FROM \App\Entity\Param p WHERE t.date BETWEEN p.fromDate AND p.toDate)
FROM App\Entity\Booking b JOIN b.tour t'
);
return $query->getResult();
}
But running this query, i got :
[Syntax Error] line 0, col 24: Error: Expected Literal, got 'SELECT' (500 Internal Server Error)
How do i acheive this query with DQL or using QueryBuilder ?
Thx for help
You forgot about putting
SELECT p.pricing FROM \App\Entity\Param p WHERE t.date BETWEEN p.fromDate AND p.toDate
into additional brackets.
It should looks like this:
$query = $this->manager->createQuery(
'SELECT SUM(b.nbPlaces * (SELECT p.pricing FROM \App\Entity\Param p WHERE t.date BETWEEN p.fromDate AND p.toDate))
FROM App\Entity\Booking b JOIN b.tour t'
);
I am writing Select statement in hive , but my requirement is to use one field in select query that comes after union of column from two tables . Tables joined have been used in the select query but I am more stuck with problem how to use union of column from two tables , Please refer SQl query below :
SELECT t1.a , t2.b FROM tab1 inner join tab2 on ( t1.c = t2.c) inner join tab3 t3 on(t2.b = t3.b );
on above query I want below result but I am stuck here :
SELECT t1.a , t2.b , (t1.d UNION t2.d ) FROM tab1 inner join tab2 on ( t1.c = t2.c) inner join tab3 t3 on(t2.b = t3.b );
I have tried writing subquery in select statement but seems not to be working as Hive does not support subquery within select statement . I have also tried writing with statement first and try to use the result in select statement but I also not seems to be working .
CREATE OR replace PROCEDURE Proc_factura
IS
BEGIN
SELECT venta.rut_cliente,
cliente.nombre,
cliente.direccion,
cliente.telefono,
venta.fecha,
vendedor.nombre,
venta.codigo_vehiculo,
vehiculo_nuevo.marca,
vehiculo_nuevo.modelo,
vehiculo_nuevo.cilindrada,
vehiculo_nuevo.precio * 1.19
FROM venta
inner join cliente
ON venta.rut_cliente = cliente.rut
inner join vendedor
ON vendedor.rut = venta.rut_vendedor
inner join vehiculo_nuevo
ON venta.codigo_vehiculo = vehiculo_nuevo.codigo;
END;
Well, you need to SELECT those values INTO something in order to make it compile (at least). Will it work, that's another question.
I have 2 tables which looks like this:
ARTICLES TABLE:
and the output should look like this:
How can I accomplish this using both sql query (i'm using sql server 2005) and using linq to sql query ?
BTW i'm using sql server 2005, asp.net with c# in Visual studio 2008.
Please help me
Thanks in anticipation
Update: Added Linq experssion that can be used if you require an OUTER join.
INNER JOIN
For an inner join ie. only get back the articles that have been bought at least once, you can use the following.
LINQ 2 SQL
from a in Articles
join c in CustomersRecords on
a.Article_Name equals c.Article_Name
group a by new {a.SNo, a.Article_Name} into g
select new
{
SNo = g.Key.SNo,
Article_Name = g.Key.Article_Name,
Total_Items_Bought = g.Count()
}
The above translates to the following SQL
SELECT COUNT(*) AS [Total_Items_Bought], [t0].[SNo], [t0].[Article_Name]
FROM [Articles] AS [t0]
INNER JOIN [CustomersRecord] AS [t1] ON [t0].[Article_Name] = [t1].[Article_Name]
GROUP BY [t0].[SNo], [t0].[Article_Name]
Which when cleaned-up a little gives you
SELECT a.SNo,
a.Article_Name,
COUNT(*) AS Total_Items_Bought
FROM Articles AS a
INNER JOIN CustomersRecord AS c ON a.Article_Name = c.Article_Name
GROUP BY a.SNo, a.Article_Name
LEFT OUTER JOIN
For a left outer join ie. get back all articles event those that have never been bought, you can use the following.
LINQ 2 SQL
from a in Articles
join c in CustomersRecords on
a.Article_Name equals c.Article_Name into apc
select new
{
SNo = a.SNo,
Article_Name = a.Article_Name,
Total_Items_Bought = apc.Count()
}
This translates to the following SQL
SELECT [t0].[SNo], [t0].[Article_Name], (
SELECT COUNT(*)
FROM [CustomersRecord] AS [t1]
WHERE [t0].[Article_Name] = [t1].[Article_Name]
) AS [Total_Items_Bought]
FROM [Articles] AS [t0]
select
A.SNo,
A.Article_Name,
count(C.Article_Name) as Total_Items_Bought
from Articles as A
left outer join CustomersRecord as C
on A.Article_Name = C.Article_Name
group by A.SNo, A.Article_Name
order by A.SNo
Use this for SQL
SELECT
SNO,Article_Name,
(SELECT COUNT(*) FROM CustomersRecord AS cr
WHERE cr.Article_Name = Article_Name) AS Total_Items_Bought
FROM ARTICLES
It seems that when using the following NHibernate query, I do not get a root entity when the left outer join has no records.
ICriteria critera = session.CreateCriteria(typeof(Entity));
criteria.CreateCriteria("SubTable.Property", "Property", NHibernate.SqlCommand.JoinType.LeftOuterJoin);
criteria.Add(Expression.Not(Expression.Eq("Property", value)));
The SQL that I am trying to generate is:
SELECT * FROM BaseTable
LEFT JOIN (
SELECT * FROM SubTable
WHERE Property <> value
)Sub ON Sub.ForeignKey = BaseTable.PrimaryKey
Notice that the where clause is inside the left join's select statement. That way if there arent any maching sub records, we still get a top level record. It seems like NHibernate is producing the following SQL.
SELECT * FROM BaseTable
LEFT JOIN (
SELECT * FROM SubTable
)Sub ON Sub.ForeignKey = BaseTable.PrimaryKey
WHERE Sub.Property <> value
Is there anyway to achieve that first piece of SQL? I have already tried:
ICriteria critera = session.CreateCriteria(typeof(Entity));
criteria.CreateCriteria("SubTable.Property", "Property", NHibernate.SqlCommand.JoinType.LeftOuterJoin);
criteria.Add(
Restrictions.Disjunction()
.Add(Expression.IsNull("Property"))
.Add(Expression.Not(Expression.Eq("Property", value)));
I am looking for a solution using the Criteria API.
Try this:
var hql = #"select bt
from BaseTable bt
left join bt.SubTable subt
with subt.Property <> :property";
Or perhaps:
var hql = #"select bt
from BaseTable bt
left join bt.SubTable subt
where subt.ForeignKey = bt.PrimaryKey
and subt.Property <> :property";
Finally:
var result = session.CreateQuery(hql)
.SetParameter("property", "whateverValue")
.List<BaseTable>();
I don't use nHibernate but I think this is the SQL you need to generate:
SELECT *
FROM BaseTable
LEFT JOIN SubTable sub
ON Sub.ForeignKey = BaseTable.PrimaryKey and sub.Property <> value
What you want isn;t a where clasue but an additional condition on the join. Hope that helps.