Need help writing sql query

i am trying to write sql query for a single recordset.
I have an items table with all the standard item info and an item_colorID.
i have a color_lookup table with 2 columns, item_colorID and color_ID
i have a colors table with 2 columns, color_ID and color
i want to join the tables and filter it so that a repeat region shows dynamic data of item name, description, thumb, price
and also a list/menu dynamically populated by color
filtered so that each item shows in the list/menu only the colors that the item has available.
i have tried different variations of this sql
SELECT * FROM items INNER JOIN color_lookup ON color_lookup.item_colorID = items.item_colorID INNER JOIN colors ON colors.color_ID = color_lookup.color_ID WHERE items.itemCatID = 3 ORDER BY items.itemName
but the list/menu shows every color choice multiplied by the number of items in that color
ie  White will show 80+ times.
thanks for your help,
jim balthrop

bregent,
thanks for your help.
I am building a shopping cart and i have a recordset to list the items and a repeat region for that recordset
i have a second recordset for the colors joined to the item_colorID nested inside the repeat region.
the shopping cart software has a 'lookup from recordset' choice for the add to cart servior behavior
and then i bind to the columns on the cart page.
it produces this code
if (isset($totalRows_rs_itemscat3) && $totalRows_rs_itemscat3 > 0)    {
    $row_rs_itemscat3 = WAEC_findRecordMySQL($rs_itemscat3, "item_ID", $ATC_itemID);
    if ($row_rs_itemscat3)    {
      $ATC_itemName = "".$row_rs_itemscat3['itemName']  ."";// column binding
      $ATC_itemDescription = "".$row_rs_itemscat3['itemShortDesc']  ."";// column binding
      $ATC_itemWeight = floatval("".$row_rs_itemscat3['itemWeight']  ."");// column binding
      $ATC_itemQuantity = "".$_POST["Farrington_1_Quantity_Add"]  ."";// column binding
      $ATC_itemPrice = floatval("".$row_rs_itemscat3['itemPrice']  ."");// column binding
      $ATC_itemThumbnail = "".$row_rs_itemscat3['itemThumb']  ."";// column binding
      $ATC_itemcolorchoice = "".$row_rs_colors['color']  ."";// column binding
      mysql_data_seek($rs_itemscat3, 0);
      $row_rs_itemscat3 = mysql_fetch_assoc($rs_itemscat3);
the column binding for the colors is from a different recordset and when redirecting to the cart page the color info will not show.
So my thinking is if i could get the color list/menu to populate from the same recordset as the item listing, it would solve my add to cart server behavior.
Is it possible to do this with only one recordset?
the products page and the cart page can be seen
http://www.farrington-enterprises.com/rain-gutters.php
add an item to the cart with any color choice and the color info does not carry to the cart.

Similar Messages

  • Need help with SQL Query with Inline View + Group by

    Hello Gurus,
    I would really appreciate your time and effort regarding this query. I have the following data set.
    Reference_No---Check_Number---Check_Date--------Description-------------------------------Invoice_Number----------Invoice_Type---Paid_Amount-----Vendor_Number
    1234567----------11223-------------- 7/5/2008----------paid for cleaning----------------------44345563------------------I-----------------*20.00*-------------19
    1234567----------11223--------------7/5/2008-----------Adjustment for bad quality---------44345563------------------A-----------------10.00------------19
    7654321----------11223--------------7/5/2008-----------Adjustment from last billing cycle-----23543556-------------------A--------------------50.00--------------19
    4653456----------11223--------------7/5/2008-----------paid for cleaning------------------------35654765--------------------I---------------------30.00-------------19
    Please Ignore '----', added it for clarity
    I am trying to write a query to aggregate paid_amount based on Reference_No, Check_Number, Payment_Date, Invoice_Number, 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_Number, Invoice_Type, Vendor_Number. When there are no multiple 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-------------- 7/5/2008----------paid for cleaning----------------------44345563------------------I-----------------*10.00*------------19
    7654321----------11223--------------7/5/2008-----------Adjustment from last billing cycle-----23543556-------------------A--------------------50.00--------------19
    4653456----------11223--------------7/5/2008-----------paid for cleaning------------------------35654765-------------------I---------------------30.00--------------19
    The following is my query. I am kind of lost.
    select B.Description, A.sequence_id,A.check_date, A.check_number, A.invoice_number, A.amount, A.vendor_number
    from (
    select sequence_id,check_date, check_number, invoice_number, sum(paid_amount) amount, vendor_number
    from INVOICE
    group by sequence_id,check_date, check_number, invoice_number, vendor_number
    ) A, INVOICE B
    where A.sequence_id = B.sequence_id
    Thanks,
    Nick

    It looks like it is a duplicate thread - correct me if i'm wrong in this case ->
    Need help with SQL Query with Inline View + Group by
    Regards.
    Satyaki De.

  • Need help writing a query for following scenario

    Hi all, I need some help writing a query for the following case:
    One Table : My_Table
    Row Count: App 5000
    Columns of Interest: AA and BB
    Scenario: AA contains some names of which BB contains the corresponding ID. Some
    names are appearing more than once with different IDs. For example,
    AA BB
    Dummy 10
    Me 20
    Me 30
    Me 40
    You 70
    Me 50
    Output needed: I need to write a query that will display only all the repeating names with their corresponding IDs excluding all other records.
    I would appreciate any input. Thanks

    Is it possible to have a records with the same values for AA and BB? Are you interested in these rows or do you only care about rows with the same value of AA and different BB?
    With a slight modification of a previous posting you can only select those rows that have distinct values of BB for the same value of AA
    WITH t AS (
    SELECT 'me' aa, 10 bb FROM dual
    UNION ALL
    SELECT 'me' aa, 20 bb FROM dual
    UNION ALL
    SELECT 'you' aa, 30 bb FROM dual
    UNION ALL
    SELECT 'you' aa, 30 bb FROM dual
    SELECT DISTINCT aa, bb
      FROM (SELECT aa, bb, COUNT(DISTINCT bb) OVER(PARTITION BY aa) cnt FROM t)
    WHERE cnt > 1;

  • Need Help in SQL Query

    Hi all,
    I have data in the following manner:
    CASE_NUMBER HOURS FLAG
    1000 10 0
    1000 20 0
    1000 30 1
    1000 40 0
    1000 50 1
    Here I need to Calculate the total hours for a Case_number till i see the flag as 1.
    Here the result must be 10+20+30 Hrs
    Another Example
    CASE_NUMBER HOURS FLAG
    2000 10 1
    2000 20 1
    Here the result must be only 10.
    I am struggling to write a SQL query for this.
    Anyones help will be very much greatful
    Thanks in Advance
    Regards,
    Sengathir Subbarayan

    Look up analytical functions.
    something like sum(hours) OVER (PARTITION BY case_number ORDER BY something)
    will give you the sum for all rows.
    Then you probably want to "throw away" those rows after the flag maybe by summing the flag column too, and throw away all those where the flag is greater than 1 and where it is equal to 1 except for the first one.
    I suspect you actually have some other column (other than the number of hours) that define your order - that's what you put in the ORDER BY.
    Jon

  • Need help qith sql query

    I have a table with Columns as as below
    id number primary key
    object_name varchar2(100)
    object_value varchar2(100)
    Data is stored as follows
    id object_name object_value
    10 prop_num 436
    20 city dallas
    40 Misc 1 90
    50 Misc 1 desc min amount
    60 Misc 2 700
    70 Misc 2 desc interest amount
    80 Misc 3
    80 Misc 3 desc
    SQL should return result as shown below
    id object_name object_value
    10 prop_num 436
    20 city dallas
    40 min amount 90
    50 interest amount 700
    I have done this through plsql code by creating temp table. But I need to do it in a sql query. I am stuck here
    Any pointers/help is appreciated
    Thanks
    Edited by: user9327712 on Mar 11, 2010 8:14 AM

    I have a table where the ids are stored.
    *"object name"* column values are actually another table columns stored as rows here. So in our example prop_num, city,Misc 1, Misc 1 desc, Misc 2, Misc 2
    desc,Misc 3 , Misc 3 desc are all columns. These would not change any time
    The column object_value has values stored for respective columns.
    436, dallas , 90 , min amount , 700 , interest amount are all column values. So these can change.
    id object_name object_value
    10 prop_num 436
    20 city dallas
    40 Misc 1 90
    50 Misc 1 desc min amount
    60 Misc 2 700
    70 Misc 2 desc interest amount
    80 Misc 3
    90 Misc 3 desc
    Edited by: user9327712 on Mar 11, 2010 9:57 AM
    Edited by: user9327712 on Mar 11, 2010 10:00 AM
    Edited by: user9327712 on Mar 11, 2010 10:00 AM

  • Complete novice needs help getting SQL Query into Crystal Reports XI

    Post Author: MissMarnie
    CA Forum: Data Connectivity and SQL
    So I was given an intro level web course and a monster reference guide in prep to format a report. One of our developers wrote me everything I need for the report into a SQL Query and now I'm supposed to format it in CR XII literally do not know what to do from here. I'm able to set up the correct server as a datasource, if that's useful, but I don't know how to make the querey into a bunch of formattable fields in  CR. If anyone can walk me through this, I'd be so grateful. I've attempted to look up SQL query in both the help and the book but I keep hitting a wall. The help dialog says things like "press this button" with no reference to what "this button" is. I'm sure it's obvious to the knowledgeable but I'm in a complete fog. Thanks in advance   

    Post Author: synapsevampire
    CA Forum: Data Connectivity and SQL
    IF you're trying to get assistance with setting up a query as the source for a report, try posting your Crystal version and the database type.
    Different software works differently.
    In CR 9 and above, under the connection to the database you'll see Add Command. Select that and you can paste the query in.
    As for not knowing how to generate a report, that requires experience, there's no generic solution of course..
    -k

  • Need help in SQL Query: Update a row in a table & insert the same row into another table

    I want to update a row in a table say Table A and the updated row should be inserted into another table say Table B. I need to do it in a single SQL query and i don't want to do it in PL/SQL with triggers. And i tried with MERGE statement but its working with this scenario. (Note: I'm using Oracle Database 10g Enterprise Edition Release 10.2.0.1.0).
    Thanks in Advance.

    Using Sven's code as an example, you could save the updated row in a sql plus variable. (also untested):
    SQL> var v_id number
    update tableA  
    set colB='ABC' 
    where colC='XYZ' 
    returning id into :v_id;
    insert into table A_History (ID, colA, colB, ColC)  
    select id, ColA, ColB, ColC  
    from tableA  
    where id = :v_id;   

  • Need help with SQL Query

    I am trying to build a query that sums up 12 columns depending on some accounting variables.
    example:
    SELECT gbmcu, gbco, gbfy,'',SUM(gban01 gban02 gban03 gban04 gban05+gban06+gban07+gban08+gban09+gban10+gban11+gban12) AS Oil_Volumes
    FROM PRODDTA.F0902
    WHERE GBCO = '00099'
    AND GBFY = 5
    AND GBLT = 'QU' AND GBOBJ = 5015 AND GBSUB = '105'
    GROUP BY gbmcu, gbco, gbfy
    The condition that changes is :AND GBLT = 'QU' AND GBOBJ = 5015 AND GBSUB = '105'
    I need to do this for 17 different conditions - I need to make the query as optimized as possible.
    I tried using a case statement but that takes forever (the table is over 4 million records to scan through).
    Please let me know if anyone has any suggestions on how to create something to perform these calculations.
    thanks,
    Pam

    I think I would tend to write that query as:
    SELECT gbmcu, gbco, gbfy,
           SUM(CASE WHEN gblt = 'QA' AND gbobj = 5015 AND gbsub = '105' THEN
                    gban01+gban02+gban03+gban04+gban05+gban06+gban07+gban08+gban09+gban10+gban11+gban12 END) Oil_Sales
           SUM(CASE WHEN gblt = 'QU' AND gbobj = 5015 AND gbsub = '105' THEN
                    gban01+gban02+gban03+gban04+gban05+gban06+gban07+gban08+gban09+gban10+gban11+gban12 END) Oil_Volumes
    FROM proddta.f0902
    WHERE gbco = '00099' and
          gbfy = 5 and
          gblt IN ('QA', 'QU') and
          gbpbj = 5015 and
          gbsub = '105'
    GROUP BY gbmcu, gbco, gbfySUM the CASE rather than CASE SUM. Also, as written, your query will look at all of the records in f0902 whether or not they meet one of the Case criteria, so I would put the required values in the WHERE clause as well. Your samples only show gblt changing, so you may need to make the gbpbj and gbsub predicates IN lists as well.
    If the query runs in 1.5 hours calculating one value, I would expect it to be about the same calculating 17 values, since I would bet that most of the run time is accessing the table rather than doing the math. It would almost certainly be faster than running essentially the same query 17 times.
    Indexes on some or all of the columns in the WHERE clause may help, depending on how selective the columns are. At a guess, I would suggest that gbco and gbfy would be good candidates.
    Finally, are you sure that gbsub is a character field? The only example we have is a number.
    HTH
    John

  • Need help with sql query dates

    Hi,
    I have a sql query where i need to extract some info between given dates. The where clause of this query is as follows:
    WHERE CPD_BUS_UNIT=:ESI_PRM_1
    AND CPD_VOUCHER_DATE >= :P_DATE_FROM
    AND CPD_VOUCHER_DATE < (:P_DATE_TO+1)
    When i execute the query in toad, i can view the data but not the execution plan.It gives an error ORA-00932-Inconsistent Datatypes.
    But when i remove (+1) from :P_DATE_TO, i can c the execution plan and data. Will the data be different from the previous one.
    Please suggest how to rewrite the query.

    Can you please give it a try?
    WHERE CPD_BUS_UNIT=:ESI_PRM_1
    AND CPD_VOUCHER_DATE >= :to_date(P_DATE_FROM)
    AND CPD_VOUCHER_DATE < (to_date(:P_DATE_TO)+1) Regards

  • Need help regarding sql query

    hii All
    I need help for pl/sql function.
    I build a function for monthly attendance all employees.
    but now i want to show all Sundays with 'S' and others respectively 'P' and 'A'.
    Currently Sunday also shows 'A'
    So please help
    SQL queries ... like
    SELECT DISTINCT AL.USERNAME,
    CASE WHEN DAY1 =1 THEN 'P' ELSE 'A' END DAY1,
    CASE WHEN DAY2 =1 THEN 'P' ELSE 'A' END DAY2,
    CASE WHEN DAY3 =1 THEN 'P' ELSE 'A' END DAY3,
    CASE WHEN DAY31 =1 THEN 'P' ELSE 'A' END DAY31
    FROM
    SELECT DISTINCT USERNAME, SUM(CASE WHEN
    fromdt=TRUNC(L.LOGIN_DATE) THEN
    1
    ELSE
    0
    END) DAY1
    ,SUM(CASE WHEN
    fromdt +1=TRUNC(L.LOGIN_DATE) THEN
    1
    ELSE
    0
    END) DAY2,
    SUM(CASE WHEN
    fromdt+30=TRUNC(L.LOGIN_DATE) THEN
    1
    ELSE
    0
    END) DAY31
    FROM ( SELECT DISTINCT TRUNC(LOGIN_DATE)LOGIN_DATE ,USERNAME FROM FCDM_AUDIT_TRAIL_NEW WHERE
    TRUNC(LOGIN_DATE) BETWEEN fromdt AND todt
    -- to_date( login_date, 'dd-mom-yyyy') between to_date( fromdt, 'dd-mom-yyyy') and to_date( todt, 'dd-mom-yyyy')
    ) L
    GROUP BY USERNAME
    ) AL;
    how can i show matched Sundays and show with 'SUN' or 'S'
    Regards
    vij..

    Try this way:
    SELECT USERNAME,
           MAX(CASE WHEN to_char(fromdt,'d')='1' and fromdt=TRUNC(L.LOGIN_DATE) THEN 'S'
                    WHEN to_char(fromdt,'d')!='1' and fromdt=TRUNC(L.LOGIN_DATE) THEN 'P'
                    ELSE 'A') DAY1,
           MAX(CASE WHEN to_char(fromdt+1,'d')='1' and fromdt+1=TRUNC(L.LOGIN_DATE) THEN 'S'
                    WHEN to_char(fromdt+1,'d')!='1' and fromdt+1=TRUNC(L.LOGIN_DATE) THEN 'P'
                    ELSE 'A') DAY2,
           MAX(CASE WHEN to_char(fromdt+30,'d')='1' and fromdt+30=TRUNC(L.LOGIN_DATE) THEN 'S'
                    WHEN to_char(fromdt+30,'d')!='1' and fromdt+30=TRUNC(L.LOGIN_DATE) THEN 'P'
                    ELSE 'A') DAY31
    FROM
    (SELECT DISTINCT TRUNC(LOGIN_DATE) LOGIN_DATE,
            USERNAME
       FROM FCDM_AUDIT_TRAIL_NEW
      WHERE TRUNC(LOGIN_DATE) BETWEEN fromdt AND todt
    ) L
    Group by USERNAME
    ;Max
    http://oracleitalia.wordpress.com

  • Need Help on Sql Query

    Hi,
    I have a client requirement to show a report on the device availability. The report should show the output as
    Node Availability%
    Formula for Availability = (Total No. of Failed/Total No. rows) * 100
    My Oracle Table has the following data
    NODE     SUMMARY
    172.16.10.55     Default Interface Ping fail for 172.16.10.55: ICMP timeout
    172.16.10.55     Default Interface Ping restore for 172.16.10.55
    172.16.10.55     Default Chassis Ping restore for 172.16.10.55
    172.16.10.55     Default Chassis Ping fail for 172.16.10.55: ICMP timeout
    172.16.10.55     Default Chassis Ping restore for 172.16.10.55
    172.16.10.55     Default Chassis Ping fail for 172.16.10.55: ICMP timeout
    172.16.10.55     Default Chassis Ping fail for 172.16.10.55: ICMP timeout
    172.16.10.55     Default Interface Ping restore for 172.16.10.55
    172.16.10.55     Default Interface Ping fail for 172.16.10.55: ICMP timeout
    172.16.10.56     Default Chassis Ping restore for 172.16.10.56
    172.16.10.56     Default Interface Ping fail for 172.16.10.56: ICMP timeout
    172.16.10.56     Default Chassis Ping fail for 172.16.10.56: ICMP timeout
    172.16.10.56     Default Chassis Ping restore for 172.16.10.56
    172.16.10.56     Default Chassis Ping fail for 172.16.10.56: ICMP timeout
    172.16.10.56     Default Chassis Ping restore for 172.16.10.56
    172.16.10.56     Default Chassis Ping restore for 172.16.10.56
    172.16.10.56     Default Interface Ping fail for 172.16.10.56: ICMP timeout
    In the above table the Summary column has the details like 'Ping fail' , 'Ping restore' for each Node. So, for each Node I have to compute the Total Ping Fail / (Total Ping Fail + Ping Restore) * 100 to compute the availability %.
    My output should be like the below
    Node Availability%
    172.16.10.55 55.55
    172.16.10.56 54
    Can someone please help me with query.
    I appreciate your help in advance.
    Thanks.
    Regards,
    RaviShankar.

    My Oracle Table has the following dataThat's great, but if you want maximum response to your question, then post CREATE TABLE + INSERT INTO statements.
    I currently do not have the time to turn your data into them.
    And always post the database version you're using.
    http://tkyte.blogspot.com/2005/06/how-to-ask-questions.html
    Also use the {noformat}{noformat} tag in order to post examples that benefit from staying formatted and thus readable when posted on the forum.
    Simply put the tag before and after your examples.
    For example, when you type:
    {noformat}select *
    from dual;{noformat}
    it will appear as:select *
    from dual;                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                   

  • Need help in SQL query to get only distinct records

    Hi all.
    I am new to oracle SQL and i want to write a query to get only distinct values from 2 tables.
    for example i hade 2 tables table1(employee_id,employee_no,grade) and another table2 with same structure.
    for example data in table1 is
    Employeeid employeeno Grade
    1 101 A
    1 101 B
    1 101 E
    1 101 D
    and data in table 2 is
    Employeeid employeeno Grade
    1 101 A
    1 101 B
    1 101 E
    i want to display only the fourth row like the data that doesnt exist in table2 i,e (1 101 D)
    I had too much records in both tables and i jst want to see the records that is in table 1 not in table 2 only.
    employeeid and employeeno are the same in both tables
    I hope u understand my query. I user left outer join and right outer join bt it displays all the results that and matching and unmatching. I want jst unmatched records.
    Any help is highly Appreciated.
    Thanx in Advance.
    I am using oracle 10G on windows

    Try MINUS:
    http://download.oracle.com/docs/cd/B19306_01/server.102/b14200/queries004.htm#i2054381
    SQL> -- generating sample data:
    SQL> with t1 as (
      2  select 1 employeeid, 101 employeeno, 'A' grade from dual union
      3  select 1, 101, 'B' from dual union
      4  select 1, 101, 'E' from dual union
      5  select 1, 101, 'D' from dual
      6  )
      7  ,  t2 as (
      8  select 1 employeeid, 101 employeeno, 'A' grade from dual union
      9  select 1, 101, 'B' from dual union
    10  select 1, 101, 'E' from dual
    11  )
    12  --
    13  -- actual query:
    14  --
    15  select employeeid
    16  ,      employeeno
    17  ,      grade
    18  from   t1
    19  minus
    20  select employeeid
    21  ,      employeeno
    22  ,      grade
    23  from   t2;
    EMPLOYEEID EMPLOYEENO G
             1        101 D
    1 row selected.

  • Need Help Writing a Query.

    OK, i have been trying to write a query now and have gone in several different directions and find myself stuck back at square one and was hoping someone could help me out.
    Here is my issue and i'll try to explain as best as i can.
    Example: i have the following data.
    Desc 1 --- Instruct 1 --- 2/2/2009
    Desc 1 --- Instruct 1 --- 2/7/2009
    Desc 1 --- Instruct 1 --- 2/3/2009
    Desc 1 --- Instruct 2 --- 2/5/2009
    Desc 2 --- Instruct 1 --- 2/3/2009
    Desc 2 --- Instruct 1 --- 2/1/2009
    What i need to do is write a query that will show distinct Descriptions as well as distinct instructions. (Example, Desc 1 could have 7 different instructions, so i would need 7 different lines for Desc 1, with the most recent date and a count for how many occurances of each specific instruction, for each specific Description for each of the 7 lines for Desc 1.
    Example of results would be like this
    Desc 1 --- Instruct 1 --- 2/7/2009 --- 3
    Desc 1 --- Instruct 2 --- 2/5/2009 --- 1
    Desc 2 --- Instruct 1 --- 2/3/2009 --- 2
    Hopefully that is enough information, but if anything else is needed for me to provide in order to get some help, just let me know and i will do my best to get that information.
    thanks in advance

    Frank Kulash wrote:
    Hi,
    SELECT    description
    ,         instruction
    ,         MAX (dt)     AS most_recent
    ,         COUNT (*)    AS total_num
    FROM      table_x
    GROUP BY  descrrption
    ,         instruction;"GROUP BY x, y" means the output will contain one row for every distinct combination iof x and y that occurs in the results.
    All the columns in the SELECT BY clause must be
    (a) in the GROUP BY clause,
    (b) aggregate functions (like MAX or COUNT), or
    (c) trivially dependant on (a) or (b) (for example: UPPER (description))Thank you;
    I think i was trying to over complicate things.
    So simple, yet so very effective
    thanks again

  • Need help creating SQL Query

    Hi,
    I have this table
    declare @t_date table(cut_off datetime)
    insert into @t_date
    select '20140930'
    union
    select '20140716'
    union
    select '20140715'
    union
    select '20140714'
    union
    select '20140627'
    union
    select '20140625'
    I need this result: Dates nearests to end of month
    In the table on example, I need this output
    2014-09-30
    2014-07-16
    2014-06-27
    Can anyone help me?
    Thanks a lot in advance.
    declare @t_date table(cut_off datetime)
    insert into @t_date
    select '20140930' --this is OK cause eom
    union
    select '20140716' --this is OK cause is the nearest to eom(07)
    union
    select '20140715' --this is KO cause is not the nearest to eom(07)
    union
    select '20140714' --this is KO cause is not the nearest to eom(07)
    union
    select '20140627' --this is OK cause is the nearest to eom(06)
    union
    select '20140625' --this is KO cause is not the nearest to eom(06)

    Please post DDL, so that people do not have to guess what the keys, constraints, Declarative Referential Integrity, data types, etc. in your schema are. You have no keys! Learn how to follow ISO-11179 data element naming conventions and formatting rules.
    Temporal data should use ISO-8601 formats. It is the only –- repeat, only -– format allowed in ANSI/ISO Standard SQL! Code should be in Standard SQL as much as possible and not local dialect. We would use the correct INSERT INTO syntax. 
    Oh, that silly “t_” is called a “tibble” and we laugh at it. 
    CREATE TABLE Foobar_Dates
    (foobar_date DATE NOT NULL PRIMARY KEY);
    INSERT INTO Foobar_Dates
    VALUES
    ('2014-06-25'),
    ('2014-06-27'),
    ('2014-07-14'),
    ('2014-07-15'),
    ('2014-07-16'),
    ('2014-09-30'),
    >> I need this result: for each month I need the date nearest to end of month <<
    Since SQL is a database language, we prefer to do look ups and not calculations. They can be optimized while temporal math messes up optimization. A useful idiom is a report period calendar that everyone uses so there is no way to get disagreements in the DML.
    Build good tools and re-use them! 
    The report period table gives a name to a range of dates that is common to the entire enterprise. 
    CREATE TABLE Month_Periods
    (month_name CHAR(10) NOT NULL PRIMARY KEY
       CHECK (month_name LIKE '[12][0-9][0-9][0-9]-[01][0-9]-00'),
     month_start_date DATE NOT NULL,
     month_end_date DATE NOT NULL,
      CONSTRAINT date_ordering
        CHECK (month_start_date < month_end_date),
    etc);
    These report periods can overlap or have gaps. I like the MySQL convention of using double zeroes for months and years, That is 'yyyy-mm-00' for a month within a year and 'yyyy-00-00' for the whole year. The advantages are that it will sort with the ISO-8601
    data format required by Standard SQL and it is language independent. 
    SELECT M.month_name, MAX(F.foobar_date), 
           OVER (PARTITION BY M.month_name)
      FROM Foobar_Dates AS F, 
           Month_Periods AS M 
     WHERE F.foobar_date BETWEEN M.month_start_date AND M.month_end_date;
    --CELKO-- Books in Celko Series for Morgan-Kaufmann Publishing: Analytics and OLAP in SQL / Data and Databases: Concepts in Practice Data / Measurements and Standards in SQL SQL for Smarties / SQL Programming Style / SQL Puzzles and Answers / Thinking
    in Sets / Trees and Hierarchies in SQL

  • Need help with SQL Query (query for certain IDs in a table and then IDs not in result set)

    OK. This is hard to explain but that may be my problem in constructing the query.
    I have a table that has a list of jobs. The jobs information in this table, among other things, is a short description of the job itself (think "title" in phrase form), date, and ID.
    So I can query the table to get all the jobs for a particular grouping of IDs (the only ones I am interested in) that ran successfully for a given time period. But what I want are all the jobs that did not succeed (or therefore are not present in the table) for this group of jobs and for a certain date range. But these are not the only job id's in the table.
    To get the successful ones I do the following:
    SELECT id,short_desc FROM my_table where
                id IN('1230', '1231', '1232', '1239', '1244', '1245',
                '1246', '1247', '1248', '1272', '1280', '1281', '1282',
                '1283', '1284', '1285', '1286', '1249', '1250', '1251',
                '1252', '1253', '1255', '1233', '1234', '1235', '1236',
                '1237', '1238', '1256', '1257','1258', '1254', '1290','1310')
                AND the_date > = SYSDATE - 23/24
                AND to_char(the_date, 'DD-MON-YYYY') = TO_CHAR(CURRENT_DATE, 'DD-MON-YYYY')
    I have tried to use another AND clause with NOT IN and the group or NOT EXISTS but that doesn't work as there are other jobs captured in this table with id's of course. I am only concerned with these id's:
    IN('1230', '1231', '1232', '1239', '1244', '1245',
                '1246', '1247', '1248', '1272', '1280', '1281', '1282',
                '1283', '1284', '1285', '1286', '1249', '1250', '1251',
                '1252', '1253', '1255', '1233', '1234', '1235', '1236',
                '1237', '1238', '1256', '1257','1258', '1254', '1290','1310')
    Someone said to use a temp table. I don't think I have permission and I haven't tried as I want to do this in one query if possible.
    After thinking about this I am at a loss. I tried to do this in the front end but it just became too messy.

    You do not need a physical temp table. You need an in-line view:
    WITH list as (
                  SELECT '1230' id FROM DUAL UNION ALL
                  SELECT '1231' FROM DUAL UNION ALL
                  SELECT '1232' FROM DUAL UNION ALL
                  SELECT '1239' FROM DUAL UNION ALL
                  SELECT '1244' FROM DUAL UNION ALL
                  SELECT '1245' FROM DUAL UNION ALL
                  SELECT '1246' FROM DUAL UNION ALL
                  SELECT '1247' FROM DUAL UNION ALL
                  SELECT '1248' FROM DUAL UNION ALL
                  SELECT '1272' FROM DUAL UNION ALL
                  SELECT '1280' FROM DUAL UNION ALL
                  SELECT '1281' FROM DUAL UNION ALL
                  SELECT '1282' FROM DUAL UNION ALL
                  SELECT '1283' FROM DUAL UNION ALL
                  SELECT '1284' FROM DUAL UNION ALL
                  SELECT '1285' FROM DUAL UNION ALL
                  SELECT '1286' FROM DUAL UNION ALL
                  SELECT '1249' FROM DUAL UNION ALL
                  SELECT '1250' FROM DUAL UNION ALL
                  SELECT '1251' FROM DUAL UNION ALL
                  SELECT '1252' FROM DUAL UNION ALL
                  SELECT '1253' FROM DUAL UNION ALL
                  SELECT '1255' FROM DUAL UNION ALL
                  SELECT '1233' FROM DUAL UNION ALL
                  SELECT '1234' FROM DUAL UNION ALL
                  SELECT '1235' FROM DUAL UNION ALL
                  SELECT '1236' FROM DUAL UNION ALL
                  SELECT '1237' FROM DUAL UNION ALL
                  SELECT '1238' FROM DUAL UNION ALL
                  SELECT '1256' FROM DUAL UNION ALL
                  SELECT '1257' FROM DUAL UNION ALL
                  SELECT '1258' FROM DUAL UNION ALL
                  SELECT '1254' FROM DUAL UNION ALL
                  SELECT '1290' FROM DUAL UNION ALL
                  SELECT '1310' FROM DUAL
    SELECT  l.id,
            nvl(short_desc,'This job failed') short_desc
      FROM      my_table m
      WHERE RIGTH JOIN
                list
              ON (
                      m.id = l.id
                  AND
                      the_date > = SYSDATE - 23/24
                  AND
                      the_date < TRUNC(SYSDATE) + 1
    SY.

Maybe you are looking for