adding INNER JOIN to convert ID to a value from another table - mysql

i have an SQL Script which generates the following Output:
+------------+------------+--------------------+----------------------+---------------------+
| CUSTOMERID | TOTALCOUNT | VALIDWARRANTYCOUNT | EXPIREDWARRANTYCOUNT | LASTPURCHASED |
+------------+------------+--------------------+----------------------+---------------------+
| 1 | 5 | 5 | 0 | 2013-12-24 14:37:45 |
| 2 | 3 | 3 | 0 | 2013-12-24 14:37:45 |
| 3 | 6 | 6 | 0 | 2013-10-23 13:37:45 |
+------------+------------+--------------------+----------------------+---------------------+
I would like to see the Companyname of the Customer in a additional column. I have an extra table with customer id and company name but I don't know how to extend my query. All trys result in an empty table.
Here is my Query:
SELECT p2c.customerid
, (
SELECT COUNT(*)
FROM products2customers
WHERE customerid = p2c.customerid
) AS TotalCount
, (
SELECT COUNT(*)
FROM products2customers
WHERE customerid = p2c.customerid
AND date_add(from_unixtime(purchased), INTERVAL 5 YEAR) >= CURDATE()
) AS ValidWarrantyCount
, (
SELECT COUNT(*)
FROM products2customers
WHERE customerid = p2c.customerid
AND date_add(from_unixtime(purchased), INTERVAL 5 YEAR) < CURDATE()
) AS ExpiredWarrantyCount
, (
SELECT MAX( from_unixtime(purchased) )
FROM products2customers
WHERE customerid = p2c.customerid
) AS LastPurchased
FROM (
SELECT DISTINCT
p2c.customerid
FROM
products2customers p2c
INNER JOIN
products p
ON
p2c.customerid = p.id
) AS p2c
I made an SQL Fiddle with the normal query without a join, I hope someone could give me a hint.
SQLFiddle

Just add your JOIN in the very last line, and select the company after the customer id:
SELECT p2c.customerid,
c.company
, (
SELECT COUNT(*)
FROM products2customers
WHERE customerid = p2c.customerid
) AS TotalCount
, (
SELECT COUNT(*)
FROM products2customers
WHERE customerid = p2c.customerid
AND date_add(from_unixtime(purchased), INTERVAL 5 YEAR) >= CURDATE()
) AS ValidWarrantyCount
, (
SELECT COUNT(*)
FROM products2customers
WHERE customerid = p2c.customerid
AND date_add(from_unixtime(purchased), INTERVAL 5 YEAR) < CURDATE()
) AS ExpiredWarrantyCount
, (
SELECT MAX( from_unixtime(purchased) )
FROM products2customers
WHERE customerid = p2c.customerid
) AS LastPurchased
FROM (
SELECT DISTINCT
p2c.customerid
FROM
products2customers p2c
) AS p2c
JOIN customers c ON c.id = p2c.customerid; <--
Updated fiddle:
http://sqlfiddle.com/#!2/60396/5/0

just add another join to the customer table as below
SELECT p2c.customerid, company
, (
SELECT COUNT(*)
FROM products2customers
WHERE customerid = p2c.customerid
) AS TotalCount
, (
SELECT COUNT(*)
FROM products2customers
WHERE customerid = p2c.customerid
AND date_add(from_unixtime(purchased), INTERVAL 5 YEAR) >= CURDATE()
) AS ValidWarrantyCount
, (
SELECT COUNT(*)
FROM products2customers
WHERE customerid = p2c.customerid
AND date_add(from_unixtime(purchased), INTERVAL 5 YEAR) < CURDATE()
) AS ExpiredWarrantyCount
, (
SELECT MAX( from_unixtime(purchased) )
FROM products2customers
WHERE customerid = p2c.customerid
) AS LastPurchased
FROM (
SELECT
p2c.customerid, c.company
FROM
products2customers p2c
INNER JOIN
products p
ON
p2c.customerid = p.id
inner join customers c
on c.id=p2c.customerid group by 1,2
) AS p2c

Related

Counting between dates

I need the count of all dates including the nonexistent
SELECT ifnull(COUNT(*),0) as num , date_format(c.dataCupo,"%d/%m/%Y") as data
FROM cupons c
WHERE c.dataCupo between "2017-02-02" AND "2018-05-04" AND c.proveidor!="VINCULADO" and c.empresa=1
group by date_format(c.dataCupo,"%Y-%m-%d")
//And I need to count all months including the nonexistent
SELECT ifnull(COUNT(*),0) as num , date_format(c.dataCupo,"%m/%Y") as data
FROM cupons c
WHERE c.dataCupo between "2017-02-02" AND "2018-05-04" AND c.proveidor!="VINCULADO" and c.empresa=1
group by date_format(c.dataCupo,"%Y-%m")
//And I need to count of all years including the nonexistent
SELECT ifnull(COUNT(*),0) as num , date_format(c.dataCupo,"%Y") as data
FROM cupons c
WHERE c.dataCupo between "2015-02-02" AND "2018-05-04" AND c.proveidor!="VINCULADO" and c.empresa=1
group by date_format(c.dataCupo,"%Y")
The result i want its:
02/02/2017 | 10
03/02/2017 | 0
04/02/2017 | 2
05/02/2017 | 0
....
AND
02/2017 | 50
03/2017 | 0
04/2017 | 10
AND
2015 | 0
2016 | 10
2017 | 15
2018 | 0
Easiest way to do this is with a Calendar table. This table will have a datetime column that you can join to and is really useful for reporting. Here goes an example of how to make one in MySQL.
https://gist.github.com/bryhal/4129042
Now that you have the Calendar table, you can join to it to find counts of all dates in a date range.
All days example:
select num, td.db_date
FROM
time_dimension td
left join
(SELECT ifnull(COUNT(*),0) as num , c.dataCupo as data
FROM cupons c
WHERE c.dataCupo between "2017-02-02" AND "2018-05-04" AND
c.proveidor!="VINCULADO" and c.empresa=1
group by c.dataCupo) t
on t.data = td.db_date
WHERE td.db_date between "2017-02-02" AND "2018-05-04"
All months example:
select
sum(t.num),
CONCAT(month(td.db_date),"-",year(td.db_date))
FROM
time_dimension td
left join
(SELECT
ifnull(COUNT(*),0) as num ,
c.dataCupo as data
FROM cupons c
WHERE c.dataCupo between "2017-02-02" AND "2018-05-04" AND
c.proveidor!="VINCULADO" and c.empresa=1) t
on c.data = t.data
WHERE td.db_date between "2017-02-02" AND "2018-05-04"
group by CONCAT(month(td.db_date),"-",year(td.db_date))
You should create A Temporary Table To Store All The Date Ranges Between Your Date Ranges
CREATE TEMPORARY TABLE IF NOT EXISTS AllDateRange engine=memory
SELECT DATE(cal.date) Date
FROM (
SELECT ( case when #prmToDate = #prmFromDate then #prmFromDate else
SUBDATE( #prmFromDate, INTERVAL (DATEDIFF(#prmToDate,#prmFromDate)) DAY) + INTERVAL xc DAY end ) AS Date
FROM (
SELECT #xi:=#xi+1 as xc from
(SELECT 1 UNION SELECT 2 UNION SELECT 3 UNION SELECT 4) xc1,
(SELECT 1 UNION SELECT 2 UNION SELECT 3 UNION SELECT 4) xc2,
(SELECT 1 UNION SELECT 2 UNION SELECT 3 UNION SELECT 4) xc3,
(SELECT 1 UNION SELECT 2 UNION SELECT 3 UNION SELECT 4) xc4,
(SELECT #xi:=+1) xc0
) xxc1
) cal WHERE DATE( cal.date) >= DATE(#prmFromDate) and DATE( cal.date) <= DATE(#prmToDate) ;
And Then Join It With Your Table As.
SELECT count(COALESCE(c.empresa, 0)) as num , date_format(a.Date,"%d/%m/%Y") as data from AllDateTimeRange a
left join cupons c on a.Date=date_format(c.dataCupo,"%Y-%m-%d")
WHERE c.dataCupo between #prmFromDate AND #prmToDate AND c.proveidor!="VINCULADO" and c.empresa=1
group by date_format(c.dataCupo,"%Y-%m-%d");
Similarly Create Temp Tables For Month & Year And Then Join With Your Primary Table, as above in order to get your required results for month and year respectively.

UNION query results in a new row instead of new column

The following query returns a result with 5 columns (
date ,lowest_hr_price ,max_hr_price ,min_price , max_price )
instead of
(date ,lowest_hr_price ,max_hr_price , min_price ,max_price , AvgPrice, AvgPieces ).
AvgPrice and AvgPieces are instead added as rows .
(select date(m.min_max_date) as date,
max(case when m.lbl='min_hr_price' then m.min_max_hr_price else null end) as lowest_hr_price,
max(case when m.lbl='max_hr_price' then m.min_max_hr_price else null end) as max_hr_price,
max(case when n.lbl='min_price' then n.min_max_price else null end) as min_price,
max(case when n.lbl='max_price' then n.min_max_price else null end) as max_price
from (select 'min_hr_price' as lbl, price as min_max_hr_price, date_time as min_max_date
from mytable
where date_time in (select min(date_time) as min_date from mytable group by date(date_time)) and symbol = 'dollar'
UNION
select 'max_hr_price', price, date_time
from mytable WHERE symbol = 'dollar'
AND date_time in (select max(date_time) as max_date from mytable WHERE symbol = 'dollar' group by date(date_time))) as m,
(
select 'min_price' as lbl,
min(date_time) as min_max_date,
min(price) as min_max_price
from mytable
WHERE symbol = 'dollar'
group by date(date_time)
UNION
select 'max_price' as lbl,
max(date_time) as min_max_date,
max(price) as min_max_price
from mytable
WHERE symbol = 'dollar'
group by date(date_time)
) n
where m.min_max_date=n.min_max_date
group by date(m.min_max_date)
order by m.min_max_date DESC
)
UNION
(SELECT null, null, date_time, avg (price) as AvgPrice, avg (pieces) as AvgPieces FROM mytable
WHERE symbol = 'dollar'
group by date(date_time))
Actual result:
date |lowest_hr_price | max_hr_price | min_price | max_price
------------------------------------------------------------------------------------
2018-03-06 | 1 | 2 | 0 | 10
NULL | NULL | {date} | {avgprice} | {avgpieces}
Expected result:
date |lowest_hr_price | max_hr_price | min_price | max_price | AvgPrice | AvgPieces
-------------------------------------------------------------------------------------------------------------
2018-03-06 | 1 | 2 | 0 | 10 | {avgprice}| {avgpieces}
if you instead of an union (select rows append one select to each others ) need all the result on the same rows
in this case you could use a cross join eg :
select distinct * from (
(select date(m.min_max_date) as date,
max(case when m.lbl='min_hr_price' then m.min_max_hr_price else null end) as lowest_hr_price,
max(case when m.lbl='max_hr_price' then m.min_max_hr_price else null end) as max_hr_price,
max(case when n.lbl='min_price' then n.min_max_price else null end) as min_price,
max(case when n.lbl='max_price' then n.min_max_price else null end) as max_price
from (select 'min_hr_price' as lbl, price as min_max_hr_price, date_time as min_max_date
from mytable
where date_time in (select min(date_time) as min_date from mytable group by date(date_time)) and symbol = 'dollar'
UNION
select 'max_hr_price', price, date_time
from mytable WHERE symbol = 'dollar'
AND date_time in (select max(date_time) as max_date from mytable WHERE symbol = 'dollar' group by date(date_time))) as m,
(
select 'min_price' as lbl,
min(date_time) as min_max_date,
min(price) as min_max_price
from mytable
WHERE symbol = 'dollar'
group by date(date_time)
UNION
select 'max_price' as lbl,
max(date_time) as min_max_date,
max(price) as min_max_price
from mytable
WHERE symbol = 'dollar'
group by date(date_time)
) n
where m.min_max_date=n.min_max_date
group by date(m.min_max_date)
order by m.min_max_date DESC
) ) T1 INNER join
(SELECT null, null, date_time, avg (price) as AvgPrice, avg (pieces) as AvgPieces FROM mytable
WHERE symbol = 'dollar'
group by date(date_time)) T2 ON date(T1.date) = date(T2.date_time)

SQL query joining a few tables (MySQL)

I need a "little" help with an SQL query (MySQL).
I have the following tables:
COURIERS table:
+------------+
| COURIER_ID |
+------------+
DELIVERIES table:
+-------------+------------+------------+
| DELIVERY_ID | COURIER_ID | START_DATE |
+-------------+------------+------------+
ORDERS table:
+----------+-------------+-------------+
| ORDER_ID | DELIVERY_ID | FINISH_DATE |
+----------+-------------+-------------+
COORDINATES table:
+-------------+-----+-----+------+
| DELIVERY_ID | LAT | LNG | DATE |
+-------------+-----+-----+------+
In the real database I have more columns in each table, but for this example the above columns are enough.
What do I need?
An SQL query that returns all couriers [COURIER_ID], their last
delivery [DELIVERY_ID] (based on last START_DATE), the
delivery's last coordinate [LAT and LNG] (based on last DATE) and the remaining orders count (total of orders of the last delivery that have no FINISH_DATE).
A courier can have no deliveries, in this case I want DELIVERY_ID =
NULL, LAT = NULL and LNG = NULL in the result.
A delivery can have no coordinates, in this case I want LAT = NULL
and LNG = NULL in the result.
What was I able to do?
SELECT c.`COURIER_ID`,
d.`DELIVERY_ID`,
r.`LAT`,
r.`LNG`,
(SELECT COUNT(DISTINCT `ORDER_ID`)
FROM `ORDERS`
WHERE `DELIVERY_ID` = d.`DELIVERY_ID`
AND `FINISH_DATE` IS NULL) AS REMAINING_ORDERS
FROM `COURIERS` AS c
LEFT JOIN `DELIVERIES` AS d USING (`COURIER_ID`)
LEFT JOIN `COORDINATES` AS r ON r.`DELIVERY_ID` = d.`DELIVERY_ID`
WHERE (CASE WHEN
(SELECT MAX(`START_DATE`)
FROM `DELIVERIES`
WHERE `COURIER_ID` = c.`COURIER_ID`) IS NULL THEN d.`START_DATE` IS NULL ELSE d.`START_DATE` =
(SELECT MAX(`START_DATE`)
FROM `DELIVERIES`
WHERE `COURIER_ID` = c.`COURIER_ID`) END)
AND (CASE WHEN
(SELECT MAX(`DATE`)
FROM `COORDINATES`
WHERE `DELIVERY_ID` = d.`DELIVERY_ID`) IS NULL THEN r.`DATE` IS NULL ELSE r.`DATE` =
(SELECT MAX(`DATE`)
FROM `COORDINATES`
WHERE `DELIVERY_ID` = d.`DELIVERY_ID`) END)
GROUP BY c.`COURIER_ID`
ORDER BY d.`START_DATE` DESC
The problem is that this query is very slow (from 5 to 20 seconds) when I have over 5k COORDINATES and it does not returns all couriers sometimes.
Thank you so much for any solution.
Try this:
SELECT C.COURIER_ID, D.DELIVERY_ID, D.START_DATE, D.FINISH_DATE,
B.LAT, B.LNG, B.DATE, C.NoOfOrders
FROM COURIERS C
LEFT JOIN ( SELECT *
FROM (SELECT *
FROM DELIVERIES D
ORDER BY D.COURIER_ID, D.START_DATE DESC
) A
GROUP BY COURIER_ID
) AS A ON C.COURIER_ID = A.COURIER_ID
LEFT JOIN ( SELECT *
FROM (SELECT *
FROM COORDINATES CO
ORDER BY CO.DELIVERY_ID, CO.DATE DESC
) B
GROUP BY CO.DELIVERY_ID
) AS B ON A.DELIVERY_ID = B.DELIVERY_ID
LEFT JOIN ( SELECT O.DELIVERY_ID, COUNT(1) NoOfOrders
FROM ORDERS O WHERE FINISH_DATE IS NULL
GROUP BY O.DELIVERY_ID
) AS C ON A.DELIVERY_ID = C.DELIVERY_ID;
I haven't been able to test this query since I don't have a mysql database set up right now, much less with this schema and sample data. But I think this will work for you:
select
c.courier_id
, d.delivery_id
, co.lat
, co.lng
, oc.cnt as remaining_orders
from
couriers c
left join (
select
d.delivery_id
, d.courier_id
from
deliveries d
inner join (
select
d.delivery_id
, max(d.start_date) as start_date
from
deliveries d
group by
d.delivery_id
) dmax on dmax.delivery_id = d.delivery_id and dmax.start_date = d.start_date
) d on d.courier_id = c.courier_id
left join (
select
c.delivery_id
, c.lat
, c.lng
from
coordinates c
inner join (
select
c.delivery_id
, max(c.date) as date
from
coordinates c
group by
c.delivery_id
) cmax on cmax.delivery_id = c.delivery_id and cmax.date = c.date
) co on co.delivery_id = d.delivery_id
left join (
select
o.delivery_id
, count(o.order_id) as cnt
from
orders o
where
o.finish_date is null
group by
o.delivery_id
) oc on oc.delivery_id = d.delivery_id

SQL: Return specified column of max() row inside SELECT

I want to return the date column for each of the rows where max() is used within the SELECT. Or maybe there is a better way of doing this?
This is how I imagine it:
SELECT
MAX(time) as time, [date column from max(time) row] as timedate,
MAX(distance) as distance, [date column from max(distance) row] as distancedate,
MAX(weight) as weight, [date column from max(weight) row] as weightdate
Here is my current SQL, this does not return the date for each of the MAX() rows.
$db->query("SELECT e.id as id, e.name, MAX(ue.time) as time, MAX(ue.weight) as weight, MAX(ue.distance) as distance
FROM `users exercises` as ue
LEFT JOIN `exercises` as e ON exerciseid = e.id
GROUP BY e.id
LIMIT 30");
id | exerciseid | date | weight | distance | time
----------------------------------------------------------
1 | 1 | 2014-06-14 | 100 | 33 | null
2 | 1 | 2013-03-03 | 500 | 11 | null
3 | 1 | 2014-11-11 | null | null | 41
Current Output:
Array
(
[id] => 1
[name] => run
[time] => 41
[weight] => 500
[distance] => 33
)
Expected Output:
Array
(
[id] => 1
[name] => run
[time] => 41
[time_date] => 2014-11-11
[weight] => 500
[weight_date] => 2013-03-03
[distance] => 33
[distance_date] => 2014-06-14
)
SQL Fiddle: http://sqlfiddle.com/#!2/75e53/1
SELECT e.id as id, e.name,
MAX(ue.time) as time,
(
select date
from `users exercises`
WHERE time = MAX(ue.time) AND ue.`userid` = $userid
LIMIT 1
) as time_date,
MAX(ue.weight) as weight,
(
select date
from `users exercises`
WHERE weight = MAX(ue.weight) AND ue.`userid` = $userid
LIMIT 1
) as weight_date,
MAX(ue.distance) as distance,
(
select date
from `users exercises`
WHERE distance = MAX(ue.distance) AND ue.`userid` = $userid
LIMIT 1
) as distance_date
FROM `users exercises` as ue
LEFT JOIN `exercises` as e ON exerciseid = e.id
WHERE ue.`userid` = $userid
GROUP BY e.id
LIMIT 30
There's probably a more efficient way to do this, but sadly my MySQL skills aren't that good; however the code below does what you want:
Solution 1
select
mx.time
, t.date as timedate
, mx.distance
, d.date as distancedate
, mx.weight
, w.date as weightdate
from
(
SELECT
MAX(`time`) as `time`
, MAX(`distance`) as `distance`
, MAX(`weight`) as `weight`
from `users exercises`
) as mx
inner join `users exercises` as t on t.time = mx.time
inner join `users exercises` as d on d.distance = mx.distance
inner join `users exercises` as w on w.weight = mx.weight;
Solution 2
select
mx.time
, (select date from `users exercises` as x where x.time = mx.time limit 1) as timedate
, mx.distance
, (select date from `users exercises` as y where y.distance = mx.distance limit 1) as distancedate
, mx.weight
, (select date from `users exercises` as z where z.weight = mx.weight limit 1) as weightdate
from
(
SELECT
MAX(`time`) as `time`
, MAX(`distance`) as `distance`
, MAX(`weight`) as `weight`
from `users exercises`
) as mx;
For anyone using a db which support partition by there is a better way of implementing this; sadly MySQL does not support that functionality currently.
SQL Fiddle: http://sqlfiddle.com/#!2/75e53/13

How to merge the result of this 2 queries in mysql server

I am actually stuck in merging the result of this two queries:
first query:
SELECT c.code, c.name, pc.sku, pc.cat_code, pp.title
FROM `cat_parent` cp, cat c, prod_cat pc, products pp
WHERE c.code = cp.cat_code
AND cp.cat_code = pc.cat_code
AND pp.sku = pc.sku
AND cp.parent_code = 01110
AND hide =0
The result I get is:
Second query:
SELECT `sku` , `update_date` , `description` , count( * ) AS total_sold
FROM `orderline`
WHERE `update_date` >= ( DATE_ADD(CURDATE( ) , INTERVAL -14 DAY ) )
AND `update_date` <= ( DATE_ADD(CURDATE( ) , INTERVAL -7 DAY ) )
GROUP BY left( sku, 7 )
ORDER BY total_sold DESC
The result:
The question I want to ask that how can I get the result by filtering the sku available in both tables.
Just bit confused on that part....any ideas will be appreciated.
This is only part of the data. there is heaps of data. Yes, I want to merge the both tables and want to find the common sku available in both tables.
My expected result will be sku, title, total sold.
Thanks, anyway I managed to get around to get the result.
My final query:
SELECT * FROM (
SELECT sku , update_date , description FROM orderline WHERE
update_date >= '2012-03-06' AND update_date <= '2012-03-07' )g
JOIN (
SELECT c.code, c.name, pc.sku, pc.cat_code FROM cat_parent cp, cat
c, prod_cat pc, products pp WHERE c.code = cp.cat_code AND cp.cat_code
= pc.cat_code AND pp.sku = pc.sku AND cp.parent_code =01110 AND hide =0 )p ON left( g.sku, 7 ) = left( p.sku, 7 )
Something like this -
SELECT
`c`.`code`, `c`.`name`, `pc`.`sku`, `pc`.`cat_code`, `pp.title`,
`ol`.`sku`, `ol`.`update_date`, `ol`.`description`, COUNT(*) AS `total_sold`
FROM `cat_parent` `cp`
INNER JOIN `cat` `c`
ON `c`.`code` = `cp`.`cat_code`
INNER JOIN `prod_cat` `pc`
ON `cp`.`cat_code` = `pc`.`cat_code`
INNER JOIN `products` `pp`
ON `pp`.`sku` = `pc`.`sku`
INNER JOIN `orderline` `ol`
ON LEFT(`pc`.`sku`, 7) = LEFT(`ol`.`sku`, 7)
WHERE `cp`.`parent_code` = 01110
AND `hide` = 0
AND `ol`.`update_date` >= ( DATE_ADD(CURDATE( ) , INTERVAL -14 DAY ) )
AND `ol`.`update_date` <= ( DATE_ADD(CURDATE( ) , INTERVAL -7 DAY ) )
GROUP BY left( `ol`.`sku`, 7 )
ORDER BY `total_sold` DESC