473,385 Members | 1,798 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

Problem with returning query counting multiple columns

Hello,

I have a MySQL table that has (among other fields) an affiliate_id,
affiliate_sub_id, and a response_code field.

- affiliate_id stores the unique number of each of my affiliates.

- affiliate_sub_id stores the affiliate ID of their affiliates.

- response_code holds a number between 1 - 6.

I have an affiliate (e.g., 1000). For affiliate 1000, I would like to
return an array with three columns:

SubID Response_1 Response_2

Each row returned, should contain each affiliate_sub_id for affiliate_id
= '1000'. Response_1 should count how many "1" SubID had, and Response_2
should count how many "2" SubID had.

Below is the code I wrote, but I get the following error in phpMyAdmin:
"WHERE affiliate_id = '1000' AND application_"
SELECT affiliate_sub_id AS SubID,
COUNT( response_code ) = '1' AS Response_1,
COUNT( response_code ) = '2' AS Response_2,
FROM table
WHERE affiliate_id = '1000'
AND application_date >= '2004-11-01'
AND application_date <= '2004-11-29'
I'm stuck. Any suggestions? :-)
Thanks,
Adam
Jul 20 '05 #1
3 1865
adam wrote:
SELECT affiliate_sub_id AS SubID,
COUNT( response_code ) = '1' AS Response_1,
COUNT( response_code ) = '2' AS Response_2,
FROM table
WHERE affiliate_id = '1000'
AND application_date >= '2004-11-01'
AND application_date <= '2004-11-29'
I'm stuck. Any suggestions? :-)


Take advantage of the fact that COUNT() doesn't count NULLs:

SELECT T.affiliate_sub_id AS SubID,
COUNT( IF(T.resp='1', 1, NULL) ) AS Response_1,
COUNT( IF(T.resp='2', 2, NULL) ) AS Response_2
FROM `table` AS T
WHERE T.affiliate_id = '1000'
AND T.application_date BETWEEN '2004-11-01' AND '2004-11-29'
GROUP BY T.sub;

I tried this out in my test database and it seems to return the required
results you described.

Actually, the second argument to IF() in both cases could be any
non-null value, it doesn't have to be the same value you were testing
for. COUNT() is only counting non-nulls, not the value.

Also notice I put `table` in backticks, because table is a SQL reserved
word.

Regards,
Bill K.
Jul 20 '05 #2
Bill Karwin wrote:
adam wrote:
SELECT affiliate_sub_id AS SubID,
COUNT( response_code ) = '1' AS Response_1,
COUNT( response_code ) = '2' AS Response_2,
FROM table
WHERE affiliate_id = '1000'
AND application_date >= '2004-11-01'
AND application_date <= '2004-11-29'
I'm stuck. Any suggestions? :-)

Take advantage of the fact that COUNT() doesn't count NULLs:

SELECT T.affiliate_sub_id AS SubID,
COUNT( IF(T.resp='1', 1, NULL) ) AS Response_1,
COUNT( IF(T.resp='2', 2, NULL) ) AS Response_2
FROM `table` AS T
WHERE T.affiliate_id = '1000'
AND T.application_date BETWEEN '2004-11-01' AND '2004-11-29'
GROUP BY T.sub;

I tried this out in my test database and it seems to return the required
results you described.

Actually, the second argument to IF() in both cases could be any
non-null value, it doesn't have to be the same value you were testing
for. COUNT() is only counting non-nulls, not the value.

Also notice I put `table` in backticks, because table is a SQL reserved
word.

Regards,
Bill K.

Bill,

Thanks very much! I'm going to try this and see how it works for me. I
really appreciate your code and comments!

Take care,
Adam
Jul 20 '05 #3
adam wrote:
Thanks very much! I'm going to try this and see how it works for me. I
really appreciate your code and comments!


I'm glad to help!
GROUP BY T.sub;


I want to correct myself: this should be GROUP BY T.affiliate_sub_id.
When I created a mockup in my test database, I abbreviated the table
name and field names, but when I posted the query yesterday I forgot to
edit this field to match your field name.

Cheers,
Bill K.
Jul 20 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Wayne Pierce | last post by:
I have a small script with PHP that queries a MySQL database to pull out one row, where I want to be able to access each of the columns separately. I have tried several different variations and am...
1
by: Craig | last post by:
Hi, I am storing information being sent to me weekly into a ms sql database. The one twist I am running into is that later down the line the information I recieve may require more columns, or...
3
by: Megan | last post by:
hi everybody- i'm having a counting problem i hope you guys and gals could give me some help with. i have a query that retrieves a bevy of information from several different tables. first let...
4
by: carl.barrett | last post by:
Hi, I have a list box that displays 2 columns. Behind it sits a query with five columns. These are Column1 (DOB), column2 (a concatenated string of Surname Forname, Title), Column3 (Surname),...
1
by: sunilkeswani | last post by:
Hi I am still new to access. I want to know how i can build a query which can display results from 4 different columns/fields Like. Field1 Field2 Field3 Field4 1 2 1 ...
22
by: MP | last post by:
vb6,ado,mdb,win2k i pass the sql string to the .Execute method on the open connection to Table_Name(const) db table fwiw (the connection opened via class wrapper:) msConnString = "Data Source="...
9
by: HC | last post by:
Hello, all, I started out thinking my problems were elsewhere but as I have worked through this I have isolated my problem, currently, as a difference between MSDE and SQL Express 2005 (I'll just...
0
by: lazybee26 | last post by:
Hello – I’m trying to findout a better solution to this approach. Currently if I have to return columns from multiple tables, I have to define my own TYPE and then return SETOF that type in...
3
by: Hasse1982 | last post by:
Hi I have a table KDOCUMENT with the columns , , , , , ,
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.