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 3 1883
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 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
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. This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
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("...");
|
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...
|
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.
|
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: &" "&&", "&
|
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
| |
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
|
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...
|
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...
|
by: Hasse1982 |
last post by:
Hi
I have a table KDOCUMENT with the columns
,
,
,
,
,
,
|
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...
|
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,...
| |
by: jinu1996 |
last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth.
The Art of Business Website Design
Your website is...
|
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();...
|
by: TSSRALBI |
last post by:
Hello
I'm a network technician in training and I need your help.
I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs.
The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols.
I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
|
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
|
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
|
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
| |
by: bsmnconsultancy |
last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...
| |