Go Back   TechArena Community > Software > Software Development
Become a Member!
Forgot your username/password?
Register Tags Active Topics RSS Search Mark Forums Read SiteMap

Tags: , , ,

Sponsored Links



Update with Inner join on same table

Software Development


Reply
 
Thread Tools Search this Thread
  #1  
Old 15-12-2009
Member
 
Join Date: Dec 2009
Posts: 2
Update with Inner join on same table

Hi!

I try to solve following situation:

The table looks like this:
Code:
pattern_id (int), sub_id (int), orderId (int), fieldname (varchar), value (varchar)
Now I wrote a query to get a list of all fieldnames, that come (by means of order) directly before the fieldname "Year" - for this i use an inner join on the same table like in this statement (info - in the table are multplie fields with name="Year", but with different sub_id):

Code:
SELECT	fieldname, value
FROM TPattern AS a
    INNER JOIN TPattern AS b ON (a.Pattern_Id=b.Pattern_Id AND a.sub_id=b.sub_id)
WHERE (a.Fieldname='Year')
    AND (b.OrderID = (a.OrderID-1))
Now I need to get an Update that changes the field "value" of the result of above query. I tried to do so with the follwing query, but it did not work.

Code:
UPDATE TPattern
    SET value="valid"
FROM TPattern AS a
    INNER JOIN TPattern AS b ON (a.Pattern_Id = b.Pattern_Id)
WHERE (a.Fieldname='Year')
    AND (b.OrderID = (a.OrderID-1))
Thanks for suggestions on how to solve this.
Philipp
Reply With Quote
  #2  
Old 16-12-2009
Member
 
Join Date: Dec 2009
Posts: 2
Re: Update with Inner join on same table

I could solve this issue by using a view of the pattern table like in following code:

Code:
/* View as help for intra-table comparison */
CREATE VIEW VPattern
AS
SELECT * FROM TPattern
GO

/* Make changes */
UPDATE TPattern
    SET value="valid"
FROM TPattern
    INNER JOIN TPattern AS b
    ON (TPattern.Pattern_Id=b.Pattern_Id AND TPattern.sub_id=b.sub_id)
WHERE (TPattern.Fieldname = 'Year')
    AND (b.OrderID = (TPattern.OrderID-1))

/* Delete view again since it was only needed for the update */
DROP VIEW VPattern

So that's it... :-)
Reply With Quote
Reply

  TechArena Community > Software > Software Development


Thread Tools Search this Thread
Search this Thread:

Advanced Search


Similar Threads for: "Update with Inner join on same table"
Thread Thread Starter Forum Replies Last Post
How can I join a tournament without selecting a table in Zynga Poker ? Elettra Video Games 4 01-06-2011 08:21 AM
Link a Table to another Table to Drop Down In Main Table himeshRES Windows Software 6 11-12-2010 02:01 PM
Getting error while using UPDATE with join Bottlenecked Software Development 5 18-01-2010 01:53 PM
How to Create view based on table-join query Preetish Windows Software 3 12-08-2009 02:44 PM
Update with Inner Join SQL Server Coldman Software Development 5 25-02-2009 10:22 PM


All times are GMT +5.5. The time now is 11:18 AM.