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

 All Forums
 SQL Server 2008 Forums
 Transact-SQL (2008)
 How to Get data from Table2 instead of Table 1?

Author  Topic 

Srinishak1
Starting Member

4 Posts

Posted - 2014-03-01 : 07:51:41
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

338 Posts

Posted - 2014-03-01 : 09:34:34
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

4 Posts

Posted - 2014-03-01 : 10:47:57
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

52326 Posts

Posted - 2014-03-01 : 11:19:52
[code]
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
[/code]

------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/
https://www.facebook.com/VmBlogs
Go to Top of Page
   

- Advertisement -