Solved

SQL SELECT Max

Posted on 2013-05-25
8
460 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:Wass_QA
  • 3
  • 3
  • 2
8 Comments
 
LVL 48

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:Wass_QA
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 48

Accepted Solution

by:
PortletPaul earned 400 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
 
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
What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

 
LVL 75

Expert Comment

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

Expert Comment

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

Author Comment

by:Wass_QA
ID: 39197226
Paul,
As always,
Thank you.
0
 

Author Closing Comment

by:Wass_QA
ID: 39197227
Thank you
0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Suggested Solutions

If you have heard of RFC822 date formats, they can be quite a challenge in SQL Server. RFC822 is an Internet standard format for email message headers, including all dates within those headers. The RFC822 protocols are available in detail at:   ht…
In this article we will get to know that how can we recover deleted data if it happens accidently. We really can recover deleted rows if we know the time when data is deleted by using the transaction log.
Sending a Secure fax is easy with eFax Corporate (http://www.enterprise.efax.com). First, Just open a new email message.  In the To field, type your recipient's fax number @efaxsend.com. You can even send a secure international fax — just include t…
Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.

762 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

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now