Author |
Topic |
pkovarik
Starting Member
6 Posts |
Posted - 2014-01-26 : 03:05:09
|
I have a table T with columns a1, a2, ..., a9, date.At one point, I get the new values ??X1, X2, ..., X9, NewDate. I need to perform a SQL query, which determines whether the table contains a record that matches the month and year of the "date" with the month and year "NewDate".- If such a record does not exist in the table then inserts a new row INSERT INTO T (a1, a2, ..., a5, date) VALUES (X1, X2, ..., X5, NewDate), and overwrites the value of an items a6, ..., a9 with the items a2, ... ,a5 of such row in the table T, which has the highest "date" (ie values of a2, ... ,a5 of the result of the query SELECT TOP 1 ... ORDER BY date DESC).- If such a record exists, then overwrites values of items a2, .., a5 and date with values X2, ..., X5 and NewDate.Does anyone know how to construct such a SQL query? Thank you. |
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2014-01-27 : 07:09:09
|
something like thisIF NOT EXISTS (SELECT 1 FROM T WHERE MONTH([date]) = MONTH(@NewDate) AND YEAR([date] = YEAR(@NewDate))INSERT T (a1,a2,..,a5,a6,a7,a8,a9,[date])SELECT TOP 1 @X1,@X2,...@X5,a2,a3,a4,a5,@NewDateFROM TORDER BY [date] DESCELSEUPDATE tSET a2=@X2,a3=@X3,a4=@X4,a5=@X5,[date]=@NewDateFROM T tWHERE DATEDIFF(mm,0,[date]) = DATEDIFF(mm,0,@NewDate) ------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/https://www.facebook.com/VmBlogs |
|
|
pkovarik
Starting Member
6 Posts |
Posted - 2014-01-28 : 17:38:37
|
Thank you, it's perfect, but ... if T is empty it doesn't INSERT into T because SELECT TOP 1 @X1,@X2,...@X5,a2,a3,a4,a5,@NewDateFROM T is empty. Could you rewrite query to match this situation? (In fact, it is not the case of empty table, the example is simplified - in fact, SELECT TOP 1 is completed with a WHERE condition, so I'm talking about when no record meets the WHERE criteria.)Thank you. |
|
|
pkovarik
Starting Member
6 Posts |
Posted - 2014-01-28 : 18:17:13
|
I see only one solution - to append second queryIF NOT EXISTS (SELECT 1 FROM T WHERE MONTH([date]) = MONTH(@NewDate) AND YEAR([date] = YEAR(@NewDate))INSERT INTO T (a1,a2,..,a5,a6,a7,a8,a9,[date])VALUES (@X1,@X2,...@X5,0,0,0,0,@NewDate);But is there any way how to do it in one query? |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2014-01-29 : 04:12:26
|
this is another wayDECLARE a2 int,@a3 int,@a4 int,@a5 intSELECT TOP 1 @a2=a2,@a3=a3,@a4=a4,@a5=a5FROM TORDER BY [date] DESCIF NOT EXISTS (SELECT 1 FROM T WHERE MONTH([date]) = MONTH(@NewDate) AND YEAR([date] = YEAR(@NewDate))INSERT T (a1,a2,..,a5,a6,a7,a8,a9,[date])SELECT @X1,@X2,...@X5,COALESCE(@a2,0),COALESCE(@a3,0),COALESCE(@a4,0),COALESCE(@a5,0),@NewDateELSEUPDATE tSET a2=@X2,a3=@X3,a4=@X4,a5=@X5,[date]=@NewDateFROM T tWHERE DATEDIFF(mm,0,[date]) = DATEDIFF(mm,0,@NewDate) ------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/https://www.facebook.com/VmBlogs |
|
|
|
|
|