www.webdeveloper.com
Results 1 to 6 of 6

Thread: Query concatenated

  1. #1
    Join Date
    Apr 2007
    Location
    Italy
    Posts
    110

    Query concatenated

    Hi to all,

    in my access db I use correctly these 3 queries:

    q_A:
    SELECT Numero, Nazione, Nome, Wp_date AS [Work Proposal], Ris AS Ris_C, Rev FROM t_mrg
    WHERE Numero Like '2013044' ORDER BY Numero DESC , Ris DESC , Rev DESC;

    with the code above I obtain this result:

    Numero Nazione Nome Work Proposal Ris_C Rev
    2013044 Italy Formazione 28/03/2013 02 02
    2013044 Italy Formazione 29/03/2013 02 01
    2013044 Italy Formazione 18/03/2013 01 01
    2013044 Italy Formazione 18/03/2013 01 00



    q_B:
    SELECT Numero, Max(Ris) AS Ris_C FROM t_mrg GROUP BY Numero
    HAVING Numero Like '2013044' ORDER BY Numero DESC , Max(Ris) DESC;

    with this result:

    Numero Ris_C
    2013044 02



    q_C:
    SELECT q_A.Numero, q_A.Nazione, q_A.Nome, q_A.[Work Proposal], q_B.Ris_C AS Ris, q_A.Rev
    FROM q_B LEFT JOIN q_A ON q_B.Ris_C = q_A.Ris_C
    GROUP BY q_A.Numero, q_A.Nazione, q_A.Nome, q_A.[Work Proposal], q_B.Ris_C, q_A.Rev;

    with this result:

    Numero Nazione Nome Work Proposal Ris_C Rev
    2013044 Italy Formazione 28/03/2013 02 02
    2013044 Italy Formazione 29/03/2013 02 01


    Here all is ok !!

    Now I have to made the same thing inside my_page.ap:

    <%@ Language=VBScript %>
    <%
    Option Explicit
    Dim objConn, objRS_ini, objRS_ris, objRS_last, str_ini, str_ris, str_last, year
    %>
    ....

    <BODY>

    <%
    '
    Set objConn = Server.CreateObject("ADODB.Connection")
    objConn.ConnectionString = "DSN=2_advance.dsn"
    objConn.Open

    str_ini = "SELECT Numero, Nazione, Nome, Wp_date AS [Work Proposal], Ris AS Ris_C, Rev FROM t_mrg WHERE Numero Like '2013044' ORDER BY Numero DESC , Ris DESC , Rev DESC;"
    Set objRS_ini = objConn.Execute(str_ini) ' equivalent of q_A

    str_ris = "SELECT Numero, Max(Ris) AS Ris_C FROM t_mrg GROUP BY Numero HAVING Numero Like '2013044' ORDER BY Numero DESC , Max(Ris) DESC;"
    Set objRS_ris = objConn.Execute(str_ris) ' equivalent of q_B

    ' str_last="SELECT str_ini.Numero, str_ini.Nazione, str_ini.Nome, str_ini.[Work Proposal], str_ris.Ris_C AS Ris, str_ini.Rev FROM str_ris LEFT JOIN str_ini ON str_ris.Ris_C = str_ini.Ris_C GROUP BY str_ini.Numero, str_ini.Nazione, str_ini.Nome, str_ini.[Work Proposal], str_ris.Ris_C, str_ini.Rev;"
    ' Set objRS_last = objConn.Execute(str_last)

    In which way can I create the equivalent of q_C??

    I have tried with str_last but when I reload my_page.asp I receive the correct error that inside db there sin' t table whose name is str_ini !!


    Do While Not objRS_ini.EOF
    count_rec = count_rec + 1
    '
    If count_rec = 1 Then
    Response.Write "<TABLE border=1><TR>"
    For iLoop = 0 to objRS_ini.Fields.Count - 1
    '
    Response.Write "<TD>" & objRS_ini.Fields(iLoop).Name & "</TD>"
    '
    Next
    Response.Write "<TR>"

    End If

    For iLoop = 0 to objRS_ini.Fields.Count - 1
    Response.Write "<TD>" & objRS_ini.Fields(iLoop).Value & "</TD>"
    Next
    Response.Write "</TR>"
    '
    objRS_ini.MoveNext
    Loop
    Response.Write "</TABLE></p>"
    '
    objRS_ini.Close : objRS_ris.Close
    Set objRS_ini = Nothing : set objRS_ris = Nothing : Set objConn = Nothing
    Set fs = nothing : Set folder = nothing
    '
    %>
    </BODY>

    Thanks a lot in advance !!!


    P.S Is it necessary to use temporary table OR put data in a varaible OR creare a stored procedure ??

  2. #2
    Join Date
    Sep 2011
    Posts
    60
    You should understand your str_ini, str_ris, str_last is asp variables and access does not know that variables... so where you send to access query
    SELECT str_ini.Numero, str_ini.Nazione, str_ini.Nome, str_ini.[Work Proposal], str_ris.Ris_C AS Ris, str_ini.Rev FROM str_ris LEFT JOIN str_ini ON str_ris.Ris_C = str_ini.Ris_C GROUP BY str_ini.Numero, str_ini.Nazione, str_ini.Nome, str_ini.[Work Proposal], str_ris.Ris_C, str_ini.Rev;
    access has no ideas about str_ini and str_ris.
    what you can do is make str_last assignment like
    str_last = 'SELECT str_ini.Numero, str_ini.Nazione, str_ini.Nome, str_ini.[Work Proposal], str_ris.Ris_C AS Ris, str_ini.Rev FROM (" + sat_ris + ") as str_ris LEFT JOIN (" + str_ini + ") as str_ini ON str_ris.Ris_C = str_ini.Ris_C GROUP BY str_ini.Numero, str_ini.Nazione, str_ini.Nome, str_ini.[Work Proposal], str_ris.Ris_C, str_ini.Rev;'
    that will embed your strings for str_ini and str_ris into your str_last

  3. #3
    Join Date
    Apr 2007
    Location
    Italy
    Posts
    110
    Thanks a lot gk53 for your help !! But up to now the problem persists ...

    If I write this string:

    str_last = 'SELECT str_ini.Numero, str_ini.Nazione, str_ini.Nome, str_ini.[Work Proposal], str_ris.Ris_C AS Ris, str_ini.Rev FROM (" + str_ris + ") as str_ris LEFT JOIN (" + str_ini + ") as str_ini ON str_ris.Ris_C = str_ini.Ris_C GROUP BY str_ini.Numero, str_ini.Nazione, str_ini.Nome, str_ini.[Work Proposal], str_ris.Ris_C, str_ini.Rev;'
    Set objRS_last = objConn.Execute(str_last)


    I receive Syntax error !!

    So I tried to modify the string as:

    str_last = "SELECT str_ini.Numero, str_ini.Nazione, str_ini.Nome, str_ini.[Work Proposal], str_ris.Ris_C AS Ris, str_ini.Rev FROM (" & str_ris & ") as str_ris LEFT JOIN (" & str_ini & ") as str_ini ON str_ris.Ris_C = str_ini.Ris_C GROUP BY str_ini.Numero, str_ini.Nazione, str_ini.Nome, str_ini.[Work Proposal], str_ris.Ris_C, str_ini.Rev;"

    but I receive syntax error for FROM !!

    I don' t know how to write well the string ...
    Last edited by il_betto; 07-29-2013 at 06:21 AM.

  4. #4
    Join Date
    Sep 2011
    Posts
    60
    did you tried to print full query and see what you have?
    after
    str_last = 'SELECT str_ini.Numero, str_ini.Nazione, str_ini.Nome, str_ini.[Work Proposal], str_ris.Ris_C AS Ris, str_ini.Rev FROM (" + str_ris + ") as str_ris LEFT JOIN (" + str_ini + ") as str_ini ON str_ris.Ris_C = str_ini.Ris_C GROUP BY str_ini.Numero, str_ini.Nazione, str_ini.Nome, str_ini.[Work Proposal], str_ris.Ris_C, str_ini.Rev;'
    add
    response.write str_last
    response.flush
    response.end
    run page in browser
    you should have your final sql on screen. Copy and try to run that in Access

  5. #5
    Join Date
    Sep 2011
    Posts
    60
    I found a problem with your page
    You need to remove ; on
    str_ini = "SELECT Numero, Nazione, Nome, Wp_date AS [Work Proposal], Ris AS Ris_C, Rev FROM t_mrg WHERE Numero Like '2013044' ORDER BY Numero DESC , Ris DESC , Rev DESC;"
    and
    str_ris = "SELECT Numero, Max(Ris) AS Ris_C FROM t_mrg GROUP BY Numero HAVING Numero Like '2013044' ORDER BY Numero DESC , Max(Ris) DESC;"
    ; id terminatin character in sql, so, when you embed str_ris or str_ini inside str_last it will terminate sql statement

    after you fix that problem
    response.write str_last
    response.flush
    response.end
    from previouse post will produce
    SELECT str_ini.Numero, str_ini.Nazione, str_ini.Nome, str_ini.[Work Proposal], str_ris.Ris_C AS Ris, str_ini.Rev
    FROM (SELECT Numero, Max(Ris) AS Ris_C
    FROM t_mrg
    GROUP BY Numero
    HAVING Numero Like '2013044' ORDER BY Numero DESC , Max(Ris) DESC) as str_ris
    LEFT JOIN (SELECT Numero, Nazione, Nome, Wp_date AS [Work Proposal], Ris AS Ris_C, Rev
    FROM t_mrg
    WHERE Numero Like '2013044'
    ORDER BY Numero DESC , Ris DESC , Rev DESC) as str_ini
    ON str_ris.Ris_C = str_ini.Ris_C
    GROUP BY str_ini.Numero, str_ini.Nazione, str_ini.Nome, str_ini.[Work Proposal], str_ris.Ris_C, str_ini.Rev;
    which looks OK. But I would recommend to run that sql in access to make sure it works

  6. #6
    Join Date
    Apr 2007
    Location
    Italy
    Posts
    110
    gk53, very very good !!! Fantastic !!!

    You have solved my problem, I am very happy !!!

    I have removed the character ";" at the end of str_ini and str_ris
    str_ini = "SELECT Numero, Activity, Qualita AS Q, Nazione, Nome, Wp_date AS [Work Proposal], Ris AS Ris_C, Rev FROM t_mrg WHERE Numero Like '2013044' AND Activity Not Like '??' ORDER BY Numero DESC , Ris DESC , Rev DESC"
    Set objRS_ini = objConn.Execute(str_ini)
    str_ris = "SELECT Numero, Activity, Max(Ris) AS Ris_C FROM t_mrg GROUP BY Numero, Activity HAVING Numero Like '2013044' AND Activity Not Like '??' ORDER BY t_mrg.Numero DESC , Max(Ris) DESC"
    Set objRS_ris = objConn.Execute(str_ris)
    str_last = "SELECT str_ini.Numero, str_ini.Nazione, str_ini.Nome, str_ini.[Work Proposal], str_ris.Ris_C AS Ris, str_ini.Rev FROM (" & str_ris & ") as str_ris LEFT JOIN (" & str_ini & ") as str_ini ON str_ris.Ris_C = str_ini.Ris_C GROUP BY str_ini.Numero, str_ini.Nazione, str_ini.Nome, str_ini.[Work Proposal], str_ris.Ris_C, str_ini.Rev;"
    'response.write str_last
    'response.flush
    'response.end
    Set objRS_last = objConn.Execute(str_last)

    Do While Not objRS_last.EOF
    '
    count_rec = count_rec + 1
    '
    If count_rec = 1 Then
    Response.Write "<TABLE border=1><TR>"
    For iLoop = 0 to objRS_last.Fields.Count - 1
    '
    Response.Write "<TD BGCOLOR=FFFFCC ALIGN=CENTER><FONT SIZE=2 color='#006600'>" & objRS_last.Fields(iLoop).Name & "</FONT></TD>"
    '
    Next
    Response.Write "<TR>"

    End If
    '
    For iLoop = 0 to objRS_last.Fields.Count - 1
    Response.Write "<TD BGCOLOR=white ALIGN=CENTER><FONT SIZE=2 color='#006600'>" & objRS_last.Fields(iLoop).Value & "</FONT></TD>"

    Next
    Response.Write "</TR>"
    '
    objRS_last.MoveNext
    Loop


    Response.Write "</TABLE></p>"


    All is OK !!! Thanks a lot again !!!

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center

"

"

X vBulletin 4.2.2 Debug Information

  • Page Generation 0.33356 seconds
  • Memory Usage 2,894KB
  • Queries Executed 13 (?)
More Information
Template Usage (32):
  • (1)SHOWTHREAD
  • (1)ad_footer_end
  • (1)ad_footer_start
  • (1)ad_global_above_footer
  • (1)ad_global_below_navbar
  • (1)ad_global_header1
  • (1)ad_global_header2
  • (1)ad_navbar_below
  • (1)ad_showthread_firstpost_sig
  • (1)ad_showthread_firstpost_start
  • (1)ad_thread_first_post_content
  • (1)ad_thread_last_post_content
  • (1)footer
  • (1)forumjump
  • (1)forumrules
  • (1)gobutton
  • (1)header
  • (1)headinclude
  • (1)headinclude_bottom
  • (6)memberaction_dropdown
  • (1)navbar
  • (4)navbar_link
  • (1)navbar_moderation
  • (1)navbar_noticebit
  • (1)navbar_tabs
  • (2)option
  • (6)postbit
  • (6)postbit_onlinestatus
  • (6)postbit_wrapper
  • (1)spacer_close
  • (1)spacer_open
  • (1)tagbit_wrapper 

Phrase Groups Available (6):
  • global
  • inlinemod
  • postbit
  • posting
  • reputationlevel
  • showthread
Included Files (26):
  • ./showthread.php
  • ./global.php
  • ./includes/class_bootstrap.php
  • ./includes/init.php
  • ./includes/class_core.php
  • ./includes/config.php
  • ./includes/functions.php
  • ./includes/functions_navigation.php
  • ./includes/class_friendly_url.php
  • ./includes/class_hook.php
  • ./includes/class_bootstrap_framework.php
  • ./vb/vb.php
  • ./vb/phrase.php
  • ./includes/functions_facebook.php
  • ./includes/functions_calendar.php
  • ./includes/functions_bigthree.php
  • ./includes/class_postbit.php
  • ./includes/class_bbcode.php
  • ./includes/functions_reputation.php
  • ./includes/functions_notice.php
  • ./packages/vbattach/attach.php
  • ./vb/types.php
  • ./vb/cache.php
  • ./vb/cache/db.php
  • ./vb/cache/observer/db.php
  • ./vb/cache/observer.php 

Hooks Called (71):
  • init_startup
  • friendlyurl_resolve_class
  • init_startup_session_setup_start
  • database_pre_fetch_array
  • database_post_fetch_array
  • init_startup_session_setup_complete
  • global_bootstrap_init_start
  • global_bootstrap_init_complete
  • cache_permissions
  • fetch_threadinfo_query
  • fetch_threadinfo
  • fetch_foruminfo
  • load_show_variables
  • load_forum_show_variables
  • global_state_check
  • global_bootstrap_complete
  • global_start
  • style_fetch
  • global_setup_complete
  • showthread_start
  • showthread_getinfo
  • strip_bbcode
  • friendlyurl_clean_fragment
  • friendlyurl_geturl
  • forumjump
  • cache_templates
  • cache_templates_process
  • template_register_var
  • template_render_output
  • fetch_template_start
  • fetch_template_complete
  • parse_templates
  • fetch_musername
  • notices_check_start
  • notices_noticebit
  • process_templates_complete
  • friendlyurl_redirect_canonical
  • showthread_post_start
  • showthread_query_postids
  • showthread_query
  • bbcode_fetch_tags
  • bbcode_create
  • showthread_postbit_create
  • postbit_factory
  • postbit_display_start
  • postbit_imicons
  • bbcode_parse_start
  • bbcode_parse_complete_precache
  • bbcode_parse_complete
  • postbit_display_complete
  • memberaction_dropdown
  • tag_fetchbit
  • tag_fetchbit_complete
  • forumrules
  • navbits
  • navbits_complete
  • build_navigation_data
  • build_navigation_array
  • check_navigation_permission
  • process_navigation_links_start
  • process_navigation_links_complete
  • set_navigation_menu_element
  • build_navigation_menudata
  • build_navigation_listdata
  • build_navigation_list
  • set_navigation_tab_main
  • set_navigation_tab_fallback
  • navigation_tab_complete
  • fb_like_button
  • showthread_complete
  • page_templates