Get data from table by County in a single row - mysql

I want to get data from table in a single row for single county. Right now as shown in images it shows Different rows for Single county as Kalamazoo. It should show single with both records in single row.
I am using following query
SELECT County, Virus, SumOfPositiveTests
FROM StatewiseData
WHERE State = 'MICHIGAN'
I want the results to be shown as following
County Virus SUMOFPOSITIVETESTS
---------------------------------------------
Kalamazoo H3N2,H3N8 3
Total sum of both H3N2 and H3N8

use MYSQL GROUP_CONCAT
The following MySQL statement will return a list of comma(,) separated 'Virus's for each group of 'County' from the StatewiseData table
SELECT County,GROUP_CONCAT(Virus),sum(SumOfPositiveTests) FROM StatewiseData where State='MICHIGAN' group by County

Try this if you are using mysql:
SELECT country, GROUP_CONCAT(virus) _virus, SUM(SumofPositiveTests) as TotalPostitiveTest
FROM StatewiseData
WHERE Country = 'Kalamzoo'
GROUP BY Country
Please mind that this will not work with most databases, as they need you to to specify only the grouping field and aggregate functions of other fields in your query.
Here is the source

Try this. this would work on IBM DB2 and MySQL. what DB you are using
SELECT DISTINCT Country, Virus,SumOfPositiveTests FROM Customers where State='MICHIGAN' group by Country;

why are you using state in where condition? (please provide list of columns do you have in a column if i missed something)
if you are only selecting the country then it should be as simple as:
SELECT County,Virus,SumOfPositiveTests FROM StatewiseData where Country='Kalamzoo'
if you want to select summary or totals of single country then you you should use sum like:
SELECT country, SUM(SumofPositiveTests) as TotalPostitiveTest
FROM StatewiseData WHERE Country='Kalamzoo' GROUP BY Country

Related

How do I get distinct value in one row and their respective average values as output in mySQL

I have a mySQL table with 100 rows and 6 columns namely ; full_name, name, score, city, gender, rating. I want the output as one column containing distinct city values (there are only 5 distinct cities initially & the user input value of his/her city will be added, namely; Delhi, Mumbai, Patna, Chennai ,Pune) and the second column having their respective avg score.
The database is linked to the python code which I am working on & use takes input which is stored in the above 6 columns. Then according to the user request, the output as analysis is showed as graphs using matplotlib. But I am really stuck at this part where I need to show a graph having X-VALUES as city names and Y-VALUES as respective avg score for that, I need to know the query to get such an output in mySQL where we get 2 columns storing the above.
How do I do it ?
SELECT city AS X,AVG(score) AS Y
FROM yourtable GROUP BY city
Is this, what you ment? Or if you want the result as one row, you add GROUP_CONCAT:
SELECT GROUP_CONCAT(X) AS gX,GROUP_CONCAT(Y) AS gY FROM
(SELECT city AS X,AVG(score) AS Y
FROM yourtable GROUP BY city) g
ok, redbull helps...
correct syntax :
select city, avg(score) from data group by city;
wrong syntax (what I was trying to do earlier) :
select data.distinct(city), data.avg(score) , check.check_city from data, (select name, distinct(city) as check_city from data) check where data.name = check.name and data.city in check.check_city;
Thanks Anyway !

SQL Query to find the common data based on two or more condition from the same table?

I have set of country codes and each country has set of accounts holder counts.
I want to fetch the same account numbers along with the country codes among all the countries which are being available in multiple countries.
For example:
Count the number of accounts grouped by country codes:
SELECT [Country Code],COUNT([Accounts]) FROM [table] GROUP BY [Country Code]

Group rows on some colums and use SUM

I have a table defined in AX like follows:
I am trying to create an SSRS report using a query to create the DataSet and as Design - PrecisionDesign.
I got this until now:
with groupings :
I would like to visually replicate this query:
SELECT Country
, City
, FirstName
, SUM(AGE)
FROM TABLESSRSGROUPING
GROUP BY Country, City, FirstName
Resulting in:
You should only need to group once within the first Row Grouping.
In the Group Properties - group on [Country] then ADD [City] then ADD [Name]
Let me know if this helps
What value do you have in the sum field in the SSRS report?
Is it
Fields!.Age.Value
or
sum(Fields!.Age.Value)
Your report is diplaying the DETAIL for all records.
The Details group has the three lines to the left, indicating that it is showing all lines and NOT grouping like you want.
Change your Details to Group on FirstName and delete your FirstName group (select DELETE GROUP ONLY not the Related Rows and Columns)
On a different note, you're adding AGEs?

Problems with CREATE VIEW

The question is to create a view called NumberOfCities_v. And then use the view to list
countries that have more than 20 cities in the descending order of number of cities.
This is my create a view statement:
CREATE VIEW NumberOfCities_v AS
SELECT
country.code as "Country Code",
country.name as Country,
count(countrycode) as "Number Of Cities"
FROM
city,
country
WHERE
city.countrycode = country.code
GROUP BY country.code, country.name
HAVING COUNT(countrycode) > 20
ORDER BY COUNT(countrycode) desc;
I don't get any records, but I think I've created the view. According to my powerpoint, I should be getting both. Apparently I have some field errors with that statement with I try to select the view with another statement. But When I use the exact statement without the create view, I get the results I need. Can someone explain why that is?
You need to fire sql query:
Select * from `NumberOfCities_v`
to get all the records generated in view.

Query using two tables with DISTINCT

I have two tables - clients and - group
I need to get county and zip from clients and group-assigned from group
When I search, I cannot get distinct results, that is, instead of the output showing 100 clients with zipcode 12345 in jones county in main st group.
I need to have each zip and county listed once by group. I have googled and attempted many ways but it is just beyond me.
Can anyone assist in steering me to the correct way
Adding GROUP BY group, city, zip to the end of your query should get you what you need. It will only return unique combinations of the three.
Presumably you have something like:
select g.*, c.county, c.zip
from clients c join groups g on <some join condition>
You want one result per group. So, add a group by clause such as:
group by g.id -- assuming id uniquely identifies each group
This will give an arbitrary value for the other fields, which may be sufficient for what you are doing. (This uses a MySQL features called Hidden Columns.)