Solved

How to select only records with the lowest value for a field of type nvarchar?

Posted on 2011-03-10
7
449 Views
Last Modified: 2012-05-11
I am developing an Access application using Access 2003 with an ADP type file wtih SQL Server 2005.

I currently use the following SQL statement to filter out records from one table named
blGetDocumentAttributes and place the result set into another table named tblGetDocumentAttributesMod.

select t1.* into dbo.tblGetDocumentAttributesMod from tblGetDocumentAttributes t1 left join tblGetDocumentAttributes t2 on t1.Account = t2.Account And t1.DocumentDate < t2.DocumentDate where t2.MailId is Null;"

What additional SQL Statement would you use if there are multipe records
in table tblGetDocumentAttributesMod wtih the same account field value and same
documentdate field value but with different MailID values and you only want to keep the
MailID (type nvarchar) value with the highest value for a particular account.
0
Comment
Question by:zimmer9
  • 4
  • 3
7 Comments
 
LVL 40

Expert Comment

by:Sharath
ID: 35101136
Try like this.
SELECT * 
  FROM dbo.tblGetDocumentAttributesMod AS t1 
 WHERE MailID = (SELECT MAX(MailID) 
                   FROM dbo.tblGetDocumentAttributesMod AS t2 
                  WHERE t1.Account = t2.Account 
                        AND t1.DocumentDate = t2.DocumentDate)

Open in new window

0
 

Author Comment

by:zimmer9
ID: 35101238
That looks good.

What if you want to keep onlly these records in table tblGetDocumentAttributesMod?
0
 
LVL 40

Expert Comment

by:Sharath
ID: 35101289
try like this.
SELECT t1.* 
  INTO dbo.tblGetDocumentAttributesMod 
  FROM tblGetDocumentAttributes t1 
       LEFT JOIN tblGetDocumentAttributes t2 
         ON t1.Account = t2.Account 
            AND t1.DocumentDate < t2.DocumentDate 
 WHERE t2.MailId IS NULL 
       AND t1.MaiID = (SELECT MAX(MailID) 
                         FROM dbo.tblGetDocumentAttributes AS t3 
                        WHERE t1.Account = t3.Account 
                              AND t1.DocumentDate = t3.DocumentDate);

Open in new window

0
Zoho SalesIQ

Hassle-free live chat software re-imagined for business growth. 2 users, always free.

 

Author Comment

by:zimmer9
ID: 35101337
I get the following message:

Msg 207, Level 16, State 1, Line 22
Invalid column name 'MaiID'.           --->  AND t1.MaiID = (SELECT MAX(MailID)
0
 
LVL 40

Expert Comment

by:Sharath
ID: 35101357
Missed the table alias.
SELECT t1.* 
  INTO dbo.tblGetDocumentAttributesMod 
  FROM tblGetDocumentAttributes t1 
       LEFT JOIN tblGetDocumentAttributes t2 
         ON t1.Account = t2.Account 
            AND t1.DocumentDate < t2.DocumentDate 
 WHERE t2.MailId IS NULL 
       AND t1.MaiID = (SELECT MAX(t3.MailID) 
                         FROM dbo.tblGetDocumentAttributes AS t3 
                        WHERE t1.Account = t3.Account 
                              AND t1.DocumentDate = t3.DocumentDate);

Open in new window

0
 

Author Comment

by:zimmer9
ID: 35101394
SELECT t1.*
  INTO dbo.tblGetDocumentAttributesMod
  FROM tblGetDocumentAttributes t1
       LEFT JOIN tblGetDocumentAttributes t2
         ON t1.Account = t2.Account
            AND t1.DocumentDate < t2.DocumentDate
 WHERE t2.MailId IS NULL
       AND t1.MaiID = (SELECT MAX(t3.MailID)          <------------  Line 22
                         FROM dbo.tblGetDocumentAttributes AS t3
                        WHERE t1.Account = t3.Account
                              AND t1.DocumentDate = t3.DocumentDate);

this still gives me the folllowing error message:

Msg 207, Level 16, State 1, Line 22
Invalid column name 'MaiID'.
0
 
LVL 40

Accepted Solution

by:
Sharath earned 500 total points
ID: 35101475
I typed the MailID wrong.
SELECT t1.* 
  INTO dbo.tblGetDocumentAttributesMod 
  FROM tblGetDocumentAttributes t1 
       LEFT JOIN tblGetDocumentAttributes t2 
         ON t1.Account = t2.Account 
            AND t1.DocumentDate < t2.DocumentDate 
 WHERE t2.MailId IS NULL 
       AND t1.MailId = (SELECT MAX(t3.MailId)         
                         FROM dbo.tblGetDocumentAttributes AS t3 
                        WHERE t1.Account = t3.Account 
                              AND t1.DocumentDate = t3.DocumentDate); 

Open in new window

0

Featured Post

What is SQL Server and how does it work?

The purpose of this paper is to provide you background on SQL Server. It’s your self-study guide for learning fundamentals. It includes both the history of SQL and its technical basics. Concepts and definitions will form the solid foundation of your future DBA expertise.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
execute a MS SQL script as a schedule SQL job 72 123
How can I exclude some wording in a like statement? 39 67
Help with SQL - TOP 10 by date and by group 13 34
CROSS APPLY 4 45
Confronted with some SQL you don't know can be a daunting task. It can be even more daunting if that SQL carries some of the old secret codes used in the Ye Olde query syntax, such as: (+)     as used in Oracle;     *=     =*    as used in Sybase …
This article describes how to use the timestamp of existing data in a database to allow Tableau to calculate the prior work day instead of relying on case statements or if statements to calculate the days of the week.
Video by: Mark
This lesson goes over how to construct ordered and unordered lists and how to create hyperlinks.
As a trusted technology advisor to your customers you are likely getting the daily question of, ‘should I put this in the cloud?’ As customer demands for cloud services increases, companies will see a shift from traditional buying patterns to new…

863 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

19 Experts available now in Live!

Get 1:1 Help Now