?
Solved

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

Posted on 2011-03-10
7
Medium Priority
?
458 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 41

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 41

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
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.

 

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 41

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 41

Accepted Solution

by:
Sharath earned 2000 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

Efficient way to get backups off site to Azure

This user guide provides instructions on how to deploy and configure both a StoneFly Scale Out NAS Enterprise Cloud Drive virtual machine and Veeam Cloud Connect in the Microsoft Azure Cloud.

Question has a verified solution.

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

Occasionally there is a need to clean table columns, especially if you have inherited legacy data. There are obviously many ways to accomplish that, including elaborate UPDATE queries with anywhere from one to numerous REPLACE functions (even within…
It is possible to export the data of a SQL Table in SSMS and generate INSERT statements. It's neatly tucked away in the generate scripts option of a database.
Are you ready to place your question in front of subject-matter experts for more timely responses? With the release of Priority Question, Premium Members, Team Accounts and Qualified Experts can now identify the emergent level of their issue, signal…
With just a little bit of  SQL and VBA, many doors open to cool things like synchronize a list box to display data relevant to other information on a form.  If you have never written code or looked at an SQL statement before, no problem! ...  give i…

840 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