I have a table that has three columns: visitations, country and user_id. I have a query to retrieve amount of all visitors per country and re-visitors per country. Now, I would like to alter my query so, that I get both amounts and a ratio of re-visitors per country (re-visitors / all visitors). I'm just learning MySQL and I feel that I don't know the tools to select all visitors and re-visitors and then use them in the ratio. Is there a way to do this in one query? Could someone help me with this? Thanks!
Here is my query for all visitors (and re-visitors if the # is removed)
SELECT sum(Visitations) AS "Amount", country
FROM E91
#WHERE Visitations > 1
GROUP BY Country
ORDER BY `Amount` DESC
Sample of the data, user is a revisitor if visitations is above 1:
user_id | country | visitations
---------|--------------|---------------
beth123 | Germany | 4
david78 | USA | 2
matt23 | UK | 1
...
The where clause starts to filter records, leaving out records you do want to count for your total. To count the revisitors you can use CASE WHEN... END:
SELECT
sum(Visitations) AS "Amount",
sum(CASE WHEN Visitations > 1 THEN 1 ELSE 0 END) as "Re-Visitors",
country
FROM E91
GROUP BY Country
ORDER BY `Amount` DESC
For further use you could do something like this:
SELECT
sum(Visitations) AS "Amount",
sum(CASE WHEN Visitations > 1 THEN 1 ELSE 0 END) as "Re-Visitors",
sum(CASE WHEN Visitations > 1 THEN 1 ELSE 0 END) / sum(Visitations) as "X",
country
FROM E91
GROUP BY Country
ORDER BY `Amount` DESC
or:
SELECT Amount, Re-visitors, "Re-visitors"/Amount, country
FROM (
SELECT
sum(Visitations) AS "Amount",
sum(CASE WHEN Visitations > 1 THEN 1 ELSE 0 END) as "Re-Visitors",
country
FROM E91
GROUP BY Country
ORDER BY `Amount` DESC
) x
The amount of visitors and re-visitors has nothing to do with the sum of the column visitations.
You can get the number of visitors (all user_ids who visited a country) by counting the number of user_ids for each country and the number of re-visitors by counting the number of user_ids for each country when the column visitations is greater than 1:
SELECT country,
SUM(visitations > 1) AS revisitors,
COUNT(*) AS visitors,
AVG(visitations > 1) AS ratio
FROM E91
GROUP BY Country
Related
Their are two column umpire1 and umpire 2.
I need to find the name of the umpire who attend maximum matches doesn't matter he is umpire1 or umpire2 in that match his occurrence should count.
for e.g-- S Ravi
37 time as umpire1
57 time as umpire2
94 -- total
Output like
Umpire Total_count
S Ravi 94
Error in SQL statement: AnalysisException: cannot resolve 'level' given input columns:
line 2 pos 14;
'Aggregate [umpire1#2497], [umpire1#2497, 'sum(CASE WHEN ('level = S Ravi) THEN 1 ELSE 0 END) AS u1#2464, 'sum(CASE WHEN ('level = SJ Davis) THEN 1 ELSE 0 END) AS u2#2465, 'sum(('u1 + 'u2)) AS total#2466]
select umpire1,
sum(case when level ='umpire1' then 1 else 0 end) as u1,
sum(case when level ='umpire2' then 1 else 0 end) as u2,
sum(u1+u2) as total
from IPL_MATCHES group by umpire1;
I need to find the name of the umpire who attend maximum matches doesn't matter he is umpire1 or umpire2 in that match his occurrence should count.
I think umpire1 and umpire2 are two columns in your table and you want to unpivot and count:
select umpire, count(*)
from ((select umpire1 as umpire from ipl_matches
) union all
(select umpire2 as umpire from ipl_matches
)
) u
group by umpire;
If you want the umpire with the most matches, then add:
order by count(*) desc
limit 1
I am trying to show a proportion of customers that signed up because he/she was being referred by other customers and customers with no referral. So far I only able to show it as numerical but I wanted to show it in percentage. Null is when the customer signs up without being referred.
The original data as follows:
CustomerID ReferralID
1000004 1000003
1000015 1000010
1000007 1000004
1000011 Null
1000026 1000004
The query that I have and return data as follows:
select customerID, COUNT(*) as proportion
from company123.customertable
group by (customerID)
order by customerID asc;
CustomerID proportion
1000004 1
1000015 1
1000007 1
1000011 1
1000026 1
Expected result
CustomerID referred non-referred
1000004 1 0
1000015 1 0
1000007 1 0
1000011 0 1
1000026 1 0
Any suggestion to show it as a percentage? Thank you in advance
Use aggregate function AVG():
select avg(ReferralID is not null) referred,
avg(ReferralID is null) non_referred
from customertable
See the demo.
Results:
> referred | non_referred
> -------: | -----------:
> 0.8000 | 0.2000
you can use an aggregate function on a case statement like this
SELECT
COUNT(*) customers,
SUM(CASE WHEN referral_id IS NOT NULL THEN 1 ELSE 0 END) / COUNT(*) AS referred,
SUM(CASE WHEN referral_id IS NULL THEN 1 ELSE 0 END) / COUNT(*) AS non_referred
FROM company123.customertable
should give you something like this (numbers are made up)
customers referred non_referred
12301 0.7128 0.2872
Here's one way which will give a percentage to 2 decimal places:
EDIT: Added % sign to output using CONCAT
SELECT customerid,
CONCAT(CAST(COUNT(ReferralID) * 100.0 / Count(*) as decimal(9,2)),'%') AS proportion
FROM company123.customertable
GROUP BY customerid
ORDER BY customerid ASC;
I have a basic table:
id client trans_date returned
1 bob 20180301 0
2 frank 20180301 0
3 bob 20180401 1
id like to get a result that groups by the client and counts how many items bought and how many returned. Like this
name bought returned
bob 1 1
frank 1 0
i tried this but it didnt work
SELECT
soldto AS name,
IF(return=0,count(id),'0') AS bought,
IF(return=1,count(id),'0') AS returned
FROM sales
WHERE sdate BETWEEN '20180201000000' AND '20180501000000'
group by soldto;
Use conditional aggregation
SELECT
soldto AS name,
COUNT(*) AS bought,
-- or maybe COUNT( CASE WHEN return = 0 THEN 1 END) AS bought,
COUNT( CASE WHEN return = 1 THEN 1 END) AS returned
FROM sales
WHERE sdate BETWEEN '20180201000000' AND '20180501000000'
GROUP BY soldto;
I am trying to group each category and count the total number of cities in each category.
The results should look like this
+------------------------------+--------------+
| GNPPopRatioCategory | CountRecords |
+------------------------------+--------------+
| 1. Equal or greater than 2% | 145 |
| 2. Equal or greater than 1% | 104 |
| 3. Equal or greater than .5% | 566 |
| 4. Rest of country | 3264 |
This is what I have so far however I cannot figure out how to group them and count the cities in each category. I have been informed that using an Inline view is more efficient however I would like to figure out this way first before I moved on to Inline Views. Thanks for the help.
Select Count(Country.GNP / City.Population) AS CountRecords,
(Select Case When CountRecords>= 2 THEN "1.Equal or greater than 2%"
When CountRecords>= 1 THEN "1.Equal or greater than 1%"
When CountRecords>= .5 THEN "1.Equal or greater than .5%"
ELSE "Rest of country" END) AS GNPPopRatioCategory
From City INNER JOIN Country ON City.Country=Country.Code
Limit 20;
City table described: ID, name, Country, District, Population
Country Table described: Code, Name, Continent, Region, SurfaceArea, IndepYear, Population, LifeExpectancy, GNP, LocalName, GovernmentForm, HeadOfState, Capital
Your query is structured in an unusual and incorrect way. You have a subquery with no from.
I think you want to take the rate of the GNP and Population and place that in categories, and then count the numbers in each category. This following query takes this approach:
Select Count(*) AS CountRecords,
(Case When Country.GNP / City.Population >= 2 THEN "1.Equal or greater than 2%"
When Country.GNP / City.Population >= 1 THEN "1.Equal or greater than 1%"
When Country.GNP / City.Population >= .5 THEN "1.Equal or greater than .5%"
ELSE "Rest of country"
END) AS GNPPopRatioCategory
From City INNER JOIN
Country
ON City.Country = Country.Code
group by (Case When Country.GNP / City.Population>= 2 THEN "1.Equal or greater than 2%"
When Country.GNP / City.Population>= 1 THEN "1.Equal or greater than 1%"
When Country.GNP / City.Population>= .5 THEN "1.Equal or greater than .5%"
ELSE "Rest of country"
END)
Limit 20;
Normally when you do a limit you want to have an order by. In this case, there are only four categories, so the limit is entirely unnecessary.
I am looking for some query help
here is the following table data
Name Runs Status
Ram 50 out
Ram 103 not out
Krish 51 out
Sam 15 out
Ram 15 out
Krish 78 not out
I am expecting a single query to give the folllowing results
Name Total >100 >50&<100 TotalTimes Notout
Ram 168 1 1 3 1
Sam 15 0 0 1 0
Krish 129 0 2 2 1
I am able to write the query to get the total, Totaltimes with the help of Group By functionalities, I am stuck with the rest
Here is the query I have come up
select Name, sum(Runs) as total, count(*) as totalTimes
from tempTable
where classID IN (Select classID from upcoming_Clases where classes_id=175)
group by Name order by total desc
I am using the Mysql Database
You can do this using case:
select Name,
sum(Runs) as total,
count(case when Runs>100 then 1 end) `>100`,
count(case when Runs>50 and Runs<100 then 1 end) `>50&<100`,
count(*) as totalTimes,
count(case when Status='not out' then 1 end) `Not Out`
from tempTable
where classID IN (Select classID from upcoming_Clases where classes_id=175)
group by Name order by total desc
You can use SUM() together with IF() to test your criteria:
SELECT
Name,
SUM(Runs) AS Total,
SUM(IF(Runs>100, 1, 0)) AS `>100`,
SUM(IF(Runs>50 AND Runs<100), 1, 0) AS `>50&<100`,
COUNT(*) AS TotalTimes,
SUM(IF(Status='not out', 1, 0)) AS Notout
FROM tempTable
WHERE classID IN (SELECT classID FROM upcoming_Clases WHERE classes_id = 175)
GROUP BY Name
ORDER BY Total DESC