dcsimg
www.webdeveloper.com
Results 1 to 6 of 6

Thread: Query concatenated

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

    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
    104
    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 07: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
    104
    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



Recent Articles