Solved

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

Posted on 2011-03-10
7
454 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
[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
  • 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
The Orion Papers

Are you interested in becoming an AWS Certified Solutions Architect?

Discover a new interactive way of training for the exam.

 

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

Increase Agility with Enabled Toolchains

Connect your existing build, deployment, management, monitoring, and collaboration platforms. From Puppet to Chef, HipChat to Slack, ServiceNow to JIRA, Splunk to New Relic and beyond, hand off data between systems to engage the right people.

Connect with xMatters.

Question has a verified solution.

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

by Mark Wills Attending one of Rob Farley's seminars the other day, I heard the phrase "The Accidental DBA" and fell in love with it. It got me thinking about the plight of the newcomer to SQL Server...  So if you are the accidental DBA, or, simp…
Use this article to create a batch file to backup a Microsoft SQL Server database to a Windows folder.  The folder can be on the local hard drive or on a network share.  This batch file will query the SQL server to get the current date & time and wi…
This is a high-level webinar that covers the history of enterprise open source database use. It addresses both the advantages companies see in using open source database technologies, as well as the fears and reservations they might have. In this…
If you're a developer or IT admin, you’re probably tasked with managing multiple websites, servers, applications, and levels of security on a daily basis. While this can be extremely time consuming, it can also be frustrating when systems aren't wor…

696 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