Help of query SQL - inner joins and the separate results

Hello

ASP VB, SQL Server

I have a structure of data base with 3 tables - users, albums and photos. each user has a identifier unique, each record has a unique albumid and also contains a column with the user name. each record in the photo has a unique id so that store the user name and the album in which the image belongs.

I'm writing a query that returns a list of the albums for a particular user (based on a user name query string) and who will also bring back the id of the first record in the table for each of these albums photo.

the closest I get is to run a query to select albumid albums where userid = varuserid with a join internal on the pictures table to remove the photo ID - problem I then it comes out all the photos from the photos table where userid = varuserid, so when I do a repeat region to display a list of albums for a certain user It produces a list of all the photos where userid = varuserid

I really want to return just a list of ID album based on the username variable, but also to return the first record in the table of photos for each of these albumids

I tried different combinations of inner joins, select distinct etc but no joy.

any suggestion would be appreciated as am floundering here...




First, you must define 'first' with regard to the photos. Is there a
timestamp? They are numbered inside the album? Do you really care who is
"first", or do you want simply a shot? You also neglected to indicate if they are
empty photo albums have been allowed. I assumed that the empty albums are not
allowed.

Whatever you decide, the answer will be similar.
SQL Server tends to get better results with joins with subqueries. You will have
See such a written request more often with subqueries, and there isn't
nothing wrong with that, but I'll use a join on a derived table. I have
have not all column names (hint, hint), so I made them, but the
Comments should help out you.

SELECT A.Title, P.PhotoID, P.Caption, A.AlbumID, P.ImagePath
FROM dbo. A albums
-build a table derived, consisting of photo ID lowest for each
album.
INNER JOIN (SELECT AlbumID, MIN (PhotoID) AS FirstPhoto FROM dbo. Photos
AlbumID GROUP) AS PM WE A.AlbumID = PM. AlbumID
-details of the photo for the photo shown in the table above
INNER JOIN dbo. Photos P on A.AlbumID = P.AlbumID AND
H. FirstPhoto = P.PhotoID
User A.UserID ='some WHERE '

"tedstar" wrote in message
News:ee4pfn$de$1@forums. Macromedia.com...
> I am writing a query that returns a list of the albums for a
> particular user (based on a user name query string) and also bring
> return
> the id of the first record in the table for each of these albums photo.

Tags: Dreamweaver

Similar Questions

  • ORA-06504: PL/SQL: return variables of the game results or the query types do not match

    Hello!

    I have a simple object type and a proecdure in which I am trying to use it to insert into another table

    -object

    CREATE ORREPLACETYPEmt_mtg ASOBJECT

    (

    ACOL NUMBER ,

    BCOL NVARCHAR2 (100)

    );

    CREATE ORREPLACETYPEREF_MTG ASTABLEOFMt_MTG ;

    -same structure as the use of sampletbl target table in the cursor query

    create table tbl_MT_MTG

    (

    ACOL NUMBER ,

    BCOL NVARCHAR2 (100)

    );

    -procedure

    CREATE ORREPLACEINTERIORTEST_PROCEDURE1

    AS

    ref_cur sys_refcursor ;

    REFR ref_mtg ;

    BEGIN

    OPEN ref_cur FOR

    Select acol,

    BCOL

    DE sampletbl rownum<10;

    Fetch ref_cur in bulk collectintorefr;

    Insert intotbl_MT_MTG(acol,bcol)selectacol,bcol fromtable(refr);

    commit;

    CLOSE Ref_cur;

    END;

    /

    When I run this procedure fails with

    ORA-06504: PL/SQL: return variables of the game results or the query types do not match

    ORA-06512: at "TEST_PROCEDURE1", line 10

    ORA-06512: at line 2

    Any help on this please...

    Thanks to an OLD POST below

    so perfect helped me! Thank you

    Tubby

    After 5 years of more :-)

    How to store refcursor in collection How to store refcursor in collection

  • Need help with query SQL Inline views + Group

    Hello gurus,

    I would really appreciate your time and effort on this application. I have the following data set.

    Reference_No---Check_Number---Check_Date---description---Invoice_Number---Invoice_Type---Paid_Amount---Vendor_Number
    1234567 11223 - 05/07/2008 -paid for cleaning- 44345563-I-* 20.00 *---19
    1234567 11223 - 05/07/2008 - 44345563 -a--10,00---19 ofbad quality adjustment
    7654321 11223 - 05/07/2008 - setting the last billing cycle - 23543556 - A - 50.00 - 19
    4653456 11223 - 05/07/2008 - paid for cleaning - 35654765 - I - 30, 00-19

    Please ignore '-' added for clarity

    I'm writing a paid_amount based on Reference_No, Check_Number, Payment_Date, Invoice_Number, aggregate query Invoice_Type, Vendor_Number and display description with Invoice_type 'I' when there are multiple records with the same Reference_No, Check_Number, Payment_Date, Invoice_Type, Invoice_Number, Vendor_Number. When there are no more records I want to display the respective Description.

    The query should return the following data set

    Reference_No---Check_Number---Check_Date---description---Invoice_Number---Invoice_Type---Paid_Amount---Vendor_Number
    1234567 11223 - 05/07/2008 -paid for cleaning- 44345563-I-* 10.00 *---19
    7654321 11223 - 05/07/2008 - setting the last billing cycle - 23543556 - A - 50.00 - 19
    4653456 11223 - 05/07/2008 - paid for cleaning - 35654765 - I - 30, 00-19
    Here's my query. I'm a little lost.

    Select b., A.sequence_id, A.check_date, A.check_number, A.invoice_number, A.amount, A.vendor_number
    de)
    Select sequence_id, check_number, check_date, invoice_number, sum (paid_amount) sum, vendor_number
    of the INVOICE
    Sequence_id group check_date, check_number, invoice_number, vendor_number
    ) A, B OF INVOICE
    where A.sequence_id = B.sequence_id


    Thank you
    Nick

    It seems that this is a duplicate thread - correct me if I am wrong in this case->

    Need help with query SQL Inline views + Group

    Kind regards.

    LOULOU.

  • Difference between Inner join and right outer join...

    Which is precisely the difference between an Inner join and right outer join...

    JOIN INTERNAL:-to return all rows from the two tables where there is a football game. That is to say. the table resulting from all the rows and columns will have values.

    AND

    RIGHT OUTER JOIN:-Returns all rows in the second table, even if there is no match in the first table.

  • Urgent help please.  Inner join caused the error ora-00933

    I ran it, works great:
    SELECT DIFFERENT EXP. EXP_ID,
    EXP. DATU_EXP_WIRE_CENTER_CLLI,
    EXP. DATU_EXP_IP,
    EXP. DATU_EXP_CLLI,
    EXP. DATU_EXP_PORT,
    EXP. DATU_EXP_NAME,
    EXP. DATU_EXP_CITY,
    EXP. DATU_EXP_STATE,
    EXP. DATU_EXP_SW_VERSION,
    DECODE (LAST_ALARM. LAST_ALARM_DATE, NULL, TO_CHAR (SYSDATE, ' YYYY/MM/DD HH24:MI:SS'),
    TO_CHAR (LAST_ALARM. LAST_ALARM_DATE, "YYYY/MM/DD HH24:MI:SS")) AS STATUS_DATE,
    DECODE (LAST_ALARM. ALARM_NAME, NULL, "disconnected", LAST_ALARM. ALARM_NAME) AS DATU_STATUS,
    DECODE (LAST_ALARM. ALARM_CLASS, NULL, 'OTHER', LAST_ALARM. ALARM_CLASS) AS IS_ERROR_STATUS,

    DECODE (LAST_RESOURCE. LAST_ALARM_DATE, NULL, ", TO_CHAR (LAST_RESOURCE. LAST_ALARM_DATE, "YYYY/MM/DD HH24:MI:SS")) AS RESOURCE_STATUS_DATE,
    DECODE (LAST_RESOURCE. RESOURCE_CODE_NAME, NULL, ", LAST_RESOURCE. RESOURCE_CODE_NAME) AS RESOURCE_STATUS,
    DECODE (LAST_RESOURCE. RESOURCE_CODE_CLASS, NULL, ", LAST_RESOURCE. RESOURCE_CODE_CLASS) AS IS_RESOURCE_ERROR_STATUS,
    DECODE (LAST_OPER. LAST_ALARM_DATE, NULL, ", TO_CHAR (LAST_OPER. LAST_ALARM_DATE, "YYYY/MM/DD HH24:MI:SS")) AS OPER_STATUS_DATE,
    DECODE (LAST_OPER. OPER_CODE_NAME, NULL, ", LAST_OPER. OPER_CODE_NAME) AS OPER_STATUS,
    DECODE (LAST_OPER. OPER_CODE_CLASS, NULL, ", LAST_OPER. OPER_CODE_CLASS) AS IS_OPER_ERROR_STATUS,

    EXP BEGIN_MAINT_WINDOW, RTU. RTU_NAME
    OF TT_DATU_EXP_UNIT_INFO EXP
    left outer join
    (SELECT distinct alarmed_datus. EXP_ID, c.ALARM_NAME, c.ALARM_TYPE, c.ALARM_CLASS and alarmed_datus. LAST_ALARM_DATE
    Of alarmed_datus (SELECT EXP_ID, MAX (ALARM_TIME) AS LAST_ALARM_DATE FROM TT_DATU_EXP_ALARM_INFO GROUP BY EXP_ID)
    inner join TT_DATU_EXP_ALARM_INFO b on b.EXP_ID = alarmed_datus. EXP_ID AND b.ALARM_TIME = alarmed_datus. LAST_ALARM_DATE
    inner join TT_DATU_EXP_ALARM_TYPES c on b.ALARM_TYPE = c.ALARM_TYPE
    ) LAST_ALARM on EXP.. EXP_ID = LAST_ALARM. EXP_ID
    left outer join
    (SELECT distinct a.EXP_ID, c.RESOURCE_CODE_NAME, c.RESOURCE_CODE_TYPE, c.RESOURCE_CODE_CLASS, a.LAST_ALARM_DATE
    FROM (SELECT EXP_ID, MAX (RESOURCE_CODE_TIME) AS LAST_ALARM_DATE
    OF TT_DATU_EXP_RESOURCE_CODE_INFO GROUP BY EXP_ID) a
    inner join TT_DATU_EXP_RESOURCE_CODE_INFO b on b.EXP_ID = a.EXP_ID AND b.RESOURCE_CODE_TIME = a.LAST_ALARM_DATE
    inner join TT_DATU_EXP_RESOURCECODE_TYPES c on b.RESOURCE_CODE_TYPE = c.RESOURCE_CODE_TYPE
    ) LAST_RESOURCE on EXP.. EXP_ID = LAST_RESOURCE. EXP_ID
    left outer join
    (SELECT distinct a.EXP_ID, c.OPER_CODE_NAME, c.OPER_CODE_TYPE, c.OPER_CODE_CLASS, a.LAST_ALARM_DATE
    FROM (SELECT EXP_ID, MAX (OPER_CODE_TIME) AS LAST_ALARM_DATE
    OF TT_DATU_EXP_OPER_CODE_INFO GROUP BY EXP_ID) a
    inner join TT_DATU_EXP_OPER_CODE_INFO b on b.EXP_ID = a.EXP_ID AND b.OPER_CODE_TIME = a.LAST_ALARM_DATE
    inner join TT_DATU_EXP_OPER_CODE_TYPES c on b.OPER_CODE_TYPE = c.OPER_CODE_TYPE) LAST_OPER on EXP.. EXP_ID = LAST_OPER. EXP_ID
    inner join TT_DATU_LRN_MAP on exp. EXP_ID = NAB NAB. EXP_ID AND TRIM (NAB. LRN) AS p_LRN
    inner join TT_RTU_TYPES on exp. RTU_TYPE_ID = RTU RTU. RTU_TYPE_ID
    WHERE THERE IS NOT (SOME SATELLITE_EXP_ID OF TT_HOST_SATELLITE WHERE EXP. EXP_ID = SATELLITE_EXP_ID)
    AND EXP.IS_PRIMARY_ADDRESS LIKE p_isPrimary;
    ON THE OTHER
    OPEN FOR V_cursor
    SELECT EXP. EXP_ID,
    EXP. DATU_EXP_WIRE_CENTER_CLLI,
    EXP. DATU_EXP_IP,
    EXP. DATU_EXP_CLLI,
    EXP. DATU_EXP_PORT,
    EXP. DATU_EXP_NAME,
    EXP. DATU_EXP_CITY,
    EXP. DATU_EXP_STATE,
    EXP. DATU_EXP_SW_VERSION,
    DECODE (LAST_ALARM. LAST_ALARM_DATE, NULL, TO_CHAR (SYSDATE, ' YYYY/MM/DD HH24:MI:SS'), TO_CHAR (LAST_ALARM. LAST_ALARM_DATE, "YYYY/MM/DD HH24:MI:SS")) AS STATUS_DATE,
    DECODE (LAST_ALARM. ALARM_NAME, NULL, "disconnected", LAST_ALARM. ALARM_NAME) AS DATU_STATUS,
    DECODE (LAST_ALARM. ALARM_CLASS, NULL, 'OTHER', LAST_ALARM. ALARM_CLASS) AS IS_ERROR_STATUS,

    DECODE (LAST_RESOURCE. LAST_ALARM_DATE, NULL, ", TO_CHAR (LAST_RESOURCE. LAST_ALARM_DATE, "YYYY/MM/DD HH24:MI:SS")) AS RESOURCE_STATUS_DATE,
    DECODE (LAST_RESOURCE. RESOURCE_CODE_NAME, NULL, ", LAST_RESOURCE. RESOURCE_CODE_NAME) AS RESOURCE_STATUS,
    DECODE (LAST_RESOURCE. RESOURCE_CODE_CLASS, NULL, ", LAST_RESOURCE. RESOURCE_CODE_CLASS) AS IS_RESOURCE_ERROR_STATUS,
    DECODE (LAST_OPER. LAST_ALARM_DATE, NULL, ", TO_CHAR (LAST_OPER. LAST_ALARM_DATE, "YYYY/MM/DD HH24:MI:SS")) AS OPER_STATUS_DATE,
    DECODE (LAST_OPER. OPER_CODE_NAME, NULL, ", LAST_OPER. OPER_CODE_NAME) AS OPER_STATUS,
    DECODE (LAST_OPER. OPER_CODE_CLASS, NULL, ", LAST_OPER. OPER_CODE_CLASS) AS IS_OPER_ERROR_STATUS,

    EXP BEGIN_MAINT_WINDOW, RTU. RTU_NAME
    OF TT_DATU_EXP_UNIT_INFO EXP
    (in left outer join
    SELECT distinct alarmed_datus. EXP_ID, c.ALARM_NAME, c.ALARM_TYPE, c.ALARM_CLASS and alarmed_datus. LAST_ALARM_DATE
    Of alarmed_datus (SELECT EXP_ID, MAX (ALARM_TIME) AS LAST_ALARM_DATE FROM TT_DATU_EXP_ALARM_INFO GROUP BY EXP_ID)
    inner join TT_DATU_EXP_ALARM_INFO b on b.EXP_ID = alarmed_datus. EXP_ID AND b.ALARM_TIME = alarmed_datus. LAST_ALARM_DATE
    inner join TT_DATU_EXP_ALARM_TYPES c on b.ALARM_TYPE = c.ALARM_TYPE)
    LAST_ALARM on EXP.. EXP_ID = LAST_ALARM. EXP_ID
    left outer join
    (SELECT distinct a.EXP_ID, c.RESOURCE_CODE_NAME, c.RESOURCE_CODE_TYPE, c.RESOURCE_CODE_CLASS, a.LAST_ALARM_DATE
    FROM (SELECT EXP_ID, MAX (RESOURCE_CODE_TIME) AS LAST_ALARM_DATE
    OF TT_DATU_EXP_RESOURCE_CODE_INFO GROUP BY EXP_ID) a
    inner join TT_DATU_EXP_RESOURCE_CODE_INFO b on b.EXP_ID = a.EXP_ID AND b.RESOURCE_CODE_TIME = a.LAST_ALARM_DATE
    inner join TT_DATU_EXP_RESOURCECODE_TYPES c on b.RESOURCE_CODE_TYPE = c.RESOURCE_CODE_TYPE) LAST_RESOURCE on EXP.. EXP_ID = LAST_RESOURCE. EXP_ID
    left outer join
    (SELECT distinct a.EXP_ID, c.OPER_CODE_NAME, c.OPER_CODE_TYPE, c.OPER_CODE_CLASS, a.LAST_ALARM_DATE
    FROM (SELECT EXP_ID, MAX (OPER_CODE_TIME) AS LAST_ALARM_DATE
    OF TT_DATU_EXP_OPER_CODE_INFO GROUP BY EXP_ID) a
    inner join TT_DATU_EXP_OPER_CODE_INFO b on b.EXP_ID = a.EXP_ID AND b.OPER_CODE_TIME = a.LAST_ALARM_DATE
    inner join TT_DATU_EXP_OPER_CODE_TYPES c on b.OPER_CODE_TYPE = c.OPER_CODE_TYPE
    ) LAST_OPER on EXP.. EXP_ID = LAST_OPER. EXP_ID ORDER BY EXP. DATU_EXP_CLLI
    inner join TT_RTU_TYPES on exp. RTU_TYPE_ID = RTU RTU. RTU_TYPE_ID
    WHERE THERE is NOT (SOME SATELLITE_EXP_ID OF TT_HOST_SATELLITE WHERE EXP.. EXP_ID = SATELLITE_EXP_ID) AND EXP.IS_PRIMARY_ADDRESS love
    p_isPrimary;


    However this one:

    SELECT EXP. EXP_ID,
    EXP. DATU_EXP_WIRE_CENTER_CLLI,
    EXP. DATU_EXP_IP,
    EXP. DATU_EXP_CLLI,
    EXP. DATU_EXP_PORT,
    EXP. DATU_EXP_NAME,
    EXP. DATU_EXP_CITY,
    EXP. DATU_EXP_STATE,
    EXP. DATU_EXP_SW_VERSION,
    DECODE (LAST_ALARM. LAST_ALARM_DATE, NULL, TO_CHAR (SYSDATE, ' YYYY/MM/DD HH24:MI:SS'),
    TO_CHAR (LAST_ALARM. LAST_ALARM_DATE, "YYYY/MM/DD HH24:MI:SS")) AS STATUS_DATE,
    DECODE (LAST_ALARM. ALARM_NAME, NULL, "disconnected", LAST_ALARM. ALARM_NAME) AS DATU_STATUS,
    DECODE (LAST_ALARM. ALARM_CLASS, NULL, 'OTHER', LAST_ALARM. ALARM_CLASS) AS IS_ERROR_STATUS,


    DECODE (LAST_RESOURCE. LAST_ALARM_DATE, NULL, ", TO_CHAR (LAST_RESOURCE. LAST_ALARM_DATE, "YYYY/MM/DD HH24:MI:SS")) AS RESOURCE_STATUS_DATE,
    DECODE (LAST_RESOURCE. RESOURCE_CODE_NAME, NULL, ", LAST_RESOURCE. RESOURCE_CODE_NAME) AS RESOURCE_STATUS,
    DECODE (LAST_RESOURCE. RESOURCE_CODE_CLASS, NULL, ", LAST_RESOURCE. RESOURCE_CODE_CLASS) AS IS_RESOURCE_ERROR_STATUS,
    DECODE (LAST_OPER. LAST_ALARM_DATE, NULL, ", TO_CHAR (LAST_OPER. LAST_ALARM_DATE, "YYYY/MM/DD HH24:MI:SS")) AS OPER_STATUS_DATE,
    DECODE (LAST_OPER. OPER_CODE_NAME, NULL, ", LAST_OPER. OPER_CODE_NAME) AS OPER_STATUS,
    DECODE (LAST_OPER. OPER_CODE_CLASS, NULL, ", LAST_OPER. OPER_CODE_CLASS) AS IS_OPER_ERROR_STATUS,


    EXP BEGIN_MAINT_WINDOW, RTU. RTU_NAME
    OF TT_DATU_EXP_UNIT_INFO EXP
    left outer join
    (
    SELECT distinct alarmed_datus. EXP_ID, c.ALARM_NAME, c.ALARM_TYPE, c.ALARM_CLASS and alarmed_datus. LAST_ALARM_DATE
    Of alarmed_datus (SELECT EXP_ID, MAX (ALARM_TIME) AS LAST_ALARM_DATE FROM TT_DATU_EXP_ALARM_INFO GROUP BY EXP_ID)
    inner join TT_DATU_EXP_ALARM_INFO b on b.EXP_ID = alarmed_datus. EXP_ID AND b.ALARM_TIME = alarmed_datus. LAST_ALARM_DATE
    inner join TT_DATU_EXP_ALARM_TYPES c on b.ALARM_TYPE = c.ALARM_TYPE) LAST_ALARM on EXP.. EXP_ID = LAST_ALARM. EXP_ID

    left outer join
    (SELECT distinct a.EXP_ID, c.RESOURCE_CODE_NAME, c.RESOURCE_CODE_TYPE, c.RESOURCE_CODE_CLASS, a.LAST_ALARM_DATE
    FROM (SELECT EXP_ID, MAX (RESOURCE_CODE_TIME) AS LAST_ALARM_DATE
    OF TT_DATU_EXP_RESOURCE_CODE_INFO GROUP BY EXP_ID) a
    inner join TT_DATU_EXP_RESOURCE_CODE_INFO b on b.EXP_ID = a.EXP_ID AND b.RESOURCE_CODE_TIME = a.LAST_ALARM_DATE
    inner join TT_DATU_EXP_RESOURCECODE_TYPES c on b.RESOURCE_CODE_TYPE = c.RESOURCE_CODE_TYPE) LAST_RESOURCE on EXP.. EXP_ID = LAST_RESOURCE. EXP_ID
    left outer join
    (SELECT distinct a.EXP_ID, c.OPER_CODE_NAME, c.OPER_CODE_TYPE, c.OPER_CODE_CLASS, a.LAST_ALARM_DATE
    FROM (SELECT EXP_ID, MAX (OPER_CODE_TIME) AS LAST_ALARM_DATE
    OF TT_DATU_EXP_OPER_CODE_INFO GROUP BY EXP_ID) a
    inner join TT_DATU_EXP_OPER_CODE_INFO b on b.EXP_ID = a.EXP_ID AND b.OPER_CODE_TIME = a.LAST_ALARM_DATE
    inner join TT_DATU_EXP_OPER_CODE_TYPES c on b.OPER_CODE_TYPE = c.OPER_CODE_TYPE
    ) LAST_OPER on EXP.. EXP_ID = LAST_OPER. EXP_ID ORDER BY EXP. DATU_EXP_CLLI
    inner join TT_RTU_TYPES on exp. RTU_TYPE_ID = RTU RTU. RTU_TYPE_ID
    WHERE the EXP.IS_PRIMARY_ADDRESS as p_isPrimary;

    It does not work kept send me errors:
    [ORA-00933: SQL not correctly completed command]


    Any guru can help? I need to have this resolved effect today.
    Thanks in advance.
  • IP number looking for PSI - need help with a sql solution better and faster

    Hello

    I have a table (one) with more 1 000 000 IP addresses and numbers (the digital equivalent of the IP address)

    I have a second table (b) which contains a mapping between the ranges of the ISP and IP. The table looks like this and more contains 150 000 entries:

    BEGIN_IP_RANGE END_IP_RANGE ISP_NAME

    --------------------------   ----------------------   --------------

    600000000 700000000 ISP_X

    800000000 900000000 ISP_Y

    I'm creating a third table (c) which connects (a) IP numbers to identify the ISP from (B). The query looks like this:

    CREATE TABLE c

    AS

    Select a.IP_ADDRESS

    b.ISP_NAME

    IP_NUMBERS a, ISP_LOOKUP b lkp

    where a.IP_NUMBER between b.BEGIN_IP_RANGE and b.END_IP_RANGE

    ;

    There is no key join between the 2 tables and so I use TO search for the access provider. The performace of it's terrible and table (c) takes several hours to create.

    Does anyone have any thoughts/ideas/suggestions on how this research can be achieved by using a solution better and faster?

    Thank you very much

    Shah

    The performace of it's terrible and table (c) takes several hours to create.

    Hours what do you say?

    See if the following can help.

    Cardinalities are similar to yours. The ETG takes 7sec. :

    SQL> create table ip_numbers (ip_address, ip_number) as
      2  select cast(to_char(level,'fm099G999G999G999', 'nls_numeric_characters=,.') as varchar2(15))
      3       , level
      4  from dual
      5  connect by level <= 1000000 ;
    
    Table created.
    
    SQL> select * from ip_numbers where rownum <= 10;
    
    IP_ADDRESS       IP_NUMBER
    --------------- ----------
    000.000.000.001          1
    000.000.000.002          2
    000.000.000.003          3
    000.000.000.004          4
    000.000.000.005          5
    000.000.000.006          6
    000.000.000.007          7
    000.000.000.008          8
    000.000.000.009          9
    000.000.000.010         10
    
    10 rows selected.
    
    SQL>
    SQL> create table isp_lookup (begin_ip_range, end_ip_range, isp_name) as
      2  select 1 + (level-1)*5
      3       , level*5
      4       , cast('ISP_'||to_char(level,'fm099999') as varchar2(10))
      5  from dual
      6  connect by level <= 200000 ;
    
    Table created.
    
    SQL> select * from isp_lookup where rownum <= 10;
    
    BEGIN_IP_RANGE END_IP_RANGE ISP_NAME
    -------------- ------------ ----------
                 1            5 ISP_000001
                 6           10 ISP_000002
                11           15 ISP_000003
                16           20 ISP_000004
                21           25 ISP_000005
                26           30 ISP_000006
                31           35 ISP_000007
                36           40 ISP_000008
                41           45 ISP_000009
                46           50 ISP_000010
    
    10 rows selected.
    
    SQL> create index ip_address_number_ix on ip_numbers (ip_number);
    
    Index created.
    
    SQL> set timing on
    SQL>
    SQL> create table ip_mappings as
      2  select /*+ use_nl(a b) */
      3         a.ip_address
      4       , b.isp_name
      5  from isp_lookup b
      6       join ip_numbers a on a.ip_number between b.begin_ip_range
      7                                            and b.end_ip_range
      8  ;
    
    Table created.
    
    Elapsed: 00:00:06.94
    SQL> select count(*) from ip_mappings;
    
      COUNT(*)
    ----------
       1000000
    
    Elapsed: 00:00:01.22
    
  • Syntax of UPDATE of Oracle with the JOIN and the WHERE clause

    I need to update one of my paintings and can't seem to get the syntax right. I tried 4 different approaches, but does not have a chance.  Any help would be greatly appreciated.

    Attempt #1: Error: not correctly completed SQL command

    UPDATE TESTDTA.F4941
    SET TESTDTA.F4941.RSDSTN=TESTDTA.F4981.FHRTDQ,TESTDTA.F4941.RSUMD1=TESTDTA.F4981.FHUOM
    FROM TESTDTA.F4941
    INNER JOIN TESTDTA.F4981
    ON TESTDTA.F4941.RSSHPN  = TESTDTA.F4981.FHSHPN
    WHERE TESTDTA.F4941.RSSHPN = 647143
    AND TESTDTA.F4941.RSDSTN = 0
    AND TESTDTA.F4941.RSADDJ  > 110365
    AND TESTDTA.F4981.FHCGC1  = 'DIS';

    Try #2: Error report: SQL error: ORA-00904: "TESTDTA." "" F4981 ". "' FHUOM ': invalid identifier 00904. 00000 - '% s: invalid identifier '.

    UPDATE (SELECT TESTDTA.F4941
    FROM TESTDTA.F4941
    INNER JOIN TESTDTA.F4981
    ON TESTDTA.F4941.RSSHPN  = TESTDTA.F4981.FHSHPN
    WHERE TESTDTA.F4941.RSSHPN = 647143
    AND TESTDTA.F4941.RSDSTN = 0
    AND TESTDTA.F4941.RSADDJ  > 110365
    AND TESTDTA.F4981.FHCGC1  = 'DIS')
    SET TESTDTA.F4941.RSDSTN=TESTDTA.F4981.FHRTDQ,TESTDTA.F4941.RSUMD1=TESTDTA.F4981.FHUOM


    Try #3: Error report: SQL error: ORA-00904: "TESTDTA." "' F4941 ': invalid identifier 00904. 00000 - '% s: invalid identifier '.

    UPDATE (SELECT *
    FROM TESTDTA.F4941
    INNER JOIN TESTDTA.F4981
    ON TESTDTA.F4941.RSSHPN  = TESTDTA.F4981.FHSHPN
    WHERE TESTDTA.F4941.RSSHPN = 647143
    AND TESTDTA.F4941.RSDSTN = 0
    AND TESTDTA.F4941.RSADDJ  > 110365
    AND TESTDTA.F4981.FHCGC1  = 'DIS')
    SET TESTDTA.F4941.RSDSTN=TESTDTA.F4981.FHRTDQ,TESTDTA.F4941.RSUMD1=TESTDTA.F4981.FHUOM;


    Try #4: Error: ORA-00925: Missing keyword 00925. 00000 - "missing IN the keyword"

    MERGE TESTDTA.F4941
    USING TESTDTA.F4981
    ON (TESTDTA.F4941.RSSHPN  = TESTDTA.F4981.FHSHPN
    AND TESTDTA.F4941.RSSHPN = 647143
    AND TESTDTA.F4941.RSDSTN = 0
    AND TESTDTA.F4941.RSADDJ  > 110365
    AND TESTDTA.F4981.FHCGC1  = 'DIS'
     
    )
    WHEN MATCHED THEN
    UPDATE SET TESTDTA.F4941.RSDSTN=TESTDTA.F4981.FHRTDQ
      
    ,TESTDTA.F4941.RSUMD1=TESTDTA.F4981.FHUOM;

    Finally managed to do work, adding DISTINCT to select.  I have pasted the SQL error in Google to begin with.  Laughing out loud!

    Code snippet work for anyone who comes across this thread...

    MERGE INTO TESTDTA. F4941 DST

    USING (SELECT DISTINCT TESTDTA. F4941. RSSHPN,

    TESTDTA. F4941. RSDSTN,

    TESTDTA. F4941. RSUMD1,

    TESTDTA. F4941. RSADDJ,

    TESTDTA. F4981. FHSHPN,

    TESTDTA. F4981. FHRTDQ,

    TESTDTA. F4981. FHUOM,

    TESTDTA. F4981. FHCGC1

    OF TESTDTA. F4941

    JOIN INTERNAL TESTDTA. F4981

    ON TESTDTA. F4941. RSSHPN = TESTDTA. F4981. FHSHPN

    WHERE - TESTDTA. F4941. RSSHPN = 647143 AND

    TESTDTA. F4941. RSDSTN = 0

    AND TESTDTA. F4941. RSADDJ > 110365

    AND TESTDTA. F4981. FHCGC1 = 'SAY') CBC

    on (DST. RSSHPN = SRC. FHSHPN)

    When matched then update

    SET DST. RSDSTN = SRC. FHRTDQ, SUMMER TIME. RSUMD1 = SRC. FHUOM;

    Thanks to everyone who helped with this.

  • Inner join and 'PIVOT' results

    Good day to all;

    Oracle Database 10g Enterprise Edition Release 10.2.0.4.0 - 64bi
    PL/SQL Release 10.2.0.4.0 - Production
    "CORE 10.2.0.4.0 Production."
    AMT for Solaris: release 10.2.0.4.0 - Production
    NLSRTL Version 10.2.0.4.0 - Production


    I have 2 paintings I want to do a query on and then "pivot" results. I understand that, since it is 10 g using the PIVOT operator is not supported.
    (Corrected code :)) issues
    create table CT
    (
    DESIGN_ID NUMBER,
    CT_ID VARCHAR2(15)  
    )
    INSERT INTO CT VALUES ('654321','10QWER123456');
    INSERT INTO CT VALUES ('987654','7ASDF654987');
    INSERT INTO CT VALUES ('321654','82CHEV852963');
     
    create table CXRF
    (
    DESIGN_ID NUMBER,    
    ROW_SEQ_NBR NUMBER,    
    XREF_CT_ID VARCHAR2(15)  
    )
    INSERT INTO CXRF VALUES ('654321','1','25ABCD');
    INSERT INTO CXRF VALUES ('654321','2','262ABCD');
    INSERT INTO CXRF VALUES ('987654','1','14WXYZ');
    INSERT INTO CXRF VALUES ('987654','2','34FRED');
    INSERT INTO CXRF VALUES ('321654','1','1TOM');
    -Oops, measure twice cut once...
    select design_id,
            ct_id,
            xref_ct_id
      from cxrf
         INNER JOIN
           ct
       on(ct.design_id = cxrf.design_id) 
    Expected results would look like this:
    DESIGN_ID             CT_ID            XREF_CT_1     XREF_CT_2
       654321       10QWER123456        25ABCD         262ABCD
    Published by: GMoney on 9 may 2013 08:47

    Published by: GMoney on 9 may 2013 08:49

    Thanks for posting examples of data and version, however, as said, it would be better if you can test, DDLS is full of typos.

    Did you visit the FAQ before posting?

    {message: id = 9360005}

    There are also effective methods in previous versions of 11 g data pivot.
    For example:

    SQL> select ct.design_id
      2       , ct.ct_id
      3       , min(case when cxrf.row_seq_nbr = 1 then cxrf.xref_ct_id end) as XREF_CT_1
      4       , min(case when cxrf.row_seq_nbr = 2 then cxrf.xref_ct_id end) as XREF_CT_2
      5  from cxrf
      6       join ct on (ct.design_id = cxrf.design_id)
      7  group by ct.design_id, ct.ct_id ;
    
     DESIGN_ID CT_ID        XREF_CT_1    XREF_CT_2
    ---------- ------------ ------------ ------------
        654321 10QWER123456 25ABCD       262ABCD
        987654 7ASDF654987  14WXYZ       34FRED
        321654 82CHEV852963 1TOM
     
    
  • Query SQL syntax - joins

    I hope it's a quick.

    I'm OK with standard inner joins that links the two tables, but what would the syntax to query three supports like this:

    tbl_Nominations

    NominationID (PK)

    Nomination

    LodgeID (foreign key to tbl_Lodges)

    tbl_Lodges

    LodgeID (PK)

    Lodge

    CountryID (Foreign Key to t/l_Countries)

    tbl_Countries

    CountryID

    Country

    Whereas for any given LodgeID I can show:

    Lodge, the appointment and the country?

    Thank you.

    Just to say that I figured it - left outer joins, no outer joins, so:

    SELECT * FROM nominations LEFT OUTER JOIN hosts designations. LodgeID = lodges. LodgeID LEFT OUTER JOIN continents we'RE staying. ContinentID is the continents. ContinentID

    gives me what I was looking for.

  • Need help on query with decimal places and trailing 0

    Community of Oracle I need help in the following situations:

    I received the following query:
    SELECT (SUM ("VALUE1") COUNT ("VALUE2"))
    FROM sometable;

    This returns the following results:
    200
    222,5

    I want to display as 222.50 222,5 value so I tried ROUND(value,2) but then came to the conclusion that the tour will not show drags me 0.
    So now I tried to_char(value,'9999DD00') and of course he's back 200.00 both 222.50.

    Now how to make it conditional so when a number has no decimal place it shows the whole number and when it has a decimal, it shows it in the format to_char(value,'9999DD00')?

    I hope I have made my situation and question correctly and I hope a solution.

    P. S.
    I need to appear like that since it is an average of the prices of the products

    Published by: Jnijman on July 3, 2010 13:06

    You can use CASES and INSTR:

    SQL> with t as ( -- generating sample data:
      2  select 200 col from dual union
      3  select 222.5 from dual
      4  )
      5  --
      6  -- actual query:
      7  --
      8  select col
      9  ,      case
     10           when instr(col, ',') > 0
     11           then to_char(col, '999d00')
     12           else to_char(col, '999' )
     13         end formatted_col
     14  from   t;
    
           COL FORMATT
    ---------- -------
           200  200
         222,5  222,50
    
  • Complex Inner Join and outer subquery where clause to join internal?

    The DDL for this post was too big and I had to put on my site:
    http://www.harbortownsolutions.com/DDLForDORTable.txt

    My goal is to create a report of sales data for 116 stores containing daily sales and count of comments in recent years. My challenge is that I am pulling data from the same table, but with different dates to get data from last year and the week in data from date of last year.


    The dates used for reports are:

    SalesDate = 06/04/2009
    SalesDateForLastYear = 2008-06-05
    SalesDateBeginningOfWeek = 01/06/2009
    SalesDateBginningOFWeekLastYear = 2008-06-02

    PSEUDOQUERYS:
    ---======= DOLLAR VARIANCE = You Said you have $100 worth of meat(inventory) but you only have $50 worth of meat
    SELECT 
        BUSI_DATE as BusinessDate, 
        ORACLE_KEY as StoreID,
        FIELD1  as Sales,
        FIELD2  as GuestCount,
        FIELD3  as DollarVariance,
        FIELD4  as Cars
    FROM MYDAILYTOTALS
    WHERE Busi_date = SalesDate
       AND ORACLE_KEY IN (SELECT StoreID From MyStores); 
    -======= ONLY Guest count and Sales are needed in comparisons against last year    
    ---======    Last YEar dates = corresponding dates for this year ie Tuesday of this 
    week matched to Tuesday of last year (except for leap year)
    SELECT 
        ORACLE_KEY as StoreID
        FIELD1  as SalesLastYear
        FIELD2  as GuestCountLastYear   
    FROM MYDAILYTOTALS
    WHERE Busi_date = SalesDateForLastYear 
       AND ORACLE_KEY IN (SELECT StoreID From MyStores);
           
    
    SELECT 
        ORACLE_KEY as StoreID
        SUM(FIELD1)  as WeekToDateSales
        SUM(FIELD2)  as WeekToDateGuestCount  
    FROM MYDAILYTOTALS
    WHERE Busi_date BETWEEN SalesDateBeginningOfWeek and SalesDate
       AND ORACLE_KEY IN (SELECT StoreID From MyStores) 
    GROUP BY ORACLE_KEY 
       --=======  
    SELECT 
        ORACLE_KEY as StoreID
        SUM(FIELD1)  as WeekToDateSalesLastYear
        SUM(FIELD2)  as WeekToDateGuestCountLastYear  
    FROM MYDAILYTOTALS
    WHERE Busi_date BETWEEN SalesDateBeginningOfWeekLastYear and SalesDateLastYear
       AND ORACLE_KEY IN (SELECT StoreID From MyStores) 
    GROUP BY ORACLE_KEY   
    Question: Since they all use the same store of nbrs, I specify on each request? Can't I just specify only once on outer query Where clause?

    Also, how would fit the following of HOEK script:
    See how I put 1 field based on another field value

    He reads a record for each store of 116 stores in the database. There are 2 situations that have been addressed by Hoeks code:
    For each of the 116 stores in the store table, there must be 1 and only
    registration of 1 stock to match the record of MyStoreTotals. However, sometimes there is no record of inventory.
    and sometimes there will be 2 (in a case where a manager of database updates)
    After daily treatment) files. If so, the record with Is_posted = 1
    is the one that should be included in the query. The following text accomplishes this:
    -chr (39) is the single quote
     Select StoreId, 
             DollarVariance 
      from   (select store.storeid, 
                       case
                              when inv.storeid is null then 'None'
                              when inv.is_posted = 0 then 'NP'
                              else chr(39)||inv.total_dol_var||chr(39)
                        end as DollarVariance, 
                        row_number() over (partition by store.storeid order by inv.is_posted desc) rn  
                   from  myInven inv 
                   right outer join mystores  store
                                  on store.storeid = inv.fk_str_main_id
                                  and inv.busi_date = to_date(SalesDate, 'dd-mon-yyyy'))
          where rn = 1;      
    Can I put this request on the place where clause of the outer to query I put on a JOIN clause?

    So I guess that's why you used the MAX.

    That is right.
    I used CASES to identify and complete the necessary columns since the first set of results (WITH).
    So then I more or less "swivels somehow" lines of this result set to the columns, each store will appear once the most.

  • Query on sample no. and the rate of NI9233

    I use NI9233 to acquire signals. Given that the node "DAQ assistant" that only entries of no.. samples and the sampling frequency. I thought that the sampling period is equal to n. samples/sample rates but couldn't always get the correct acquisition time. So, what is the quantitative correlation of no.. samples and the sampling frequency? How would I calculate the acquisition time once I put these two parameters?

    Thank you very much.

    The NI 9233 has a limited number of sampling frequencies discreet as described in the specifications under 'Debits '.  If your chosen sample rate is not supported by the module, DAQmx will force second.  For example, a sample rate of 48 kHz will be forced to 50 kHz.

  • % ROWCOUNT SQL does not return the expected result

    I have the following function within a package:

    --Update APPERY_JTI_deleted_USERS table:
    FUNCTION fn_updt_app_jti_dlt_usr(
        p_update_co      IN VARCHAR2,
        p_appery_user_id IN APPERY_JTI_deleted_USERS.appery_user_id%TYPE,
        p_outlet_code    IN APPERY_JTI_deleted_USERS.outlet_code%TYPE)
      RETURN NUMBER
    AS
    lv_sql       VARCHAR2(4000);
    lv_rowcount  NUMBER := 0;
    BEGIN
    
    lv_sql := 'UPDATE APPERY_JTI_deleted_USERS SET '||p_update_co||' = 1 WHERE '||p_update_co||' = 0 AND OUTLET_CODE = '''||p_outlet_code||''' AND APPERY_USER_ID = '''||p_appery_user_id||'''';
    
    --EXECUTE IMMEDIATE lv_sql;
    EXECUTE IMMEDIATE 'BEGIN ' || lv_sql || '; :z := sql%rowcount; END; ' USING OUT lv_rowcount ;
    
    RETURN lv_rowcount;
      
    EXCEPTION
    WHEN OTHERS THEN
      RETURN -1;  
    END fn_updt_app_jti_dlt_usr;
    
    

    The function called several times as part of a job. Basically, the main function (Say M), call a few functions (for example A1... A9). Each of these functions is to do something and updated the application using the above function. Therefore, each of the nine functions will call the above function with different parameters.

    The problem that I am facing is:

    First run, invoke only first of all for the above function will return the positive result ($sql rowcount > 0). When I run M the second time, only first and second calls for the above function will be to return positive results and so on.

    How odd, it's that if I change the values again and run a function M, it will start from the beginning. And what strange more, the dynamic update statement is executed correctly and data are updated successfully. It's just sql rowcount % do not return is not the expected results.

    I tried to run execute immediately without (begin, end). I also tried to run it without HELP ON. It's always the same. I'm really confused, what Miss me here.

    I don't know if this is relevant. But for what it's worth, invoking the function above updates the SAME lines in the table appery_jti_deleted_users that the columns are different. So for all the work, the same lines need to be updated. Each function (A1... A9) will call the above function to update a different column of these lines.

    If you REALLY want to help you having US SHOW, not tell us:

    1. WHAT you do

    2. HOW to

    3. WHAT results you get

    4. WHAT results you expect to get

    First run, invoke only first of all for the above function will return the positive result ($sql rowcount > 0). When I run M the second time, only first and second calls for the above function will be to return positive results and so on.

    How odd, it's that if I change the values again and run a function M, it will start from the beginning. And what strange more, the dynamic update statement is executed correctly and data are updated successfully. It's just sql rowcount % do not return is not the expected results.

    Perhaps that the foregoing is true and maybe he's not. We have NO WAY of knowing because you don't SHOW US anything.

    I tried to run execute immediately without (begin, end). I also tried to run it without HELP ON. It's always the same.

    Yet once you showed us NOTHING >

    You do NOT FOLLOW best practices when you use dynamic sql statements: the sql statement real so that you can print:

    1 SEE what's running - make sure there are no syntax errors, and it seems to be what you wanted

    2 EXECUTE/TEST manually - to see if he really runs without error and see what results it really give.

    3. FIX any syntax or other problems and retest it

    Instrument properly your code and you won't have the problems you are having.

    You should know by now what are the parameters are transmitted when you run the function. So, there are at least three sets of these parameters.

    So you should be able to:

    1 run the instrumented manually function three times

    2. see that all three SQL statements are

    3 manually run these instructions

    4. see that the number of lines is after each execution

    We have no idea of what the output of the function is every time you call it. Apparently, you have, if you save the results of the function somewhere, but you didn't post any of this info.

    We need real details to help you - just listen to your story is not enough.

  • I can't open the pictures and need help please with my Windows Vista and the WindowsLivePhotogallery error Code: 0x8007007e. Carole

    I have a Windows Vista laptop with many pictures.  I can not open the photos on my Windows Live Photo gallery and have received

    Error code: 0x8007007e.

    How this can be solved?

    Thank you.  Carole

    This forum is for Microsoft Security Essentials.

    Please go to the Windows Live forum to get help.

    http://windowslivehelp.com/

  • A hierarchical report query SQL formatting to display the output as a tree

    4.2.1

    THM:2

    Hello world

    I have a simple request which when I run it should show in a tree like structure as seen in this link

    Hierarchical queries in Oracle SQL

    Any ideas on how I can get this output show upward in an area of report page apex? I tried to put it in a classic report, and it shows just all in one line given the length and spacing. I don't want to use the tree for this.

    Thank you!

    Ryansun-Oracle wrote:

    I have a simple request which when I run it should show in a tree like structure as seen in this link

    Hierarchical queries in Oracle SQL

    Any ideas on how I can get this output show upward in an area of report page apex? I tried to put it in a classic report, and it shows just all in one line given the length and spacing. I don't want to use the tree for this.

    I found that the standard reports and report templates are not really useful in this situation.

    According to exactly how it should be used, you might want to watch a dynamic list using one of the models of hierarchical list, but I tend to use this Tyler Muth & Rob Van Wijk. This can be used in a region in connection with a column named custom report template.

Maybe you are looking for