I am having an issue with SQL select statement. I am trying to get the percentage using below logic.
For example, I have two tables. One is TableA and another TableB
TableA has column ID, A1, A2.., Get total distinct count of A1 as "X".
TableB has column ID, B1, B2, FK_A1. Get count of B2 as "Y".
Get (Y/X)*100 as Total Percentage.
I was able to do it using subqueries but would like to use a simple and effective statement. Is it possible to get all the above 3 cases in one select statement? Your help would be highly appreciated.
Select
(Select count(distinct A1) from TableA) As C1,
(Select count(B2) from TableB Inner Join TableA ON TableB.FK_A1=TableA.A1)
C2)
Try this query
SELECT ( COUNT(B2) / COUNT(DISTINCT A1) ) * 100 AS TOTAL_PERC FROM TABLEA A INNER JOIN TABLEB B ON TABLEB.FK_A1 = TABLEA.A1;
You can use a simple join between two tables:
SELECT
( COUNT(DISTINCT A1) / COUNT(B2) ) * 100 AS PRCNTG
FROM
TABLEA A
INNER JOIN TABLEB B ON TABLEB.FK_A1 = TABLEA.A1;
apply DISTINCT on B2 if needed in your case.
apply OUTER join if needed in your case.
Cheers!!
You can use inner join with group by to achieve this. You can also use cte to make your distinct records separately.
; with cte as (
select A1, Count(distinct A1) as CountA from tableA)
, ct as (
select distinct FK_A1 , count(b2) as Count from tableB)
select 100 * ct.count/cte.CountA as totalpercentage
from cte
inner join ct
on cte.A1=ct.FK_A1
In MS Access:
I am trying to compare two tables with:
- TABLE1.docnumb1 = TABLE2.docnumb2
- looking for: TABLE1.sum <> TABLE2.sum2
But query retrieves an error: syntax error in from clause (or when creating left join I get an error that JOIN isn't supported):
SELECT docnumb1, sum
FROM Table1
JOIN Table2 ON docnumb1 = docnumb2;
How do I query the rows with different values?
looking to your sample (image)
you could compare the subquery for sum
select t1.rownumb, t1.sum1 -t2.sum2
from (
SELECT rownumb, sum(value) sum1
FROM Table1
group by rownumb
) t1
INNER JOIN
(
SELECT rownumb, sum(value) sum2
FROM Table2
group by rownumb
) t2 ON t1.rownumb = t2.rownumb and (t1.sum1 -t2.sum2 ) <> 0
use left join
SELECT docnumb1, sum
FROM Table1 a
left JOIN Table2 b ON a.docnumb1 = b.docnumb2 and a.value=b.value
where b.docnumb2 is null
I don't know if my title is understandable or not, may be someone can help edit my title?
All I want to do is, for example:
I have a table like this
Engineering appears 5 times with different article_category_abbr, and I want to select only one row with the biggest value of num.
Here, it will be Engineering-ENG-192, and Geriatrics&Gerontology will be Geriatrics&Gerontology-CLM-26
But I don't know how to do it on the whole table using mysql
Join your table to a subquery which finds the greatest num value for each sc group.
SELECT t1.*
FROM yourTable t1
INNER JOIN
(
SELECT sc, MAX(num) AS max_num
FROM yourTable
GROUP BY sc
) t2
ON t1.sc = t2.sc AND
t1.num = t2.max_num;
You can have a subquery that gets the largest value for each sc and the resulting rows will then be joined with the table itself based from two columns - sc and num.
SELECT a.*
FROM tableName a
INNER JOIN
(
SELECT sc, MAX(num) AS Num
FROM tableName
GROUP BY sc
) b ON a.sc = b.sc
AND a.num = b.num
Here's a Demo
USE MAX function and GROUP BY like this. Here is more information.
SELECT myID, classTitle, subField, MAX(score) FROM myTable GROUP BY myID, classTitle, subField
How to convert this result:
Group | Sum
Services | 11120.99
Vendas | 3738.00
Into:
Group | Sum
Services | 74.84
Vendas | 25.16
That is, the second displays the results as percentages of total.
This is what I tried:
SELECT categories.cat AS 'Group', SUM(atual) AS 'Sum'
FROM `table1` INNER JOIN
categories
ON table1.category_id=categories.id
GROUP BY categoria
you can left join a total sum that is not grouped or split up, and divide that by your sum query. this way you are just doing the total select once for faster runtime
SELECT cat, sum_atual, sum_atual/total_atual as percent_atual
FROM
( SELECT categories.cat AS cat, SUM(atual) AS sum_atual
FROM `table1`
JOIN categories ON table1.category_id=categories.id
GROUP BY categoria
) t
LEFT JOIN
( SELECT SUM(atual) as total_atual
FROM `table1`
) t1
SELECT categories.cat AS categoria,
SUM(atual) * 100 / (select sum(atual) from table1) AS percentages
FROM `table1`
INNER JOIN categories ON table1.category_id=categories.id
GROUP BY categoria
You can do this several ways. One is to just use a subquery in the select clause. As written below, this assumes that the category_id column in table1 always matches categories:
SELECT c.categoria AS "Group", SUM(t1.atual) AS "Sum",
SUM(t1.atual) / (SELECT SUM(t1.atual) FROM table1) as "Percent"
FROM `table1` t1 INNER JOIN
categories c
ON t1.category_id = c.id
GROUP BY c.categoria;
I changed the group by clause as well. It is a good idea for the group by and select to use the same columns. And I added table aliases to all the column references, another good practice.
Long time user, first time poster. I've found similar questions/answers, typically involving subqueries, but I'm not sure how to apply to my situation.
I have 3 tables:
table1
id
table2
id | val (each id has 1 of 3 possible values)
table3
id | val (each id has 1 of 3 possible values)
EDIT: Example: (table1 = unique id of everyone who attended a theme park; table2 = which attraction each visitor visited first; table3 = which attraction each visitor visited second).
I want to write a query to look up 7 different counts:
(1) count of the unique ids in table1
(2) count of the number of ids that have each of the possible values in table2
(3) count of the number of ids that have each of the possible values in table3
My MySQL query:
SELECT
count(DISTINCT table1.id) AS x1,
SUM(IF(table2.val='1'),1,0)) AS x2,
SUM(IF(table2.val='2'),1,0)) AS x3,
SUM(IF(table2.val='3'),1,0)) AS x4,
SUM(IF(table3.val='1'),1,0)) AS x5,
SUM(IF(table3.val='2'),1,0)) AS x6,
SUM(IF(table3.val='3'),1,0)) AS x7
FROM
table1
LEFT JOIN
table2 ON table1.id=table2.id
LEFT JOIN
table3 ON table1.id=table3.id
Results:
x1 = correct (because of DISTINCT)
x2,x3,x4 = correct
x5,x6,x7 = TWICE the number they should be (because I'm getting cartesian product?)
Any suggestions?
You are getting a Cartesian result. Since you are not showing how many "1", "2" or "3" counts per "ID", just do a select sum() from those tables by themselves. Since a sum with no group by will always result in ONE record, you don't need any join and it will pull the results of one record per each summary with no Cartesian result. Since your original query was LEFT JOIN to the others, the ID would have already existed on table 1, so why re-query count distinct in each sub-table.
SELECT
SumForTable1.x1,
SumForTable2.x2,
SumForTable2.x3,
SumForTable2.x4,
SumForTable3.x5,
SumForTable3.x6,
SumForTable3.x7
FROM
( select count(DISTINCT table1.id) AS x1
from table1 ) SumForTable1,
( select SUM(IF(table2.val='1'), 1, 0)) AS x2,
SUM(IF(table2.val='2'), 1, 0)) AS x3,
SUM(IF(table2.val='3'), 1, 0)) AS x4
from table2 ) SumForTable2,
( select SUM(IF(table3.val='1'), 1, 0)) AS x5,
SUM(IF(table3.val='2'), 1, 0)) AS x6,
SUM(IF(table3.val='3'), 1, 0)) AS x7
from table3 ) SumForTable3
My guess is you issue is that id is not unique in table1. So even though it is unique in table2/3 (according to your description) each row in table2/3 is joined to two rows in table1 and thus counted twice. Has nothing to do with the left joins, normal inner joins would have the same issue.
If mysql (which I don't know real well) lets you do inline views like oracle does, then you can fix it by writing your query as:
SELECT
count(view1.id) AS x1,
SUM(IF(table2.val='1'),1,0)) AS x2,
SUM(IF(table2.val='2'),1,0)) AS x3,
SUM(IF(table2.val='3'),1,0)) AS x4,
SUM(IF(table3.val='1'),1,0)) AS x5,
SUM(IF(table3.val='2'),1,0)) AS x6,
SUM(IF(table3.val='3'),1,0)) AS x7
FROM
( SELECT DISTINCT table1.id
FROM table1
) view1
LEFT JOIN
table2 ON view1.id=table2.id
LEFT JOIN
table3 ON view1.id=table3.id
I'd remove duplicates on every table:
SELECT
count(t1.id) AS t1,
SUM(IF(t2.val=1,1,0)) AS t21,
SUM(IF(t2.val=2,1,0)) AS t22,
SUM(IF(t2.val=3,1,0)) AS t23,
SUM(IF(t3.val=1,1,0)) AS t31,
SUM(IF(t3.val=2,1,0)) AS t32,
SUM(IF(t3.val=3,1,0)) AS t33
FROM (SELECT DISTINCT * FROM table1) as t1
JOIN (SELECT DISTINCT * FROM table2) as t2 ON t1.id=t2.id
JOIN (SELECT DISTINCT * FROM table3) as t3 ON t1.id=t3.id;