Tycoon Talk
Become a Big fish!
The number 1 forum for online business!
Post topics, ask questions, share your knowledge.
Tycoon Talk is part of Freelancer.com - find skilled workers online at a fraction of the cost.

The Database Forum


You are currently viewing our The Database Forum as a guest. Please register to participate.
Login



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

Latest Blog Post:
JAMISONTUNES
Posts: 2,898
Name: Keith Marshall
Location: Connecticut
Trades: 0
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 online now
Reply With Quote
View Public Profile
 
 
Register now for full access!
Old 07-23-2008, 06:52 PM Re: Is it possible to count columns...
JeremyMiller's Avatar
WT Moderator

Posts: 1,712
Name: Jeremy Miller
Location: Las Vegas, NV
Trades: 0
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

Please login or register to view this content. Registration is FREE
JeremyMiller is offline
Reply With Quote
View Public Profile Visit JeremyMiller's homepage!
 
Old 07-23-2008, 08:36 PM Re: Is it possible to count columns...
Learning Newbie's Avatar
Defies a Status

Latest Blog Post:
Astounding Republican Paranoia
Posts: 5,662
Name: John Alexander
Trades: 0
Unpivot the table to get a representation as rows, instead of columns.
__________________

Please login or register to view this content. Registration is FREE


Please login or register to view this content. Registration is FREE
Learning Newbie is offline
Reply With Quote
View Public Profile
 
Old 07-23-2008, 09:02 PM Re: Is it possible to count columns...
JeremyMiller's Avatar
WT Moderator

Posts: 1,712
Name: Jeremy Miller
Location: Las Vegas, NV
Trades: 0
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

Please login or register to view this content. Registration is FREE
JeremyMiller is offline
Reply With Quote
View Public Profile Visit JeremyMiller's homepage!
 
Old 07-23-2008, 09:22 PM Re: Is it possible to count columns...
JeremyMiller's Avatar
WT Moderator

Posts: 1,712
Name: Jeremy Miller
Location: Las Vegas, NV
Trades: 0
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

Please login or register to view this content. Registration is FREE
JeremyMiller is offline
Reply With Quote
View Public Profile Visit JeremyMiller's homepage!
 
Old 07-23-2008, 11:39 PM Re: Is it possible to count columns...
mgraphic's Avatar
Truth Seeker

Latest Blog Post:
JAMISONTUNES
Posts: 2,898
Name: Keith Marshall
Location: Connecticut
Trades: 0
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 online now
Reply With Quote
View Public Profile
 
Old 07-24-2008, 02:30 AM Re: Is it possible to count columns...
JeremyMiller's Avatar
WT Moderator

Posts: 1,712
Name: Jeremy Miller
Location: Las Vegas, NV
Trades: 0
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

Please login or register to view this content. Registration is FREE
JeremyMiller is offline
Reply With Quote
View Public Profile Visit JeremyMiller's homepage!
 
Old 07-24-2008, 03:56 AM Re: Is it possible to count columns...
VirtuosiMedia's Avatar
Web Design Made Simple

Posts: 1,228
Trades: 0
Couldn't you just get an array length from your result set?
__________________
Want new web resources every day? - Follow me on
Please login or register to view this content. Registration is FREE


Please login or register to view this content. Registration is FREE


Please login or register to view this content. Registration is FREE
|
Please login or register to view this content. Registration is FREE
VirtuosiMedia is offline
Reply With Quote
View Public Profile Visit VirtuosiMedia's homepage!
 
Old 07-24-2008, 04:37 AM Re: Is it possible to count columns...
tripy's Avatar
Do not try this at home!

Posts: 3,621
Name: Thierry
Location: I'm the uber Spaminator !
Trades: 0
This could (may) be of use to you: http://www.jumbabox.com/2008/06/usin...base-tutorial/
__________________
Only a biker knows why a dog sticks his head out the window.
tripy is offline
Reply With Quote
View Public Profile Visit tripy's homepage!
 
Old 07-24-2008, 03:09 PM Re: Is it possible to count columns...
mgraphic's Avatar
Truth Seeker

Latest Blog Post:
JAMISONTUNES
Posts: 2,898
Name: Keith Marshall
Location: Connecticut
Trades: 0
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 online now
Reply With Quote
View Public Profile
 
Old 07-24-2008, 03:19 PM Re: Is it possible to count columns...
Learning Newbie's Avatar
Defies a Status

Latest Blog Post:
Astounding Republican Paranoia
Posts: 5,662
Name: John Alexander
Trades: 0
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
__________________

Please login or register to view this content. Registration is FREE


Please login or register to view this content. Registration is FREE
Learning Newbie is offline
Reply With Quote
View Public Profile
 
Old 07-24-2008, 03:21 PM Re: Is it possible to count columns...
JeremyMiller's Avatar
WT Moderator

Posts: 1,712
Name: Jeremy Miller
Location: Las Vegas, NV
Trades: 0
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

Please login or register to view this content. Registration is FREE
JeremyMiller is offline
Reply With Quote
View Public Profile Visit JeremyMiller's homepage!
 
Old 07-24-2008, 04:26 PM Re: Is it possible to count columns...
Learning Newbie's Avatar
Defies a Status

Latest Blog Post:
Astounding Republican Paranoia
Posts: 5,662
Name: John Alexander
Trades: 0
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.
__________________

Please login or register to view this content. Registration is FREE


Please login or register to view this content. Registration is FREE
Learning Newbie is offline
Reply With Quote
View Public Profile
 
Old 07-24-2008, 08:22 PM Re: Is it possible to count columns...
mgraphic's Avatar
Truth Seeker

Latest Blog Post:
JAMISONTUNES
Posts: 2,898
Name: Keith Marshall
Location: Connecticut
Trades: 0
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 online now
Reply With Quote
View Public Profile
 
Old 07-24-2008, 08:52 PM Re: Is it possible to count columns...
Learning Newbie's Avatar
Defies a Status

Latest Blog Post:
Astounding Republican Paranoia
Posts: 5,662
Name: John Alexander
Trades: 0
We're all forever learning - as long as we're open to it!

Here's something I copied from the MS documentation. I'm not sure where to find the corresponding MySQL?

Quote:
UNPIVOT performs almost the reverse operation of PIVOT, by rotating columns into rows. Suppose the table produced in the previous example is stored in the database as pvt, and you want to rotate the column identifiers Emp1, Emp2, Emp3, Emp4, and Emp5 into row values that correspond to a particular vendor. This means that you must identify two additional columns. The column that will contain the column values that you are rotating (Emp1, Emp2,...) will be called Employee, and the column that will hold the values that currently reside under the columns being rotated will be called Orders. These columns correspond to the pivot_column and value_column, respectively, in the Transact-SQL definition. Here is the query.
Copy Code

--Create the table and insert values as portrayed in the previous example.
CREATE TABLE pvt (VendorID int, Emp1 int, Emp2 int,
Emp3 int, Emp4 int, Emp5 int)
GO
INSERT INTO pvt VALUES (1,4,3,5,4,4)
INSERT INTO pvt VALUES (2,4,1,5,5,5)
INSERT INTO pvt VALUES (3,4,3,5,4,4)
INSERT INTO pvt VALUES (4,4,2,5,5,4)
INSERT INTO pvt VALUES (5,5,1,5,5,5)
GO
--Unpivot the table.
SELECT VendorID, Employee, Orders
FROM
(SELECT VendorID, Emp1, Emp2, Emp3, Emp4, Emp5
FROM pvt) p
UNPIVOT
(Orders FOR Employee IN
(Emp1, Emp2, Emp3, Emp4, Emp5)
)AS unpvt
GO
Here is a partial result set.
Copy Code

VendorID Employee Orders
1 Emp1 4
1 Emp2 3
1 Emp3 5
1 Emp4 4
1 Emp5 4
2 Emp1 4
2 Emp2 1
2 Emp3 5
2 Emp4 5
2 Emp5 5
__________________

Please login or register to view this content. Registration is FREE


Please login or register to view this content. Registration is FREE
Learning Newbie is offline
Reply With Quote
View Public Profile
 
Old 07-26-2008, 04:37 PM Re: Is it possible to count columns...
mgraphic's Avatar
Truth Seeker

Latest Blog Post:
JAMISONTUNES
Posts: 2,898
Name: Keith Marshall
Location: Connecticut
Trades: 0
Well - I have been derailed and put on other projects so I have not had the chance to try these solutions yet. Thank you for your help though.

You help is appreciated, thanks for sharing your knowledge. I wanted to give TP before it got too late (I wish I could give 2-fold).

__________________

<mgraphic /> - I don't have a solution but I admire the problem.
mgraphic is online now
Reply With Quote
View Public Profile
 
Reply     « Reply to Is it possible to count columns...
 

Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are Off
Pingbacks are Off
Refbacks are Off





   
RSS Feed  Feeds: RSS   JS   XML
RSS Feed  Feeds for this forum: RSS   JS   XML



Page generated in 0.53843 seconds with 12 queries