Quantcast
Channel: Transact-SQL forum
Viewing all articles
Browse latest Browse all 23857

how to give where condition in following query

$
0
0

Hi,

I am going to fetch the data in two facilities like "FA_2003", "FA_2005". First i am fetching the details in 3tabales then filter by using the account and timeid. I am 1st insert facility and peermanentsubsidy and update the capsupplement for "FA_2003". Again i do the same but the capsupplement updated by last value(i.e "FA_2005"). because i coudnt give the where condition in update query how can i give where for below query?

DECLARE   @temp_REVENUE TABLE( FACILITY VARCHAR(10),PERMANENTSUBSIDY INT,CAPSUPPLEMENT INT)


declare @temp table (ACCOUNT varchar(20),FACILITY varchar(20),VERSION varchar(20),TIMEID varchar(20),SIGNEDDATA DECIMAL(25,10),stats varchar(20))
insert into @temp(ACCOUNT,FACILITY,VERSION,TIMEID,SIGNEDDATA,stats)
	select ACCOUNT,FACILITY,VERSION,TIMEID,SIGNEDDATA,STATS from (
	select ACCOUNT,FACILITY,VERSION,TIMEID,SIGNEDDATA,STATS from dbo.tblFAC2PLANNING 
	union all 
	select ACCOUNT,FACILITY,VERSION,TIMEID,SIGNEDDATA,STATS from dbo.tblFACTPLANNING 
	union all
	select ACCOUNT,FACILITY,VERSION,TIMEID,SIGNEDDATA,STATS from dbo.tblFACTWBPLANNING 
) x
where VERSION='ACTUAL' AND FACILITY='FA_2003'

INSERT INTO @temp_REVENUE(PERMANENTSUBSIDY,FACILITY)
SELECT ABS(SUM(SIGNEDDATA)),'FA_2003'
FROM @temp
 WHERE
TIMEID IN
(SELECT TIMEID FROM dbo.mbrTime 
WHERE ID =@CURRENT_MONTH)
  AND ACCOUNT='AC_50001' 
 
UPDATE   @temp_REVENUE SET CAPSUPPLEMENT  =
(SELECT ABS(SUM(SIGNEDDATA))
FROM @temp
where
TIMEID IN
(SELECT TIMEID FROM dbo.mbrTime 
WHERE ID =@CURRENT_MONTH)
AND  ACCOUNT='AC_50007' AND FACILITY='FA_2003') 
---FOR SECOND FACILITY------
declare @temp table (ACCOUNT varchar(20),FACILITY varchar(20),VERSION varchar(20),TIMEID varchar(20),SIGNEDDATA DECIMAL(25,10),stats varchar(20))
insert into @temp1(ACCOUNT,FACILITY,VERSION,TIMEID,SIGNEDDATA,stats)
	select ACCOUNT,FACILITY,VERSION,TIMEID,SIGNEDDATA,STATS from (
	select ACCOUNT,FACILITY,VERSION,TIMEID,SIGNEDDATA,STATS from dbo.tblFAC2PLANNING 
	union all 
	select ACCOUNT,FACILITY,VERSION,TIMEID,SIGNEDDATA,STATS from dbo.tblFACTPLANNING 
	union all
	select ACCOUNT,FACILITY,VERSION,TIMEID,SIGNEDDATA,STATS from dbo.tblFACTWBPLANNING 
) x
where VERSION='ACTUAL' AND FACILITY='FA_2031'

INSERT INTO @temp_REVENUE(PERMANENTSUBSIDY,FACILITY)
SELECT ABS(SUM(SIGNEDDATA)),'FA_2031'
FROM @temp1
 WHERE
TIMEID IN
(SELECT TIMEID FROM dbo.mbrTime 
WHERE ID =@CURRENT_MONTH)
  AND ACCOUNT='AC_50001' 
 
UPDATE   @temp_REVENUE SET CAPSUPPLEMENT  =
(SELECT ABS(SUM(SIGNEDDATA))
FROM @temp1
where
TIMEID IN
(SELECT TIMEID FROM dbo.mbrTime 
WHERE ID =@CURRENT_MONTH)
AND  ACCOUNT='AC_50007' AND FACILITY='FA_2031') 


Viewing all articles
Browse latest Browse all 23857

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>