Function/Procedure help - just want to build simple query and show results

I'd like a simple "histogram" function - perhaps there is one built-in I'm not aware of. I just want to count discrete values of a column. So I'm constantly writing queries of the form:
select column_name, count(*)
from table_name
group by column_name
order by column_name;
and I'm trying to write a function that will allow me to just type:
execute whatever(column_name, table_name)
or select whatever(column_name, table_name) from ?
and get the equivalent results from the query above.
How do you, or can you, write a procedure to take column/table names as inputs, substitue them into a query, execute the query and just display the results of that query? As I'm trying to figure out how to do this via google search, I keep going down rabbit holes of creating temporary tables/data types/records/refcursors etc. and I can't get any of it to compile. I'm not a DBA or SQL expert, so I'm hoping there's a simple way to accomplish this (without having to store and then process the "results" one row at a time)

If I understand the requirement correctly, a simple pipelined function will do it:
Dynamic
GROUP_VALUE                    NUM_RECS
10                                    1
20                                    2
30                                    6
40                                    1
50                                   45
60                                    5
70                                    1
80                                   34
90                                    3
100                                   6
110                                   2
                                      1
12 rows selected.
  1  SELECT *
  2* FROM TABLE (Gen_Counts.Group_Count ('EMPLOYEES', 'DEPARTMENT_ID'))
Static
DEPARTMENT_ID   COUNT(*)
           10          1
           20          2
           30          6
           40          1
           50         45
           60          5
           70          1
           80         34
           90          3
          100          6
          110          2
                       1
12 rows selected.
  1  SELECT department_id, Count(*)
  2    FROM employees
  3   GROUP BY department_id
  4*  ORDER BY 1The types and package code is:
CREATE OR REPLACE TYPE group_count_type IS OBJECT
    (group_value VARCHAR2(4000), num_recs NUMBER);
CREATE OR REPLACE TYPE group_count_list_type IS TABLE OF group_count_type;
CREATE OR REPLACE PACKAGE Gen_Counts IS
FUNCTION Group_Count (p_tab_name VARCHAR2, p_col_name VARCHAR2) RETURN group_count_list_type PIPELINED;
END Gen_Counts;
CREATE OR REPLACE PACKAGE BODY Gen_Counts IS
FUNCTION Group_Count (p_tab_name VARCHAR2, p_col_name VARCHAR2) RETURN group_count_list_type PIPELINED IS
  l_cur             SYS_REFCURSOR;
  l_rec_list    group_count_list_type;
  l_cur_str     VARCHAR2(1000) := 'SELECT group_count_type (' || p_col_name || ', Count(*)) FROM ' || p_tab_name ||
    ' GROUP BY ' || p_col_name || ' ORDER BY ' || p_col_name;
BEGIN
  OPEN l_cur FOR l_cur_str;
  DBMS_Output.Put_Line (l_cur_str);
  LOOP
    FETCH l_cur BULK COLLECT
     INTO l_rec_list LIMIT 1000;
    EXIT WHEN l_rec_list.COUNT = 0;
    FOR i IN 1..l_rec_list.COUNT LOOP
      PIPE ROW (group_count_type (l_rec_list(i).group_value, l_rec_list(i).num_recs));
    END LOOP;
  END LOOP;
END Group_Count;
END Gen_Counts;
/Edited by: BrendanP on 06-Sep-2012 07:18
Just to be clear - I'm not saying it's a great idea to do that, just that's a way of doing it technically. I'd just use the static way myself.

Similar Messages

  • HT201250 I am having trouble setting up my time capsule.  the amber light is flashing.  i have a Verizon FIOS network that i use for the internet (WiFi) and do not want to configure the Time Capsule to do that function.  I just want the Time Capsule to ba

    I am having trouble setting up my time capsule.  the amber light is flashing.  i have a Verizon FIOS network that i use for the internet (WiFi) and do not want to configure the Time Capsule to do that function.  I just want the Time Capsule to back up.

    Simple.. bridge the TC.. in the apple utility in the NAT and DHCP area.
    Plug it into the FIOS router.. you can turn off wireless in the TC if you want.. or use it.. it can work faster if you are nearby and can select 5ghz.. but up to you.

  • Hi, I have moved to India from france and I am trying to update my payment options to the Indian visa card but it seems that no matter what I do I cant do it. I dont want to create a new ID..Plz Help!!!Just want to change my location and my payment detail

    Hi, I have moved to India from france and I am trying to update my payment options to the Indian visa card but it seems that no matter what I do I cant do it. I dont want to create a new ID..Plz Help!!!Just want to change my location and my payment details!! thanks in advance!!

    These are two possible approaches that will normally work to move an existing library to a new computer.
    Method 1
    Backup the library with this User Tip.
    Deauthorize the old computer if you no longer want to access protected content on it.
    Restore the backup to your new computer using the same tool used to back it up.
    Keep your backup up-to-date in future.
    Method 2
    Connect the two computers to the same network. Share your <User's Music> folder from the old computer and copy the entire iTunes library folder into the <User's Music> folder on the new one. Again, deauthorize the old computer if no longer required.
    Both methods should give the new computer a working clone of the library that was on the old one. As far as iTunes is concerned this is still the "home" library for your devices so you shouldn't have any issues with iTunes wanting to erase and reload.
    I'd recommend method 1 since it establishes an ongoing backup for your library.
    Note if you have iOS devices and haven't moved your contacts and calendar items across then you should create one dummy entry of each in your new profile and iTunes should  merge the existing data from the device.
    If your media folder has been split out from the main iTunes folder you may need to do some preparatory work to make it easier to move. See make a split library portable.
    Should you be in the unfortunate position where you are no longer able to access your original library or a backup then then seeRecover your iTunes library from your iPod or iOS device for advice on how to set up your devices with a new library with the maximum preservation of data.
    tt2

  • I have an editable pdf made in Acrobat. Viewing it Adobe Reader Mobile (Android) and I can fill it fine. I want to be able to fill the form but it keeps autosaving. I just want to email the form and then it goes back to an unfilled form. How do i do that?

    I have an editable pdf made in Acrobat. Viewing it Adobe Reader Mobile (Android) and I can fill it fine. I want to be able to fill the form but it keeps autosaving. I just want to email the form and then it goes back to an unfilled form. How do i do that?

    Currently, Adobe Reader for Android does not provide an option to disable auto-save.
    In the Reader home screen (where your files are displayed), you can make a copy of the original PDF document, open the copy (instead of opening the original document), make changes to the copy.  Once you email the copy, you can delete it.
    For more information, please see Help > How To... > Copy, edit, delete, manage.
    Although it is not the most convenient way to accomplish the task, that's the only feasible workaround that is available in Adobe Reader for Android right now.

  • Error message has just occurred... This copy of itunes is corrupted. Please reinstall itunes. I'm using a Macbook operating on version 10.6.8 - not that this means anything to me. I just want to preserve my library and playlists! What do I do?!

    Error message has just occurred... This copy of itunes is corrupted. Please reinstall itunes. I'm using a Macbook operating on version 10.6.8 - not that this means anything to me. I just want to preserve my library and playlists! What do I do?!

    Let's try a repair install of iTunes.
    Restart the PC first.
    If you're using Vista or 7, now head into your Uninstall a program control panel, select "iTunes" and then click "Repair".
    If you're using XP, head into your Add or Remove Programs control panel, select "iTunes", and click "Change". Select "Repair" and click "Next" as per the following screenshot:
    Can you launch your iTunes now?

  • HT201328 Welcome to After I IPHONE 4 black and I want to open internationally so when you want to update the lock and leave me a sign I just want to open an apple and I have developed from my Mac in Saudi Arabia&

    Welcome to After I IPHONE 4 black and I want to open internationally so when you want to update the lock and leave me a sign I just want to open an apple and I have developed from my Mac in Saudi Arabia&amp; the
    <Edited by Host>

    If you want to unlock an iPhone you must contact the carrier it is locked to.
    You are not communicating with Apple in these forums; we are just other users.

  • I just want to launch the program and retouch a photo.[was:lost]

    i just want to launch the program and retouch a photo. why am i so lost?

    Exactly my point. With all the "training" and "introduction" and promotional
    hoopla on the CC platform, I couldn't even find a way to LAUNCH photoshop.
    I eventually simply downloaded Photoshop Elements. Problem solved.
    Your Partner in Development
    Robert Ott PMP
    Earth Energy Exchange, Ltd.
    631-371-6842

  • I have problem  in signing in I don't like read and accept terms I cannot access this I don't know about this read and accept terms this is poor, I just like android's simple option and simple singing in, but how to use apple I'd?

    I have problem  in signing in I don't like read and accept terms I cannot access this I don't know about this read and accept terms this is poor, I just like android's simple option and simple singing in, but how to use apple I'd?

    Your computer is called an iMac.
    Did you turn proxies on?  If so, you should turn them off.

  • Whenever I open firefox a add ons screen comes up with the yahoo web page behind it. . I just want the Yahoo web page and do not want to close the add ons page every time I use my computer.

    Whenever I open firefox a add ons screen comes up with the yahoo web page behind it. . I just want the Yahoo web page and do not want to close the add ons page every time I use my computer.
    I don't know what else to say about this. It is a screen that comes up in its own window everytime I open Firefox

    Please check your home page setting and see if about:addons is listed. If it is, please remove it
    https://support.mozilla.com/en-US/kb/How+to+set+the+home+page

  • Hello, I just want to formate the computer and start fresh??

    Hello, I just want to formate the computer and start fresh??

    NavarroSergio,
    since you have a Late 2011 model, you can use OS X Internet Recovery to start fresh.
    First, you’ll need to completely erase your internal disk. Boot your MacBook Pro into Recovery mode by holding down a Command key and the R key as it starts up. Once the Mac OS X Utilities menu appears, select Disk Utility. On the left-hand side of the Disk Utility window, select your internal disk (typically the first item in the list). On the right-hand side, click on the Erase tab. In the Format dropdown menu, select “Mac OS Extended (Journaled)”; in the Name textbox, enter a name for the reformatted volume (“Macintosh HD” is typical). Press the Erase… button, and erase your disk. When the erasure is completed, exit Disk Utility and select Shutdown from the Apple menu.
    The next time that you start up, OS X Internet Recovery will download your MacBook Pro’s original version of OS X (either 10.7.2 or 10.7.3) from Apple’s servers, and then install it. When it finishes, you’ll again see the welcome video just like when you first got it. Fill in your relevant information as you did the first time, and when you log in for the first time, run Software Update to get back to 10.7.5.

  • I just want to keep CSx updated and continue to use it like I have been.

    I'm an individual.. One computer!, no team or bank of computers... Just me..
    I don't need a cloud! I don't want a cloud!
    I just want to keep CSx updated. and continue to use it like I have been.
    Cost me about $600 every other year to keep this updated.
    I can afford $600/every other year.
    What do I need to do to keep using The Design Premium CSx ??

    Short answer:   You are [or eventually will be] outdated and out of luck.
    I've been a master collection user since it's been availaboe, but I've been struggling with this and other questions, and have come to the conclusion that I need to be proactive in looking for alternatives, so when the day comes that CS6 stops working on new hardware, I'll have options.
    My biggest concern is that with the current CC model, if a user decides to stop, or can't afford, their subscription fee, thirty days later they loose access to ALL THEIR CREATIVE WORK-- without monthly activation,  all the CC software STOPS WORKING!
    Moreover, after everyone is on the CC hook, what is to stop Adobe from charging $100 per month, or $300....  As a huge company driven by investors and quarterly profits they will charge whatever the market will bear.  Many will be held hostage to the subscription fee, no matter how high it goes, because they won't be able to afford to loose access their creative work.  So... going forward the market will probably bear a lot more than $49 per month.
    Then, if this wasn't enough risk, there is also the dependence on the web and Adobe's IT infrastructure to keep the software "activated".  I know it is probably extremely unlikely, but what if they suffer a cyber-attack like many other large and theoretically "secure" companies?  What kind of havoc could an attacker wreak with the creative community if suddenly all our software stopped working?  You may laugh, but I just read an article today on cNet Security about how Twitter's domain registration was hacked by SEA-- at least for a brief time, SEA was the admin for the Twitter domain records.
    Since hearing Adobe's plans, I realised that over the years, with the exception of 3D, I've gravitated to Adobe for all my creative work-- they really do make great software.  Even though this represents a huge investment in time and "learning curve", I've started to seriously evaluate alternatives.  When the day comes that CS6 is no longer working on new hardware, I hopefully will already be using other brands.
    My current thinking:  If Apple makes FCP 11 a professional tool again, that would be my first choice for video.  If they don't, I've been taking a hard look at Sony Vegas-- or there is always Avid.  For compositing, while I really know and love After Effects, I have a copy of Eyeon's Digital Fusion that came free with Lightwave3D that I've never even installed.  It is now out of the closet and sitting on my desk, waiting until I have a few minutes to install it to give it a try.  For photo editing and vector graphics, I've been looking at the Corel suite-- I haven't used Corel in years, as it always seemed "clunky" compared to Illustrator,  but it is a viable alternative, and used and loved by many.  Photoshop is an industry standard and I've used it for fifteen years, but I also have both Gimp and Cinepaint on Mac, and use them frequently already.  They aren't as capable or as slick as Photoshop, but they do everything I need, so photo editing is covered.  As far as web tools, Dreamweaver is nice, but web development can be done with any editor.  Flash (and all browser plugin technology) is being supplanted by HTML5 and Javascript, so loosing Flash, going forward, is no great loss.  I haven't used it in over a year anyway, but f I really need 2D animation there is always (gasp) Silverlight, and maybe also other tools that I'm currently unaware of.  InDesign isn't a concern for me as I almost never do print.
    So bottom line for me:   If Adobe works out a compromise where the software keeps working even if the subscription lapses, then it would be a different ballgame for me. I'd even be willing to agree to pay two or three year contract before the perpetual license kicked in.  However, as it stands, the CC subscription (and potentially vanishing software) represents a level of trust in Adobe that I'm not prepared to give.

  • Can I build a query to show monthly sales by item by business partner?

    Hi all
    Is it possible to build a query to show monthly sales (quantity not value) by item by business partner?
    So the table would look something like this.
                   Jan     Feb     Mar     Apr
    Item 1      10       4         8         7
    Item 2      4         3         5         6
    Item 3      4        12        9         3
    Item 4      1         0         1         2
    Etc...
    As you can see, the monthly figure needs to be quantity of the item and not sales value.
    Would be grateful for any help.
    Many thanks.
    Wendy

    Hi,
    Try this:
    declare @code as varchar(15)
    set @code = ( select max(ta.cardcode) from OINV ta where ta.cardcode = [%0])
    Select [a] as Cardcode, [B] as Cardname, [c] as Item#, [D] as Descr,[1] as Jan,[2] as Feb,[3]as Mar,[4] as April,[5] as May,[6] as June,[7] as July ,[8] as Aug,[9] as Sept,[10] as Oct ,[11]as Nov,[12] as Dec
    from(
    SELECT T0.[CardCode] as  A , T0.[CardName] as  B, T1.[ItemCode] as C, T1.[Dscription] as D, sum(T1.[Quantity]) as t,month(T0.[DocDate]) as month FROM OINV T0  INNER JOIN INV1 T1 ON T0.DocEntry = T1.DocEntry WHERE year( T0.[DocDate]) = 2014 and t0.cardcode = @code GROUP BY T0.[CardCode], T0.[CardName], T1.[ItemCode], T1.[Dscription],T0.[DocDate] ) S
    pivot
    (sum(t) for month IN ([1],[2],[3],[4],[5],[6],[7],[8],[9],[10],[11],[12])) P
    Thanks & Regards,
    Nagarajan

  • I want to use one apple id in my macbook and iPad but i don't want pics to get sync and show in both devices, how should i stop this ?

    I want to use one apple id in my macbook and iPad but i don't want pics to get sync and show in both devices, how should i stop this ?

    Turn off My PhotoStream in iCloud on both the iPad and the Mac. PhotoStream is what shares your photos (unless you are using iCloud Photo Library Beta, in which case, all photos uploaded are accessible from all devices signed onto the same Apple/iCloud ID).
    Cheers,
    GB

  • I have a power PC that makes abunch of noise when plugged in but the power light just blinks when I press it and shows no graphics wht is wrong

    I have a power PC that makes abunch of noise when plugged in but the power light just blinks when I press it and shows no graphics wht is wrong

    Click here for information; if the computer beeps three or fewer times, remove and reinsert the computer's RAM. If it beeps four or more times, it needs to be repaired. If it's a G5, it will ignore RAM modules that aren't part of a matching pair.
    (76386)

  • HT204266 I erase all application of my iphone 4 with erase restore and now it's in restore mode. Now is there any way to restore my iphone without backup. Please help me. I have no need my previous file i just want my i phone ok and work smoothly as new p

    Hi,
    I erase all application of my iphone 4 with erase restore without any backup & i also lose the product serial number. Now it's in restore mode. Now is there any way to restore my iphone without backup. Please help me. I have no need my previous file I just want my iphone4 ok and work smoothly as like new ipone justlike i bought it at first time. I can't restore it. So Please help me to solv this proble.

    See Here  > without the Backup  >  http://support.apple.com/kb/HT4137

Maybe you are looking for