This query returned 385 strings
SELECT order_number
FROM `order`
inner join order_delivery_data on `order`.order_id = order_delivery_data.order_id
where order_status = 'delivered' AND order_statusUpdatedAt >= DATE_SUB(CURRENT_DATE, INTERVAL 7 DAY)
and order_delivery_data_name in ('London')
this returned 74 strings
SELECT order_number
FROM `order`
inner join order_delivery_data on `order`.order_id = order_delivery_data.order_id
where order_status = 'delivered' AND order_statusUpdatedAt BETWEEN DATE_SUB(CURRENT_DATE, INTERVAL 10 DAY)
AND DATE_SUB(CURRENT_DATE, INTERVAL 7 DAY)
and order_delivery_data_name in ('London')
How join it, that in that there are two columns
try this:
SELECT t1.order_number as order_t1, t2.order_number as order_t2 FROM
( SELECT order_number,`order`.order_id
FROM `order`
inner join order_delivery_data on `order`.order_id = order_delivery_data.order_id
where order_status = 'delivered' AND order_statusUpdatedAt BETWEEN DATE_SUB(CURRENT_DATE, INTERVAL 10 DAY)
AND DATE_SUB(CURRENT_DATE, INTERVAL 7 DAY)
and order_delivery_data_name in ('London')
) as t1
LEFT JOIN
(SELECT order_number,`order`.order_id
FROM `order`
inner join order_delivery_data on `order`.order_id = order_delivery_data.order_id
where order_status = 'delivered' AND order_statusUpdatedAt BETWEEN DATE_SUB(CURRENT_DATE, INTERVAL 10 DAY)
AND DATE_SUB(CURRENT_DATE, INTERVAL 7 DAY)
and order_delivery_data_name in ('London')
) as t2
ON t1.order_id = t2.order_id
UNION ALL
SELECT t1.order_number as order_t1, t2.order_number as order_t2 FROM
( SELECT order_number,`order`.order_id
FROM `order`
inner join order_delivery_data on `order`.order_id = order_delivery_data.order_id
where order_status = 'delivered' AND order_statusUpdatedAt BETWEEN DATE_SUB(CURRENT_DATE, INTERVAL 10 DAY)
AND DATE_SUB(CURRENT_DATE, INTERVAL 7 DAY)
and order_delivery_data_name in ('London')
) as t1
RIGHT JOIN
(SELECT order_number,`order`.order_id
FROM `order`
inner join order_delivery_data on `order`.order_id = order_delivery_data.order_id
where order_status = 'delivered' AND order_statusUpdatedAt BETWEEN DATE_SUB(CURRENT_DATE, INTERVAL 10 DAY)
AND DATE_SUB(CURRENT_DATE, INTERVAL 7 DAY)
and order_delivery_data_name in ('London')
) as t2
ON t1.order_id = t2.order_id;
You need to use FULL JOIN, i.e. UNION ALL when using mysql:
first_query UNION ALL second_query
Related
I have been struggling to make a single sql query that would give the result of all 3 queries in one. I suppose I would need to use full joins.
These 3 queries are all grouped by hour_key and store_id and ordered by hour_key.
The 3 queries are the following :
select SUM(qty_invoiced) as diag, DATE_FORMAT(a.created_at, '%Y-%m-%d-%h') as hour_key, b.store_id
from sales_flat_order_item a inner join
sales_flat_order b
on (a.order_id = b.entity_id)
where a.created_at BETWEEN (NOW() - INTERVAL 7 DAY) AND NOW() AND
a.parent_item_id IS NULL AND
a.sku LIKE 'D-%' AND b.status in ('processing', 'complete', 'printed', 'ready_to_print', 'ready_to_ship')
GROUP BY DATE_FORMAT(a.created_at, '%Y-%m-%d-%h'), b.store_id
ORDER BY hour_key DESC
select SUM(grand_total) as grand_total, store_id , DATE_FORMAT(created_at, '%Y-%m-%d-%h') as hour_key, SUM(shipping_amount) as shipping
from sales_flat_order
where created_at BETWEEN (NOW() - INTERVAL 7 DAY) AND NOW() AND status in ('processing', 'complete', 'printed', 'ready_to_print', 'ready_to_ship')
GROUP BY DATE_FORMAT(created_at, '%Y-%m-%d-%h'), store_id
ORDER BY hour_key DESC
SELECT COUNT(a.entity_id) as totalPaniers, a.store_id, DATE_FORMAT(a.created_at, '%Y-%m-%d-%h') as hour_key
FROM sales_flat_quote a
WHERE a.created_at BETWEEN (NOW() - INTERVAL 7 DAY) AND NOW()
GROUP BY DATE_FORMAT(a.created_at, '%Y-%m-%d-%h'), a.store_id
ORDER BY hour_key DESC
We don't know what your table structure is, so my best guess is
SELECT
DATE_FORMAT(a.created_at, '%Y-%m-%d-%h') as hour_key,
COUNT(entity_id) as totalPaniers,
store_id,
SUM(grand_total) as grand_total,
SUM(shipping_amount) as shipping,
SUM(qty_invoiced) as diag
FROM
sales_flat_quote a
[LEFT] JOIN sales_flat_order b ON a.store_id=b.store_id
INNER JOIN sales_flat_order_item c ON c.order_id=b.entity_id
WHERE
a.created_at BETWEEN (NOW() - INTERVAL 7 DAY) AND NOW()
AND b.created_at BETWEEN (NOW() - INTERVAL 7 DAY) AND NOW()
AND c.created_at BETWEEN (NOW() - INTERVAL 7 DAY) AND NOW()
AND b.`status` IN ('processing', 'complete', 'printed', 'ready_to_print', 'ready_to_ship')
AND c.parent_item_id IS NULL
AND c.sku LIKE 'D-%'
GROUP BY
b.created_at, store_id
ORDER BY hour_key DESC
I have this query
SELECT concat(order_delivery_data.order_delivery_data_name,' sent'),
sum(case `order`.order_status when 'sent' then 1 else 0 end) '0-7 days'
FROM order_delivery_data
INNER JOIN `order` ON order_delivery_data.order_id = `order`.order_id
where order_delivery_data.order_delivery_data_name in ('Казахстан КАЗПОЧТА')
and order_statusUpdatedAt >= DATE_SUB(CURRENT_DATE, INTERVAL 7 DAY)
UNION
SELECT concat(order_delivery_data.order_delivery_data_name,' parcelonaplace'),
sum(case `order`.order_status when 'parcel-on-a-place' then 1 else 0 end) parcelonaplace
FROM order_delivery_data
INNER JOIN `order` ON order_delivery_data.order_id = `order`.order_id
where order_delivery_data.order_delivery_data_name in ('Казахстан КАЗПОЧТА')
and order_statusUpdatedAt >= DATE_SUB(CURRENT_DATE, INTERVAL 7 DAY)
Which is show 2 columns.
How get one more columns in same query like this?
Use conditional aggregation in your case statements and alter your where clause.
Depending on desired results you may need to alter the 2nd sum to use a between date range instead of just > interval of 14 days.
SELECT concat(order_delivery_data.order_delivery_data_name,' sent'),
sum(case When `order`.order_status = 'sent'
AND order_statusUpdatedAt >= DATE_SUB(CURRENT_DATE, INTERVAL 7 DAY) then 1 else 0 end) '0-7 days',
sum(case When `order`.order_status = 'sent'
AND order_statusUpdatedAt >= DATE_SUB(CURRENT_DATE, INTERVAL 14 DAY) then 1 else 0 end) '8-14 days'
FROM order_delivery_data
INNER JOIN `order` ON order_delivery_data.order_id = `order`.order_id
WHERE order_delivery_data.order_delivery_data_name in ('Казахстан КАЗПОЧТА')
AND order_statusUpdatedAt >= DATE_SUB(CURRENT_DATE, INTERVAL 14 DAY)
UNION
SELECT concat(order_delivery_data.order_delivery_data_name,' parcelonaplace'),
sum(case when `order`.order_status = 'parcel-on-a-place'
AND order_statusUpdatedAt >= DATE_SUB(CURRENT_DATE, INTERVAL 7 DAY) then 1 else 0 end) `parcelonaplace 0-7`,
sum(case when `order`.order_status = 'parcel-on-a-place'
AND order_statusUpdatedAt >= DATE_SUB(CURRENT_DATE, INTERVAL 14 DAY) then 1 else 0 end) `parcelonaplace 8-14
FROM order_delivery_data
INNER JOIN `order` ON order_delivery_data.order_id = `order`.order_id
WHERE order_delivery_data.order_delivery_data_name in ('Казахстан КАЗПОЧТА')
AND order_statusUpdatedAt >= DATE_SUB(CURRENT_DATE, INTERVAL 14 DAY)
I have the following query which counts the number of bookings for a selected day
select count(*)
from isBooked inner join booking
on isbooked.BookingID = booking.bookingID
where '2015-08-09' between booking.startDate and booking.endDate;
I want to run this query for the next 7 days and display the count for each day, for example
day count
1 10
2 9
3 18
4 6
5 1
6 9
7 14
Basically it can be an UNION of 7 queries :
(
SELECT CURDATE() AS Date, COUNT(*) AS Available
FROM isBooked INNER JOIN booking
ON isbooked.BookingID = booking.bookingID
WHERE CURDATE() BETWEEN booking.startDate and booking.endDate;
)
UNION ALL
(
SELECT DATE_ADD(CURDATE(), INTERVAL 1 DAY) AS Date, COUNT(*) AS Available
FROM isBooked INNER JOIN booking
ON isbooked.BookingID = booking.bookingID
WHERE DATE_ADD(CURDATE(), INTERVAL 1 DAY) BETWEEN booking.startDate and booking.endDate;
)
UNION ALL
(
SELECT DATE_ADD(CURDATE(), INTERVAL 2 DAY) AS Date, COUNT(*) AS Available
FROM isBooked INNER JOIN booking
ON isbooked.BookingID = booking.bookingID
WHERE DATE_ADD(CURDATE(), INTERVAL 2 DAY) BETWEEN booking.startDate and booking.endDate;
)
UNION ALL
(
SELECT DATE_ADD(CURDATE(), INTERVAL 3 DAY) AS Date, COUNT(*) AS Available
FROM isBooked INNER JOIN booking
ON isbooked.BookingID = booking.bookingID
WHERE DATE_ADD(CURDATE(), INTERVAL 3 DAY) BETWEEN booking.startDate and booking.endDate;
)
UNION ALL
(
SELECT DATE_ADD(CURDATE(), INTERVAL 4 DAY) AS Date, COUNT(*) AS Available
FROM isBooked INNER JOIN booking
ON isbooked.BookingID = booking.bookingID
WHERE DATE_ADD(CURDATE(), INTERVAL 4 DAY) BETWEEN booking.startDate and booking.endDate;
)
UNION ALL
(
SELECT DATE_ADD(CURDATE(), INTERVAL 5 DAY) AS Date, COUNT(*) AS Available
FROM isBooked INNER JOIN booking
ON isbooked.BookingID = booking.bookingID
WHERE DATE_ADD(CURDATE(), INTERVAL 5 DAY) BETWEEN booking.startDate and booking.endDate;
)
UNION ALL
(
SELECT DATE_ADD(CURDATE(), INTERVAL 6 DAY) AS Date, COUNT(*) AS Available
FROM isBooked INNER JOIN booking
ON isbooked.BookingID = booking.bookingID
WHERE DATE_ADD(CURDATE(), INTERVAL 6 DAY) BETWEEN booking.startDate and booking.endDate;
)
But if you have a large amount of data to manage, you shoud consider processing it regularily on your database and putting it in some cache file or other table.
If you want to do this for a lot of days, an UNION of N SELECT's will not be efficient. In this case I would recommand defining a (temporary) table containing the days, and doing a sigle query with a JOIN on the dates, for example :
CREATE TEMPORARY TABLE dates (day DATE); -- not necessarily temporary
INSERT INTO dates (day) values ('2015-01-01'), ....
SELECT dates.day AS Date, COUNT(*) AS Available
FROM isBooked INNER JOIN booking
ON isbooked.BookingID = booking.bookingID
WHERE dates.day BETWEEN booking.startDate and booking.endDate
GROUP BY dates.day;
I would like to combine different results from the same table as one big result.
SELECT host_name,stats_avgcpu,stats_avgmem,stats_avgswap,stats_avgiowait
FROM sar_stats,sar_hosts,sar_appgroups,sar_environments
WHERE stats_host = host_id
AND host_environment = env_id
AND env_name = 'Staging 2'
AND host_appgroup = group_id
AND group_name = 'Pervasive'
AND DATE(stats_report_time) = DATE_SUB(curdate(), INTERVAL 1 DAY)
SELECT AVG(stats_avgcpu),AVG(stats_avgmem),AVG(stats_avgswap),AVG(stats_avgiowait)
FROM sar_stats
WHERE stats_id = "stat_id of the first query" and DATE(stats_report_time)
BETWEEN DATE_SUB(curdate(), INTERVAL 8 DAY) and DATE_SUB(curdate(), INTERVAL 1 DAY)
SELECT AVG(stats_avgcpu),AVG(stats_avgmem),AVG(stats_avgswap),AVG(stats_avgiowait)
FROM sar_stats
WHERE stats_id = "stat_id of the first query" and DATE(stats_report_time)
BETWEEN DATE_SUB(curdate(), INTERVAL 31 DAY) and DATE_SUB(curdate(), INTERVAL 1 DAY)
Desired output would be something like ...
host_name|stats_avgcpu|stats_avgmem|stats_avgswap|stats_avgiowait|7daycpuavg|7daymemavg|7dayswapavg|7dayiowaitavg|30daycpuavg|30daymemavg|....etc
SQL Fiddle
http://sqlfiddle.com/#!8/4930b/3
It seems like this is what you want. I updated the first query to use proper ANSI JOIN syntax and then for the additional two queries they were joined via a LEFT JOIN on the stats_host field:
SELECT s.stats_host,
h.host_name,
s.stats_avgcpu,
s.stats_avgmem,
s.stats_avgswap,
s.stats_avgiowait,
s7.7dayavgcpu,
s7.7dayavgmem,
s7.7dayavgswap,
s7.7dayavgiowait,
s30.30dayavgcpu,
s30.30dayavgmem,
s30.30dayavgswap,
s30.30dayavgiowait
FROM sar_stats s
INNER JOIN sar_hosts h
on s.stats_host = h.host_id
INNER JOIN sar_appgroups a
on h.host_appgroup = a.group_id
and a.group_name = 'Pervasive'
INNER JOIN sar_environments e
on h.host_environment = e.env_id
and e.env_name = 'Staging 2'
LEFT JOIN
(
SELECT s.stats_host,
AVG(s.stats_avgcpu) AS '7dayavgcpu',
AVG(s.stats_avgmem) AS '7dayavgmem',
AVG(s.stats_avgswap) AS '7dayavgswap',
AVG(s.stats_avgiowait) AS '7dayavgiowait'
FROM sar_stats s
WHERE DATE(stats_report_time) BETWEEN DATE_SUB(curdate(), INTERVAL 8 DAY) AND DATE_SUB(curdate(), INTERVAL 1 DAY)
GROUP BY s.stats_host
) s7
on s.stats_host = s7.stats_host
LEFT JOIN
(
SELECT s.stats_host,
AVG(s.stats_avgcpu) AS '30dayavgcpu',
AVG(s.stats_avgmem) AS '30dayavgmem',
AVG(s.stats_avgswap) AS '30dayavgswap',
AVG(s.stats_avgiowait) AS '30dayavgiowait'
FROM sar_stats s
WHERE DATE(s.stats_report_time) BETWEEN DATE_SUB(curdate(), INTERVAL 31 DAY) AND DATE_SUB(curdate(), INTERVAL 1 DAY)
GROUP BY s.stats_host
) s30
on s.stats_host = s30.stats_host
WHERE DATE(s.stats_report_time) = DATE_SUB(curdate(), INTERVAL 1 DAY);
see SQL Fiddle with Demo
Can anyone help me with this MySQL query?
SELECT p.ProductID,
p.StoreID,
p.DiscountPercentage
FROM Products p
WHERE p.IsSpecial = 1
AND p.SpecialDate >= date_sub(now(),interval 15 minute)
AND p.DiscountPercentage >= ?DiscountPercentage
AND p.ProductID NOT IN (SELECT lf.LiveFeedID
From LiveFeed lf
WHERE p.ProductID = lf.ProductID
AND lf.DateAdded >= date_sub(now(),interval 30 day))
AND p.StoreID NOT IN (SELECT lf.LiveFeedID
From LiveFeed lf
WHERE p.StoreID = lf.StoreID
AND lf.DateAdded >= date_sub(now(),interval 6 hour))
ORDER BY p.StoreID, p.DiscountPercentage DESC
I'm trying join where the ProductID is not in the livefeed table in the last 30 days and where the storeid is not in the livefeed table in the last 6 hours, but it does not seem to be working. Any idea what I'm doing wrong?
At a glance, it would appear that your first subquery should be selecting ProductID, not LiveFeedID and your second subquery should be selecting StoreID not LiveFeedID
I'm too late:
SELECT p.ProductID,
p.StoreID,
p.DiscountPercentage
FROM Products p
WHERE p.IsSpecial = 1
AND p.SpecialDate >= date_sub(now(),interval 15 minute)
AND p.DiscountPercentage >= ?DiscountPercentage
AND p.ProductID NOT IN (SELECT lf.productid
FROM LIVEFEED lf
WHERE lf.DateAdded >= DATE_SUB(NOW(), INTERVAL 30 DAY))
AND p.storeid NOT IN (SELECT lf.storeid
FROM LIVEFEED lf
WHERE lf.DateAdded >= DATE_SUB(NOW(), INTERVAL 6 HOUR))
ORDER BY p.StoreID, p.DiscountPercentage DESC
You were using EXISTS syntax with a correllated subquery...
I'm trying to get the top discount for each store.
In that case, use:
SELECT p.StoreID,
MAX(p.DiscountPercentage)
FROM Products p
WHERE p.IsSpecial = 1
AND p.SpecialDate >= date_sub(now(),interval 15 minute)
AND p.DiscountPercentage >= ?DiscountPercentage
AND p.ProductID NOT IN (SELECT lf.productid
FROM LIVEFEED lf
WHERE lf.DateAdded >= DATE_SUB(NOW(), INTERVAL 30 DAY))
AND p.storeid NOT IN (SELECT lf.storeid
FROM LIVEFEED lf
WHERE lf.DateAdded >= DATE_SUB(NOW(), INTERVAL 6 HOUR))
GROUP BY p.storeid
ORDER BY p.StoreID, p.DiscountPercentage DESC