Please start any new threads on our new site at We've got lots of great SQL Server experts to answer whatever question you can come up with.

Our new SQL Server Forums are live! Come on over! We've restricted the ability to create new threads on these forums.

SQL Server Forums
Profile | Active Topics | Members | Search | Forum FAQ
Save Password
Forgot your Password?

 All Forums
 SQL Server 2008 Forums
 Transact-SQL (2008)
 How to Get data from Table2 instead of Table 1?
 Reply to Topic
 Printer Friendly
Author Previous Topic Topic Next Topic  

Starting Member

4 Posts

Posted - 03/01/2014 :  07:51:41  Show Profile  Reply with Quote
Hi All,

Kindly help me my requirement?

I have two tables and similar column like (year, week, day, and shifts, Totalvalue)
Table 1 having full data of 2013 but Table2 few weeks of a month.

Some Total value has changed in Table2 for a particular week so here we consider Table2.
Some records having similarly in both table.
Few days missing a week in Table2
Few days missing a week in Table1.

My requirement
Whenever full the records from Table 1 and Table 2 but if data exist in table 2, Get data from table 2 if does not exist get data from Table 1 for passing parameter to particular month.

How to get it..
please give Query for the same. Thanks.


Constraint Violating Yak Guru

338 Posts

Posted - 03/01/2014 :  09:34:34  Show Profile  Reply with Quote
Do you want to achieve using query? Why not stored proc? Because this logic can be easily written using stored proc.
Go to Top of Page

Starting Member

4 Posts

Posted - 03/01/2014 :  10:47:57  Show Profile  Reply with Quote
Because already having stored procedure and this scenario needs to be implement instead of existing.. even though.. Kindly share your ideas please

Go to Top of Page

Very Important crosS Applying yaK Herder

52326 Posts

Posted - 03/01/2014 :  11:19:52  Show Profile  Reply with Quote

SELECT COALESCE(t2.year,t1.year) AS [year],
COALESCE(t2.week,t1.week) as [week],
COALESCE(, as [day],
COALESCE(t2.shifts,t1.shifts) as [shifts],
COALESCE(t2.TotalValue,t1.TotalValue) as TotalValue
FROM table1 t1
FULL JOIN table2 t2
ON t2.year = t1.year 
AND t2.week = t1.week
and = 
and t2.shifts = t1.shifts

SQL Server MVP
Go to Top of Page
  Previous Topic Topic Next Topic  
 Reply to Topic
 Printer Friendly
Jump To:
SQL Server Forums © 2000-2009 SQLTeam Publishing, LLC Go To Top Of Page
This page was generated in 0.02 seconds. Powered By: Snitz Forums 2000