SQL: Group By specific values and add up their count column - mysql

I need to group a chunk of values into it's own, and get a total count for each group.
name|count
alex 3
amy 6
james 3
john 33
joe 12
zane 1
pane 5
I have a sql query like this but I think it is incorrect. basically I need to add the total count for each group, and right now it just counts the number of rows.
select Name,
count(case when Text like 'alex' or Text like 'amy' then 1 end) as group1,
count(case when Text like 'j%' then 1 end) as group2,
count(case when Text like 'zane' or Text like 'pane' then 1 end) as group3,
from MyTable
group by Name
The important criteria is that I need to group people by their names, this grouping is not trivial and needs to be done specifically by individual values.

Related

mysql sorting data in two levels possible?

Iam trying to analyze some date from a mysql table.
The data is a representation of incomming calls, each line represents one call
category purpose
cars question
bikes question
cars question
cars complaints
scooters question
bikes complaints
now for the plotting I need the data to look like this
category cat_count question complaints
cars 3 2 1
bikes 2 1 1
scooters 1 1
I figured out that I can sort and count by one field by using something like this
SELECT category, count(*) FROM stat GROUP BY category ORDER BY count(*) desc;
which will give me
category count
cars 3
bikes 2
scooters 1
but how can I add the purpose counts to that output?
I would usually write a php or bash script, but if its possible to do it in mysql I would rather do it like that instead of having a 3 loop script noone will understand in 1 year :-))
Thanks in advance for any hint (even if the hint is "impossible")
You can do this way:
SELECT category, count(*) as cat_count,
SUM(CASE WHEN purpose='question' THEN 1 ELSE 0 END) as question,
SUM(CASE WHEN purpose='complaints' THEN 1 ELSE 0 END) as complaints
FROM stat
GROUP BY category
ORDER BY count(*) desc;
Result:
CATEGORY CAT_COUNT QUESTION COMPLAINTS
cars 3 2 1
bikes 2 1 1
scooters 1 1 0
See result in SQL Fiddle.

Sort and Group by Time

I have the following table:
Line Date Time Order Qty
3 1140531 24737 S215037 1
3 1140531 24737 S215037 1
2 1140531 14955 S215058 1
2 1140531 14955 S215058 1
2 1140531 24055 S215059 1
2 1140531 24055 S215060 1
3 1140530 25319 S215099 1
3 1140530 25319 S215099 1
I need to be able to display the Line, work order, sum of qty (I need to show the order that was completed first on desc based on date and time). My end result should look like this:
Line Order Sum of Qty
2 S215058 2
2 S215059 1
2 S215060 1
3 S215099 2
3 S215037 2
Attempt:
I have a query that updates the data into a table where it's ordered by Line, Date, Time
then I apply the following query:
SELECT Line, Order, Sum(Qty)
From MyTable
Group by Line, Order
When I group the items, it groups my Orders based on alphabetical order and not the time. Please help!
I feel some contradiction between your description and expected result as you mention the word desc. Anyway the following query will give you the end result.
SELECT `Line`, `Order`, Sum(Qty)
From MyTable
Group by `Line`, `Order`
order by `Line`, `Date`, `Time`, `Order`;
SQL Fiddle here: http://www.sqlfiddle.com/#!2/456bf/8

MySQL query to return multiple summed and grouped columns

Part 2 to the question asked here -
MySQL query to return multiple summed columns
I have the following data in my SQL table. Imagine that there are many more rows spanning years worth of data.
uid user category count date
--------------------------------------------------
1 henry RED 4 2013-04-01
2 john BLUE 3 2013-04-11
4 eric GREEN 2 2013-04-22
3 alice RED 5 2013-05-01
5 eric BLUE 2 2013-05-12
6 john RED 3 2013-05-23
7 henry GREEN 2 2013-06-03
8 eric RED 3 2013-06-08
9 john BLUE 5 2013-07-11
... (more) ...
I would like a query that gives me back the following data, grouped in columns according to the date. I know how to use the GROUP BY command to group data into rows. But I am not sure how to do it by columns.
category May 2013 April 2013 ... (more)
------------------------------------
RED 8 4
BLUE 2 3
GREEN 0 2
I realize there are probably a lot of options, as far as using date and time functions. I'm just looking more for the basic structure of how to set up the logic for SQL to give me an arbitrary number of columns.
You should try summing by cases (and sum 0 when not true). Like the following:
SELECT
...
SUM(CASE WHEN date BETWEEN ... AND ... THEN 'count' ELSE 0 END) as 'May 2013',
SUM(CASE WHEN date BETWEEN ... AND ... THEN 'count' ELSE 0 END) as 'April 2013',
etc.
FROM ...,
GROUP BY category
I think you must use Procedural Language My SQL to looping the month/year.
Try something like this:
SELECT
category,
SUM(CASE WHEN date BETWEEN '2013-05-01' AND '2013-06-01' THEN count ELSE 0 END) AS 'May 2013',
SUM(CASE WHEN date BETWEEN '2013-04-01' AND '2013-05-01' THEN count ELSE 0 END) AS 'April 2013',
... (more)
FROM dbtable
GROUP BY category

Query return 1 row when no data present

I am running several instances of the same query. The below version should not be returning any data but I am getting 1 row with 'null' in each of the columns:
SELECT (CASE ScanName WHEN 'SYSTEM-HQ' THEN 'HQ
System' END) AS System,
sum(CASE pspplMSSeverity WHEN 1 THEN 10 WHEN 2 THEN 9 WHEN
3 THEN 6 WHEN 4 THEN 3 END) AS Score,
(sum(CASE pspplMSSeverity WHEN 1 THEN 10 WHEN 2 THEN 9
WHEN 3 THEN 6 WHEN 4 THEN 3 END)/COUNT(pspplMSSeverity)) AS
Grade
FROM missingpatches
WHERE ScanName like '%SYSTEM-HQ%'
ORDER BY LAST_UPDATE DESC LIMIT 1
How can I modify this query to ensure that I am only returning data when valid values exist?
Could this be due to the use of the Case and Sum within the primary SQL statement that are causing the Null data to be returned?
SELECT (CASE ScanName WHEN 'SYSTEM-HQ' THEN 'HQ
System' END) AS System,
sum(CASE pspplMSSeverity WHEN 1 THEN 10 WHEN 2 THEN 9 WHEN
3 THEN 6 WHEN 4 THEN 3 END) AS Score,
(sum(CASE pspplMSSeverity WHEN 1 THEN 10 WHEN 2 THEN 9
WHEN 3 THEN 6 WHEN 4 THEN 3 END)/COUNT(pspplMSSeverity)) AS
Grade
FROM missingpatches
WHERE ScanName like '%SYSTEM-HQ%'
HAVING System IS NOT NULL # Added
ORDER BY LAST_UPDATE DESC LIMIT 1
Try add a HAVING.
Most probably the table missingpatches contains 1 row that satisfy
WHERE ScanName like '%SYSTEM-HQ%'
i.e. a row with ScanName that contains 'SYSTEM-HQ' but not exactly equal to 'SYSTEM-HQ' which you are equating with in the select's 1st col.
In SQL, the columns are evaluated after tables are joined and where clauses are evaluated. Hence, the row you are seeing, is actually meeting the where clause criteria.

Select with a where clause and without it in the same query

I'm trying to find a way to sum amounts that match a specific term, and also amounts that don't match it. For example, if my table looks like this
user amount description
1 34 bike
1 78 toys
2 3 fuel
2 12 bike
I'm trying to get a table that will look like this in the end:
user amount spent on bike amount spent total
1 34 112
2 12 15
I'm using mysql
You can use a CASE statement within a SUM grouping:
SELECT user,
SUM(CASE WHEN description = 'bike' THEN amount ELSE 0 END) bike_amount,
SUM(amount) total_amount
FROM mytable
GROUP BY user