SQL Server Forums
Profile | Register | Active Topics | Members | Search | Forum FAQ
 
Register Now and get your question answered!
Username:
Password:
Save Password
Forgot your Password?

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

Srinishak1
Starting Member

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

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




Rajan

sqllover
Constraint Violating Yak Guru

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

Srinishak1
Starting Member

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


Rajan
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

India
52325 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(t2.day,t1.day) 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 t2.day = t1.day 
and t2.shifts = t1.shifts


------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/
https://www.facebook.com/VmBlogs
Go to Top of Page
  Previous Topic Topic Next Topic  
 New 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.06 seconds. Powered By: Snitz Forums 2000