Help with query to return results from group within table?

Hi
Im a total noob, so please be gentle....!
I am looking at a table that has a WORD column and a WORD_TYPE_ID column.
Words can have the same type_id.
What I am trying to do is write a query that will return the longest word for each word_type_id.
I have been trying for hours to get this and all I seem to get is either error messages or I return the longest word in the whole WORD column.
The furthest I can get before things break down is.....
select word
from table
where
length(word) =
select
max(length(word))
from table
Any help on this or if I could be pointed in the right direction it would be greatly appreciated.
Thanks

Hi,
Welcome to the forum!
Here's one way:
SELECT    word_type_id
,       MIN (word) KEEP (DENSE_RANK LAST ORDDER BY LENGTH (word))     AS longest_word
FROM       table_x
GROUP BY  word_type_id;If there happens to be a tie within some word_type_id (that is, 2 or more words have exactly the same length, which is the longest in that group) the expression above will return the first one, in alphabetic order. (That's what MIN means here.)
Edited by: Frank Kulash on Aug 11, 2009 1:56 PM
You almost had it.
As you noticed, you were getting the longest row in the whole table. That's because your sub-query was looking at the whole table.
If you correlate the sub-query to the row in the main table, as shown below, you can get the longest word in each group:
select  word
from     table     m                              -- m for main
where      length (word) = (
                            select  max (length(word))
               from     table
               where     word_type_id  = m.word_type_id     -- New
               );

Similar Messages

  • Help with query to return last possible value

    Can someone please help me create a query for the situation below?
    Table: TABLEA
    Columns:
    FACID VARCHAR2(10),
    DEPTID VARCHAR2(10),
    CHARGENUMBER NUMBER(10)
    I have the following data:
    A, B, 1
    A, B, 2
    C, D, 3
    C, D, 4
    Would like to return the following:
    A, B, 2
    C, D, 4
    In other words, I would like to return the last possible CHARGENUMBER for FACID and DEPTID.
    The table does not have any indexes and it gets loaded from a spreadsheet in that order.
    Thanks so much!

    amazongirl wrote:
    Can someone please help me create a query for the situation below?
    Table: TABLEA
    Columns:
    FACID VARCHAR2(10),
    DEPTID VARCHAR2(10),
    CHARGENUMBER NUMBER(10)
    I have the following data:
    A, B, 1
    A, B, 2
    C, D, 3
    C, D, 4
    Would like to return the following:
    A, B, 2
    C, D, 4
    In other words, I would like to return the last possible CHARGENUMBER for FACID and DEPTID.
    The table does not have any indexes and it gets loaded from a spreadsheet in that order.
    Thanks so much!Hi girl from amazon,
    I hope you give me a note because I answered for a lot of questions and nobody aprecieted that. If you don't me a little point I wil leave this forum because isn't so funny to work for nothing.
    You are my last chance to stay here!
    FACID      DEPTID     CHARGENUMBER
    A          B                     1
    A          B                     2
    C          D                     3
    C          D                     4
    HR: XE > select x.FACID, x.DEPTID , x.CHARGENUMBER from tableA x,
      2   (select DEPTID, FACID, max(CHARGENUMBER) max_val from tableA
      3    group by  DEPTID, FACID ) y
      4    WHERE x.DEPTID = y.DEPTID and
      5          x.FACID  = y.FACID and
      6          x.CHARGENUMBER =max_val;
    FACID      DEPTID     CHARGENUMBER
    A          B                     2
    C          D                     4Regards,
    Ion

  • Help with writing and retrieving data from a table field with type "LCHR"

    Hi Experts,
    I need help with writing and reading data from a database table field which has a type of "LCHR". I have given an example of the original code but don't know what to change it to in order to fix it and still read in the original data that's stored in the LCHR field.
    Basically we have two Function modules, one that saves list data to a database table and one that reads in this data. Both Function modules have an identicle table which has an array of fields from type INT4, CHAR, and type P. The INT4 field is the first one.
    Incidentally this worked in the 4.7 non-unicode system but is now dumping in the new ECC6 Unicode system.
    Thanks in advance,
    C
    SAVING THE LIST DATA TO DB
    DATA: L_WA(800).
    LOOP AT T_TAB into L_WA.
    ZDBTAB-DATALEN = STRLEN( L_WA ).
    MOVE: L_WA to ZDBTAB-RAWDATA.
    ZDBTAB-LINENUM = SY-TABIX.
    INSERT ZDBTAB.
    READING THE DATA FROM DB
    DATA: BEGIN OF T_DATA,
                 SEQNR type ZDBTAB-LINENUM,
                 DATA type ZDBTAB-RAWDATA,
               END OF T_TAB.
    Select the data.
    SELECT linenum rawdata from ZDBTAB into table T_DATA
         WHERE repid = w_repname
         AND rundate = w_rundate
         ORDER BY linenum.
    Populate calling Internal Table.
    LOOP AT T-DATA.
    APPEND T_DATA to T_TAB.
    ENDLOOP.

    Hi Anuj,
    The unicode flag is active.
    When I run our report and then to try and save the list data a dump is happening at the following point
    LOOP AT T_TAB into L_WA.
    As I say, T_TAB consists of different fields and field types whereas L_WA is CHAR 800. The dump mentions UC_OBJECTS_NOT_CONVERTIBLE
    When I try to load a saved list the dump is happening at the following point
    APPEND T_DATA-RAWDATA to T_TAB.
    T_DATA-RAWDATA is type LCHR and T_TAB consists of different fields and field types.
    In both examples the dumps mention UC_OBJECTS_NOT_CONVERTIBLE
    Regards
    C

  • Return results from ADEP in AS3 Arrays, not ArrayCollections

    Hi all,
    Is there any way to force ADEP to return results from data services in simple AS3 Arrays, not ArrayCollections? Here are my situation:
    In my project I use ADEP Data Management Services. To connect to ADEP services we use RTMP channel defined in services-config.xml:
    <?xml version="1.0" encoding="UTF-8"?>
    <services-config>
         <services>
              <service-include file-path="remoting-config.xml" />
              <service-include file-path="proxy-config.xml" />
              <service-include file-path="messaging-config.xml" />
              <service-include file-path="data-management-config.xml" />
              <service-include file-path="managed-remoting-config.xml" />
              <service class="fiber.data.services.ModelDeploymentService" id="model-deploy-service" />
              <default-channels>
                   <channel ref="my-rtmp"/>
              </default-channels>
         </services>
         <channel-definition id="my-rtmp" class="mx.messaging.channels.RTMPChannel">
              <endpoint url="rtmp://{server.name}:1000" class="flex.messaging.endpoints.RTMPEndpoint"/>
              <properties>
                   <idle-timeout-minutes>20</idle-timeout-minutes>
                   <block-rtmpt-polling-clients>true</block-rtmpt-polling-clients>
                   <rtmpt-poll-wait-millis-on-client>0</rtmpt-poll-wait-millis-on-client>
              </properties>
         </channel-definition>
    </services-config>
    To manage data in database we defined data services in data-management-config.xml like this:
    <?xml version="1.0" encoding="UTF-8"?>
    <service id="data-service" class="flex.data.DataService">
        <adapters>
            <adapter-definition id="java-dao" class="flex.data.adapters.JavaAdapter"/>
            <adapter-definition id="mr-java-dao" class="flex.data.adapters.ManagedRemotingDataAdapter" />
            <adapter-definition id="actionscript" class="flex.data.adapters.ASObjectAdapter" default="true"/>
        </adapters>
        <default-channels>
            <channel ref="my-rtmp"/>
        </default-channels>
        <destination id="SomeProduct.SomeItems">
            <adapter ref="java-dao" />
            <properties>
                <source>flex.data.assemblers.SQLAssembler</source>
                <scope>application</scope>
                <metadata>
                    <identity property="ID" />
                </metadata>
                <server>
                    <database>
                        <datasource>java:comp/env/jdbc/SP</datasource>
                    </database>
                    <actionscript-class>com.somecompany.classes.SomeCoolClass</actionscript-class>
                    <create-item>
                        <procedure name="SomeItems_Insert">
                            <procedure-param property-value="#Session_ID#" />
                            <procedure-param property-value="#Division_ID#" />
                            <procedure-param property-value="#Salesrep_ID#" />
                            <procedure-param property-value="#Area_Code#" />
                            <procedure-param property-value="#Item_ID#" />
                        </procedure>
                        <id-query>SELECT IDENT_CURRENT('Work_Area_Item')</id-query>
                    </create-item>
                    <fill>
                        <name>all</name>
                        <procedure name="SomeItems_Get">
                            <procedure-param property-value="#Session_ID#" />
                            <procedure-param property-value="#Last_Sync_Time#" />
                        </procedure>
                    </fill>
                    <update-item>
                        <procedure name="SomeItems_Update">
                            <procedure-param property-value="#Session_ID#" />
                            <procedure-param property-value="#Division_ID#" />
                            <procedure-param property-value="#Salesrep_ID#" />
                            <procedure-param property-value="#Area_Code#" />
                            <procedure-param property-value="#Item_ID#" />
                       </procedure>
                    </update-item>
                    <delete-item>
                        <procedure name="SomeItems_Delete">
                            <procedure-param property-value="#Session_ID#" />
                            <procedure-param property-value="#Item_ID#" />
                        </procedure>
                    </delete-item>
                </server>
            </properties>
        </destination>
    </service>
    By default, ADEP returns results from SomeProduct.SomeItems destination to Flex side as ArrayCollection of SomeCoolClass instances but I need the data to be returned in simple AS3 Arrays. Recently, I found that there is small optional serialization configuration in channel-definition that should resolve my problem. So I updated my channel-definition in services-config.xml to this:
    <channel-definition id="my-rtmp" class="mx.messaging.channels.RTMPChannel">
         <endpoint url="rtmp://{server.name}:1000" class="flex.messaging.endpoints.RTMPEndpoint"/>
         <properties>
              <serialization>
                   <legacy-collection>true</legacy-collection>
              </serialization>
              <idle-timeout-minutes>20</idle-timeout-minutes>
              <block-rtmpt-polling-clients>true</block-rtmpt-polling-clients>
              <rtmpt-poll-wait-millis-on-client>0</rtmpt-poll-wait-millis-on-client>
         </properties>
    </channel-definition>
    However, result are still returned in ArrayCollections.
    Any ideas?
    Thanks in advance

    Thom Parker answered this here: http://forums.adobe.com/message/2614570#2614570
    Answer copied below:
    "The problem is that when the focus is on the text box
    it's in edit mode. It's only displaying the value interactively entered by
    the user, or as a consequence of the change event.  What you need to do is
    force the focus off of the text box in code.  You can do a little trick
    where you bounce it to a tiny transparent field, which then bounds the focus
    back so it doesn't look like the focus changed."
    What I ended up doing was calling up the dialog box, then using setfocus with no parameters to remove focus from the field, as follows:
    this.rawValue = this.dialogBoxFunction(this.rawValue); // passing current value so dialog box defaults to that value
    xfa.host.setFocus();
    Cheers,
    Marty.

  • Help with intricate search and results display

    Hi All,
    I am looking for help with a problem I have, my knowledge on Numbers is limited, I have learnt a lot by trial and error but I do not know where to start on this problem.
    What I am trying to do is display result from sheet 1 onto sheet 2 when I enter the letters of the REFERENCE into cell C3. Below is an example of the result I am tring to achieve -
    SHEET 2
    Reference
    AB
    Reference
    SZone
    Parts
    LZone
    Parts
    AB10
    3
    75
    2
    100
    AB10
    3
    75
    2
    100
    AB10
    3
    75
    2
    100
    AB10
    3
    75
    2
    100
    AB11
    3
    75
    2
    100
    AB11
    3
    75
    2
    100
    AB11
    3
    75
    2
    100
    AB11
    3
    75
    2
    100
    AB12
    3
    75
    2
    100
    AB12
    3
    75
    2
    100
    AB13
    3
    75
    3
    75
    AB13
    3
    75
    3
    75
    AB14
    3
    75
    3
    75
    AB14
    3
    75
    3
    75
    AB15
    3
    75
    1
    200
    AB15
    3
    75
    1
    200
    AB16
    3
    75
    3
    75
    AB16
    3
    75
    3
    75
    AB21
    3
    75
    3
    75
    AB21
    3
    75
    3
    75
    AB22
    3
    75
    3
    75
    I have searched for AB by entering it in cell B3 on Sheet 2, an auotmatic search has been carried out on Sheet 1 and all the columns have been brought into sheet 2 with the corresponding data and placed under the COLUMN HEADERS. I have over 4000 lines on Sheet 1, although the most result that will ever be pulled through will be 150.
    When the data is pulled in I will need to do other calculation in the COLUMNS F, G and H so the data need to only be mapped to COLUMNS A to E.
    Also I want to be able to use this spreadsheet on my iPad.
    Has anybody got an idea/solution that will help.
    Thanks in advance.
    Ian

    Ian,
    We've had a recent report of troubles with large tables in the iOS version of Numbers, so you may want to consider ways to accomplish your goals without the full 4000-row set of data.
    I re-read the problem statement in your original post and see that I should have known that you were needing a solution for both platforms. But, I'm still not clear on what part you have figured out and what part you still need help with. We often refer to your second table as a "Breakout Table". There are different ways to program it. My favorite is to add a column to the main table that identifies rows that meet the transfer criteria and assigns them a serial number to help determine where they should go in the second table.
    Here's an example:
    In the new column on the right edge of the table T1, the expression is:
    =IF(ISERROR(FIND(T2 :: $A$1, A)), "", COUNT($F$1:F1))
    Note that the first cell in the Aux column is seeded with a zero.
    The expression in the body cells of the second table, T2, is:
    =INDEX(T1, MATCH(ROW()-1, T1 :: $F, 0), COLUMN())
    Note that the search term is to be entered into Cell A1 of table T2.
    Does this give you a start?
    Jerry

  • Forcing the ReadAllQuery to return results from DB instead of cache

    How can I programmatically setup a ReadAllQuery to always return results from DB instead of cache. I dont want to use session.refreshObject - as this will result in 2 lookups - first for the actual search from cache or db and then a second search on each object on the DB. I am using Toplink 11.1.1

    You can use,
    query.refreshIdentityMapResult()
    or,
    query.setShouldRefreshIdentityMapResult(true)
    James : http://www.eclipselink.org

  • Sharepoint 2013 returning results from userprofiles

    Hello, I need some help in cleaning up the results from a search query.
    I've created a new page within SharePoint 2013 and added 2 script editors.
    One script editor holds a search box:
    <div unselectable="on">
    <label unselectable="on">First Name Search: </label>
    <input id="searchTextBox" type="text">
    <input id="getColleagues" type="button" value="Search">
    </div>
    <div id="resultsDiv" unselectable="on">
    </div>
    The other holds the search query:
    <script src="http://site/SiteAssets/jquery.min.js"></script>
    <script type="text/javascript">
    $(document).ready(function () {
    var spAppWebUrl = "http://site";
    $("#searchButton").click(function () {
    //var queryUrl = spAppWebUrl + "/_api/search/query?querytext='" + $("#searchTextBox").val() + "'";
    var wildcard = $("#searchTextBox").val() + "*";
    var queryUrl = spAppWebUrl + "/_api/search/query?querytext='*'&sourceid='b09a7990-05ea-4af9-81ef-edfab16c4e31'&rowlimit='500'&selectproperties='FirstName, LastName, PictureURL, SipAddress, PreferredName, WorkEmail'&refinementfilters='FirstName:"+ (wildcard) + "'";
    alert(queryUrl);
    $.ajax({ url: queryUrl, method: "GET", headers: { "Accept": "application/json; odata=verbose" }, success: onQuerySuccess, error: onQueryError });
    function onQuerySuccess(data) {
    var results = data.d.query.PrimaryQueryResult.RelevantResults.Table.Rows.results;
    $("#resultsDiv").append('<table>');
    $.each(results, function () {
    $("#resultsDiv").append('<tr>');
    $.each(this.Cells.results, function () {
    $("#resultsDiv").append('<td>' + this.Value + '</td>');
    $("#resultsDiv").append('</tr>');
    $("#resultsDiv").append('</table>');
    function onQueryError(error) {
    $("#resultsDiv").append(error.statusText)
    </script>
    I am getting results, but not just the selectedproperties: FirstName, LastName, PictureURL, SipAddress, PreferredName, WorkEmail.
    Can someone help me to get just the selectedproperties of FirstName, LastName, PictureURL, SipAddress, PreferredName, WorkEmail?
    Thanks

    Hi,
    According to your post, my understanding is that you want to get the User Profile properties using REST API.
    If you want to use Search Query, here is a tool for your reference:
    http://sp2013searchtool.codeplex.com/
    If you just want to get the User Profile properties, we can use SP.UserProfiles.PeopleManager
    to achieve it.
    Example:
    Get Multiple Properties for the current user:
    http://siteurl/_api/SP.UserProfiles.PeopleManager/GetMyProperties?$select=PictureUrl,AccountName
    More information is here:
    http://www.vrdmn.com/2013/07/sharepoint-2013-get-userprofile.html
    Best Regards
    Dennis Guo
    TechNet Community Support

  • I can't prevent Spotlight from returning results from emails and Safari

    Try as I might, I can't prevent Spotlight from returning results from emails and from Safari web browser history.
    I've toggled the two relevant boxes in the Spotlight system preferences, and rebuilt the index, but it still returns results from categories that I have deselected (like emails and webpages).
    The system preferences say "Only selected categories will appear in Spotlight search results". If only it were true!

    I think this could be a reason, your settings regarding indexing of mail messages didn't stick.
    I'm having other problems with security update 2008-005.
    It'd be interesting to know if OnyX is able to fix your problem again. Hopefully its doings survive the next update.

  • I need help with controlling two .swf's from third.

    Hi, thanks for reading!
    I need help with controlling two .swf's from third.
    I have a problem where I need to use a corporate designed
    .swf in a digital signage solution, but have been told by the legal
    department that it can not be modified in any way, I also can't
    have the source file yada yada. I pulled the .swfs from their
    website and I decompiled them to see what I was up against.
    The main swf that I need to control is HCIC.swf and the
    problem is it starts w/ a preloader, which after loading stops on a
    frame that requires user input (button press) on a play button,
    before the movie will proceed and play through.
    What I have done so far is to create a container swf,
    HCIC_container.swf that will act as Target for the HCIC.swf, and
    allow me to send actionscript to the file I'm not allowed to
    modify.
    I managed to get that done with the help of someone on
    another forum. It was my hope that the following script would just
    start HCIC.swf at a frame past the preloader and play button, and
    just play through.
    var container:MovieClip = createEmptyMovieClip("container",
    getNextHighestDepth());
    var mcLoader:MovieClipLoader = new MovieClipLoader();
    mcLoader.addListener(this);
    mcLoader.loadClip("MCIC.swf", container);
    function onLoadInit(mc:MovieClip) {
    mc.gotoAndPlay(14);
    But unfortunately it didn't solve my problem. Because there
    is a media-controller.swf, that is being loaded by HCIC.swf that
    has the controls including the play button to start HCIC.swf.
    Here's a link to a .zip file with all 3 .swf files, and all 3
    .fla files.
    http://www.axiscc.com/temp/HCIC.zip
    What I need to do is automatically start the HCIC.swf file
    bypassing the pre-loader and play button without editing it or the
    media-controller.swf in anyway. So all the scripting needs to be
    done in HCIC_container.swf.
    I know this is confusing, and its difficult to explain, but
    if you look at the files it should make sense.
    ActionScripting is far from my strong point, so I'm
    definitely over my head here.
    Thanks for your help.

    Got my solution on another forum.
    http://www.actionscript.org/forums/showthread.php3?t=146827

  • How do I get multiple return results from a function

    IDBASKET IDSTAGE DTSTAGE
    3 1 24-JAN-03
    3 5 25-JAN-03
    4 1 13-FEB-03
    4 5 13-FEB-03
    5 3 21-FEB-03
    I input is a single IDBASKET number from this table and this function works fine only if it has one IDSTAGE per idbasket. (idbasket#5)
    But how do I get it to return a result for an IDBASKET when it has multiple IDSTAGE? (idbasket#3 & 4)
    THANKS MUCH,
    MAT
    SQL> CREATE OR REPLACE FUNCTION status_desc_sf
    2 (p_code NUMBER)
    3 RETURN VARCHAR2
    4 IS
    5 lv_output_txt VARCHAR2(30);
    6 BEGIN
    7 IF p_code = 1 THEN lv_output_txt := 'Order submitted';
    8 ELSIF p_code = 2 THEN lv_output_txt := 'Accepted, sent to shipping';
    9 ELSIF p_code = 3 THEN lv_output_txt := 'Backordered';
    10 ELSIF p_code = 4 THEN lv_output_txt := 'Cancelled';
    11 ELSIF p_code = 5 THEN lv_output_txt := 'Shipped';
    12 ELSE lv_output_txt := 'No information';
    13 END IF;
    14 RETURN lv_output_txt;
    15 END;
    16 /

    Duplicate thread:
    How do I get multiple return results from a function

  • Compare two results from the same table

    i have two results from the same table that i would like to compare. below is my query and the results i want to compare
    SELECT tblItemRoutingBOM.ItemRevID, tblItem.ItemID, tblItem.PartNum, tblItem.ItemName, tblItem.ManufacturerPartNum AS [Mfg Part#], tblItemRoutingBOM.Quantity
    FROM tblItemRouting
    INNER JOIN tblItemRoutingBOM ON tblItemRouting.ItemRoutingID = tblItemRoutingBOM.ItemRoutingID
    INNER JOIN tblItem ON tblItemRoutingBOM.ItemID = tblItem.ItemID
    WHERE tblItemRoutingBOM.ItemRevID in (61,70)
    as you can see i am returning two records using the where clause
    ItemRevID, ItemID, PartNum, ItemName, Manufacturer, Mfg Part#, Quantity
    61,121,331503,.233 Aluminum Sheet,,1
    70,121,331503,.233 Aluminum Sheet,,3
    now what i am looking for is to combine these two together into one row with the following added.  two columns for each QTY, QTY1 = 1 and QTY2 = 3 with a third column added that is the difference between the two QTY Diff = 2
    Any thoughts?

    Here are the two statements that i want to combine, results for each are attached
    SELECT tblItem.ItemID, Sum(tblItemRoutingBOM.Quantity) AS SumOfQuantity, tblItem.PartNum AS [Part #],
    tblItem.ItemName, tblManufacturer.ManufacturerName AS Manufacturer, tblItem.ManufacturerPartNum AS [Mfg Part#]
    FROM tblItemRouting
    INNER JOIN tblItemRoutingBOM ON tblItemRouting.ItemRoutingID = tblItemRoutingBOM.ItemRoutingID
    INNER JOIN tblItem ON tblItemRoutingBOM.ItemID = tblItem.ItemID
    INNER JOIN tblUnits ON tblItem.UnitID = tblUnits.UnitID
    LEFT JOIN tblManufacturer ON tblItem.ManufacturerID = tblManufacturer.ManufacturerID
    WHERE tblItemRoutingBOM.ItemRevID=61
    GROUP BY tblItem.ItemID,tblItem.PartNum,tblItem.ItemName,tblManufacturer.ManufacturerName,tblItem.ManufacturerPartNum
    SELECT tblItem.ItemID, Sum(tblItemRoutingBOM.Quantity) AS Quantity, tblItem.PartNum AS [Part #],
    tblItem.ItemName, tblManufacturer.ManufacturerName AS Manufacturer, tblItem.ManufacturerPartNum AS [Mfg Part#]
    FROM tblItemRouting
    INNER JOIN tblItemRoutingBOM ON tblItemRouting.ItemRoutingID = tblItemRoutingBOM.ItemRoutingID
    INNER JOIN tblItem ON tblItemRoutingBOM.ItemID = tblItem.ItemID
    INNER JOIN tblUnits ON tblItem.UnitID = tblUnits.UnitID
    LEFT JOIN tblManufacturer ON tblItem.ManufacturerID = tblManufacturer.ManufacturerID
    WHERE tblItemRoutingBOM.ItemRevID=70
    GROUP BY tblItem.ItemID,tblItem.PartNum,tblItem.ItemName,tblManufacturer.ManufacturerName,tblItem.ManufacturerPartNum
    114,11,55002,Pepsi Blue Cap,NULL,
    117,5,331501,Marigold Yellow For ABS,NULL,
    121,1,331503,.233 Aluminum Sheet,NULL,
    125,2,331504,Velvet Vinyl .008,NULL,
    114,33,55002,Pepsi Blue Cap,NULL,
    117,15,331501,Marigold Yellow For ABS,NULL,
    121,3,331503,.233 Aluminum Sheet,NULL,
    125,6,331504,Velvet Vinyl .008,NULL,
    my returned result should combine above with two extra columns (two extra columns because i have two results to combine)
    114, 11, 33, 22, 55002, Pepsi Blue Cap, NULL,
    117, 5, 15, 10, 331501, Marigold Yellow For ABS, NULL
    121,1, 3, 2, 331503, .233 Aluminum Sheet, NULL
    125, 2, 6, 4, 331504, Velvet Vinyl .008, NULL
    Columns go as such, ID, QTY1 (for 61), QTY2 (for 70), Diff (QTY1-QTY2), PartNum, ItemName, Mfg, Mfg Part#
    IF the results from one of those two are empty then i would see something like this
    114, 11, 0, 11, 55002, Pepsi Blue Cap, NULL,
    117, 5, 0, 5, 331501, Marigold Yellow For ABS, NULL
    121,1, 0, 1, 331503, .233 Aluminum Sheet, NULL
    125, 2, 0, 2, 331504, Velvet Vinyl .008, NULL

  • Returning results that fall within the current financial year

    Hi Everyone,
    I am using MSSQL Server 2008R2 and I am interested in returning rows from a 'financial' table that fall within the current year (each row contains a 'Entered Date'). I am located in Australia so my financial year consists of all entries between the date
    01/07/xx to the 30/06/yy.
    Can anybody suggest some code, perhaps using the datediff() function, or other functions as required to achieve what I need?
    Kind Regards,
    David

    Hi,
    Try the Below Script.Hope it works
    DECLARE @StartOfFinancialYear
    DATETIME
    -- This gets 1st April for the current year
    SET @StartOfFinancialYear
    = CAST(YEAR(GETDATE())
    AS VARCHAR)
    + '0701'
    SELECT
    FROM
    <YourTable>
    WHERE DateField
    >= @StartOfFinancialYear
    AND DateField <
    DATEADD(yy, 1, @StartOfFinancialYear)
    -- less than 1st July NEXT year
    Regards, PS

  • Need help with query joining several tables into a single return line

    what i have:
    tableA:
    puid, task
    id0, task0
    id1, task1
    id2, task2
    tableB:
    puid, seq, state
    id0, 0, foo
    id0, 1, bar
    id0, 2, me
    id1, 0, foo
    id2, 0, foo
    id2, 1, bar
    tableC:
    puid, seq, date
    id0, 0, 12/21
    id0, 1, 12/22
    id0, 2, 12/22
    id1, 0, 12/23
    id2, 0, 12/22
    id2, 1, 12/23
    what i'd like to return:
    id0, task0, 12/21, 12/22, 12/22
    id1, task1, 12/23, N/A, N/A
    id2, task2, 12/22, 12/23, N/A
    N/A doesn't mean return the string "N/A"... it just means there was no value, so we don't need anything in this column (null?)
    i can get output like below through several joins, however i was hoping to condense each "id" into a single line...
    id0, task0, 12/21
    id0, task0, 12/22
    id0, task0, 12/23
    id1, task1, 12/23
    is this possible fairly easily?
    Edited by: user9979830 on Mar 29, 2011 10:53 AM
    Edited by: user9979830 on Mar 29, 2011 10:58 AM

    Hi,
    Welcome to the forum!
    user9979830 wrote:
    what i have:...Thanks for posting that so clearly!
    Whenever you have a question, it's even better if you post CREATE TABLE and INSERT statements for your sample data, like this:
    CREATE TABLE     tablea
    (       puid     VARCHAR2 (5)
    ,     task     VARCHAR2 (5)
    INSERT INTO tablea (puid, task) VALUES ('id0',  'task0');
    INSERT INTO tablea (puid, task) VALUES ('id1',  'task1');
    INSERT INTO tablea (puid, task) VALUES ('id2',  'task2');
    CREATE TABLE     tablec
    (       puid     VARCHAR2 (5)
    ,     seq     NUMBER (3)
    ,     dt     DATE          -- DATE is not a good column name
    INSERT INTO tablec (puid, seq, dt) VALUES ('id0',  0,  DATE '2010-12-21');
    INSERT INTO tablec (puid, seq, dt) VALUES ('id0',  1,  DATE '2010-12-22');
    INSERT INTO tablec (puid, seq, dt) VALUES ('id0',  2,  DATE '2010-12-22');
    INSERT INTO tablec (puid, seq, dt) VALUES ('id1',  0,  DATE '2010-12-23');
    INSERT INTO tablec (puid, seq, dt) VALUES ('id2',  0,  DATE '2010-12-22');
    INSERT INTO tablec (puid, seq, dt) VALUES ('id2',  1,  DATE '2010-12-23');This way, people can re-create the problem and test their ideas.
    It doesn't look like tableb plays any role in this problem, so I didn't post it.
    Explain how you get the results from that data. For example, why do you want this row in the results:
    PUID  TASK  DT1        DT2        DT3
    id0   task0 12/21/2010 12/22/2010 12/22/2010rather than, say
    PUID  TASK  DT1        DT2        DT3
    id0   task0 12/22/2010 12/21/2010 12/22/2010? Does 12/21 have to go in the first column because it is the earliest date, or is it because 12/21 is related to the lowest seq value? Or do you even care about the order, just as long as all 3 dates are shown?
    Always say what version of Oracle you're uisng. The query below will work in Oracle 9 (and up), but starting in Oracle 11, the SELECT ... PIVOT feature could help you.
    i can get output like below through several joins, however i was hoping to condense each "id" into a single line... Condensing the output, so that there's only one line for each puid, sounds like a job for "GROUP BY puid":
    WITH     got_r_num     AS
         SELECT     puid
         ,     dt
         ,     ROW_NUMBER () OVER ( PARTITION BY  puid
                                   ORDER BY          seq          -- and/or dt
                           )         AS r_num
         FROM    tablec
    --     WHERE     ...     -- If you need any filtering, put it here
    SELECT       a.puid
    ,       a.task
    ,       MIN (CASE WHEN r.r_num = 1 THEN r.dt END)     AS dt1
    ,       MIN (CASE WHEN r.r_num = 2 THEN r.dt END)     AS dt2
    ,       MIN (CASE WHEN r.r_num = 3 THEN r.dt END)     AS dt3
    ,       MIN (CASE WHEN r.r_num = 4 THEN r.dt END)     AS dt4
    FROM       tablea    a
    JOIN       got_r_num r  ON   a.puid  = r.puid
    GROUP BY  a.puid
    ,            a.task
    ORDER BY  a.puid
    ;I'm guessing that you want the dates arranged by seq; that is, for each puid, the date related to the lowest seq comes first, regardless of whther that date is the earliest date for that puid or not. If that's not what you need, then change the analytic ORDER BY clause.
    This does not assume that the seq values are always consecutive integers (0, 1, 2, ...) for each puid. You can skip, or even duplicate values. However, if the values are always consecutive integers, starting from 0, then you could simplify this. You won't need a sub-query at all; just use seq instead of r_num in the main query.
    Here's the output I got from the query above:
    PUID  TASK  DT1        DT2        DT3        DT4
    id0   task0 12/21/2010 12/22/2010 12/22/2010
    id1   task1 12/23/2010
    id2   task2 12/22/2010 12/23/2010As posted, the query will display the first 4 dts for each puid.
    If there are fewer than 4 dts for a puid, the query will still work. It will leave some columns NULL at the end.
    If there are more than 4 dts for a puid, the query will still work. It will display the first 4, and ignore the others.
    There's nothing special about the number 4; you could make it 3, or 5, or 35, but whatever number you choose, you have to hard-code that many columns into the query, and always get that many columns of output.
    For various ways to deal with a variable number of pivoted coolumns, see the following thread:
    PL/SQL
    This question actually doesn't have anything to do with SQL*Plus; it's strictly a SQL question, and SQL questions are best posted on the "SQL and PL/SQL" forum:
    PL/SQL
    If you're not sure whether a question is more of a SQL question or a SQL*Plus question, then post it on the SQL forum. Many more people pay attention to that forum than to this one.

  • Rookie...can't return results from JDBC query?

    I am trying to get my web service to return results to the client from my JDBC query. They "try/catch statements sem to be in the way. I can't return anything within them or outside of them. Can someone tell me how I could get my results to return to where I want them to?
    At present, all this does is return the value "Dummy" to my client. Shouldn't the value be reset by the results of the query coming in?
    package hello;
    import java.sql.*;
    public class HelloImpl implements HelloIF {
    String s2 = "Dummy";
    public String sayHello(String s) {
    try {
    Class.forName("oracle.jdbc.driver.OracleDriver");
    catch( Exception e ) {
    System.out.println("Failed to load driver.");
    try {
    Connection conn = DriverManager.getConnection("jdbc:oracle:thin:liggy11/fisher@dligman:1521:dligman");
    Statement stmt = conn.createStatement();
    String query = "SELECT end_phrase FROM rap_lines WHERE start_phrase = '" + s + "'";
    ResultSet rs = stmt.executeQuery(query) ;
    while (rs.next()) {
    s2 = rs.getString("end_phrase");
    System.out.println("Got another row. s2 is now [" + s2 + "]");
    catch( Exception e ) {
    e.printStackTrace();
    return new String(s2);

    liggy11,
    I believe the smart-ass forum is the most appropriate
    place for replies such as yours. Excuse me while I
    get with some honeys....
    Peace in the middle east,
    mrcheeks.
    I'm Sorry Miss Jackson!

  • 11g: Multicolumn pivot - help with query

    Hi everyone,
    My first attempt at posting here was not very successful. I have now read the rules, and done more homework.
    I'm fairly new to Oracle databases, but have some experience with MySQL and Postgres from earlier.
    First of all, here is an example table of what I have today. This is only an extract of the full table, but these fields are the interesting ones.
    CREATE TABLE trans
         ("FROM_LIC" int, "FROM_LOCATION" varchar2(18), "TO_LOCATION" varchar2(18), "CREATE_DT" timestamp)
    INSERT ALL
         INTO trans ("FROM_LIC", "FROM_LOCATION", "TO_LOCATION", "CREATE_DT")
               VALUES (4100002563, '215', 'INN_MONO_05', '04-Mar-2013 11:54:21 AM')
         INTO trans ("FROM_LIC", "FROM_LOCATION", "TO_LOCATION", "CREATE_DT")
               VALUES (4100002563, 'INN_MONO_05', 'INN_MONO_06_BANE_R', '04-Mar-2013 11:55:08 AM')
         INTO trans ("FROM_LIC", "FROM_LOCATION", "TO_LOCATION", "CREATE_DT")
               VALUES (4100002563, 'INN_MONO_06_BANE_R', 'TROLLEY_19', '04-Mar-2013 12:01:06 PM')
         INTO trans ("FROM_LIC", "FROM_LOCATION", "TO_LOCATION", "CREATE_DT")
               VALUES (4100002563, 'TROLLEY_19', 'UT_OPPLAST_5_2', '04-Mar-2013 12:01:56 PM')
         INTO trans ("FROM_LIC", "FROM_LOCATION", "TO_LOCATION", "CREATE_DT")
               VALUES (4100002563, 'UT_OPPLAST_5_2', 'STG010801', '04-Mar-2013 12:01:56 PM')
         INTO trans ("FROM_LIC", "FROM_LOCATION", "TO_LOCATION", "CREATE_DT")
               VALUES (4100003259, '231', 'INN_MONO_04', '04-Mar-2013 02:18:31 PM')
         INTO trans ("FROM_LIC", "FROM_LOCATION", "TO_LOCATION", "CREATE_DT")
               VALUES (4100003259, 'INN_MONO_04', 'INN_MONO_04_BANE_L', '04-Mar-2013 02:19:28 PM')
         INTO trans ("FROM_LIC", "FROM_LOCATION", "TO_LOCATION", "CREATE_DT")
               VALUES (4100003259, 'INN_MONO_04_BANE_L', 'TROLLEY_5', '04-Mar-2013 02:22:41 PM')
         INTO trans ("FROM_LIC", "FROM_LOCATION", "TO_LOCATION", "CREATE_DT")
               VALUES (4100003259, 'TROLLEY_5', 'UT_OPPLAST_3_1', '04-Mar-2013 02:23:35 PM')
         INTO trans ("FROM_LIC", "FROM_LOCATION", "TO_LOCATION", "CREATE_DT")
               VALUES (4100003262, '243', 'INN_MONO_06', '04-Mar-2013 01:37:49 PM')
         INTO trans ("FROM_LIC", "FROM_LOCATION", "TO_LOCATION", "CREATE_DT")
               VALUES (4100003262, 'INN_MONO_06', 'INN_MONO_06_BANE_R', '04-Mar-2013 01:39:09 PM')
         INTO trans ("FROM_LIC", "FROM_LOCATION", "TO_LOCATION", "CREATE_DT")
               VALUES (4100003262, 'INN_MONO_06_BANE_R', 'TROLLEY_10', '04-Mar-2013 01:43:48 PM')
         INTO trans ("FROM_LIC", "FROM_LOCATION", "TO_LOCATION", "CREATE_DT")
               VALUES (4100003262, 'TROLLEY_10', 'UT_OPPLAST_5_2', '04-Mar-2013 01:44:58 PM')
         INTO trans ("FROM_LIC", "FROM_LOCATION", "TO_LOCATION", "CREATE_DT")
               VALUES (4100003262, 'UT_OPPLAST_5_2', 'STG010904', '04-Mar-2013 01:46:30 PM')
         INTO trans ("FROM_LIC", "FROM_LOCATION", "TO_LOCATION", "CREATE_DT")
               VALUES (4100003263, '231', 'INN_MONO_04', '04-Mar-2013 02:18:31 PM')
         INTO trans ("FROM_LIC", "FROM_LOCATION", "TO_LOCATION", "CREATE_DT")
               VALUES (4100003263, 'INN_MONO_04', 'INN_MONO_04_BANE_R', '04-Mar-2013 02:19:20 PM')
    SELECT * FROM dual
    ;As of now, I have a query that returns a data grid from the table, looking exactly like the example table I've included.
    Here is a copy of the query that gives the example table as a result from the table in our database:
    select * from (
        select distinct from_lic, from_location, to_location, create_dt from trans where to_location like 'INN_MONO___' and create_dt > sysdate-(3/24)
        union
        select distinct from_lic, from_location, to_location, create_dt from trans where from_location like 'INN_MONO___' and to_location like 'INN_MONO%BANE%' and create_dt > sysdate-(3/24)
        union
        select distinct from_lic, from_location, to_location, create_dt from trans where from_location like 'INN_MONO%BANE%' and to_location like 'TROLLEY%' and create_dt > sysdate-(3/24)
        union
        select distinct from_lic, from_location, to_location, create_dt from trans where from_location like 'TROLLEY%' and to_location like 'UT_OPPLAST____' and create_dt > sysdate-(3/24)
        union
        select distinct from_lic, from_location, to_location, create_dt from trans where from_location like 'UT_OPPLAST____%' and (to_location like 'STG%' or to_location like 'UT_OPPLAST%GULV') and create_dt > sysdate-(3/24)
      ) order by from_lic, create_dtI would be delighted if you could help me formulating a new query that gives THIS output, from the same table, with the same constraints as in the original query:
    CREATE TABLE new_table
         ("FROM_LIC" int, "FLOOR" timestamp, "INFEED" timestamp, "TROLLEY" timestamp, "OUTFEED" timestamp, "STAGING" varchar2(16))
    INSERT ALL
         INTO new_table ("FROM_LIC", "FLOOR", "INFEED", "TROLLEY", "OUTFEED", "STAGING")
               VALUES (4100002563, '04-Mar-2013 11:54:00 AM', '04-Mar-2013 11:55:00 AM', '04-Mar-2013 12:01:00 PM', '04-Mar-2013 12:01:00 PM', '03.04.2013 12:01')
         INTO new_table ("FROM_LIC", "FLOOR", "INFEED", "TROLLEY", "OUTFEED", "STAGING")
               VALUES (4100003259, '04-Mar-2013 02:18:00 PM', '04-Mar-2013 02:19:00 PM', '04-Mar-2013 02:22:00 PM', '04-Mar-2013 02:23:00 PM', NULL)
         INTO new_table ("FROM_LIC", "FLOOR", "INFEED", "TROLLEY", "OUTFEED", "STAGING")
               VALUES (4100003262, '04-Mar-2013 01:37:00 PM', '04-Mar-2013 01:39:00 PM', '04-Mar-2013 01:43:00 PM', '04-Mar-2013 01:44:00 PM', '03.04.2013 13:46')
    SELECT * FROM dual
    ;What happened here is that for each instance of from_lic, the line yielded from
    select distinct from_lic, from_location, to_location, create_dt from trans where to_location like 'INN_MONO___' and create_dt > sysdate-(3/24)The value for CREATE_DT ---> in the new column called FLOOR.
    And the following for the rest of the columns:
    select distinct from_lic, from_location, to_location, create_dt from trans where from_location like 'INN_MONO___' and to_location like 'INN_MONO%BANE%' and create_dt > sysdate-(3/24)The value for CREATE_DT ---> INFEED.
    select distinct from_lic, from_location, to_location, create_dt from trans where from_location like 'INN_MONO%BANE%' and to_location like 'TROLLEY%' and create_dt > sysdate-(3/24)The value for CREATE_DT ---> TROLLEY.
    select distinct from_lic, from_location, to_location, create_dt from trans where from_location like 'TROLLEY%' and to_location like 'UT_OPPLAST____' and create_dt > sysdate-(3/24)The value for CREATE_DT ---> OUTFEED.
    select distinct from_lic, from_location, to_location, create_dt from trans where from_location like 'UT_OPPLAST____%' and (to_location like 'STG%' or to_location like 'UT_OPPLAST%GULV') and create_dt > sysdate-(3/24)The value for CREATE_DT ---> STAGING.
    Originally I was thinking of a pivot for this, but there is possibly better ways to accomplish this.
    I would like to have the data in the described format to make data mining easier.
    The query will run on a table with >3M lines.
    I hope this way of formulating the question is better, and more comprehensible.
    Please do not hesitate to ask questions, and I'll do my best to answer.
    I can test any suggested queries in the production database when necessary.
    Edited by: 997749 on Apr 3, 2013 5:18 AM
    Edited by: 997749 on Apr 3, 2013 6:20 AM

    Hi,
    what about something like this:
    with trans as
       SELECT 4100002563 from_lic, 'INN_MONO_05'        to_location, TO_DATE('03.04.2013 11:54:21', 'DD.MM.YYYY HH24:MI:SS') create_dt FROM DUAL UNION ALL
       SELECT 4100002563 from_lic, 'INN_MONO_06_BANE_R' to_location, TO_DATE('03.04.2013 11:55:08', 'DD.MM.YYYY HH24:MI:SS') create_dt FROM DUAL UNION ALL
       SELECT 4100002563 from_lic, 'TROLLEY_19'         to_location, TO_DATE('03.04.2013 12:01:06', 'DD.MM.YYYY HH24:MI:SS') create_dt FROM DUAL UNION ALL
       SELECT 4100002563 from_lic, 'STG010801'          to_location, TO_DATE('03.04.2013 12:01:56', 'DD.MM.YYYY HH24:MI:SS') create_dt FROM DUAL UNION ALL
       SELECT 4100002563 from_lic, 'UT_OPPLAST_5_2'     to_location, TO_DATE('03.04.2013 12:01:56', 'DD.MM.YYYY HH24:MI:SS') create_dt FROM DUAL UNION ALL
       SELECT 4100005037 from_lic, 'INN_MONO_05'        to_location, TO_DATE('03.04.2013 11:18:31', 'DD.MM.YYYY HH24:MI:SS') create_dt FROM DUAL UNION ALL
       SELECT 4100005037 from_lic, 'INN_MONO_04_BANE_R' to_location, TO_DATE('03.04.2013 11:21:54', 'DD.MM.YYYY HH24:MI:SS') create_dt FROM DUAL UNION ALL
       SELECT 4100005037 from_lic, 'TROLLEY_16'         to_location, TO_DATE('03.04.2013 11:25:43', 'DD.MM.YYYY HH24:MI:SS') create_dt FROM DUAL UNION ALL
       SELECT 4100005037 from_lic, 'UT_OPPLAST_3_4'     to_location, TO_DATE('03.04.2013 11:26:37', 'DD.MM.YYYY HH24:MI:SS') create_dt FROM DUAL UNION ALL
       SELECT 4100005037 from_lic, 'STG010703'          to_location, TO_DATE('03.04.2013 11:27:31', 'DD.MM.YYYY HH24:MI:SS') create_dt FROM DUAL UNION ALL
       SELECT 4100006658 from_lic, 'INN_MONO_08'        to_location, TO_DATE('03.04.2013 11:00:31', 'DD.MM.YYYY HH24:MI:SS') create_dt FROM DUAL UNION ALL
       SELECT 4100006658 from_lic, 'INN_MONO_08_BANE_L' to_location, TO_DATE('03.04.2013 11:02:35', 'DD.MM.YYYY HH24:MI:SS') create_dt FROM DUAL UNION ALL
       SELECT 4100006658 from_lic, 'TROLLEY_22'         to_location, TO_DATE('03.04.2013 11:07:54', 'DD.MM.YYYY HH24:MI:SS') create_dt FROM DUAL UNION ALL
       SELECT 4100006658 from_lic, 'UT_OPPLAST_7_3'     to_location, TO_DATE('03.04.2013 11:09:03', 'DD.MM.YYYY HH24:MI:SS') create_dt FROM DUAL
    , got_category AS
       SELECT from_lic
            , CASE
                 WHEN to_location like 'INN_MONO%BANE%'
                 THEN 'INFEED'
                 WHEN to_location like 'INN_MONO%'
                 THEN 'FLOOR'
                 WHEN to_location like 'TROLLEY%'
                 THEN 'TROLLEY'
                 WHEN to_location like 'STG' OR to_location like 'UT%GULV' 
                 THEN 'STAGED'
                 WHEN to_location like 'UT_OPPLAST%'
                 THEN 'OUTFEED'
                 ELSE 'UNKNOWN'
              END as cat
            , create_dt
         FROM trans
        WHERE create_dt > (sysdate -10/24) -- adjusted for different timezone
    SELECT * --FROM_LIC, lic_a, lic_b, lic_c, lic_d, lic_e
      FROM got_category
    PIVOT(MAX(create_dt) FOR cat IN('FLOOR'   as floor,
                                     'INFEED'  as infeed,
                                     'TROLLEY' as trolley,
                                     'OUTFEED' as ooutfeed,
                                     'STAGED'  as staged));
      FROM_LIC FLOOR                  INFEED                 TROLLEY                OOUTFEED               STAGED               
    4100002563 03-APR-2013 11:54:21   03-APR-2013 11:55:08   03-APR-2013 12:01:06   03-APR-2013 12:01:56                        
    4100005037 03-APR-2013 11:18:31   03-APR-2013 11:21:54   03-APR-2013 11:25:43   03-APR-2013 11:26:37                        
    4100006658 03-APR-2013 11:00:31   03-APR-2013 11:02:35   03-APR-2013 11:07:54   03-APR-2013 11:09:03                         If not useful, please post your input data (CREATE TABLE and INSERT statements) and your expected output.
    Regards.
    Al

Maybe you are looking for

  • LR2 Vista 64 Unexpected Restart

    I am running LR2 under Vista 64 Home Premium on a high end Intel E8400 PC with 8GB RAM, fast disk, 2 nVidia video cards, dual monitors, etc. At first, this combination ran with no issues. LR is awesome tom use on this machine--it flys. However, into

  • Vid card for TV to HD Display yet?????/

    Aloha All, I am wondering where to find a vid card that allows cable hookup to see tv on my 30" HD display. I am not looking for a unit to record TV. I am looking for a card to fit into the pci express slot and be able to do both computer and TV func

  • What can i do about a sluggish mouse?

    Does anybody have a solution for a sluggish mouse? My battery level is 99% but it is slow to respond.

  • How to apply style with grep script to finding question mark

    app.findGrepPreferences = app.changeGrepPreferences = null;       app.findGrepPreferences.findWhat = "\?"; //app.findGrepPreferences.SpecialCharacters = SpecialCharacters.QUESTION_MARK; app.findGrepPreferences.appliedParagraphStyle = "myParS"; app.ac

  • Execute mapping on condition (suggestions plz)

    Hi, I'am loading huge flat files in a warehouse and I need to make sure no file is loaded twice. I guess there are a few possibilities, but feedback is always pleasant. Is it possible to use a pre-mapping operator here? ( ie. user a procedure to chec