Results 1 to 7 of 7

Thread: [RESOLVED] Jet Query to T-SQL Query Help

  1. #1

    Thread Starter
    Frenzied Member FishGuy's Avatar
    Join Date
    Mar 2005
    Location
    Bradford UK
    Posts
    1,708

    Resolved [RESOLVED] Jet Query to T-SQL Query Help

    I wrote a Crosstab query in access and now want it to run as an sp on SQL Server, I know T-SQL dosent support TRANSFORM and Pivot and have previously got round this using CASE. However I picked up a utility to transform my original jet SQL to T-SQL and seems to just utilise aliases and group by. However the SQL generates an error "The Identifier that starts with 'Select ------ is too long must.maximum of 128" I would really like this to work as there will be a varying amount of columns as the data changes so CASE doesnt seem appropriate.
    Here is my origional Jet SQL-

    TRANSFORM Sum(a.IssueValue) AS SumOfIssueValue
    SELECT [AgentFirst] & " " & [AgentSurname] AS Agent, Sum(a.IssueValue) AS [Total Of IssueValue]
    FROM [SELECT TBL_LK_Agents.AgentID, TBL_LK_Agents.AgentSurname, TBL_LK_Agents.AgentFirst, TBLAds.IssueValue, TBLAds.IssueNo
    FROM TBL_LK_Agents INNER JOIN ((TBLSales INNER JOIN TBLAds ON TBLSales.SaleNo = TBLAds.SaleNo) INNER JOIN TBLClientCompany ON TBLSales.CompanyNo = TBLClientCompany.CompanyNo) ON TBL_LK_Agents.AgentID = TBLSales.SaleMadeBy
    WHERE TBLSales.Cancelled = 0
    GROUP BY TBL_LK_Agents.AgentID, TBL_LK_Agents.AgentSurname, TBL_LK_Agents.AgentFirst, TBLAds.IssueNo,TBLAds.IssueValue]. AS a
    GROUP BY [AgentFirst] & " " & [AgentSurname]
    PIVOT a.IssueNo;


    And Here is my T-SQL
    CREATE PROCEDURE sp_IssueTotalsCross
    AS SELECT [AgentFirst] + ' ' + [AgentSurname] AS Agent,
    a.IssueNo, SUM(a.IssueValue) AS SumOfIssueValue
    FROM
    [SELECT TBL_LK_Agents.AgentID, TBL_LK_Agents.AgentSurname, TBL_LK_Agents.AgentFirst, TBLAds.IssueValue, TBLAds.IssueNo
    FROM TBL_LK_Agents
    INNER JOIN ((TBLSales
    INNER JOIN TBLAds
    ON TBLSales.SaleNo = TBLAds.SaleNo)
    INNER JOIN TBLClientCompany
    ON TBLSales.CompanyNo = TBLClientCompany.CompanyNo)
    ON TBL_LK_Agents.AgentID = TBLSales.SaleMadeBy
    WHERE TBLSales.Cancelled = 0
    GROUP BY TBL_LK_Agents.AgentID, TBL_LK_Agents.AgentSurname, TBL_LK_Agents.AgentFirst, TBLAds.IssueNo,TBLAds.IssueValue], a.IssueNo
    ORDER BY TBL_LK_Agents

    Any help much appreciated

  2. #2
    Frenzied Member PilgrimPete's Avatar
    Join Date
    Feb 2002
    Posts
    1,313

    Re: Jet Query to T-SQL Query Help

    Hi. I'm intrigued by three things here - the period after the subquery in your Jet SQL, the use of square brackets in the T-SQL query, and the reason/use for "a.IssueNo".
    I'd try something like this first:
    Code:
    CREATE PROCEDURE sp_IssueTotalsCross 
    AS 
    SELECT [AgentFirst] + ' ' + [AgentSurname] AS Agent,
    a.IssueNo, SUM(a.IssueValue) AS SumOfIssueValue
    FROM 
    (SELECT TBL_LK_Agents.AgentID, TBL_LK_Agents.AgentSurname, TBL_LK_Agents.AgentFirst, TBLAds.IssueValue, TBLAds.IssueNo 
    FROM TBL_LK_Agents
    INNER JOIN ((TBLSales 
    INNER JOIN TBLAds 
    ON TBLSales.SaleNo = TBLAds.SaleNo) 
    INNER JOIN TBLClientCompany
    ON TBLSales.CompanyNo = TBLClientCompany.CompanyNo) 
    ON TBL_LK_Agents.AgentID = TBLSales.SaleMadeBy 
    WHERE TBLSales.Cancelled = 0
    GROUP BY TBL_LK_Agents.AgentID, TBL_LK_Agents.AgentSurname, TBL_LK_Agents.AgentFirst, TBLAds.IssueNo,TBLAds.IssueValue) AS a
    ORDER BY TBL_LK_Agents
    and see if that progresses things any...

  3. #3
    Frenzied Member PilgrimPete's Avatar
    Join Date
    Feb 2002
    Posts
    1,313

    Re: Jet Query to T-SQL Query Help

    Oh, and the ORDER BY clause is a bit iffy too...
    Code:
    ORDER BY TBL_LK_Agents
    I assume that TBL_LK_Agents is a table. Which field do you want to sort by?

  4. #4

    Thread Starter
    Frenzied Member FishGuy's Avatar
    Join Date
    Mar 2005
    Location
    Bradford UK
    Posts
    1,708

    Re: Jet Query to T-SQL Query Help

    Thanks I tried your suggestion which has cleared the previous error and I have got rid of the order by just to simplify it for the moment, I now get the error that my columns a.AgentFirst etc are invalid in the select list because they are not contained in an aggregate function or there is no Group By clause.

  5. #5

    Thread Starter
    Frenzied Member FishGuy's Avatar
    Join Date
    Mar 2005
    Location
    Bradford UK
    Posts
    1,708

    Re: Jet Query to T-SQL Query Help

    Just so Its easier to see what I want to acheive someting like this


    Agent Issue 1 Issue 2 Issue 3 etc
    Jon £100 £200 £50
    Bill £200 £300 £0

  6. #6
    Frenzied Member PilgrimPete's Avatar
    Join Date
    Feb 2002
    Posts
    1,313

    Re: Jet Query to T-SQL Query Help

    Have you tried adding a GROUP BY Clause?
    Something like:
    Code:
    GROUP BY a.AgentFirst, a.AgentSurname, a.IssueNo

  7. #7

    Thread Starter
    Frenzied Member FishGuy's Avatar
    Join Date
    Mar 2005
    Location
    Bradford UK
    Posts
    1,708

    Re: Jet Query to T-SQL Query Help

    Thanks for your help Pete I left the query as Jet SQL in the end in an access database then let my colleage use it from there as a datasource. Not ideal but it wont be run very often anway, thanks for your help though.

Posting Permissions

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



Click Here to Expand Forum to Full Width