Here is my SQL query:
SELECT (
SELECT COUNT(appointment_doctor) FROM appointment
JOIN doctor ON appointment.appointment_doctor = doctor.doctor_id
WHERE appointment_doctor = doctor_id
) * doctor_payment_rate AS fees FROM doctor
JOIN doctor_payment ON doctor.doctor_id=doctor_payment.doctor_payment_doctor
Result:
fees 500 1000 615 11615 1160 615 1610 285 660 220 3835 215 615 555 555 1065 1110 620 445 220 660 625 60 1715 615 10 115 615 60 625 60 330 615 625 720 625 615 670 615 500 615 500
I want the sum of all these value as total_fee.
Try this query:
SELECT SUM (
(SELECT COUNT(appointment_doctor) FROM appointment
JOIN doctor ON appointment.appointment_doctor = doctor.doctor_id
WHERE appointment_doctor = doctor_id) * doctor_payment_rate))
AS total_fee
FROM doctor
JOIN doctor_payment
ON doctor.doctor_id=doctor_payment.doctor_payment_doctor
Use mysql sum function SUM() function returns the total sum of a numeric column
SELECT SUM (
SELECT (
SELECT COUNT(appointment_doctor) FROM appointment
JOIN doctor ON appointment.appointment_doctor = doctor.doctor_id
WHERE appointment_doctor = doctor_id
)
select
sum(select
count(appointment_doctor)
from
appointment
join
doctor
on appointment.appointment_doctor= doctor.doctor_id
where
appointment_doctor=doctor_id)*doctor_payment_rate as 'Total fee'
from
doctor
join
doctor_payment
on doctor.doctor_id=doctor_payment.doctor_payment_doctor
Related
TABLE ONE DATA - po_header
PO_ID PROJ_ID SUP_ID TOT_SUM SUBM_DATE
32 5555 AccAYOU 99.00 2016-11-29
33 5555 AccAYOU 990.00 2016-11-29
34 25412 AccAYOU 248778595.08 2016-11-30
TABLE TWO DATA - po_details
PO_ID amount
32 110.00
33 1500000.00
34 565079266.00
34 1.00
How can I Run the following SQL ?
INSERT INTO po_header (TOT_SUM) VALUES (SELECT SUM(amount)
FROM po_details WHERE PO_ID = '34') WHERE PO_ID ='34';
Even it's un correct syntax but this is what you want:
UPDATE po_header h
SET TOT_SUM = (Select sum(amount) From po_details d Where d.po_id = h.po_id)
UPDATE po_header SET TOT_SUM = (SELECT SUM(amount) FROM po_details WHERE PO_ID='34') WHERE PO_ID='34';
i am looking to find the avg cost for the total cost of each order but my grouping function is invalid
SELECT AVG(SUM(quantityOrdered*priceEach)) AS total
FROM orderdetails od
GROUP BY orderNumber
below is a snippet of my database
orderNumber productCode quantityOrdered priceEach orderLineNumber
10100 S24_3969 49 35.29 1
10101 S18_2325 25 108.06 4
10101 S18_2795 26 167.06 1
10101 S24_1937 45 32.53 3
10101 S24_2022 46 44.35 2
10102 S18_1342 39 95.55 2
10102 S18_1367 41 43.13 1
10103 S10_1949 26 214.3 11
10103 S10_4962 42 119.67 4
10103 S12_1666 27 121.64 8
10103 S18_1097 35 94.5 10
10103 S18_2432 22 58.34 2
10103 S18_2949 27 92.19 12
10103 S18_2957 35 61.84 14
10103 S18_3136 25 86.92 13
10103 S18_3320 46 86.31 16
It's invalid use cannot use two aggregate functions together
SELECT SUM(quantityOrdered*priceEach) AS total
FROM orderdetails od
GROUP BY orderNumber
Having SUM(quantityOrdered*priceEach)>(SELECT AVG(quantityOrdered*priceEach) FROM orderdetails)
Just calculate the average using sum() divided by a number:
SELECT SUM(quantityOrdered*priceEach) / COUNT(DISTINCT orderNumber) AS total
FROM orderdetails od ;
You do it in two steps.
SQL Fiddle Demo
Calculate the Total of each order.
SELECT orderNumber, SUM(quantityOrdered*priceEach) AS total
FROM orderdetails od
GROUP BY orderNumber
Calculate the Average between all the totals
SELECT AVG(total)
FROM ( SELECT orderNumber, SUM(quantityOrdered*priceEach) AS total
FROM orderdetails od
GROUP BY orderNumber
) T
EDIT: I miss the last step after checking Ritesh answer.
SELECT o.*, t.global_avg
FROM (SELECT orderNumber, SUM(quantityOrdered*priceEach) AS order_total
FROM orderdetails od
GROUP BY orderNumber) o
CROSS JOIN
(SELECT AVG(total) global_avg
FROM ( SELECT orderNumber, SUM(quantityOrdered*priceEach) AS total
FROM orderdetails od
GROUP BY orderNumber
) t
) t
WHERE o.order_total > t.global_avg;
OUTPUT:
Here I want to get the previous row balance value in to my field.
The last id of customer_id '16' of balance is 200, but I want to get the previous ids value in to the field and this is my table
id order_id customer_id amount actual_amount paid_amount balance type
25 11 16 100.00 50.00 50.00 Cash
26 12 16 200.00 100.00 100.00 Cash
27 13 16 150.00 100.00 50.00 Cash
28 14 16 300.00 250.00 50.00 Cash
29 14 16 170.00 100.00 70.00 Cash
30 15 16 100 170.00 70.00 100.00 Cash
31 16 16 400 500.00 300.00 200.00 Cash
this is my model
public function order_balance($order_code)
{
$this->db->join('services','payment.customer_id=services.customer_id','left');
$this->db->select('payment.*,payment.balance,payment.actual_amount,payment.customer_id');
$this->db->order_by('payment.id','desc');
$query = $this->db->get_where('payment',array('code' => $order_code));
return $query->previous_row();
}
This is my control:
public function final_payment($order_code)
{
$data['active_mn']='';
$data['result'] = $this->Account_model->order_balance($order_code);
$this->load->view('final_payment',$data);
}
My services table looks like this:
id code customer_id particulars
11 ORD00011 16 phone
12 ORD00012 16 gdf
13 ORD00013 16 ghgfh
14 ORD00014 16 tv
15 ORD00015 16 ghfg
16 ORD00016 16 tv
17 ORD00017 16 gdfg
18 ORD00018 16 desk
19 ORD00019 16 gdf
My result should be like this:
id order_id customer_id amount actual_amount paid_amount balance type
31 16 16 400 500.00 300.00 100.00 Cash
One option here would be to do a self join with the payment table, joining on the customer_id and order_id columns. You can add another join function call and then use the result in your select. The following solution uses a raw query since Codeigniter does not seem to tolerate arithmetic in the join condition:
public function order_balance($order_code)
{
$this->db->query("SELECT p1.*, p2.balance AS previous_balance FROM payment p1 INNER JOIN payment p2 ON p1.order_id = p2.order_id + 1 AND p1.customer_id = p2.customer_id LEFT JOIN services s ON p1.customer_id = s.customer_id ORDER BY p1.id DESC");
$query = $this->db->get_where('p1', array('code' => $order_code));
return $query->previous_row();
}
The query has the form:
SELECT p1.*, p2.balance AS previous_balance
FROM payment p1 INNER JOIN payment p2
ON p1.order_id = p2.order_id + 1 AND
p1.customer_id = p2.customer_id
LEFT JOIN services s
ON p1.customer_id = s.customer_id
ORDER BY p1.id DESC
There are lots of ways of writng this. Here's one way. I don't know codeigniter, so you'll have to reverse engineer it...
SELECT a.id
, a.order_id
, a.customer_id
, a.amount
, a.actual_amount
, a.paid_amount
, b.balance
, a.type
FROM
( SELECT x.*
, MAX(y.id) prev
FROM payment x
JOIN payment y
ON y.id < x.id
AND y.customer_id = x.customer_id
GROUP
BY x.id
) a
JOIN payment b
ON b.id = a.prev
ORDER
BY id DESC LIMIT 1;
I have the 2 following select in Mysql:
1st select:
(SELECT DISTINCT `Online_playerdatabase_v2`.`Player`,
Online_playerdatabase_v2.First_Deposit_Date As FirstDep,
TRUNCATE(Online_playerdatabase_v2.Balance,2) as Balance
FROM Online_playerdatabase_v2
WHERE `Online_playerdatabase_v2`.`Player`<>'Player'
ORDER BY `Online_playerdatabase_v2`.`Balance` DESC;
2d select:
SELECT DISTINCT(Online_customer_activity_v2.Customers) as Player,
max(Online_customer_activity_v2.Date) as LastAction
FROM Online_customer_activity_v2
WHERE `Online_customer_activity_v2`.`Total_Bets`>0
Group by Online_customer_activity_v2.Customers
Output Select 1
Player FirstDep Balance
Ray 2014-10-19 9100.00
Ramzi 2014-11-02 9.61
tareq 2014-11-06 805.00
STAN 2014-10-17 7.50
Bill 2014-03-25 68.40
karam 2014-11-16 676.50
Abdul 2014-11-13 650.00
Renaud 2014-03-12 507.00
John 2014-11-22 500.00
Output select 2
Player LastAction
John 2015-11-13
Bill 2014-12-14
Renaud 2015-03-14
Abdul 2015-11-16
Ray 2015-11-22
STAN 2015-10-29
Ramzi 2015-11-10
Tarek 2015-05-10
karam 2014-12-10
Abdul 2015-02-10
Desired Output, a join on both Select that adds following calculations:
active days (FirstDep-LastAction) and Days_last_Visit (CurrentDate - Last Action)
Summarized in following table:
Player FirstDep Balance LastAction Active_days Days_last_Visit
Ray 2014-10-19 9100.00 2015-11-22 399 1
Ramzi 2014-11-02 9.61 2015-11-10 373 13
tareq 2014-11-06 805.00 2015-05-10 185 197
STAN 2014-10-17 7.50 2015-10-29 377 25
Bill 2014-03-25 68.40 2014-12-14 264 344
karam 2014-11-16 676.50 2014-12-10 24 348
Abdul 2014-11-13 650.00 2015-02-10 89 286
Renaud 2014-03-12 507.00 2015-03-14 367 254
John 2014-11-22 500.00 2015-11-13 356 10
Your help is greatly appreciated!
Thanks
The following query should give the result you want. I will add that I joined the two tables from your intermediate queries above using the Player field. This is not a very robust way to join, because the name may not be unique among all players in the table. A better way to join would be to use a unique identifier of some sort.
SELECT t1.Player, t1.FirstDep, t1.Balance, t2.LastAction,
DATEDIFF(t2.LastAction, t1.FirstDep) AS Active_days,
DATEDIFF(NOW(), t2.LastAction) AS Days_last_Visit
FROM
(
SELECT DISTINCT `Online_playerdatabase_v2`.`Player`,
Online_playerdatabase_v2.First_Deposit_Date AS FirstDep,
TRUNCATE(Online_playerdatabase_v2.Balance,2) AS Balance
FROM Online_playerdatabase_v2
WHERE `Online_playerdatabase_v2`.`Player` <> 'Player'
) t1
INNER JOIN
(
SELECT DISTINCT(Online_customer_activity_v2.Customers) AS Player,
MAX(Online_customer_activity_v2.Date) AS LastAction
FROM Online_customer_activity_v2
WHERE `Online_customer_activity_v2`.`Total_Bets` > 0
GROUP BY Online_customer_activity_v2.Customers
) t2
ON t1.`Player` = t2.`Player`
You need to join the 2 selects as subqueries in a 3rd select using the player field. The Active_days and Days_last_Visit fields can be calculated using the DateDiff() function.
SELECT *
,DateDiff(t2.LastAction,t1.FirstDep) AS Active_days
,DateDiff(CURDATE(), t2.LastAction) AS Days_last_Visit
FROM
(SELECT DISTINCT `Online_playerdatabase_v2`.`Player`,
Online_playerdatabase_v2.First_Deposit_Date As FirstDep,
TRUNCATE(Online_playerdatabase_v2.Balance,2) as Balance
FROM Online_playerdatabase_v2
WHERE `Online_playerdatabase_v2`.`Player`<>'Player'
ORDER BY `Online_playerdatabase_v2`.`Balance` DESC) t1
LEFT JOIN
(SELECT DISTINCT(Online_customer_activity_v2.Customers) as Player,
max(Online_customer_activity_v2.Date) as LastAction
FROM Online_customer_activity_v2
WHERE `Online_customer_activity_v2`.`Total_Bets`>0
Group by Online_customer_activity_v2.Customers) t2
ON t1.Player=t2.Player
You have to consider, however, how you join the 2 datasets. I used left join, since the players table will probably hold all players, but you may want to go for inner join or simulate a full outer join depending your requirements and your data.
I have 2 tables and is as follows
select Event_ID,[Gross Salary] from tblEar where Event_ID=14
Result:
Event_ID Gross Salary
14 56128
14 51984
14 42028
And:
select EventId, [Order Date],Amount from tblBudget where EventId=14
Result:
EventId Order Date Amount
14 10/10/2011 20000
14 10/10/2011 20000
14 20/03/2012 2500
14 02/04/2012 -50000
if i write a join statment on these 2 tables to get it is retrieving duplicate records.I used Distinct But no Positive Result.
select DISTINCT tba.[Order Date],ISNULL(tba.Amount,0),ISNULL(te.[Gross Salary],0) from tblBudget tba
join
tblEar te on tba.EventId=te.Event_ID where tba.EventId=14
I got the following ans:
Order Date (No column name) (No column name)
2011-10-10 20000.00 42028.00
2011-10-10 20000.00 51984.00
2011-10-10 20000.00 56128.00
2012-03-20 2500.00 42028.00
2012-03-20 2500.00 51984.00
2012-03-20 2500.00 56128.00
2012-04-02 -50000.00 42028.00
2012-04-02 -50000.00 51984.00
2012-04-02 -50000.00 56128.00
Can any one show the Way to get Accuarate data
I guess you want to group the data and aggregate the amounts:
SELECT tba.[Order Date], SUM(tba.Amount), SUM(te.[Gross Salary])
FROM tblBudget tba
JOIN tblEar te on tba.EventId = te.Event_ID
WHERE tba.EventId = 14
GROUP BY tba.[Order Date]