SQL: Aggregate function returning 0's when using WHERE filter - mysql

I have a table with 10 columns listing results of a marketing campaign including person name, date, residence area, request, and response.
The request column is always a number, indicating the number of survey requests we've sent a specific person.
The response column is a number from 0 to X, representing the number of surveys a person has responded to take.
Now, when I look through the actual table, there are probably around 10% response rate. With lots of non-zero entries in the response column.
However, when I write an aggregate function like this:
SELECT person, date, SUM(requests), SUM(response)
FROM analytics.SurveyResults0304
WHERE group_type = 'Youth'
GROUP BY person, date;
I get a correct number for SUM(requests), but I get a big fat "0" for SUM(response)?
It's the same for all 3 group types. It returns a 0 for SUM(response)
Update: it works fine when I don't include the group_type filter, but how come I can't use it with the WHERE filter?
Thanks!!!
EDIT2: Sample Table
Person Date Group_Type Requests Response Neighborhood FirstName
-------- -------- ----------- -------- -------- ------------ ---------
Nixon 3/3/2013 Youth 3 3 Chinatown Richard
Clinton 3/3/2013 Youth 4 0 Gunhill Bill
Mao 3/3/2013 Youth 5 0 Berryville Chairman
Nixon 3/4/2013 Youth 17 2 Townsford Richard
Gates 3/3/2013 Elderly 41 5 Chinatown Bill
Gates 3/4/2013 Elderly 0 0 Chinatown Bill
Gates 3/5/2013 Elderly 0 0 Chinatown Bill
Gates 3/6/2013 Elderly 0 0 Chinatown Bill
For example
When I do:
SELECT SUM(requests), SUM(response)
FROM analytics.SurveyResults0304
WHERE group_type = 'Youth';
It returns 70 for request, and 0 for response across the board.

Your code seems to be working. See SQL Fiddle built from your data sample.
May be you have extra spaces and/or tabs in your Group_Type column?
PERSON DATE SUM(REQUESTS) SUM(RESPONSE)
--------------------------------------------------------
Clinton March, 03 2013 00:00:00+0000 4 0
Mao March, 03 2013 00:00:00+0000 5 0
Nixon March, 03 2013 00:00:00+0000 3 3
Nixon March, 04 2013 00:00:00+0000 17 2

Related

MySQL: Select Results Not As Expected: Newbie Alert

I used SQL back in the 90s and starting to get back into it for a project at work.
I want to store departmental budget data so I can run queries and use it in Power Bi.
I have a simple table with columns for the department (fund) and corresponding monthly expense info. I am trying to run a query where I can see the expenses for one fund under the supplies account -- can't get it to work. I know I will probably kick myself when I see the solution so here we go...
Select * FROM expenses
FundID Fund FY Account January February March April May June July August
9999 tester 2019 Grant Assistance to INT'L Individuals 1 2 3 4 5 6 7 8
9999 tester 2019 Grant Assistance to INT'L Organizations 21 22 23 24 25 26 27 28
9999 tester 2019 Grant Assistance to U.S. Individuals 31 32 33 34 35 36 37 38
9999 tester 2019 Grant Assistance to U. S. Organizations 41 42 43 44 45 46 47 48
9999 tester 2019 Salaries, etc. 4500 4500 4500 4500 4500 4500 4500 4500
Table = expenses
Columns
FundID, Fund, FY, Account, January, February… December
SELECT *
FROM expenses;
— results in all data from sample table (as expected)
SELECT *
FROM expenses
WHERE Fund = 'associate1';
— results in all data for ‘associate1’ (as expected)
SELECT *
FROM expenses
WHERE Fund = 'associate1' AND Account = 'Supplies';
— results in alert advising ‘empty result set (i.e. zero rows) (not expected)
—- rows/record is populated
Expected:
FundID Fund FY…. December populated with associate1’s data for supplies.
I also attempted query: SELECT * FROM expenses WHERE Account = ‘Supplies’; (Nothing)

MySQL - Connect three different tables in transposed fashion

I have three tables:
business:
id name
1 Charlie's Bakery
2 Mark's Pizza
3 Rob's Market
balanco_manual:
id business_id year unit
012 1 2015 ones
123 1 2014 tens
364 2 2014 cents
conta_balanco:
id conta balanco_id valor
412 12.3 012 12324
344 12.5 012 54632
414 14.1 364 344122
789 12 364 2312415
646 12 123 342
I need to combine them all in the business table and make them look like this:
business:
id name 12.3-2015 12.5-2015 11.56-2015 12-2014 2015-unit 2014-unit
1 Charlie's Bakery 12324 54632 NaN 342 ones tens
2 Mark's Pizza NaN NaN NaN 2312415 NaN cents
3 Rob's Market NaN NaN NaN NaN NaN NaN
Explaining a little bit further: the business table has basic registries about the businesses, balanco_manual has yearly information of each one of those businesses and conta_balanco has details of the yearly information in balanco_manual.
Trying to put that last table into words:
- First I need to join business with balanco_manual, combining the "id" column in business with the "business_id" column in balanco_manual. Note that I combine unit and year in one single column named "[year]-unit". Let's call this table "new_business" to make it easir to understand
- After, I need to combine "new_business" with conta_balanco in a similar way we did with the "unit" column. Each "conta" should be combined with the year and become a column "conta-[year]".
I'm quite a beginner with SQL and I'm having interesting difficulties. Could someone help me to crack that out?

Auto sum in a mysql table when a data is entered in another mysql table

I have 2 mysql tables :- entry and result
The structure of entry is as follows :-
Name Department Units_Sold Month
John Sales 3 January
John Sales 6 January
Ana Retail 11 January
Rick Marketing 1 February
Vicky Sales 4 March
Ana Retail 2 March
Vicky Sales 9 March
Vicky Sales 1 March
Each user enters the number of unit sold. Name, Department and Month are fetched from the server, when a user logs in and enters the "Units Sold".
When this entry is being made by the user I want another table to automatically update the monthly report. The 2nd table "result" is having the following structure :-
Name Department January February March
John Sales 9 0 0
Ana Retail 11 0 2
Rick Marketing 0 1 0
Vicky Sales 0 0 14
In this table, the fields:- Name, Department, January, February,... December are static. I want to update the sum of "Units Sold" by each user for month when he/she makes an entry into the 1st table.
Can anyone, please suggest on this ?
Thanks !!
What you are looking to do is produce what's called a Pivot Table, using the data stored in your first table. It's a relatively complex collection of aggregate functions that is well worth learning - especially if you have anything to do with financial reporting. This site has a good intro into doing what you are asking.

Programming Rookie - Limiting From 2 Tables

I am attempting to determine "First Time Givers", people who gave in FY2015 but never given before. I also need to ignore users who gave for a certain reason (appealCode).
Below is example of some of the fields my tables have and what information needs limited.
**FundLedger**
Account ID EntryAmount GiftReceivedDt AppealCode
1000 $500 7/1/2014 1
1000 $500 2/2/2002 2
2000 $25 8/1/2014 1
2000 $25 9/1/2014 1
3000 $100 10/1/2014 1
4000 $1,000 11/1/2014 2
**ConstituentTotals**
ConstituentID FirstTransactionDate LastTransactionDate CashAmount
1000 2/2/2002 1/1/2014 $1,000
2000 3/1/2014 4/1/2014 $50
3000 5/1/2014 5/1/2014 $100
4000 11/1/2014 11/1/2014 $1,000
What I need is to find the number of constituents who gave between 6/1/2014 and today, who have never given before, and the gift was not given to AppealCode 2.
So the number I need from the sample information is '2'.
**Information Needed**
ConstituentID CashAmount FirstTransactionDate LastTransactionDate AppealCode
2000 $50 3/1/2014 4/1/2014 1
3000 $100 5/1/2014 5/1/2014 1
As of now I can either get the number of people who gave if I ignore the AppealCode, or I can get the AppealCode limited but I get ALL transactions of the giver.
Currently at this stage, it pulls the count 77,000 times, one for each entry in the Ledger.
'SELECT
Number_Of_New_Donors = ( SELECT COUNT(a.ConstituentID)
From dbo.FundConstituentTotals a
RIGHT JOIN dbo.FundLedger b
ON a.ConstituentID = b.AccountID
WHERE (a.FirstTransactionDT between '6/1/2014' and '5/31/2015'
AND a.CashAmount > '0'
AND a.GivingYear = '2015'
AND A.GivingYear !< '2015')
AND (b.GiftReceivedDt between '6/1/2014' and '5/31/2015'
AND b.RecordTypeID != '0'
AND b.RecordTypeID != '-1'
AND b.RecordTypeID != '2'))
FROM FundConstituentTotals'
Suggested Response Results:
ConstituentID FundConstituentTotalID ConstituentID GivingYear PledgeAmount CashAmount NonCashAmount FirstTrans
49427 77314 49427 2015 0 25 0 1/13/2015
49427 77314 49427 2015 0 25 0 1/13/2015
49427 77314 49427 2015 0 25 0 1/13/2015
49427 77314 49427 2015 0 25 0 1/13/2015
Just found that the data is innacurate, FirstTransactionDate does not provide the date of the first transaction, just the date the transaction begin being posted to the ledger (SOMEONE MESSED THIS UP IN THE PAST). I will have to use GiftReceivedDate between DATES, and find a way to remove people if they have dates prior to 2014.
SELECT Number_Of_New_Donors = ( SELECT COUNT(a.ConstituentID)
From dbo.FundConstituentTotals a
LEFT JOIN dbo.FundLedger b
ON a.ConstituentID = b.AccountID
AND a.LastTransactionDate = b.GiftReceivedDt
WHERE a.FirstTransactionDT between '2015-01-01' and '2015-05-31'
AND b.AppealCode != 2)
This seems to do what you want. FirstTransaction is this year (since it is the first, there can't be previous ones), and appeal code isn't 2. What is the rest of your code trying to do?

SQL: Finding averages and grouping by two parameters

ID Year Month Price
001 1990 JAN 6
001 1990 FEB 8
...
001 1990 DEC 4
001 1991 JAN 7
...
001 2000 DEC 6
002 1990 JAN 7
...
Given a table formatted like the one above, how can you find the average yearly price for each item (of each year)? So for example, I'd like to have a new table that looks like:
ID Year Avg_price
001 1990 7
001 1991 12
...
002 1990 11
...
I've tried the following code:
SELECT ID, Year, AVG(Price)
FROM DATA
GROUP BY ID, Year
But end up getting 0 for each of the averages. The ordering seems to be working correctly though, so I'm not sure why this is. Any help would be greatly appreciated.
EDIT: It turns out there was nothing wrong with my SQL code at all. I guess the answer was simply a bug. Thanks for all your replies, everyone.
Your SQL looks fine to me (Checked with MS SQL).
SQL Fiddle Demo
Please doublecheck with MySQL. ;-)