Database Help
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me
Go Back   Codewalkers ForumsOther TechnologiesDatabase Help

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Codewalkers Forums Sponsor:
  #1  
Old March 5th, 2003, 08:32 AM
dizy dizy is offline
Codewalkers Newbie (0 - 499 posts)
 
Join Date: Apr 2007
Location: davao city, philippines
Posts: 2 dizy User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
expand fields to become column headers

hi, how can i convert fields into a row? i mean, i want to expand a certain column in such a way that there will be no redundancy in the column.

example from table:
TOOLS YEAR PURCHASE
hammer 2000 2
plier 2000 1
screw 2000 4
hammer 2001 3
plier 2001 1
screw 2001 4
hammer 2002 2
plier 2002 1
screw 2002 1

i want to convert it this way!
TOOLS 2000 2001 2002 sum(purchase)
hammer 2 3 2 7
plier 1 1 1 3
screw 4 4 1 9

in this way, it is much easy to understand. the column 2000 involves the sum of the tools in this year and so on. the sum(purchase) is the total number of purchase from year 2000, 2001, and 2002 of a certain tool.

our proposed statement would be to include an HTAB word in the query. for example:
select tools, htab(year) from product group by tool;

the htab would recognize the column to expand based from the fields.

i am making a udf for mysql and had a hard time doing this.

can somebody please help me with the codes for doing the udf to come up with such table? included here is my whole udf program code but not yet finished. thanks!

php Code:
Original - php Code
  1.  
  2.  
  3. struct htab_data
  4. {
  5.   unsigned long long count;
  6.   long long total;
  7. };
  8.  
  9.  
  10. my_bool
  11. htab_init( UDF_INIT* initid, UDF_ARGS* args, char* message )
  12. {
  13.   struct htab_data* data;
  14.  
  15.   if (args->arg_count < 1)
  16.   {
  17.     strcpy(
  18.        message,
  19.        "wrong number of arguments: HTAB() requires one argument"
  20.        );
  21.     return 1;
  22.   }
  23.  
  24.   if ((args->arg_type[0] != INT_RESULT))/* || (args->arg_type[1] != INT_RESULT*/
  25.   {
  26.     strcpy(
  27.        message,
  28.        "wrong argument type: HTAB() requires SOMETHING"
  29.        );
  30.     return 1;
  31.   }
  32.  
  33.   /*
  34.   **    force arguments to double.
  35.   */
  36.   /*args->arg_type[0]   = REAL_RESULT;
  37.     args->arg_type[1]   = REAL_RESULT;*/
  38.  
  39.   initid->maybe_null    = 0;        // The result may be null
  40.   initid->decimals  = 0;    // We want 4 decimals in the result
  41.   initid->max_length    = 20;      // 6 digits + . + 10 decimals
  42.  
  43.   data = new struct htab_data;
  44.   data->total = 0;
  45.  
  46.   initid->ptr = (char*)data;
  47.  
  48.   return 0;
  49. }
  50.  
  51. void
  52. avgcost_deinit( UDF_INIT* initid )
  53. {
  54.   delete initid->ptr;
  55. }
  56.  
  57. void
  58. htab_reset( UDF_INIT* initid, UDF_ARGS* args, char* is_null, char* message )
  59. {
  60.   struct htab_data* data = (struct htab_data*)initid->ptr;
  61.   data->total = 0;
  62.   data->count = 0;
  63.  
  64.   *is_null = 0;
  65.   htab_add( initid, args, is_null, message );
  66. }
  67.  
  68.  
  69. void
  70. htab_add( UDF_INIT* initid, UDF_ARGS* args, char* is_null, char* message )
  71. {
  72.   if (args->args[0] && args->args[1])
  73.   {
  74.     struct htab_data* data  = (struct htab_data*)initid->ptr;
  75.     long long quantity    = *((long long*)args->args[0]);
  76.     long long newquantity   = data->total + quantity;
  77.     //long long number  = *((long long*)args->args[1]);
  78.  
  79.     data->count++;
  80.  
  81.     if (   ((data->total >= 0) && (quantity < 0))
  82.        || ((data->total0) && (quantity > 0)) )
  83.     {
  84.       /*
  85.       **    passing from + to - or from - to +
  86.       */
  87.       if (   ((quantity < 0) && (newquantity < 0))
  88.          || ((quantity > 0) && (newquantity > 0)) )
  89.       {
  90.         data->total = (newquantity);
  91.       }
  92.       /*
  93.       **    sub q if totalq > 0
  94.       **    add q if totalq < 0
  95.       */
  96.       else
  97.       {
  98.         //number      = data->total;
  99.         data->total  = (newquantity);
  100.       }
  101.     }
  102.     else
  103.     {
  104.       data->total   += (quantity);
  105.     }
  106.  
  107.     if (data->total== 0)
  108.       data->total = 0;
  109.   }
  110. }
  111.  
  112.  
  113. long long
  114. htab( UDF_INIT* initid, UDF_ARGS* args, char* is_null, char* error )
  115. {
  116.   /* This is the function in which the fields defined in the
  117.      htab() would be converted into a column head*/
  118.  
  119.   struct htab_data* data = (struct htab_data*)initid->ptr;
  120.   if (!data->count || !data->total)
  121.   {
  122.     *is_null = 1;
  123.     return 0;
  124.   }
  125.  
  126.   *is_null = 0;
  127.   return data->total;
  128. }

Reply With Quote
Reply

Viewing: Codewalkers ForumsOther TechnologiesDatabase Help > expand fields to become column headers


Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


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

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump


Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 





© 2003-2008 by Developer Shed. All rights reserved. DS Cluster 4 hosted by Hostway