Results 1 to 7 of 7
  1. #1
    CuongPham is offline Novice
    Windows 8 Access 2007
    Join Date
    Dec 2013
    Posts
    4

    Post Problem with the characters of query when alias column


    Hi everyone.

    I have a query: (Test with database NorthWind)

    SELECT Employees.FirstName, SUM([ABC].[1994]) AS ['1994'], SUM([ABC].[1995]) AS ['1995'], SUM([ABC].[1996]) AS ['1996'], SUM(([ABC].[1994]) + ([ABC].[1995]) + ([ABC].[1996])) AS ToTalAmount
    FROM Employees LEFT JOIN (SELECT Employees.EmployeeID
    ,YEAR(Orders.OrderDate) AS [YEAR]
    ,Switch (YEAR(Orders.OrderDate) = 1994, SUM([Order Details].Quantity)) AS '1994'
    ,Switch(YEAR(Orders.OrderDate) = 1995, SUM([Order Details].Quantity)) AS '1995'
    ,Switch (YEAR(Orders.OrderDate) = 1996, SUM([Order Details].Quantity)) AS '1996'
    ,SUM([Order Details].Quantity) AS Total
    FROM (Orders
    LEFT JOIN [Order Details] ON [Order Details].OrderID = Orders.OrderID)
    LEFT JOIN Employees ON Orders.EmployeeID = Employees.EmployeeID
    GROUP BY Employees.EmployeeID,YEAR(Orders.OrderDate)) AS ABC ON Employees.EmployeeID = [ABC].EmployeeID
    GROUP BY Employees.FirstName;

    When I run query. Instead of, it will show data:

    FirtName 1994 1995 1996 TotalAmount
    Andrew 766 2748 2541 6055
    Anne 550 898 1222 2670
    Janet 840 3853 3159 7852
    Laura 617 2947 2349 5913
    Margaret 1836 5086 2876 9798
    Michael 623 1786 1118 3527
    Nancy 1084 3830 2898 7812
    Robert 485 2243 1926 4654
    Steven 580 1616 840 3036

    But It shows dialog because Access understood column [] as a parameter.
    How can fix it ?

    Thanks everyone very much.

  2. #2
    June7's Avatar
    June7 is online now VIP
    Windows 7 64bit Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    52,929
    The [] are not issue. Access can't find a field. Which field triggers the popup?

    Why are you using apostrophes in field names?

    Don't use Year as field name. Year is a reserved word.
    How to attach file: http://www.accessforums.net/showthread.php?t=70301 To provide db: copy, remove confidential data, run compact & repair, zip w/Windows Compression.

  3. #3
    CuongPham is offline Novice
    Windows 8 Access 2007
    Join Date
    Dec 2013
    Posts
    4
    Thanks June7 very much.
    But I want to print 4 columns which will show data as above. I run them in SQL, It's nice...but in Access
    How can I fix it ???

  4. #4
    June7's Avatar
    June7 is online now VIP
    Windows 7 64bit Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    52,929
    Which field triggers the popup?

    Try:

    ,Switch(YEAR(Orders.OrderDate) = 1994, SUM([Order Details].Quantity)) AS [1994]
    ,Switch(YEAR(Orders.OrderDate) = 1995, SUM([Order Details].Quantity)) AS [1995]
    ,Switch(YEAR(Orders.OrderDate) = 1996, SUM([Order Details].Quantity)) AS [1996]
    How to attach file: http://www.accessforums.net/showthread.php?t=70301 To provide db: copy, remove confidential data, run compact & repair, zip w/Windows Compression.

  5. #5
    CuongPham is offline Novice
    Windows 8 Access 2007
    Join Date
    Dec 2013
    Posts
    4
    Wow... It's very nice
    Thanks June7 very very much

    Ah TotalAmount which dont show data..Can you fix it ??? :|

  6. #6
    June7's Avatar
    June7 is online now VIP
    Windows 7 64bit Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    52,929
    If any term is null the result will be null because arithmetic with null returns null. Handle possible null.

    SUM(Nz([ABC].[1994],0) + Nz([ABC].[1995],0) + Nz([ABC].[1996],0))
    How to attach file: http://www.accessforums.net/showthread.php?t=70301 To provide db: copy, remove confidential data, run compact & repair, zip w/Windows Compression.

  7. #7
    CuongPham is offline Novice
    Windows 8 Access 2007
    Join Date
    Dec 2013
    Posts
    4
    Ok...I fixed it with IIF
    thanks June7 very very much
    Best Regards

Please reply to this thread with any new information or opinions.

Similar Threads

  1. Replies: 9
    Last Post: 02-11-2013, 03:09 PM
  2. Replies: 4
    Last Post: 07-27-2012, 07:04 AM
  3. Replies: 7
    Last Post: 02-20-2012, 07:19 PM
  4. Replies: 5
    Last Post: 10-18-2010, 04:56 AM
  5. Alias Help
    By shexe in forum Queries
    Replies: 3
    Last Post: 09-07-2010, 12:28 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Other Forums: Microsoft Office Forums