I need to get the total of each unique page. However I'm not sure how to approach this. For example
If they are 12x index.php's and 64x home.php's. I need something like
PAGE | TOTAL
index.php | 12
home.php | 64
My table is as follows:
id | page
0 | index.php
1 | index.php
3 | home.php
etc
This will be used to get the total of each page for page visits
SELECT PAGE, COUNT(id) AS TOTAL
FROM TABLE_NAME
GROUP BY PAGE
Related
I have a Table (T_agents) of agents each has a number of call in a field called NCH I want to create another field call NCHpercent that is the percentage of calls taken by that agent. So the formula is NCH/Total NCH.
So in the query builder I have the following and formula but it dosent work :(
NCHpercent: [NCH.T_agents] / ( SUM(SELECT [NCH.T_agents] FROM [T_agents]) )
What am I doing wrong ?
This would be easier if we could see the table structure as that impacts everything. However I hope I follow this correctly, but I imagine your table (T_agents) as something like:
+-------+-------------+------+
| ID | Agents | NCH |
+-------+-------------+------+
| 1 | agent_1 | 1 |
| 1 | agent_1 | 1 |
| 1 | agent_2 | 2 |
| 1 | agent_3 | 1 |
+-------+-------------+------+
Now assuming that is correct (and NCH is not a unique ID but a total number of calls then we can use a query like this to calculate percentage - note this is not stored in a table, this is just to display the percentage value in a query- I've also added the sum of the total in for the sake of it:
SELECT SUM([T_Agents].NCH) AS total_SUM, [T_agents].Agents, ((SUM(T_agents.NCH))/(select SUM(t_agents.NCH )from T_agents)*100) AS NCHPercent
FROM T_agents
GROUP BY [t_agents].Agents;
In my test the results would be:
2, agent_1, 40
2, agent_2, 40
1, agent_3, 20
However if I got this wrong and the NCH column is in fact
Ok. I just found the answer soing some trial an error. The answer is this code:
NCHperc: [AHT_Tenure].[Calls Handled]/(SELECT Sum(AHT_Tenure.[Calls Handled]) AS [SumaDeCalls Handled]
FROM AHT_Tenure)
By the way thank you guys. And actually the agents name dosent matter for this query since all I wanted was the percentage on each row.
I have a visitor counter, the table looks like this:
id | country_code | datetime | Browser | ...
---------------------------------------------------------
1 | FR | 2014-06-20 05:00:28 | FireFox |
2 | US | 2014-06-20 05:00:28 | Chrome |
3 | ZW | 2014-06-20 05:00:28 | IE |
I want to count how many visitor I had (for example) in an hour at a certain day.
The query looks like this:
SELECT HOUR(datetime), COUNT(*) as hits
FROM counter_table WHERE datetime >= CURDATE()
GROUP BY HOUR(datetime) WITH ROLLUP
No problem with this query.
But I also want count how many visitors I got from a certain country.
I tried everything like GROUP BY HOUR(datetime), country_code WITH ROLLUP (I do not need a hourly ROLLUP for the country, I need hourly ROLLUP for hits) or JOIN queries but I can't find a good solution.
The best thing I could come up with was something like this:
SUM(IF(country_code = "AF", 1,0)) AS Afghanistan,
...
SUM(IF(country_code = "ZW", 1,0)) AS Zimbabwe
But the problem is that there are almost 400 countries in the world. I am not sure if such a long query like above would be good for performance. Unfortunately performance is very important in this case because the table is huge. But otherwise this solution provides exactly what I want.
Maybe there is another database better than MySQL for this kind of problem?
to simplify, I got a database of registered users, I want to count how many emails there are for each email domain name (note I do not know all domain names)
for example,
Users table-
id | email
------------------
1 | test#hotmail.com
2 | test#somesite.aaa<--unknown to me
3 | test#unknownsite.aaa<--unknown to me
4 | test#hotmail.com
5 | test#somesite.aaa<--unknown to me
6 | test#yahoo.com
7 | test#yahoo.com
8 | test#hotmail.com
( note: I want to count each email without specifying which email exactly )
so the result I want is
suffix | count
hotmail.com | 3
somesite.aaa | 2
unknownsite.aaa| 1
yahoo.com | 2
again, I stress this, I do not know unknownsite.aaa nor can i mention it in a statement because it is unknown to me, i hope I am clear.
So essentially I want to make a statistic of what my website users use as an email host website.
but like I said and I will repeat the third time, I do not know every mailhost that exists.
I am going to investigate this more, I have a feeling this is something mysql cannot handle.
EDIT:
I came up with the following solution, but seems tiny bit redundant, which I hate :P
select substring_index(`email`,'#',-1),count(*) as count from users group by substring_index(`email`,'#',-1) order by count ASC;
SELECT SUBSTRING_INDEX(email, '#', -1) AS suffix, COUNT(*) AS count
FROM users
GROUP BY suffix
See it on sqlfiddle.
use RLIKE or REGEXP in mysql
http://dev.mysql.com/doc/refman/5.0/en/regexp.html
I have two tables, Page and Hyperlink to store Web page URLs and their connections(hyperlinks) respectively. In the Page table, I have a page_id column that is AUTO_INCREMENT for all unique entries. In the Hyperlink table, I use the page_id for both source and destination for the hyperlink.
Table: Page
page_id | page_url
--------------------
1 | a.com
2 | b.com
3 | c.com
4 | d.com
Table: Hyperlink
hyperlink_id | source_id | destination_id
1 | 1 | 2
2 | 1 | 3
3 | 2 | 4
4 | 4 | 4
I want to retrieve a ResultSet that returns me two Strings in a row, a.com and b.com, given the hyperlink_id. Let's assume the hyperlink_id is 1.
I tried using the query below but it didn't seem to work.
SELECT Page.page_url, Page.page_url from Hyperlink
JOIN Page
ON Page.page_id = Hyperlink.source_id
AND Page.page_id = Hyperlink.destination_id
where Hyperlink.hyperlink_id = 1
This query returned me an empty ResultSet.
However, this query worked for hyperlink_id = 4. I am thinking this query doesn't work when source_id != destination_id. What is wrong with my SELECT statement?
Your query can be re-written as:
SELECT Page.page_url, Page.page_url
FROM Hyperlink
JOIN Page
ON Page.page_id = Hyperlink.source_id
AND Hyperlink.source_id = Hyperlink.destination_id
WHERE Hyperlink.hyperlink_id = 1
This implies that Hyperlink.source_id must equal Hyperlink.destination_id and you only have one row in your table that fulfills this condition. The one where the hyperlink_id = 4.
Maybe you meant the following instead?
SELECT p1.page_url, p2.page_url
FROM Hyperlink
JOIN Page p1
ON p1.page_id = Hyperlink.source_id
JOIN Page p2
AND p2.page_id = Hyperlink.destination_id
WHERE Hyperlink.hyperlink_id = 1
This will access the Page table twice to get the information you need for the two different IDs
Let's say we have this table:
Symbol | Size
A | 12
B | 5
A | 3
A | 6
B | 8
And we want a view like this:
Symbol | Size
A | 21
B | 13
So we use this:
Select Symbol, sum(Size) from table group by Symbol order by Symbol ASC
But instead we get this:
Symbol | Size
A | 12
B | 5
What am I doing wrong?!
You are doing it right, you should expect the correct results. Could you please supply more information about the DB you are using, additional schemas, etc?
Maybe you have some unique index on Symbol?
Try to execute the following to "sanity-test" your system:
SELECT SUM(Size) FROM table
Should result in 34
SELECT Symbol, Count(*) FROM table GROUP BY Symbol
Should results in 3 and 2
If both of the above work perfectly as you noted, please try:
SELECT Symbol, Count(*), Sum(Size) FROM table GROUP BY Symbol
This is your code, with the additions of Count(*) and without the ORDER BY clause. If that does not work after the two above do, I'm really puzzled...
I found out that somewhere in the Select commands that leaded to the Un-SUMable table instead of a left join there was a simple join.Although I still don't get why that should mess up the calculation, I changed that and now it works... I'm sorry I couldn't upload the whole thing...