Could use some help please. I have a table with user info and I need to create a report that shows a summary of the count of each state where status is complete.
Data in DB - there is more but this is what I need to keep track of.
status |State
open | New York
closed | Florida
open | New York
open | California
open New York
Output should say:
State Count
New York 3
California 1
Can someone be so kind to help me with what the query looks like?
this should help.
select state, count(*) from tablename where Status= 'open'
Group by state
Try;
select
state, count(status) cnt
from tbl
where status = 'open'
group by state
I would use:
SELECT state, count(status) FROM table WHERE status = 'open' GROUP BY state
Related
I'm new to SQL and am a bit confused on how I would write a query in order to get the count of state in a different table.
Ie i have this table [student]
id
school_code
0
0123
1
2345
2
2345
And this other table [school]
school_code
name
State
0123
xxyy
New Jersey
2345
xyxy
Washington
3456
yxyx
Colarado
I want to find out how I would get this table which tells me the entries for state by checking each student and making a count of how often that state occurs, ordered by most occurrences in student table.
State
No. times occured (iterating through student)
Washington
2
New Jersey
1
SELECT school.state, count(school.state)
FROM student, school
WHERE student.school_code = school.school_code
GROUP BY school.state
ORDER BY count(school.state)`
I'm not sure whether this would be iterating through each student and counting them?
Or just natural-joinging student and school and then counting all the states
When I run this on data supplied, the numbers of times occurred is a really low number which doesn't seem right?
We can simply JOIN the two tables and COUNT the school code in the students table, with GROUP BY state:
SELECT
sc.state, COUNT(st.school_code)
FROM
school sc
JOIN student st
ON sc.school_code = st.school_code
GROUP BY sc.state;
We can try out here: db<>fiddle
Today i made a post about a issue i was having with sql, with the help of some people over there i figured out how my query had to be.
Now i have a different question, the result of my previous query:
select
count(case when Job = 'Garbageman' then 1 end) as GarbageCount,
count(case when Job = 'Delivery' then 1 end) as DeliveryCount
from Job
are:
GarbageCount: DelivryCount:
4 5
What i am trying to achieve is the following:
Job: Count:
Garbage 4
Delivery 5
My table is the following:
User: Job:
Mark Garbageman
Dirk Garbageman
Henk Garbageman
Steven Garbageman
Mark Delivery
Dirk Delivery
Henk Delivery
Steven Delivery
Stevens Delivery
Anyone know how my sql query has to look like?
Group by is all you're looking for:
select job, count(*)
from yourtable
group by job
I have a table similar to the below:
id | name | direction |
--------------------------------------
1 Jhon Washington, DC
2 Diego Miami, Florida
3 Michael Orlando, Florida
4 Jenny Olympia, washington
5 Joe Austin, Texas
6 Barack Denver, Colorado
and I want to count how many people live in a specific state:
Washington 2
Florida 2
Texas 1
Colorado 1
How can I do this? (By the way this is just an question with an academic point of view )
Thanks in advance!
Postgres offers the function split_part(), which will break up a string by a delimiter. You want the second part (the part after the comma):
select split_part(direction, ', ', 2) as state, count(*)
from t
group by split_part(direction, ', ', 2);
Initially I would obtain the state from the direction field. Once you have that, it's quite simple:
SELECT state, count(*) as total FROM initial_table group by state.
To obtain the state, some functions depending on the dbms are useful. It depends on the language.
A possible pseudocode (given a function like substring_index of MySQL) for the query would be:
SELECT substring_index(direction,',',-1) as state, count(*) as total
FROM initial_table group by substring_index(direction,',',-1)
Edit: As it is suggested above, the query should return 1 for the Washington state.
My way do making such a queries is two-step - first, prepare fields you need, second, do you grouping or other calculation. That way you're following DRY principle and don't repeating yourself. I think CTE is the best tool for this:
with cte as (
-- we don't need other fields, only state
select
split_part(direction, ', ', 2) as state
from table1
)
select state, count(*)
from cte
group by state
sql fiddle demo
If you writing queries that way, it's easy to change grouping field in the future.
Hope that helps, and remember - readability counts! :)
I want to do some statistic for the Point in my appliation,this is the columns for Point table:
id type city
1 food NewYork
2 food Washington
3 sport NewYork
4 food .....
Each point belongs to a certain type and located at the certain city.
Now I want to caculate the numbers of points in different city for each type.
For example, there are two types here :food and sport.
Then I want to know:
how many points of `food` and `sport` at NewYork
how many points of `food` and `sport` at Washington
how many points of `food` and `sport` at Chicago
......
I have tried this:
select type,count(*) as num from point group by type ;
But I can not group the by the city.
How to make it?
Update
id type city
1 food NewYork
2 sport NewYork
3 food Chicago
4 food San
And I want to get something like this:
NewYork Chicago San
food 2 1 1
sport 1 0 0
I will use the html table and chart to display these datas.
So I need to do the counting, I can use something like this:
select count(*) from point where type='food' and city ='San'
select count(*) from point where type='food' and city ='NewYork'
....
However I think this is a bad idea,so I wonder if I can use the sql to do the counting.
BTW,for these table data,how do people organization their structure using json?
this's what you want:
SELECT city,
COUNT(CASE WHEN [type] = 'food' THEN 1 END) AS FoodCount,
COUNT(CASE WHEN [type] = 'sport' THEN 1 END) AS SportCount
FROM point
GROUP BY city
UPDATE:
To get the results in an aggregated row/column format you need to use a pivot table. In Access it's called a Crosstab query. You can use the Crosstab query wizard to generate the query via a nice UI or cut straight to the SQL:
TRANSFORM COUNT(id) AS CountOfId
SELECT type
FROM point
GROUP BY type
PIVOT city
The grouping is used to count the number of Id's for each type. The additional PIVOT clause groups the data by city and displays each grouping in a separate column. The end result looks something like this:
NewYork Chicago San
food 2 1 1
sport 1 0 0
Say i have the following table:
id Name Status Date
1 John Working 11/11/2003
2 John Working 03/03/2004
3 John Quit 04/04/2004
4 John Quit 04/05/2004
5 John Quit 04/06/2004
6 Joey Working 03/05/2009
7 Joey Working 02/06/2009
8 Joey Quit 02/07/2009
9 Joey Quit 02/08/2009
10 Joey Quit 02/09/2009
I want to get the date when the change between working and quit occured, so that i get:
3 John Quit 04/04/2004
8 Joey Quit 02/07/2009
How would i do that?
select ID, Name, Status, Date
from tableName
where ID in (
select min(ID)
from tableName
where Status = 'Quit'
)
This will get the first record that has the status of 'Quit' for each person. It is not necessarily the chronologically first record though. If that is what you're are looking for let me know. Also, if so, can you be sure there won't be any duplicated names; this could be problematic if there are two different John's or Joey's.
select id, name, date
from tableName WHERE date =
(select MIN(date)
FROM tableName
WHERE NAME='<name>' AND date >= (select MAX(date)
FROM tableName WHERE NAME='<name>' AND status = 'Working')
)
Hopefully I didn't screw things up badly in the nested query there. This would only give you 1 user, you could wrap it in a function and call it passing the username or do something like make a temp table with distinct name inserted into it and loop through those names.
A lot of options exist for bringing it form just one user to all users.