Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

SQL SELECT Max

Posted on 2013-05-25
8
Medium Priority
?
472 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Visualize your virtual and backup environments

Create well-organized and polished visualizations of your virtual and backup environments when planning VMware vSphere, Microsoft Hyper-V or Veeam deployments. It helps you to gain better visibility and valuable business insights.

 
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

Visualize your virtual and backup environments

Create well-organized and polished visualizations of your virtual and backup environments when planning VMware vSphere, Microsoft Hyper-V or Veeam deployments. It helps you to gain better visibility and valuable business insights.

Question has a verified solution.

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

I have written a PowerShell script to "walk" the security structure of each SQL instance to find:         Each Login (Windows or SQL)             * Its Server Roles             * Every database to which the login is mapped             * The associated "Database User" for this …
How to leverage one TLS certificate to encrypt Microsoft SQL traffic and Remote Desktop Services, versus creating multiple tickets for the same server.
This is my first video review of Microsoft Bookings, I will be doing a part two with a bit more information, but wanted to get this out to you folks.
Sometimes it takes a new vantage point, apart from our everyday security practices, to truly see our Active Directory (AD) vulnerabilities. We get used to implementing the same techniques and checking the same areas for a breach. This pattern can re…

721 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