473,747 Members | 2,946 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Problem with returning query counting multiple columns

Hello,

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

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

- affiliate_sub_i d 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_i d 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_i d AS SubID,
COUNT( response_code ) = '1' AS Response_1,
COUNT( response_code ) = '2' AS Response_2,
FROM table
WHERE affiliate_id = '1000'
AND application_dat e >= '2004-11-01'
AND application_dat e <= '2004-11-29'
I'm stuck. Any suggestions? :-)
Thanks,
Adam
Jul 20 '05 #1
3 1879
adam wrote:
SELECT affiliate_sub_i d AS SubID,
COUNT( response_code ) = '1' AS Response_1,
COUNT( response_code ) = '2' AS Response_2,
FROM table
WHERE affiliate_id = '1000'
AND application_dat e >= '2004-11-01'
AND application_dat e <= '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_d ate 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_i d AS SubID,
COUNT( response_code ) = '1' AS Response_1,
COUNT( response_code ) = '2' AS Response_2,
FROM table
WHERE affiliate_id = '1000'
AND application_dat e >= '2004-11-01'
AND application_dat e <= '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_d ate 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
3498
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 able to get the entire row to print, but when I attempt to access the individual columns I get an error. Here is what I have so far: if (isset($_POST)): $link = mysql_connect('...','...','...'); mysql_select_db("...");
1
2202
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 columns might be renamed so having a static database call is out of the question. I was using mysql but for certain reasons switched to ms sql 2000. Currently before ms sql, I query the database for show fields which returns all the database data such...
3
1963
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 me give you a little background. this database is kind of like a human resources database. it collects info about people, where they work, and if they have any work-related issues where they work.
4
5370
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), column4 (Forename) and column5 (title). Columns 3,4 and 5 are not shown in the list box only the first two. DOB Name: &" "&&", "&
1
3677
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 0 1 1 0 0
22
12486
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=" & msDbFilename moConn.Properties("Persist Security Info") = False moConn.ConnectionString = msConnString moConn.CursorLocation = adUseClient moConn.Mode = adModeReadWrite' or using default...same result
9
5759
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 call it Express for simplicity). I have, to try to simplify things, put the exact same DB on two systems, one running MSDE and one running Express. Both have 2 Ghz processors (one Intel, one AMD), both have a decent amount of RAM (Intel system...
0
3603
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 the function. Iíve provided an example below. Now, if I have to add a column to the select query, I have drop the existing TYPE definition, create a new TYPE with the new column added to it, and then modify the function sql to return this...
3
2510
by: Hasse1982 | last post by:
Hi I have a table KDOCUMENT with the columns , , , , , ,
0
8979
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, weíll explore What is ONU, What Is Router, ONU & Routerís main usage, and What is the difference between ONU and Router. Letís take a closer look ! Part I. Meaning of...
0
8818
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
9522
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
9307
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9223
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
6790
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Duprť who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6067
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
1
3296
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
2771
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.