I have two tables (champ_value and champ_form), i tried two queries but i didn't obtain the result that I want,
please can you give me other solution
Thanks in advance.
+-------------+-----------+---------------------+---------------+
| champ value | | | |
| | | | |
| v_id | v_value | v_fk_order_item_id | v_fk_champ_id |
| 220 | Bernad | 20000 | 1 |
| 221 | Lagaf | 20000 | 2 |
| 500 | Vincent | 20000 | 1 |
| 501 | Pernault | 20000 | 2 |
+-------------+-----------+---------------------+---------------+
+------------+-------------+---------------+-------------+
| champ_form | | | |
| cf_id | cf_position | cf_fk_form_id | cf_champ_id |
| 330 | 10 | 1800 | 1 |
| 331 | 12 | 1800 | 2 |
| 630 | 13 | 1800 | 1 |
| 631 | 14 | 1800 | 2 |
+------------+-------------+---------------+-------------+
the desired result will be like this :
+----------------+-------+-------------+-------------+----------+
| desired result | | | | |
| | | | | |
| v_id | cf_id | cf_position | cf_champ_id | v_value |
| 220 | 330 | 10 | 1 | Bernard |
| 221 | 331 | 12 | 2 | Lagaf |
| 500 | 630 | 13 | 1 | Vincent |
| 501 | 631 | 14 | 2 | Pernault |
+----------------+-------+-------------+-------------+----------+
I tried this first query :
SELECT v.v_id, cf.cf_id, cf.cf_position, cf.cf_champ_id, v.v_value
FROM champ_form cf
JOIN champ_value v ON v.v_fk_champ_id = cf.cf_champ_id
WHERE cf.cf_fk_form_id =1800
AND v.v_fk_order_item_id =20000
GROUP BY v_id
ORDER BY cf.cf_position
and I obtain this :
+---------------+-------+-------------+-------------+----------+
| group by v_id | | | | |
| | | | | |
| v_id | cf_id | cf_position | cf_champ_id | v_value |
| 220 | 330 | 10 | 1 | Bernard |
| 221 | 330 | 10 | 1 | Vincent |
| 500 | 331 | 12 | 2 | Lagaf |
| 501 | 331 | 12 | 2 | Pernault |
+---------------+-------+-------------+-------------+----------+
and the second query :
SELECT v.v_id, cf.cf_id, cf.cf_position, cf.cf_champ_id, v.v_value
FROM champ_form cf
JOIN champ_value v ON v.v_fk_champ_id = cf.cf_champ_id
WHERE cf.cf_fk_form_id =1800
AND v.v_fk_order_item_id =20000
GROUP BY cf.cf_id
ORDER BY cf.cf_position
+----------------+-------+-------------+-------------+---------+
| group by cf_id | | | | |
| | | | | |
| v_id | cf_id | cf_position | cf_champ_id | v_value |
| 220 | 330 | 10 | 1 | Bernard |
| 221 | 331 | 12 | 2 | Lagaf |
| 220 | 630 | 13 | 1 | Bernard |
| 221 | 631 | 14 | 2 | Lagaf |
+----------------+-------+-------------+-------------+---------+
The first query gives the good values but not the correct positions
and the second gives the correct positions but not the correct values.
Without describing the desired behavior is difficult to understand what you want. But probably the problem is that the selected columns are not in the GROUP BY.
Try the following:
SELECT aa.v_id, cc.cf_id, cc.cf_position, cc.cf_champ_id, aa.v_value
FROM champ_value AS aa
INNER JOIN (
SELECT _aa.v_id
FROM champ_value AS _aa
INNER JOIN champ_form AS _bb
ON _aa.v_fk_champ_id = _bb.cf_champ_id
WHERE _aa.cf_fk_form_id = 1800 AND _bb.v_fk_order_item_id = 20000
GROUP BY _aa.v_id
) AS bb
ON aa.v_id = bb.v_id
INNER JOIN champ_form AS cc
ON aa.v_fk_champ_id = cc.cf_champ_id
ORDER BY aa.cf_position
More info here and here.
This does display according to position - still not convinced it's safe though!
/*
create table champ_value ( v_id int, v_value varchar(10), v_fk_order_item_id int, v_fk_champ_id int);
truncate table champ_value;
insert into champ_value values
(220 , 'Bernad' , 20000 , 1 ),
(221 , 'Lagaf' , 20000 , 2 ),
(500 , 'Vincent' , 20000 , 1 ),
(501 , 'Pernault' , 20000 , 2 );
create table champ_form(cf_id int,cf_position int, cf_fk_form_id int ,cf_champ_id int);
insert into champ_form values
(330 , 10 , 1800 , 1 ),
(331 , 12 , 1800 , 2 ),
(630 , 13 , 1800 , 1 ),
(631 , 14 , 1800 , 2 );
+----------------+-------+-------------+-------------+----------+
| desired result | | | | |
| | | | | |
| v_id | cf_id | cf_position | cf_champ_id | v_value |
| 220 | 330 | 10 | 1 | Bernard |
| 221 | 331 | 12 | 2 | Lagaf |
| 500 | 630 | 13 | 1 | Vincent |
| 501 | 631 | 14 | 2 | Pernault |
+----------------+-------+-------------+-------------+----------+
*/
SELECT s.v_id,t.cf_id, t.cf_position ,t.cf_champ_id ,s.v_value
FROM
(
SELECT I.v_id,v_value,v_fk_order_item_id,v_fk_champ_id,
#RN:=#RN + 1 RN
FROM (SELECT #RN:=0) RN,champ_value I
order by i.v_id asc
) S
LEFT OUTER JOIN
(SELECT cf_id,cf_position, cf_fk_form_id,cf_champ_id,
#RN1:=#RN1 + 1 RN1
FROM (SELECT #RN1:=0) RN1, champ_form E
order by e.cf_id asc
) T ON T.RN1 = S.RN
Actual Result
+------+-------+-------------+-------------+----------+
| v_id | cf_id | cf_position | cf_champ_id | v_value |
+------+-------+-------------+-------------+----------+
| 220 | 330 | 10 | 1 | Bernad |
| 221 | 331 | 12 | 2 | Lagaf |
| 500 | 630 | 13 | 1 | Vincent |
| 501 | 631 | 14 | 2 | Pernault |
+------+-------+-------------+-------------+----------+
Related
I have two tables orders and customers:
select * from orders;
+------+---------------------+-------------+--------+
| oid | date | customer_id | amount |
+------+---------------------+-------------+--------+
| 102 | 2009-10-08 00:00:00 | 4 | 300 |
| 100 | 2009-10-08 00:00:00 | 3 | 15000 |
| 101 | 2008-10-08 00:00:00 | 2 | 1300 |
| 105 | 2010-10-08 00:00:00 | 1 | 400 |
| 106 | 2014-12-23 00:00:00 | 3 | 300 |
+------+---------------------+-------------+--------+
select * from customers;
+------+--------+------+-----------+----------+
| id | name | age | address | salary |
+------+--------+------+-----------+----------+
| 1 | ramesh | 32 | Ahmedabad | 2000.00 |
| 2 | khilan | 25 | delhi | 1500.00 |
| 3 | muffy | 22 | bhopal | 8500.00 |
| 4 | suresh | 48 | mumbai | 24000.00 |
| 1 | ramesh | 32 | Ahmedabad | 300.00 |
| 5 | akil | 21 | madurai | 1000.00 |
| 6 | rajesh | 22 | delhi | 5000.00 |
+------+--------+------+-----------+----------+
What I'm trying to do is to do SUM(salary) from customers and subtract it with the SUM(amount) from orders table. I have tried with this query:
SELECT id ,NAME,SUM(salary),SUM(amount),SUM(salary)-SUM(amount)
FROM customers a LEFT JOIN orders b ON a.id=b.customer_id
GROUP BY NAME;
This will return the following result, which some of them return incorrect value:
+------+--------+-------------+-------------+-------------------------+
| id | name | SUM(salary) | SUM(amount) | SUM(salary)-SUM(amount) |
+------+--------+-------------+-------------+-------------------------+
| 5 | akil | 1000.00 | NULL | NULL |
| 2 | khilan | 1500.00 | 1300 | 200.00 |
| 3 | muffy | 17000.00 | 15300 | 1700.00 |
| 6 | rajesh | 5000.00 | NULL | NULL |
| 1 | ramesh | 2300.00 | 800 | 1500.00 |
| 4 | suresh | 24000.00 | 300 | 23700.00 |
+------+--------+-------------+-------------+-------------------------+
My expected output is as following:
+------+--------+-------------+-------------+-------------------------+
| id | name | SUM(salary) | SUM(amount) | SUM(salary)-SUM(amount) |
+------+--------+-------------+-------------+-------------------------+
| 5 | akil | 1000.00 | NULL | 1000 |
| 2 | khilan | 1500.00 | 1300 | 200.00 |
| 3 | muffy | 8500 | 15300 | -6800 |
| 6 | rajesh | 5000.00 | NULL | 5000 |
| 1 | ramesh | 2300.00 | 400 | 1900.00 |
| 4 | suresh | 24000.00 | 300 | 23700.00 |
+------+--------+-------------+-------------+-------------------------+
One way is to make calculation on orders into sub-query.
To cater for NULL value, you can use IFNULL(value,0).
SELECT id,NAME,SUM(salary),amt,SUM(salary)-IFNULL(amt,0)
FROM customers a LEFT JOIN
(SELECT customer_id, SUM(amount) amt FROM orders GROUP BY customer_id) b
ON a.id=b.customer_id
GROUP BY NAME;
I have these tables ;
Mysql Tables[Click Here]
attendance table
+-----+------------+--------+---------+--+
| id | atdnc_date | emp_id | isleave | |
+-----+------------+--------+---------+--+
| 138 | 2018-11-22 | 1024 | 1 | |
| 133 | 2018-11-21 | 6 | 0 | |
| 130 | 2018-11-20 | 6 | 0 | |
| 129 | 2018-11-20 | 71 | 1 | |
| 126 | 2018-11-19 | 817 | 0 | |
| 122 | 2018-11-18 | 355 | 0 | |
| 123 | 2018-11-18 | 6 | 0 | |
| 190 | 2018-11-17 | 355 | 1 | |
| 119 | 2018-11-17 | 817 | 0 | |
| 117 | 2018-11-17 | 6 | 0 | |
| 116 | 2018-11-17 | 71 | 0 | |
| 113 | 2018-11-16 | 817 | 0 | |
+-----+------------+--------+---------+--+
driver table
+-----+-----------+-------------+--+
| id | driver_id | driver_lice | |
+-----+-----------+-------------+--+
| 131 | 817 | B67408 | |
| 132 | 450 | B548093 | |
| 133 | 6 | B1394007 | |
| 134 | 355 | B772739 | |
| 135 | 1024 | B204085 | |
| 136 | 71 | B2442775 | |
| 137 | 88881 | B2056537 | |
| 138 | 729 | B8265 | |
| 139 | 600 | B393570 | |
| 140 | 748 | B2228230 | |
+-----+-----------+-------------+--+
vehicle table
+-----+---------------+-----------+--+
| id | vehicle_id | driver_id | |
+-----+---------------+-----------+--+
| 137 | VH000137 | 817 | |
| 138 | VH000138 | 817 | |
| 139 | VH000139 | 600 | |
| 140 | VH000140 | 1024 | |
| 141 | VH000141 | 450 | |
| 142 | VH000142 | 600 | |
| 143 | VH000143 | 729 | |
| 144 | VH000144 | 817 | |
+-----+---------------+-----------+--+
vehicle_status table
+---------------+----------+----------------+--+
| vehicle_id | order_id | current_status | |
+---------------+----------+----------------+--+
| VH000137 | OR000130 | completed | |
| VH000138 | OR000131 | arrived_to_sup | |
| VH000139 | OR000132 | completed | |
| VH000140 | OR000133 | cancelled | |
| VH000141 | OR000134 | completed | |
| VH000142 | OR000135 | arrived_to_sup | |
| VH000143 | OR000136 | arrived_to_sup | |
| VH000144 | OR000137 | completed | |
+---------------+----------+----------------+--+
And i want get list of all drivers that ,
attendance.atdnc_date <> '2018-11-17' AND attendance.isleave<>1
OR that attendance row is null according to driver
AND
vehicle_status.current_status = 'cancelled' OR vehicle_status.current_status = 'completed'
OR, OR that vehicle_status row is null according to driver
here i tried queries:
1)
SELECT * FROM
(SELECT driver.* FROM driver LEFT JOIN (SELECT vehicle.* FROM vehicle_status INNER JOIN vehicle ON vehicle_status.vehicle_id=vehicle.vehicle_id WHERE current_status <> 'completed' AND current_status <> 'cancelled') k
ON driver.driver_id=k.driver_id INNER JOIN(SELECT driver.driver_id FROM driver LEFT JOIN (SELECT emp_id FROM `attendance` WHERE isleave=1 AND atdnc_date='2018-11-17') as x ON x.emp_id=driver.driver_id WHERE x.emp_id IS NULL ) d ON driver.driver_id=d.driver_id ) as x
2)
SELECT * FROM driver
LEFT JOIN vehicle ON driver.driver_id=vehicle.driver_id
LEFT JOIN vehicle_status ON vehicle.vehicle_id=vehicle_status.vehicle_id
LEFT JOIN attendance ON driver.driver_id=attendance.emp_id WHERE (vehicle_status.current_status ="completed" OR vehicle_status.current_status ="cancelled" OR vehicle_status.current_status IS NULL)
AND ((attendance.atdnc_date = '2018-11-17' AND attendance.isleave = 0) OR (attendance.atdnc_date IS NULL) ) GROUP BY driver.driver_id
3)
SELECT * FROM (SELECT driver.* FROM driver LEFT JOIN (SELECT vehicle.* FROM vehicle_status INNER JOIN vehicle ON vehicle_status.vehicle_id=vehicle.vehicle_id WHERE current_status <> 'completed' AND current_status <> 'cancelled') k ON driver.driver_id=k.driver_id WHERE k.driver_id IS NULL) x
INNER JOIN (SELECT driver.* FROM driver LEFT JOIN attendance ON driver.driver_id=attendance.emp_id WHERE ((attendance.atdnc_date <> '2018-11-17' AND attendance.isleave <> 0) OR (attendance.atdnc_date IS NULL) )) b ON x.driver_id=b.driver_id
i think this working.. but can be slow when using with large set of data.
4)
select b.* FROM (SELECT driver.* FROM driver LEFT JOIN (SELECT vehicle.* FROM vehicle_status INNER JOIN vehicle ON vehicle_status.vehicle_id=vehicle.vehicle_id WHERE current_status <> 'completed' AND current_status <> 'cancelled') k
ON driver.driver_id=k.driver_id WHERE k.driver_id IS NULL) b
LEFT JOIN (SELECT driver.driver_id FROM attendance INNER JOIN driver ON attendance.emp_id=driver.driver_id WHERE attendance.atdnc_date='2018-11-17' AND attendance.isleave='1') k ON b.driver_id=k.driver_id
WHERE k.driver_id IS NULL AND status ='1'
these queries sometimes work. but when attendance, and vehicle_status have multiple rows with driver, these quires fail. and show multiple drivers or show drivers that not meet the conditions :(
expected result
+-----+-----------+-------------+--+
| id | driver_id | driver_lice | |
+-----+-----------+-------------+--+
| 132 | 450 | B548093 | |
| 133 | 6 | B1394007 | |
| 136 | 71 | B2442775 | |
| 137 | 88881 | B2056537 | |
| 138 | 729 | B8265 | |
| 140 | 748 | B2228230 | |
| 138 | 1024| B204085 | |
+-----+-----------+-------------+--+
There is two table in database.
== tblOrder ==
OrderId
Customer
== tblOrderItem ==
OrderItemId
OrderId
ItemId
Qty
Price
My Query :-
SELECT o.OrderId,o.Customer,oi.ItemId,oi.Qty,oi.Price
FROM tblorder o
JOIN tblorderitem oi ON oi.OrderId=o.OrderId
Result :-
+---------+----------+--------+-----+-------+
| OrderId | Customer | ItemId | Qty | Price |
+---------+----------+--------+-----+-------+
| 1001 | john day | 501 | 1 | 10 |
| 1001 | john day | 502 | 2 | 9 |
| 1002 | amy gill | 201 | 5 | 2 |
| 1003 | hardcaur | 501 | 1 | 10 |
| 1003 | hardcaur | 509 | 2 | 5 |
| 1003 | hardcaur | 201 | 2 | 2 |
+---------+----------+--------+-----+-------+
I want to generate SNO and SNOI(Temp Serial No) in select statement so that result will be like this :-
+------+---------+----------+------+--------+-----+-------+
| SNO | OrderId | Customer | SNOI | ItemId | Qty | Price |
+------+---------+----------+------+--------+-----+-------+
| 1 | 1001 | john day | 1 | 501 | 1 | 10 |
| 1 | 1001 | john day | 2 | 502 | 2 | 9 |
| 2 | 1002 | amy gill | 1 | 201 | 5 | 2 |
| 3 | 1003 | hardcaur | 1 | 501 | 1 | 10 |
| 3 | 1003 | hardcaur | 2 | 509 | 2 | 5 |
| 3 | 1003 | hardcaur | 3 | 201 | 2 | 2 |
What will be my query?
You can use variables.
set #ord ='';
set #val1 =1;
set #val2 =0;
select SR_No_1, OrderId, Customer, SR_No_2, ItemId, Qty, Price
from
(
SELECT t.*,
#val1 := if(#ord=OrderId, #val1+1, 1) as SR_No_2,
#val2 := if(#ord=OrderId,#val2, #val2+1) as SR_No_1,
#ord := OrderId
FROM table1 t
) t
ORDER BY orderId asc;
Result:
+---------+---------+----------+---------+--------+-----+-------+
| SR_No_1 | OrderId | Customer | SR_No_2 | ItemId | Qty | Price |
+---------+---------+----------+---------+--------+-----+-------+
| 1 | 1001 | john day | 1 | 501 | 1 | 10 |
| 1 | 1001 | john day | 2 | 502 | 2 | 9 |
| 2 | 1002 | amy gill | 1 | 201 | 5 | 2 |
| 3 | 1003 | hardcaur | 1 | 501 | 1 | 10 |
| 3 | 1003 | hardcaur | 2 | 509 | 2 | 5 |
| 3 | 1003 | hardcaur | 3 | 201 | 2 | 2 |
+---------+---------+----------+---------+--------+-----+-------+
DEMO
P.S. Kindly note that for demonstration purpose i have inserted data to one table. you can modify the query by introducing the join between 2 tables
I am very new to MySQL and also this is my first question if there is any mistakes please forgive me.
I have 6 tables i want to take report using this 6 tables. One of my report to access all the 6 tables and print the data as per the Query we write.Now the problem is i have write my query but this will return lot of duplicate data.How to avoid that guide me please.
Demo Data:
add_employees
-------------------
| eid | name |
-------------------
| 1 | Mohanraj |
-------------------
| 2 | pradeep |
-------------------
| 3 | kumar |
-------------------
| 4 | Murali |
-------------------
add_vehicle
---------------------
| vid | vnumber |
---------------------
| 1 | TN22BQ6226 |
---------------------
| 2 | TN37CM9014 |
---------------------
| 3 | TN38BR9217 |
---------------------
| 4 | TN38BT5680 |
---------------------
third_table
--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
| tid | vid | emp_id | entry_date | pick_place | start_time | drop_place | stop_time | pickupkm | drops | type_of_trip | travelkm | tamt | dates |
----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
| 1 | TN22BQ6226 | 3 | 5-5-2017 | Airport | 03:15 am | ESI | 06:30 am | 10500 | 20500 | Cash | 10000 | 5000 |2017-05-05 |
| 2 | TN22BQ6226 | 3 | 6-5-2017 | Hopes | 09:44 am | ESI | 12:30 pm | 12500 | 2500 | Cash | 1250 | 3500 |2017-05-06 |
| 3 | TN22BQ6226 | 3 | 5-5-2017 | Place1 | 03:15 pm | Place2 | 06:30 pm | 1500 | 1800 | Cash | 300 | 1500 |2017-05-05 |
fourth_table
---------------------------------------------------------------------------------------------------------------------------------------------------
| fid | vid | emp_id | expcal | exp1 | exp2 | exp3 | exp4 | exp5 | exp_amt | exp_desc | dates |
----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
| 1 | TN22BQ6226 | 3 | 100 | | | 100 | 100 | | | | 2017-05-05 |
| 2 | TN22BQ6226 | 3 | 50 | | | 50 | 50 | | | | 2017-05-06 |
five_table
| fi_id | vid | emp_id | totkm | totamt | expenses | today_balance | handover_amt | balance_amt | handover_to | plstatus | entry_date | entry_time |
----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
| 1 | TN22BQ6226 | 3 | 10000 | 5000 | 100 | 4900 | 4000 | 400 | ram | PROFIT | 2017-05-05 | 04:35:21 |
| 2 | TN22BQ6226 | 3 | 1250 | 3500 | 100 | 3400 | 3000 | 200 | raj | PROFIT | 2017-05-06 | 04:36:58 |
shift
---------------------------------------------------------------------------------------------------------------------------------------------------
| sid | emp_id | vid | opeing_km | opeing_cash | closing_km | closing_cash | opeing_date | opeing_time | closing_date | closing_time | status |
----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
| 1 | 3 | TN22BQ6226 | 0 | 0 | 10000 | 0 | 2017-05-05 | 04:32:17 | 2017-05-05 | 04:35:21 | 1 |
| 2 | 3 | TN22BQ6226 | 20500 | 5000 | 1250 | 0 | 2017-05-06 | 04:34:55 | 2017-05-06 | 04:36:58 | 1 |
And i try this query i get wrong out,
SELECT a.vnumber
, a.vname
, b.eid
, b.name
, b.mobile
, c.tid
, c.vid
, c.emp_id
, c.pick_place
, c.start_time
, c.drop_place
, c.stop_time
, c.pickupkm
, c.drops
, c.type_of_trip
, c.travelkm
, c.tamt
, c.dates
, d.vid
, d.emp_id
, d.expcal
, d.exp1
, d.exp2
, d.exp3
, d.exp4
, d.exp5
, d.expamt
, d.expdesc
, d.dates
, e.emp_id
, e.vid
, e.opeing_km
, e.opeing_cash
, e.closing_km
, e.closing_cash
, e.opeing_date
, e.opeing_time
, e.closing_date
, e.closing_time
, f.vid
, f.emp_id
, f.totkm
, f.totamt
, f.expenses
, f.handover_amt
, f.balance_amt
, f.handover_to
, f.plstatus
, f.entry_date
FROM add_vehicle a
JOIN third_table c
ON a.vnumber = c.vid
JOIN add_employees b
ON b.eid = c.emp_id
JOIN fourth_table d
ON a.vnumber = d.vid
JOIN shift e
ON b.eid = e.emp_id
JOIN five_table f
ON a.vnumber = f.vid
, (SELECT #rownum := 0) r
WHERE c.type_of_trip IS NOT NULL
AND c.dates BETWEEN '2017-05-05' AND '2017-06-05'
AND d.dates BETWEEN '2017-05-05' AND '2017-06-05'
AND f.entry_date BETWEEN '2017-05-05' AND '2017-06-05'
AND c.vid = 'TN22BQ6226'
AND f.vid = 'TN22BQ6226'
AND e.vid = 'TN22BQ6226'
GROUP
BY c.tid
ORDER
BY c.dates
, f.entry_date DESC
i get fourth_table and five_table values are repeated like this.
one and two
I want the output like this,
| vnumber | vname | eid | name | tid | vid | emp_id | pick_place | start_time | drop_place | stop_time | pickup_km | drops | type_of_trip | travel_km | tamt | dates | vid | emp_id |expcal |exp1 | exp2 | exp3 | exp4 | exp5 | expamt| expdesc | dates | emp_id | vid | opening_km | opening_cash | closing_km | closing_cash | opeing_date | opeing_time | closing_date | closing_time | vid | emp_id | totkm | totamt | expenses | handover_amt | balance_amt| handover_to | plstatus | entry_date |
-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
| TN22BQ6226 | Mahindra Logan | 3 | kumar | 1 | TN22BQ6226 | 3 | Airport | 03:15 am | ESI | 06:30 am | 10500 | 20500 | Cash | 10000 | 5000| 2017-05-05 | TN22BQ6226 | 3 | 100 | | | 100 | 100 | | 100 | |2017-05-05 | 3 | TN22BQ6226 | 0 | 0 | 10000 | 0 | 2017-05-05 | 04:32:17 | 2017-05-05 | 04:35:21 | TN22BQ6226 | 3 | 10000| 5000 | 100 | 4000 | 400 | ram | PROFIT | 2017-05-05 |
| TN22BQ6226 | Mahindra Logan | 3 | kumar | 2 | TN22BQ6226 | 3 | Hopes | 09:44 am | ESI | 12:01 pm | 1250 | 2500 | Cash | 1250 | 3500| 2017-05-06 | TN22BQ6226 | 3 | 50 | | | 50 | 50 | | 50 | |2017-05-06 | 3 | TN22BQ6226 | 20500 | 5000 | 1250 | 0 | 2017-05-06 | 04:34:55 | 2017-05-06 | 04:36:58 | TN22BQ6226 | 3 | 1250| 3500 | 100 | 3400 | 200 | raj | PROFIT | 2017-05-06 |
| TN22BQ6226 | Mahindra Logan | 3 | kumar | 3 | TN22BQ6226 | 3 | place one | 03:15 pm | place two | 06:15 pm | 1500 | 1800 | Cash | 300 | 1500| 2017-05-05 | TN22BQ6226 | 3 | 100 | | | 100 | 100 | | 100 | |2017-05-05 | 3 | TN22BQ6226 | 0 | 0 | 10000 | 0 | 2017-05-05 | 04:32:17 | 2017-05-05 | 04:35:21 | TN22BQ6226 | 3 | 10000| 5000 | 100 | 4000 | 400 | ram | PROFIT | 2017-05-05 |
sorry for big question.
Too long for a comment...
Let's start by simplifying the problem as follows:
SELECT a.vid
, d.emp_id
, d.expcal
, f.entry_date
, f.entry_time
FROM add_vehicle a
JOIN fourth_table d
ON d.vid = a.vnumber
JOIN five_table f
ON f.vid = a.vnumber
vid emp_id expcal entry_date entry_time
1 3 100 2017-05-05 04:35:21
1 3 50 2017-05-05 04:35:21
1 3 100 2017-05-06 04:36:58
1 3 50 2017-05-06 04:36:58
http://sqlfiddle.com/#!9/62f3e5/21
Considering only the columns provided above, how does this differ from the desired result?
Incidentally - always store dates and times as a single entity
I have the following tables:
clients:
| id | name | code | zone |
--------------------------------
| 1 | client 1 | a1b1 | zone1|
| 2 | client 2 | a2b2 | zone2|
contacts:
| id_contact | first_name | last_name |
----------------------------------------
| 11 | first1 | last1 |
| 22 | first2 | last2 |
| 33 | first3 | last3 |
| 44 | first4 | last4 |
client_contacts:
| id_client | id_contact |
--------------------------
| 1 | 11 |
| 1 | 22 |
| 1 | 33 |
| 2 | 11 |
| 2 | 44 |
offers:
| id_offer | id_client | value |
--------------------------
| 111 | 1 | 100 |
| 222 | 1 | 200 |
| 333 | 1 | 300 |
| 444 | 2 | 400 |
I would like through a optimal select to obtain:
| id_client | name | code | zone | contacts_pers | total_offer_value |
----------------------------------------------------------------------------
| 1 | client 1 | a1b1 | zone1 | first1 last1; | 600 |
first2 last2;
first3 last3;
| 2 | client 2 | a2b2 | zone2 | first1 last1; | 400 |
first4 last4;
I know how to get the desired result with "group_concat" and stored procedures for "total_offer_value". But how to get the desired result from a single efficient select?
SELECT c.id, c.name, c.code, c.zone, GROUP_CONCAT(DISTINCT CONCAT(co.first_name, " ", c.last_name) SEPARATOR ";") AS contact_pers, func_total_offer_value(c.id) AS total_offer_value
FROM clients c
LEFT OUTER JOIN (client_contacts cc, contacts co) ON ( c.id = cc.id_client AND cc.id_contact = co.id_contact )
GROUP BY c.id