Getting SUM of two distinct procedure - mysql

I have two procedures that calculate two data. Now i would like to get the sum of those two output. Is it possible in sql?
ex:
Select bio.*,
SUM (cnt_report,cnt_report_2) as TOTAL --CAN I DO THIS?
cnt_report + cnt_report_2 as Total --This doesn't seem to work
from biographical bio
LEFT JOIN (
SELECT cr.id, COUNT (*) AS cnt_report
FROM report cr
GROUP BY cr.id
) cr11 ON bio.id = cr11.id
LEFT JOIN (
SELECT cr.id2,
COUNT (*) AS cnt_report_2
FROM report cr
GROUP BY cr.id2
) cr11 ON bio.id = cr11.id2

Your Sum function will work with a little tweak, but you will need to add a GROUP BY line at the end. Also, you seem to have named your two sub queries with the same alias. Try this:
Select bio.*,
SUM (cr11_1.cnt_report + cr11_2.cnt_report_2) as TOTAL
from biographical bio
LEFT JOIN (
SELECT cr.id, COUNT (*) AS cnt_report
FROM report cr
GROUP BY cr.id
) cr11_1 ON bio.id = cr11_1.id
LEFT JOIN (
SELECT cr.id2,
COUNT (*) AS cnt_report_2
FROM report cr
GROUP BY cr.id2
) cr11_2 ON bio.id = cr11_2.id2
GROUP BY bio.*

You can not do this:
sum(something, something_else)
but you can do this:
sum(something + something_else)
You can figure out how to implement that into your query. Remember that if you are selecting other fields along with an aggregate, you need a group by clause.

Related

Mysql - I have 3 unique tables, need a hint on getting the details with the count

I have 3 tables which are interconnected and i want to select columns from two tables and counts from table 3. If anyone is aware on this, any hint would be appreciated.
Below is the sql i tried, but the count is getting repeated
SELECT distinct p.p_id, p.p_f6, p.p_l4,m.m_id, (
SELECT COUNT(*)
FROM ttokens t where t.pdetail_id = p.pdetail_id
) AS token_count
FROM tparking p,ttokens t LEFT join ttokens_md m ON t.trefn_id = m.trefn_id
WHERE t.pdetail_id = p.pdetail_id
You can try to use JOIN with subquery to get your count instead of selcet subquery.
SELECT p.p_id, p.p_f6, p.p_l4,m.m_id,t.cnt
FROM tparking p
JOIN (
SELECT pdetail_id,COUNT(*) cnt
FROM ttokens
GROUP BY pdetail_id
) t ON t.pdetail_id = p.pdetail_id
LEFT join ttokens_md m ON t.trefn_id = m.trefn_id
Note
I would use JOIN instead of , comma with where condition to connect two tables,, is an old style.

what will be correct MySql query?

I am performing the following query having some syntax error:
SELECT count (tbl_staff.staff_id as staff_number),SELECT count (tbl_client.client_id as client_number),SELECT count (tbl_appointment.appt_id as appt_number),SELECT count (tbl_subscription.subscription_id as subscription_number)
FROM tbl_subscription
LEFT JOIN tbl_staff
ON (
tbl_staff.merchant_id = tbl_subscription.merchant_id)
LEFT JOIN tbl_appointment
ON (
tbl_appointment.merchant_id = tbl_subscription.merchant_id)
LEFT JOIN tbl_client
ON (
tbl_client.merchant_id = tbl_subscription.merchant_id)
WHERE tbl_subscription.subscription_id=1;
I want get the count of staff_id, client_d, appointment_id on particular Subscription_id.
Your select list is close, but has a few mistakes. Namely, you only need a single SELECT in your query (not one per field) and the "as ..." descriptor belongs outside the parenthesis.
So this part of the query
SELECT count (tbl_staff.staff_id as staff_number),
SELECT count (tbl_client.client_id as client_number),
SELECT count (tbl_appointment.appt_id as appt_number),
SELECT count (tbl_subscription.subscription_id as subscription_number)
FROM tbl_subscription
would become
SELECT count (tbl_staff.staff_id) as staff_number,
count (tbl_client.client_id) as client_number,
count (tbl_appointment.appt_id) as appt_number,
count (tbl_subscription.subscription_id) as subscription_number
FROM tbl_subscription

MySQL Query, multiple counts and sums

I have a MySQL query that outputs to a php table but I'm having issues in joining two tables that both use a COUNT:
$query = "SELECT mqe.registration,
COUNT(*) AS numberofenqs,
COUNT(DISTINCT ucv.ip) AS unique_views,
SUM(ucv.views) AS total_views
FROM main_quick_enquiries AS mqe
LEFT OUTER JOIN used_car_views AS ucv
ON ucv.numberplate = mqe.registration
WHERE mqe.registration IS NOT NULL
GROUP BY mqe.registration ORDER BY numberofenqs DESC";
The query runs, but the number within the numberofenqs column is always wrong as i know from performing that query on its own that it comes in with the correct result:
SELECT registration, COUNT(*) AS numberofenqs FROM main_quick_enquiries GROUP BY registration ORDER BY numberofenqs DESC
Why is the COUNT(*) not working correctly in top query code and where is it getting the figures from?
it could be because of LEFT OUTER JOIN ...
Try to run this:
SELECT registration
, count(*)
FROM main_quick_enquiries
GROUP BY registration
and compare it with this result
SELECT mqe.registration
, count(*)
FROM main_quick_enquiries mqe
LEFT OUTER JOIN used_car_views ucv
ON ucv.numberplate = mqe.registration
GROUP BY mqe.registration
There could be a problem :) in duplicity rows... try to find one specific registration number, and compare the details of both query
SELECT *
FROM main_quick_enquiries
WHERE registration = XXXX
+
SELECT *
FROM main_quick_enquiries mqe
LEFT OUTER JOIN used_car_views ucv
ON ucv.numberplate = mqe.registration
WHERE registration = XXXX
you should see the diffs
Thanks All, but I think I've nailed it with COUNT(DISTINCT mqe.id) instead of COUNT(*).

Combining an INNER JOIN query with Count of different values

I am trying to link two tables with similar column. I need to find out how many values differ from table1.column1 and table 2.column1:
My current query:
SELECT i10_descr.i10_code, gems_pcsi9.i10_code
FROM i10_descr INNER JOIN gems_pcsi9 ON i10_descr.i10_code = gems_pcsi9.i10_code
ORDER BY i10_descr.i10_code;
I know this query shows the matching codes of each table: I cannot figure out how to COUNT the missing/different codes in the tables.
Also, I have to compute the ratio of codes.
Any help, tips, or direction is much appreciated.
Thanks
You could use an anti-join pattern to get a list of i10_code that exist in one table, but not the other. For example:
SELECT i.i10_code
FROM i10_descr i
LEFT
JOIN gems_pcsi9 g
ON g.i10_code = i.i10_code
WHERE g.i10_code IS NULL
ORDER BY i.i10_code
If you just want a count, you could use COUNT(i.i10_code) and/or COUNT(DISINCT i.i10_code) in the SELECT list and remove the ORDER BY clause.
To get the i10_code in the gems table that aren't in the i10 table, you'd do the same thing but invert the query so that gems is the "driving" table. e.g.
SELECT COUNT(DISTINCT g.i10_code) AS cnt_diff
FROM gems_pcsi9 g
LEFT
JOIN i10_descr i
ON i.i10_code = g.i10_code
WHERE i.i10_code IS NULL
If you want to combine the number of differences, you can combine the two queries by making them inline views:
SELECT d.cnt_diff + e.cnt_diff AS total_diff
FROM (
SELECT COUNT(DISTINCT g.i10_code) AS cnt_diff
FROM gems_pcsi9 g
LEFT
JOIN i10_descr i
ON i.i10_code = g.i10_code
WHERE i.i10_code IS NULL
) d
CROSS
JOIN (
SELECT COUNT(DISTINCT i.i10_code) AS cnt_diff
FROM i10_descr i
LEFT
JOIN gems_pcsi9 g
ON g.i10_code = i.i10_code
WHERE g.i10_code IS NULL
) e
NOTE: the COUNT aggregate will omit NULL values. The query would need to be tweaked if you also wanted to "count" rows that had NULL values for i10_code. You'd use COUNT(DISTINCT ) if you want just a number of distinct values that are different. A COUNT() would give a number of rows. These two results would be different if you had multiple rows with the same i10_code value.
To get a "ratio" of codes, assuming that at this point, the "differences" don't matter, you get a count of codes from each table. The queries to do that could be used inline views:
SELECT d.cnt / e.cnt AS ratio_cnt_g_over_cnt_i
, d.cnt AS cnt_g
, e.cnt AS cnt_i
FROM (
SELECT COUNT(DISTINCT g.i10_code) AS cnt
FROM gems_pcsi9 g
) d
CROSS
JOIN (
SELECT COUNT(DISTINCT i.i10_code) AS cnt
FROM i10_descr i
) e
An alternative method is to use union all with aggregation:
select in_i10descr, in_gems_pcsi9, count(*) as numcodes
from (select code, max(in_i10descr) as in_i10descr, max(in_gems_pcsi9) as in_gems_pcsi9
from ((select i10_descr.i10_code as code, 1 as in_i10descr, 0 as in_gems_pcsi9
from i10_descr
) union all
(select gems_pcsi9.i10_code, 0, 1
gems_pcsi9.i10_code
)
) t
group by code
) c
group by in_i10descr, in_gems_pcsi9;
This will calculate counts of things in each table separately and in both tables.

SQL: Display multiple record values in one field from a subquery

I have an SQL LIKE:
SELECT S.*,
(SELECT I.NAME FROM institution I, inst_map IM
WHERE IM.STUDENT = S.ID AND IM.INSTITUTION = I.ID) as INSTITUTIONS
FROM student S
In this case it is possible for my subquery to return multiple records (I will get an error: Subquery returns more than 1 row).
How to show those multiple values from my subquery in one field (in my case INSTITUTIONS ) separated by commas?
All ideas are welcome.
Try this query -
SELECT s.*, GROUP_CONCAT(t.NAME) INSTITUTIONS FROM student s
LEFT JOIN (SELECT * FROM institution i
JOIN inst_map im
ON im.INSTITUTION = i.ID
) t
ON s.ID = t.STUDENT
GROUP BY s.ID
The GROUP_CONCAT function will help you to get values separated by commas.
DECLARE #List VARCHAR(5000)
SELECT #List = COALESCE(#List + ', ' + Display, Display)
FROM TestTable
Order By Display
query is taken from following link, and the article explain the query perfectly, I hope it works
http://www.mitchelsellers.com/blogs/articletype/articleview/articleid/289/creating-comma-separated-list-in-sql.aspx
P.S Its for SQL Server, i guess