I have the following query:
SELECT SUM( cost )
FROM (
SELECT s.cost
FROM sandwiches AS s
WHERE s.name = "Cheese Steak"
)
UNION (
SELECT p.cost
FROM pizza AS p
WHERE TYPE = "Plain"
AND SIZE = "L"
)
That gives me an error of:
#1248 - Every derived table must have its own alias
You need to alias your temp tables
SELECT SUM( cost )
FROM
(
(
SELECT s.cost
FROM sandwiches AS s
WHERE s.name = "Cheese Steak"
) AS T1
UNION
(
SELECT p.cost
FROM pizza AS p
WHERE TYPE = "Plain"
AND SIZE = "L"
) AS T2
) AS T
Do you want the whole Sum?
SELECT
( SELECT SUM(s.cost)
FROM sandwiches AS s
WHERE s.name = "Cheese Steak"
)
+
( SELECT SUM(p.cost)
FROM pizza AS p
WHERE p.TYPE = "Plain"
AND p.SIZE = "L"
)
The following form should do the job:
SELECT SUM(cost) FROM (
SELECT cost FROM sandwiches WHERE name = "Cheese Steak"
UNION
SELECT cost FROM pizza WHERE TYPE = "Plain" AND SIZE = "L"
) as temp
MySQL only requires a temporary table name for the subselect.
Related
I have following SQL:
SELECT
M.*
FROM
(
SELECT MAX(counter) AS FirstUserDate, imdb_id, language, season, aufloesung, episode
FROM autofehlerserie
GROUP BY imdb_id, language
) foo
JOIN
autofehlerserie M ON foo.imdb_id = M.imdb_id AND foo.language = M.language
ORDER BY
foo.FirstUserDate DESC, M.imdb_id, M.aufloesung, cast(M.season as int), cast(M.episode as int)
But I want only SELECT items WHERE marker not = "D"
Where I have to add this on this select query?
Use it like below :
SELECT
M.*
FROM
(
SELECT MAX(counter) AS FirstUserDate, imdb_id, language, season, aufloesung, episode
FROM autofehlerserie
WHERE marker <> 'D'
GROUP BY imdb_id, language, season, aufloesung, episode
) foo
JOIN
autofehlerserie M ON foo.imdb_id = M.imdb_id AND foo.language = M.language and M.marker <> 'D'
ORDER BY
foo.FirstUserDate DESC, M.imdb_id, M.aufloesung, cast(M.season as int), cast(M.episode as int)
I think you might actually want:
select m.*
from autofehlerserie m
where not exists (select 1
from autofehlerserie m2
where m2.imdb_id = m.imdb_id AND m2.language = m.language and
m2.marker = 'D'
);
This returns all rows in your table where there is no other row with the same imdb_id and language whose marker is 'D'.
I try to use a field from joined table "glpi_items_tickets" into a subquery like this :
SELECT
NAME
FROM
(
SELECT
NAME
FROM glpi_computers
WHERE id = git.items_id
) AS t1
UNION
(
SELECT
NAME
FROM glpi_monitors
WHERE id = git.items_id
)
UNION
(
SELECT
NAME
FROM glpi_networkequipments
WHERE id = git.items_id
)
UNION
(
SELECT
NAME
FROM glpi_printers
WHERE id = git.items_id
)
) AS aliasIT
but i have an error of type : "#1054 - Unknown Column 'git.items_id' in where clause"
The entire request :
SELECT
gt.id,
(
SELECT
NAME
FROM
(
SELECT
NAME
FROM glpi_computers
WHERE id = git.items_id
) AS t1
UNION
(
SELECT
NAME
FROM glpi_monitors
WHERE id = git.items_id
)
UNION
(
SELECT
NAME
FROM glpi_networkequipments
WHERE id = git.items_id
)
UNION
(
SELECT
NAME
FROM glpi_printers
WHERE id = git.items_id
)
) AS aliasIT
FROM glpi_tickets gt
INNER JOIN glpi_items_tickets git
ON gt.id = git.tickets_id;
Do you have a solution for used the field "glpi_items_tickets.items_id" into my subqueries?
SELECT ...
FROM
glpi_tickets gt
INNER JOIN glpi_items_tickets git
ON git.tickets_id = gt.id;
INNER JOIN
(
SELECT NAME, id FROM glpi_computers
UNION
SELECT NAME, id FROM glpi_monitors
UNION
SELECT NAME, id FROM glpi_networkequipments
UNION
SELECT NAME, id FROM glpi_printers
) g
ON g.id = git.items_id
Use the full table name instead of the alias.
Replace every git.items_id with glpi_items_tickets.items_id.
The alias does not exist during the compilation of the subquery, since the outer query (where you set your alias) can not be compiled until the inner querys are considered valid.
I have 2 tables (THour_IN and THour_OUT) that have identical schemas:
Columns for THour_IN: Name|date|HourIN
Colums for THour_OUT: Name|date|HourOUT
I make query:
SELECT THour_IN.Name, THour_IN.date, THour_IN.HourIN, THour_OUT.HourOUT FROM THour_IN LEFT JOIN THour_OUT ON (Hour_IN.Name = THour_OUT.Name) AND (Hour_IN.date = THour_OUT.date);
But this is not correct in my case, because I have multiple rows wtih the same date in the tables. The result is:
Name date HourIN HourOUT
AAA 24/11/2013 17:33:06 20:33:27
AAA 24/11/2013 17:33:06 16:36:06
AAA 24/11/2013 07:33:27 20:33:27
AAA 24/11/2013 07:33:27 16:36:06
BBB 18/11/2013 16:36:06
BBB 19/11/2013 07:33:30
BBB 21/11/2013 07:29:24 08:33:22
BBB 22/11/2013 07:33:30 16:34:53
It should be for date 24/11/2013 First HourIN(07:33:27) with Fisrt HourOUT (16:36:06), Second HourIN (17:33:06) with Second HourOUT (20:33:06) Any ideas?
Build segments, then intersect segments.
SELECT
P1.Name, P1.date, P1.HourIN, P1.HourOUT
FROM (
SELECT
I.Name, I.date, I.HourIN, O.HourOUT
FROM
THour_IN AS I
LEFT JOIN THour_OUT AS O
ON (I.Name = O.Name) AND (I.date = O.date)
AND I.HourIN < H.HourOUT
) AS P1
INNER JOIN (
SELECT
I.Name, I.date, I.HourIN, O.HourOUT
FROM
THour_IN AS I
LEFT JOIN THour_OUT AS O
ON (I.Name = O.Name) AND (I.date = O.date)
AND I.HourIN < H.HourOUT
) AS P2
ON P1.name = P2.name AND P1.date = P2.date
AND P1.HourIN <> P2.HourIN and P1.HourOUT <> P2.HourOUT
AND P1.HourOUT > P2.HourIN
AND (P1.HourIN = P2.HourIN AND P1.HourOUT < P2.HourOUT
OR P1.HourIN > P2.HourIN AND P1.HourOUT = P2.HourOUT)
You could use something like so:
SELECT
t.Name,
t.Date,
t.HourIN, (
SELECT Top 1 HourOut
FROM THourOUT o
WHERE o.Name=t.Name AND o.Date=t.date And o.HourOUT>t.HourIN
ORDER BY o.HourOUT,o.ID ) AS HrOut
FROM THourIN AS t
ORDER BY t.Date, t.HourIN;
Note that I have added an ID to the OUT table to ensure that top 1 does not return duplicates.
Here is an example of inserting missing values. It depends on a Numbers table containing integers from 0 or 1 to the highest number of missing values. A number table is useful in many ways.
INSERT INTO thourin
(name,
[date])
SELECT q.name,
q.DATE
FROM (SELECT Outs.name,
Outs.DATE,
Outs.countofout,
Ins.countofin
FROM (SELECT o.name,
o.DATE,
Count(o.name) AS CountOfOut
FROM thourout o
GROUP BY o.name,
o.DATE) AS Outs
LEFT JOIN (SELECT t.name,
t.DATE,
Count(t.name) AS CountOfIn
FROM thourin t
GROUP BY t.name,
t.DATE) AS Ins
ON ( Outs.name = Ins.name )
AND ( Outs.DATE = Ins.DATE )
WHERE (( ( Ins.countofin ) <> [countofout]
OR ( Ins.countofin ) IS NULL ))) AS q,
numbers AS n
WHERE (( ( n.counter ) > 0
AND ( n.counter ) <= [countofout] - [countofin] ))
I'm attempting to combine a few queries and can't seem to nail it down. I was wondering if someone could point me in the right direction.
Here are the statements:
SELECT
I.id,
I.custname,
I.custemail,
I.sku,
DATE_FORMAT(FROM_UNIXTIME(I.ts), '%l:%i:%s %p, %c/%e/%Y') AS ts
FROM images I
WHERE I.stat = 0
SELECT
COUNT(*) AS total1
FROM images
WHERE stat = 1 AND sku = ?
SELECT
COUNT(*) AS total2
FROM images
WHERE stat = 1
AND sku IN (SELECT subsku FROM combo WHERE sku = ?)
Right now I'm using the 3 separate queries and am using code to add the two totals and display them. But I now need to be able to sort by the sum of the totals, so I'd like to get all of that data into one statement.. something like:
SELECT
I.id,
I.custname,
I.custemail,
I.sku,
DATE_FORMAT(FROM_UNIXTIME(I.ts), '%l:%i:%s %p, %c/%e/%Y') AS ts,
SUM(total1+total2)
FROM images I
WHERE I.stat = 0
But I'm unsure how to do that. I tried the code below, but it failed:
SELECT
I.id,
I.custname,
I.custemail,
I.sku,
DATE_FORMAT(FROM_UNIXTIME(I.ts),'%l:%i:%s %p, %c/%e/%Y') AS ts,
(
SELECT COUNT(*) AS total1 FROM images WHERE stat = 1 AND (
sku IN (
SELECT subsku FROM combo WHERE sku = I.sku
) OR sku = I.sku)
) AS skuct
FROM images I
WHERE stat = 0
Any help would be greatly appreciated. Many thanks!
UPDATE
First off thanks to everyone who has offered assistance. I've been working on the query and think I'm getting closer, but I'm now hitting a 'subquery returns more than 1 row' error:
SELECT
I.id,
I.custname,
I.custemail,
I.sku,
DATE_FORMAT(FROM_UNIXTIME(I.ts), '%l:%i:%s %p, %c/%e/%Y') AS ts,
(
SELECT COUNT(*)
FROM images
WHERE stat = 1
AND sku = I.sku
OR sku IN(
SELECT subsku FROM combo WHERE sku = I.sku
)
GROUP BY sku
) AS total
FROM images I
WHERE stat = 0
The problem is that the subquery SELECT subsku FROM combo WHERE... returns a resultset (0+ rows) vs a scalar. If I can figure out that part, I think this will work.
Select I.id, I.custname, I.custemail, I.sku
, DATE_FORMAT(FROM_UNIXTIME(I.ts), '%l:%i:%s %p, %c/%e/%Y') AS ts
, (
Select Sum( Case
When I1.sku = ? Then 1
When I1.sku In( Select subsku From combo As S1 Where S1.sku = ? ) Then 1
Else 0
End ) As Total
From images As I1
Where I1.stat = 1
) As Total
From images As I
Where stat = 0
Another solution
Select I.id, I.custname, I.custemail, I.sku
, DATE_FORMAT(FROM_UNIXTIME(I.ts), '%l:%i:%s %p, %c/%e/%Y') AS ts
, (
Select Count(*)
From images As I1
Where I1.stat = 1
And (
I1.sku = ?
Or I1.sku In ( Select subsku From combo As S1 Where S1.sku = ? )
)
) As Total
From images As I
Where stat = 0
Addition
Another possible solution:
Select I.id, I.custname, I.custemail, I.sku
, DATE_FORMAT(FROM_UNIXTIME(I.ts), '%l:%i:%s %p, %c/%e/%Y') AS ts
, (
Select Sum( Cnt )
From (
Select Count(*) As Cnt
From images As I1
Left Join combo As C1
On C1.sku = I1.sku
Where I1.stat = 1
And I1.sku = ?
And C1.PrimaryKeyCol Is Null
Union All
Select Count( Distinct I1.PrimaryKeyCol )
From images As I1
Join combo As C1
On C1.sku = I1.sku
Where I1.stat = 1
And I1.sku = ?
) As Z
) As Total
From images As I
Where stat = 0
Edit
If you are looking for the count by image in which you correlate the counts to the outer table images sku column, that's entirely different. For that, I would use a derived table:
Select I.id, I.custname, I.custemail, I.sku
, DATE_FORMAT(FROM_UNIXTIME(I.ts), '%l:%i:%s %p, %c/%e/%Y') AS ts
, Counts.Total
From images As I
Join (
Select Z.sku, Sum(Z.Cnt) As Total
From (
Select I1.sku, Count(*) As Cnt
From images As I1
Left Join combo As C1
On C1.sku = I1.sku
Where I1.stat = 1
And C1.PrimaryKeyCol Is Null
Group By I1.sku
Union All
Select I1.sku, Count( Distinct I1.PrimaryKeyCol )
From images As I1
Join combo As C1
On C1.sku = I1.sku
Where I1.stat = 1
Group By I1.sku
) As Z
Group By Z.sku
) As Counts
On Counts.sku = I.sku
Where stat = 0
Obviously in all cases, replace PrimaryKeyCol with the name of the actual primary key column of the images table.
Have you considered changing the WHERE logic to perform the count in one.
Assuming the two counts are mutually exclusive you could just OR the two conditions.
Use the cross join . . .
select t1.*, t2.total1, t3.total2
from
(
SELECT I.id, I.custname, I.custemail, I.sku,
DATE_FORMAT(FROM_UNIXTIME(I.ts), '%l:%i:%s %p, %c/%e/%Y') AS ts
FROM images I
WHERE I.stat = 0
) t1
cross join
(
SELECT COUNT(*) AS total1
FROM images
WHERE stat = 1 AND sku = ?
) t2
cross join
(
SELECT COUNT(*) AS total2
FROM images
WHERE stat = 1 AND sku IN (SELECT subsku FROM combo WHERE sku = ?)
) t3
You might be able to make this more efficient, since they are all going after the same table. A single aggregation with a case statement per WHERE clause would probably be more efficient.
I'm trying to run this query:
UPDATE anothertable
INNER JOIN (SELECT *,
LEAST(table1.from_price, table2.from_price, table3.from_price) AS cheapestPrice
FROM (SELECT * FROM table1 v WHERE hotelid >= 1
UNION
SELECT * FROM table2 c WHERE hotelid >= 1
UNION
SELECT * FROM table3 k WHERE hotelid >= 1) AS temp
GROUP BY temp.hotelid, temp.country) AS i ON anothertable.id = i.hotelid
AND anothertable.country = i.country
SET price = i.cheapestPrice,
op = i.to
However I cannot get the LEAST function to get access to a field called "from_price".
Ideas?
You should use Min instead of Least:
Update anothertable
Join (
Select hotelid, country, to
, Min(from_price) AS cheapestPrice
From (
Select hotelid, country, from_price, to
From table1 v
Where hotelid >= 1
Union
Select hotelid, country, from_price, to
From table2 c
Where hotelid >= 1
Union
Select hotelid, country, from_price, to
From table3 k
Where hotelid >= 1
) AS temp
Group By temp.hotelid, temp.country, temp.to
) As i
On anothertable.id = i.hotelid
And anothertable.country = i.country
Set price = i.cheapestPrice
, op = i.to
Edit
As pointed out in comments, I omitted the to column from the inner temp query. However, it occurs to me that it isn't clear how to should be included because you are using an awful feature of MySQL with respect to declaring the Group By columns. I'm assuming that you need to include to in the Group By however if that is not the case, you should be explicit about what aggregate function it should use on the to column.
Here's an alternate where I use Min on the to column:
Update anothertable
Join (
Select temp.hotelid, temp.country
, Min(temp.to) As to
, Min(temp.from_price) AS cheapestPrice
From (
Select v.hotelid, v.country, v.from_price, v.to
From table1 v
Where hotelid >= 1
Union
Select c.hotelid, c.country, c.from_price, c.to
From table2 c
Where hotelid >= 1
Union
Select k.hotelid, k.country, k.from_price, k.to
From table3 k
Where hotelid >= 1
) AS temp
Group By temp.hotelid, temp.country
) As i
On anothertable.id = i.hotelid
And anothertable.country = i.country
Set price = i.cheapestPrice
, op = i.to