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 2005 Forums
 Transact-SQL (2005)
 Add value of field in 1st record only to sum

Author  Topic 

snufse
Constraint Violating Yak Guru

469 Posts

Posted - 2008-09-09 : 11:46:06
I have an existing query where I need to do following:

For field "b.GBBORG" I need to add the value of the 1st record only to the sum. The rest of the fields (b.GBan01, b.GBan02 etc) are OK. Not sure how to do this. Tried to use FIRST but it did not work. Thank you.



select 
* from openquery(JDEPROD,'select DISTINCT c.GMSUB as gbsub,c.GmMCU as gbmcu, GMUM ,''U'' as UOMType, ''1'' as UOMSeq, a.GBUPMJ,
''Production'' as CostTypeDescription,
(SUM(COALESCE(a.GBan01,0)+COALESCE(a.GBan02,0)+COALESCE(a.GBan03,0)+COALESCE(a.GBan04,0)+COALESCE(a.GBan05,0)+COALESCE(a.GBan06,0)+
COALESCE(a.GBan07,0)+COALESCE(a.GBan08,0)+COALESCE(a.GBan09,0)+COALESCE(a.GBan10,0)+COALESCE(a.GBan11,0)+COALESCE(a.GBan12,0)+COALESCE(a.GBan13,0)+COALESCE(a.GBan14,0)))*.01 as actual
,(SUM(COALESCE(b.GBBORG ,0)+COALESCE(b.GBan01,0)+COALESCE(b.GBan02,0)+COALESCE(b.GBan03,0)+COALESCE(b.GBan04,0)+COALESCE(b.GBan05,0)+COALESCE(b.GBan06,0)+
COALESCE(b.GBan07,0)+COALESCE(b.GBan08,0)+COALESCE(b.GBan09,0)+COALESCE(b.GBan10,0)+COALESCE(b.GBan11,0)+COALESCE(b.GBan12,0)+COALESCE(b.GBan13,0)+COALESCE(b.GBan14,0)))*.01 as budget
from VGIPRDDTA.F0901 c
inner join VGIPRDDTA.F0006 on GMMCU=MCMCU and GMCO = MCCO
left join VGIPRDDTA.F0902 a
on c.GMMCU=a.GbMCU and c.GMCO = a.GbCO and c.GMSUB=a.GbSUB and a.GBLT=''AU'' and a.gbobj like ''% %''
left join VGIPRDDTA.F0902 b
on c.GMMCU=b.GbMCU and c.GMCO = b.GbCO and c.GMSUB=b.GbSUB and b.GBLT=''JU'' and b.gbobj like ''% %''
where c.GMOBJ like ''% %'' and c.GMSUB <> '' '' and c.GMMCU <>'' '' and
MCSTYL in (''51'', ''52'', ''53'', ''54'',''03'', ''04'', ''21'',''05'',''11'') and mcpecc in ('''',''K'')
GROUP BY c.gmSUB,c.gmMCU, GMUM,a.GBUPMJ')

georgev
Posting Yak Master

122 Posts

Posted - 2008-09-09 : 12:15:34
How do you define the first record?

You might have to resort to a subquery returning Top 1


George
Go to Top of Page

snufse
Constraint Violating Yak Guru

469 Posts

Posted - 2008-09-09 : 12:33:48
Not sure what you mean "define the first record".
Go to Top of Page

SwePeso
Patron Saint of Lost Yaks

30421 Posts

Posted - 2008-09-09 : 12:36:49
If you have some identity column on the source data, you can
1) get "first record" for gbborg column
2) use a case statement for summing the column, ie sum(case when colid = @colid then b.gbborg else 0 end)



E 12°55'05.63"
N 56°04'39.26"
Go to Top of Page

SwePeso
Patron Saint of Lost Yaks

30421 Posts

Posted - 2008-09-09 : 12:39:29
I think better yet is to have a trigger in source table and a new column with a bit value denoting if record is "first" or not.
Then you easy can do step 2.



E 12°55'05.63"
N 56°04'39.26"
Go to Top of Page
   

- Advertisement -