Joint values from different queries into one - mysql

I have 6 tables of data.
I have 2 different queries that produce a table like this
| PERCENTAGE | COMPANY | TYPE |
(the companies are always the same in all tables, the only thing that changes is the PERCENTAGE and the type). Each query gets the total_percentage of an given company for one of the 2 different types.
For example:
Query 1 result:
| PERCENTAGE | COMPANY | TYPE |
0.0213 | Apple | Phones
0.3452 | Microsoft | Phones
Query 2:
|PERCENTAGE | COMPANY | TYPE |
0.4243 | Apple | Computers
...
And the result is:
Result:
| SUMMED_TOTAL| COMPANY |
0.0213 + 0.4243 | Apple
...
The summed percentage rows wont sum > 1, dont worry.
What i'd like to do is get the TOTAL_PERCENTAGE, from all types (all 2 queries) summed by company.
Any idea of an query? Im a begginer so it's kinda hard. You can literally use the names to do the query.

Use union all and group by:
select company, type, sum(percentage)
from ((select company, type, percentage from query1) union all
(select company, type, percentage from query2)
) ct
group by company, type;

Related

How to select for percentage and count calculatio from different table

Please i need help to complete the code to do the following:
calculate the percentage of voters who have voted based on batches.
get the number of voters by batch.
my table :
voters table
+-------------+-------------+----------------+
| stud_id | name | batch |
+-------------+-------------+----------------+
| 1 | Peter | 2016 |
| 2 | John | 2017 |
| 3 | Wick | 2017 |
+-------------+-------------+----------------+
vote table
+-------------+----------------+
| vote_id | stud_id |
+-------------+----------------+
| 1 | 1 |
| 2 | 2 |
+-------------+----------------+
ive tried this query:
SELECT voters.batch,COUNT(*) AS voted_batch, 100.0 * COUNT(*) / (SELECT COUNT(*) FROM vote) AS percentage
FROM vote JOIN voters WHERE voters.stud_id=vote.stud_id
GROUP BY batch asc
the code can only display the percentage of voting (not from a batch) and can only do a total of voting and I am confused to show a total of voters
and my expected table selection is:
+-------------+----------------+----------------+----------------+
| batch |total_batch | voted_batch | percentage |
+-------------+----------------+----------------+----------------+
|2016 | 1 | 1 | 100 |
|2017 | 2 | 1 | 50 |
+-------------+----------------+----------------+----------------+
Much appreciate for your support, thank you very much.
To differentiate between the total number of students in a batch, and the number of students who voted in a given batch, you need to use different fields in your COUNT expressions:
SELECT
voters.batch,
COUNT(*) AS total_batch,
COUNT(vote.vote_id) AS voted_batch,
(COUNT(vote.vote_id) * 1.0) / (COUNT(*)) AS percentage
FROM voters
LEFT JOIN vote ON
vote.stud_id = voters.stud_id
GROUP BY voters.batch
Note that:
COUNT(*) counts all students, even those who haven't voted (since we're using a LEFT JOIN).
COUNT(vote.vote_id), i.e. counting by the primary key of the table on the right side of the join, only counts students who have voted.
We have to multiple one of the COUNTs by 1.0, to coerce the result to a decimal (otherwise, you run into integer division).
Finally, you might want to consider changing your table names to be consistent: One is named voters (plural), while the other is named vote (singular). To avoid starting a religious war, I won't tell you which one to go for.

count groupings of multiple columns

I have a table of tickets to multiple dates of shows shows. basically, it looks like this...
+----+---------------+--------------+-----------+
| ID | ticket_holder | ticket_buyer | show_date |
+----+---------------+--------------+-----------+
ticket_holder and ticket_buyer are both user ids
If I wanted to count the total number of tickets that one ticket holder has, I could group by that holder and count the rows, but I want more stats than that.
I want to know a user's total bought tickets, how many they hold and how many shows they've bought tickets for.
+------+---------+--------+-------+
| USER | HOLDING | BOUGHT | DATES |
+------+---------+--------+-------+
| 1 | 12 | 24 | 7 |
+------+---------+--------+-------+
| 2 | 3 | 4 | 2 |
+------+---------+--------+-------+
| 3 | 1 | 2 | 1 |
+------+---------+--------+-------+
is it possible to put all this in a query, or do i need to do php stuff to make it happen?
I would do it in multiple queries. You can't group by either ticket_holder or ticket_buyer like you want, in a single query. If you try GROUP BY ticket_holder, ticket_buyer then it will group by both columns, which is not what you want.
SELECT ticket_holder, COUNT(*) AS tickets_held
FROM `a table of tickets` GROUP BY ticket_holder;
SELECT ticket_buyer, COUNT(*) as tickets_bought
FROM `a table of tickets` GROUP BY ticket_buyer;
SELECT ticket_buyer, COUNT(DISTINCT show_date) AS shows_bought
FROM `a table of tickets` GROUP BY ticket_buyer;
Not every task has to be accomplished in a single query! It's part of the design of SQL that it should be used by some application language, and you're expected to handle formatting and display in the application.

SQL: How to get distinct value and it's count in separate column

I have a MySQL table with a column called "level" like below.
| level
-----------
| High
| High
| Medium
| Low
| Low
| Medium
How would I go about counting the number of times each of these values occurs outputting a table like below from one query for a MS Chart (pie) control data source.
level | count
---------------
High | 1
Medium | 4
Low | 2
Group by the level. Aggregate functions like count() apply to each group
select level, count(*) as cnt
from your_table
group by level

Getting Distinct Value Counts Across Multiple Fields/Tables in One MySQL Query

I have a table called visits which contains the following
link_id, id, browser, country, referer
Now, this basically records visits of a certain link and inserts the browser, country and referer of whomever visted that link in a database
Now I need to show statistics for each link
I used the following query to get me all the browsers
SELECT browser, COUNT(browser) FROM visits GROUP BY browser
Which produced something like
Browser Count(Browser)
Internet Explorer | 5
Chrome | 3
Now this worked as expected for browsers only but I'm looking for a way to count all occurrences of referers, browsers and countries in one single query.
Is there a way to do this?
To count multiple, different occurence counts of values in the DB can very easily be done in just one query.
Keep in mind, the column header in SELECT COUNT(tablename) returns only one column, with only one numeric value. For every distinct value (from the GROUP BY clause), you have two columns: Value, Count. To count for different fields, you'll need three: Field, Value, Count, and if you want to count different fields in different tables, you'll need four: Table, Field, Value, Count.
Observe how I am using UNION below for two different tables:
SELECT
"Table1" AS TableName,
"Field1" AS Field,
Field1 AS Value,
COUNT(Field1) AS COUNT
FROM Table1
GROUP BY Value
UNION
SELECT
"Table2" as TableName,
"Field2" as Field,
Field2 as Value,
COUNT(Field2) AS COUNT
FROM Table2
GROUP BY Value
You'll notice I need to use aliases: "Table2" as TableName, this is because the UNION'd columns ought to have matching column headers.
So you can visualize what this returns, take a look:
+-------------------+----------------+----------+--------+
| TableName | Field | Value | COUNT |
+-------------------+----------------+----------+--------+
| ItemFee | PaymentType | | 228 |
| ItemFee | PaymentType | All | 1 |
| ItemFee | PaymentType | PaidOnly | 1 |
| Person | Presenter | | 692258 |
| Person | Presenter | N | 590 |
| Person | Presenter | Y | 8103 |
+-------------------+----------------+----------+--------+

Combine count rows in MySQL

I've got a table in MySQL that looks roughly like:
value | count
-------------
Fred | 7
FRED | 1
Roger | 3
roger | 1
That is, it was created with string ops outside of MySQL, so the values are case- and trailing-whitespace-sensitive.
I want it to look like:
value | count
-------------
Fred | 8
Roger | 4
That is, managed by MySQL, with value a primary key. It's not important which one (of "Fred" or "FRED") is kept.
I know how to do this in code. I also know how to generate a list of problem values (with a self-join). But I'd like to come up with a SQL update/delete to migrate my table, and I can't think of anything.
If I knew that no pair of records had variants of one value, with the same count (like ("Fred",4) and ("FRED",4)), then I think I can do it with a self-join to copy the counts, and then an update to remove the zeros. But I have no such guarantee.
Is there something simple I'm missing, or is this one of those cases where you just write a short function outside of the database?
Thanks!
As an example of how to obtain the results you are looking for with a SQL query alone:
SELECT UPPER(value) AS name, SUM(count) AS qty FROM table GROUP BY name;
If you make a new table to hold the correct values, you INSERT the above query to populate the new table as so:
INSERT INTO newtable (SELECT UPPER(value) AS name, SUM(count) AS qty FROM table GROUP BY name);
Strangely, MySQL seems to do this for you. I just tested this in MySQL 5.1.47:
create table c (value varchar(10), count int);
insert into c values ('Fred',7), ('FRED',1), ('Roger',3), ('roger',1);
select * from c;
+-------+-------+
| value | count |
+-------+-------+
| Fred | 7 |
| FRED | 1 |
| Roger | 3 |
| roger | 1 |
+-------+-------+
select value, sum(count) from c group by value;
+-------+------------+
| value | sum(count) |
+-------+------------+
| Fred | 8 |
| Roger | 4 |
+-------+------------+
I was surprised to see MySQL transform the strings like that, and I'm not sure I can explain why it did that. I was expecting to have to get four distinct rows, and to have to use some string functions to map the values to a canonical form.