Reply
Is it possible to count columns...
Old 07-23-2008, 05:17 PM Is it possible to count columns...
mgraphic's Avatar
Truth Seeker

Latest Blog Post:
Wireless Audio
Posts: 2,320
Name: Keith Marshall
Location: West Hartford, CT
Hi everyone, I have a strange question for you. Is it possible to return the count of columns that meet a condition just as you can count rows? The engine I'm using is MySQL.

Here is my table structure:
Code:
CREATE TABLE intelligent_offer (
  products_id int(11) unsigned NOT NULL default '0',
  pid1 int(11) unsigned NOT NULL default '0',
  score1 float(11,3) NOT NULL default '0.000',
  pid2 int(11) unsigned NOT NULL default '0',
  score2 float(11,3) NOT NULL default '0.000',
  pid3 int(11) unsigned NOT NULL default '0',
  score3 float(11,3) NOT NULL default '0.000',
  pid4 int(11) unsigned NOT NULL default '0',
  score4 float(11,3) NOT NULL default '0.000',
  pid5 int(11) unsigned NOT NULL default '0',
  score5 float(11,3) NOT NULL default '0.000',
  pid6 int(11) unsigned NOT NULL default '0',
  score6 float(11,3) NOT NULL default '0.000',
  pid7 int(11) unsigned NOT NULL default '0',
  score7 float(11,3) NOT NULL default '0.000',
  pid8 int(11) unsigned NOT NULL default '0',
  score8 float(11,3) NOT NULL default '0.000',
  pid9 int(11) unsigned NOT NULL default '0',
  score9 float(11,3) NOT NULL default '0.000',
  pid10 int(11) unsigned NOT NULL default '0',
  score10 float(11,3) NOT NULL default '0.000',
  PRIMARY KEY  (products_id)
) ENGINE=MyISAM;
Here is what I want to do: I want to return 100 pidXX values that are greater than 0 so I can display the results in a paging list.

Most rows have all 10 pidXX columns over zero value, but there are many that don't.

Any suggestions or ideas how I could tackle this problem another way?

Thank you!
__________________

<mgraphic /> - I don't have a solution but I admire the problem.
mgraphic is offline
Reply With Quote
View Public Profile
 
When You Register, These Ads Go Away!
Old 07-23-2008, 05:52 PM Re: Is it possible to count columns...
JeremyMiller's Avatar
Full-Time TeraTasker

Posts: 984
Name: Jeremy Miller
Location: Reno, NV
Could you provide the exact question you're trying to ask the database? I'm a bit confused on what you're wanting: Do all pidXX columns need to be > 0? A subset? Or, are you searching by, for example, pid9 that you want to be > 0?
__________________
Jeremy Miller - TeraTask Technologies, LLC
Content Farmer - Automated Posting for Content & Blog Sites
JeremyMiller is offline
Reply With Quote
View Public Profile Visit JeremyMiller's homepage!
 
Old 07-23-2008, 07:36 PM Re: Is it possible to count columns...
Learning Newbie's Avatar
Moderator

Latest Blog Post:
My Wish for Webmaster Talk
Posts: 5,181
Name: John Alexander
Unpivot the table to get a representation as rows, instead of columns.
__________________
4 ways to improve the lives of the "bottom billion"

"HEY YOU KIDS GET OFF MY LAWN!" -John McCain
Learning Newbie is offline
Reply With Quote
View Public Profile
 
Old 07-23-2008, 08:02 PM Re: Is it possible to count columns...
JeremyMiller's Avatar
Full-Time TeraTasker

Posts: 984
Name: Jeremy Miller
Location: Reno, NV
Oh. I just re-read your post. Let me think about it for a bit; but, just to make sure, what you want to do is return each row with at least one non-zero in it, but only so many rows that the count of all non-zeroes across all rows == 100. In something closer to English. If the first row had 2 of its values as non-zero and the second row and 3 non-zeroes, then those 2 rows would count for 5 of the 100 results required.
__________________
Jeremy Miller - TeraTask Technologies, LLC
Content Farmer - Automated Posting for Content & Blog Sites
JeremyMiller is offline
Reply With Quote
View Public Profile Visit JeremyMiller's homepage!
 
Old 07-23-2008, 08:22 PM Re: Is it possible to count columns...
JeremyMiller's Avatar
Full-Time TeraTasker

Posts: 984
Name: Jeremy Miller
Location: Reno, NV
OK. Here's what I came up with.

To get the total number of entries per row with a pidxx value > 0, use

Code:
SELECT (pid1>0)+(pid2>0)+(pid3>0)+(pid4>0)+(pid5>0)+(pid6>0)+(pid7>0)+(pid8>0)+(pid9>0)+(pid10>0)  as col_count FROM `intelligent_offer`
The problem is that I don't think you can filter based on a rolling sum (though maybe a stored procedure or function would help?)

Based on what I understand of your requirements, you may want to try a table structure like this:

Code:
CREATE TABLE intelligent_offer (
  int_off int(10) unsigned NOT NULL auto_increment,
  entry_num enum('1','2','3','4','5','6','7','8','9','10') NOT NULL default '1',
  pid int(10) unsigned NOT NULL,
  score float unsigned NOT NULL,
  PRIMARY KEY  (int_off)
) ENGINE=MyISAM;
entry_num's type could be changed and you'd be able to store even more ids, plus the way to answer your question with that structure (and the SQL I gave above) provides more power.

Hope that helps.
__________________
Jeremy Miller - TeraTask Technologies, LLC
Content Farmer - Automated Posting for Content & Blog Sites
JeremyMiller is offline
Reply With Quote
View Public Profile Visit JeremyMiller's homepage!
 
Old 07-23-2008, 10:39 PM Re: Is it possible to count columns...
mgraphic's Avatar
Truth Seeker

Latest Blog Post:
Wireless Audio
Posts: 2,320
Name: Keith Marshall
Location: West Hartford, CT
Let me try to clear up my explaination:

With the data structure above, I have an automated script that fills the table with other browsed and shopped items related to each product item from our analytics data feed. Each product_id will at least have 1 pidXX with a related id number, but not necessarly will have all 10 pidXX columns filled (they will default to zereo value).

Now I want to build a "Top Sellers" page from the data from this table and want to show the top 100 pidXX items using pagination. How could I define that in the query? If a pidXX value is zereo, I don't want that counted as part of the 100 results.


Thanks for the help - I hope this may answer your questions!
__________________

<mgraphic /> - I don't have a solution but I admire the problem.
mgraphic is offline
Reply With Quote
View Public Profile
 
Old 07-24-2008, 01:30 AM Re: Is it possible to count columns...
JeremyMiller's Avatar
Full-Time TeraTasker

Posts: 984
Name: Jeremy Miller
Location: Reno, NV
Hmmm...Maybe sum up the matching values in each row and then order by that? Is that what you're talking about? I feel kinda dumb. I know I can help. I just don't get it. Must be a day to be dense for me.
__________________
Jeremy Miller - TeraTask Technologies, LLC
Content Farmer - Automated Posting for Content & Blog Sites
JeremyMiller is offline
Reply With Quote
View Public Profile Visit JeremyMiller's homepage!
 
Old 07-24-2008, 02:56 AM Re: Is it possible to count columns...
VirtuosiMedia's Avatar
Webmaster Talker

Posts: 735
Couldn't you just get an array length from your result set?
__________________
RSS Feed Tutorial
VirtuosiMedia is offline
Reply With Quote
View Public Profile Visit VirtuosiMedia's homepage!
 
Old 07-24-2008, 03:37 AM Re: Is it possible to count columns...
tripy's Avatar
Fetchez la vache!

Posts: 2,054
Name: Thierry
Location: In the void
This could (may) be of use to you: http://www.jumbabox.com/2008/06/usin...base-tutorial/
__________________
Listen to the ducky: "This is awesome!!!"

tripy is online now
Reply With Quote
View Public Profile
 
Old 07-24-2008, 02:09 PM Re: Is it possible to count columns...
mgraphic's Avatar
Truth Seeker

Latest Blog Post:
Wireless Audio
Posts: 2,320
Name: Keith Marshall
Location: West Hartford, CT
Quote:
Originally Posted by JeremyMiller View Post
Hmmm...Maybe sum up the matching values in each row and then order by that? Is that what you're talking about? I feel kinda dumb. I know I can help. I just don't get it. Must be a day to be dense for me.
This is similar to the option I was thinking. Let me give you a sample list of row data so you can see whar I am talking about. These rows go with the data structure above.

Code:
INSERT INTO `intelligent_offer` VALUES (10759, 10362, 17998.561, 4155, 17998.561, 10752, 13998.880, 10460, 11999.040, 10654, 11999.040, 10750, 11999.040, 10756, 11999.040, 11284, 7999.360, 10016, 5999.520, 10033, 5999.520);
INSERT INTO `intelligent_offer` VALUES (10760, 10613, 17998.561, 10642, 17998.561, 10750, 17998.561, 10753, 17998.561, 10757, 17998.561, 13014, 17998.561, 13257, 17998.561, 0, 0.000, 0, 0.000, 0, 0.000);
INSERT INTO `intelligent_offer` VALUES (10761, 10084, 8999.280, 10364, 8999.280, 10654, 8999.280, 10753, 8999.280, 0, 0.000, 0, 0.000, 0, 0.000, 0, 0.000, 0, 0.000, 0, 0.000);
INSERT INTO `intelligent_offer` VALUES (10763, 10706, 7023.940, 12973, 6584.940, 10781, 5121.620, 11308, 4389.960, 0, 0.000, 0, 40.000, 0, 0.000, 0, 0.000, 0, 0.000, 0, 0.000);
INSERT INTO `intelligent_offer` VALUES (10764, 10903, 10974.900, 10728, 9755.470, 0, 0.000, 0, 0.000, 0, 0.000, 0, 0.000, 0, 0.000, 0, 0.000, 0, 0.000, 0, 0.000);
INSERT INTO `intelligent_offer` VALUES (10766, 11512, 4703.530, 10770, 4703.530, 12954, 3135.690, 11455, 3135.690, 10772, 3135.690, 10620, 3135.690, 13071, 3135.690, 0, 0.000, 0, 0.000, 0, 0.000);
INSERT INTO `intelligent_offer` VALUES (10769, 10773, 6584.940, 12706, 4389.960, 10901, 4268.020, 9611, 3658.300, 15760, 3658.300, 15764, 3658.300, 2025, 3658.300, 0, 0.000, 0, 0.000, 0, 0.000);
INSERT INTO `intelligent_offer` VALUES (10770, 12954, 10974.900, 10773, 7316.600, 10778, 5487.450, 0, 0.000, 0, 0.000, 0, 0.000, 0, 0.000, 0, 0.000, 0, 0.000, 0, 0.000);
INSERT INTO `intelligent_offer` VALUES (10771, 12727, 6584.940, 12932, 6584.940, 10777, 5853.280, 12091, 4389.960, 14543, 4389.960, 0, 0.000, 0, 0.000, 0, 0.000, 0, 0.000, 0, 0.000);
INSERT INTO `intelligent_offer` VALUES (10772, 11580, 4115.590, 12104, 4115.590, 10722, 3919.610, 10629, 3658.300, 10769, 3658.300, 15787, 3658.300, 9066, 3292.470, 10766, 3135.690, 0, 0.000, 0, 0.000);
INSERT INTO `intelligent_offer` VALUES (10773, 11495, 14633.200, 10911, 7316.600, 10770, 7316.600, 0, 0.000, 0, 0.000, 0, 0.000, 0, 0.000, 0, 0.000, 0, 0.000, 0, 0.000);
INSERT INTO `intelligent_offer` VALUES (10777, 3823, 10974.900, 5588, 7316.600, 4716, 7316.600, 8674, 7316.600, 4343, 7316.600, 2586, 7316.600, 10771, 5853.280, 10778, 5487.450, 7584, 5487.450, 4699, 4877.730);
INSERT INTO `intelligent_offer` VALUES (10778, 12615, 8231.180, 14543, 5487.450, 10777, 5487.450, 10770, 5487.450, 10764, 3658.300, 10771, 3292.470, 12932, 2743.730, 12596, 2743.730, 0, 0.000, 0, 0.000);
From this list, could you return 50 of the highest scoring pidXX values?


Quote:
Originally Posted by VirtuosiMedia View Post
Couldn't you just get an array length from your result set?
I may just have to revert to something this simple if I can't get the db to do it.

Quote:
Originally Posted by tripy View Post
Thanks for the link, but it was over my head and worked with excell spreadsheet (?)
__________________

<mgraphic /> - I don't have a solution but I admire the problem.
mgraphic is offline
Reply With Quote
View Public Profile
 
Old 07-24-2008, 02:19 PM Re: Is it possible to count columns...
Learning Newbie's Avatar
Moderator

Latest Blog Post:
My Wish for Webmaster Talk
Posts: 5,181
Name: John Alexander
Quote:
Originally Posted by mgraphic View Post
I may just have to revert to something this simple if I can't get the db to do it.
To get the database to do it without writing miles of SQL code, you need to express the data as rows, not columns. There's no other way, that doesn't involve enough text to fill a hardcover.

An unpivot query is the easiest way to do this. Failing that, you can use SQL to write and then execute a series of union subqueries, such that the data will be presented as

ProductID, PID, Score
ProductID, PID, Score
ProductID, PID, Score
ProductID, PID, Score
__________________
4 ways to improve the lives of the "bottom billion"

"HEY YOU KIDS GET OFF MY LAWN!" -John McCain
Learning Newbie is offline
Reply With Quote
View Public Profile
 
Old 07-24-2008, 02:21 PM Re: Is it possible to count columns...
JeremyMiller's Avatar
Full-Time TeraTasker

Posts: 984
Name: Jeremy Miller
Location: Reno, NV
So, Learning, the alternate table structure I provided above, right? It seemed to me like the database needed to be normalized to answer the query being asked. It's an easy question with the other table structure, very hard with the current.
__________________
Jeremy Miller - TeraTask Technologies, LLC
Content Farmer - Automated Posting for Content & Blog Sites
JeremyMiller is offline
Reply With Quote
View Public Profile Visit JeremyMiller's homepage!
 
Old 07-24-2008, 03:26 PM Re: Is it possible to count columns...
Learning Newbie's Avatar
Moderator

Latest Blog Post:
My Wish for Webmaster Talk
Posts: 5,181
Name: John Alexander
Quote:
Originally Posted by JeremyMiller View Post
So, Learning, the alternate table structure I provided above, right? It seemed to me like the database needed to be normalized to answer the query being asked. It's an easy question with the other table structure, very hard with the current.
Yours was the perfect answer, and I would guess your table structure will pose other benefits beyond this one.
__________________
4 ways to improve the lives of the "bottom billion"

"HEY YOU KIDS GET OFF MY LAWN!" -John McCain
Learning Newbie is offline
Reply With Quote
View Public Profile
 
Old 07-24-2008, 07:22 PM Re: Is it possible to count columns...
mgraphic's Avatar
Truth Seeker

Latest Blog Post:
Wireless Audio
Posts: 2,320
Name: Keith Marshall
Location: West Hartford, CT
Quote:
Originally Posted by Learning Newbie View Post
To get the database to do it without writing miles of SQL code, you need to express the data as rows, not columns. There's no other way, that doesn't involve enough text to fill a hardcover.

An unpivot query is the easiest way to do this. Failing that, you can use SQL to write and then execute a series of union subqueries, such that the data will be presented as
OK - I will look into this, unpivot query is a brand new concept for me, I have never heard of it until now. I'm always and forever learning!
__________________

<mgraphic /> - I don't have a solution but I admire the problem.
mgraphic is offline
Reply With Quote
View Public Profile
 
Old 07-24-2008, 07:52 PM Re: Is it possible to count columns...
Learning Newbie's Avatar
Moderator

Latest Blog Post:
My Wish for Webmaster Talk
Posts: 5,181