Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

SQL SELECT Max

Posted on 2013-05-25
8
Medium Priority
?
473 Views
Last Modified: 2013-05-25
Hello,
I'm trying to use below code to find the last Invoice number (Highest) per client.

Results I'm getting is all Invoices / per client.

Can you please help.

Select (Select Name From Clients where Clients.AccountNumber = AROpentransactions.AccountNumber) AS [Client],
AccountNumber, MAX (TransactionDate), TransactionNumber,OrdersSubtotalAmount,SalesAmount,TaxAmount,ARAmount,
(Select Accountlocked From Clients where Clients.AccountNumber = AROpentransactions.AccountNumber) AS [Accountlocked]  
from ARopentransactions
where (Select Accountlocked From Clients where Clients.AccountNumber = AROpentransactions.AccountNumber) = 1
AND TransactionType = 1
GROUP BY AccountNumber,TransactionNumber,OrdersSubtotalAmount,SalesAmount,TaxAmount,ARAmount

Thanks,
0
Comment
Question by:W.E.B
  • 3
  • 3
  • 2
8 Comments
 
LVL 49

Expert Comment

by:PortletPaul
ID: 39197187
please try this
SELECT
      Clients.NAME AS [Client]
    , OT.AccountNumber
    , OT.TransactionDate
    , OT.TransactionNumber
    , OT.OrdersSubtotalAmount
    , OT.SalesAmount
    , OT.TaxAmount
    , OT.ARAmount
    , Clients.Accountlocked
FROM ARopentransactions as OT
INNER JOIN (
            SELECT
              AccountNumber
            , row_number() over (partition by AccountNumber order by TransactionDate DESC) as row_ref
            FROM ARopentransactions
            WHERE TransactionType = 1
           ) AS Latest ON OT.AccountNumber = Latest.AccountNumber
                      AND OT.TransactionDate = Latest.TransactionDate
INNER JOIN Clients
        ON  OT.AccountNumber = Clients.AccountNumber
WHERE Latest.row_ref = 1

Open in new window

0
 

Author Comment

by:W.E.B
ID: 39197189
Hello Paul,
I get error

Msg 207, Level 16, State 1, Line 19
Invalid column name 'TransactionDate'.

at this line
           ) AS Latest ON OT.AccountNumber = Latest.AccountNumber
                      AND OT.TransactionDate = Latest.TransactionDate

thanks
0
 
LVL 49

Accepted Solution

by:
PortletPaul earned 1600 total points
ID: 39197209
forgot an entry in that query (line 15)
SELECT
      Clients.NAME AS [Client]
    , OT.AccountNumber
    , OT.TransactionDate
    , OT.TransactionNumber
    , OT.OrdersSubtotalAmount
    , OT.SalesAmount
    , OT.TaxAmount
    , OT.ARAmount
    , Clients.Accountlocked
FROM ARopentransactions as OT
INNER JOIN (
            SELECT
              AccountNumber
            , TransactionDate
            , row_number() over (partition by AccountNumber order by TransactionDate DESC) as row_ref
            FROM ARopentransactions
            WHERE TransactionType = 1
           ) AS Latest ON OT.AccountNumber = Latest.AccountNumber
                      AND OT.TransactionDate = Latest.TransactionDate
INNER JOIN Clients
        ON  OT.AccountNumber = Clients.AccountNumber
WHERE Latest.row_ref = 1

Open in new window

is there a unique row identifier in table ARopentransactions? if there is this would be more efficient
SELECT
      Clients.NAME AS [Client]
    , OT.AccountNumber
    , OT.TransactionDate
    , OT.TransactionNumber
    , OT.OrdersSubtotalAmount
    , OT.SalesAmount
    , OT.TaxAmount
    , OT.ARAmount
    , Clients.Accountlocked
FROM ARopentransactions as OT
INNER JOIN (
            SELECT
              ID --<< unique identiry of this table
            , TransactionDate
            , row_number() over (partition by AccountNumber order by TransactionDate DESC) as row_ref
            FROM ARopentransactions
            WHERE TransactionType = 1
           ) AS Latest ON OT.ID = Latest.ID --<< unique identiry of this table
INNER JOIN Clients
        ON  OT.AccountNumber = Clients.AccountNumber
WHERE Latest.row_ref = 1

Open in new window

0
NFR key for Veeam Backup for Microsoft Office 365

Veeam is happy to provide a free NFR license (for 1 year, up to 10 users). This license allows for the non‑production use of Veeam Backup for Microsoft Office 365 in your home lab without any feature limitations.

 
LVL 75

Expert Comment

by:Anthony Perkins
ID: 39197212
I suspect you may find the derived table is just missing the TransactionDate column (but no points if I am right).  In other words, it should be:
SELECT  Clients.NAME AS [Client],
        OT.AccountNumber,
        OT.TransactionDate,
        OT.TransactionNumber,
        OT.OrdersSubtotalAmount,
        OT.SalesAmount,
        OT.TaxAmount,
        OT.ARAmount,
        Clients.Accountlocked
FROM    ARopentransactions AS OT
        INNER JOIN (SELECT  AccountNumber,
                            TransactionDate,
                            ROW_NUMBER() OVER (PARTITION BY AccountNumber ORDER BY TransactionDate DESC) AS row_ref
                    FROM    ARopentransactions
                    WHERE   TransactionType = 1
                   ) AS Latest ON OT.AccountNumber = Latest.AccountNumber
                                  AND OT.TransactionDate = Latest.TransactionDate
        INNER JOIN Clients ON OT.AccountNumber = Clients.AccountNumber
WHERE   Latest.row_ref = 1

Open in new window

0
 
LVL 75

Expert Comment

by:Anthony Perkins
ID: 39197213
Oops, sorry about that.  I did not see your last comment.
0
 
LVL 49

Expert Comment

by:PortletPaul
ID: 39197222
:) no problem, & you spotted the ommission
0
 

Author Comment

by:W.E.B
ID: 39197226
Paul,
As always,
Thank you.
0
 

Author Closing Comment

by:W.E.B
ID: 39197227
Thank you
0

Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

SQL Server engine let you use a Windows account or a SQL Server account to connect to a SQL Server instance. This can be configured immediatly during the SQL Server installation or after in the Server Authentication section in the Server properties …
Ever needed a SQL 2008 Database replicated/mirrored/log shipped on another server but you can't take the downtime inflicted by initial snapshot or disconnect while T-logs are restored or mirror applied? You can use SQL Server Initialize from Backup…
Want to learn how to record your desktop screen without having to use an outside camera. Click on this video and learn how to use the cool google extension called "Screencastify"! Step 1: Open a new google tab Step 2: Go to the left hand upper corn…
In response to a need for security and privacy, and to continue fostering an environment members can turn to for support, solutions, and education, Experts Exchange has created anonymous question capabilities. This new feature is available to our Pr…

879 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question