Solved

T-Sql  update join query

Posted on 2008-10-06
2
1,074 Views
Last Modified: 2012-05-05
Hello,  I have rows I enter into an intermediate table and perform some operations, in the final process I end up inserting  into the main table one row per  each entity defined as same firstname, lastname, storenumber and dateofbirth.  Before I do that, I need to update a field called fillename with the value of the filename based on the max dateentered field for that entity, if the fiilename field is not null.  It is possible that all rows for the entity have null for filename.

Assuming the rows of data below in my intermediate table, I want to update the filename  field all 3 to the filename field to 'blat.jpg'
FirstName LastName    DOB   StoreID  Filename DateEntered
John        Doe     10/1/1966  55         blah.jpg      10/1/2007
John       Doe         10/1/1966    55          blat.jpg      5/11/2008
John       Doe     10/1/1966    55         NULL        9/01/2008
 
Soething like this, but adding the grouping or ranking by datenetered and  adding the filename field to query:

UPDATE      mybuf
SET      filename = A.filename
FROM      tbl_MyBuffer mybuf
      INNER JOIN (SELECT FirstName,LastName,DateOfBirth, StoreID,DateEntered                
                   FROM tbl_MyBuffer   WHERE Len(isNull(fileName,'')) > 0
                     GROUP BY Firstname,Lastname,DateOfBirth,StoreID) A
            ON A.FirstName = mybuf.FirstName
            AND A.lastName = mybuf.lastName
            AND isnull(A.DateOfBirth,'') = isnull(mybuf.DateOfBirth ,'')
            AND A.StoreID = mybuf.StoreID

Since I am using SQL 2005 would this lend itself to rank() ?

Thanks!
0
Comment
Question by:vhpcomp
2 Comments
 
LVL 69

Accepted Solution

by:
ScottPletcher earned 500 total points
ID: 22651090
UPDATE      mybuf
SET      filename = B.filename
FROM      tbl_MyBuffer mybuf
      INNER JOIN (SELECT FirstName,LastName,DateOfBirth, StoreID,MAX(DateEntered) AS DateEntered
                   FROM tbl_MyBuffer   WHERE fileName > ''
                     GROUP BY Firstname,Lastname,DateOfBirth,StoreID) A
            ON A.FirstName = mybuf.FirstName
            AND A.lastName = mybuf.lastName
            AND isnull(A.DateOfBirth,'') = isnull(mybuf.DateOfBirth ,'')
            AND A.StoreID = mybuf.StoreID
      INNER JOIN tbl_MyBuffer B
            ON B.FirstName = A.FirstName
            AND B.lastName = A.lastName
            AND isnull(B.DateOfBirth,'') = isnull(A.DateOfBirth ,'')
            AND B.StoreID = A.StoreID
            AND B.DateEntered = A.DateEntered
0
 
LVL 1

Author Closing Comment

by:vhpcomp
ID: 31503432
Works perfectly, thanks.
0

Featured Post

Comprehensive Backup Solutions for Microsoft

Acronis protects the complete Microsoft technology stack: Windows Server, Windows PC, laptop and Surface data; Microsoft business applications; Microsoft Hyper-V; Azure VMs; Microsoft Windows Server 2016; Microsoft Exchange 2016 and SQL Server 2016.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
SQL Query 34 79
Near realtime alert if SQL Server services stop. 20 53
Using Aggregate Functions to Count 3 32
TSQL DateADD update Question 4 25
This article explains how to reset the password of the sa account on a Microsoft SQL Server.  The steps in this article work in SQL 2005, 2008, 2008 R2, 2012, 2014 and 2016.
For both online and offline retail, the cross-channel business is the most recent pattern in the B2C trade space.
Familiarize people with the process of retrieving data from SQL Server using an Access pass-thru query. Microsoft Access is a very powerful client/server development tool. One of the ways that you can retrieve data from a SQL Server is by using a pa…
This video shows how to set up a shell script to accept a positional parameter when called, pass that to a SQL script, accept the output from the statement back and then manipulate it in the Shell.

914 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

14 Experts available now in Live!

Get 1:1 Help Now