X-Received: by 10.129.109.81 with SMTP id i78mr9926568ywc.63.1480609300349; Thu, 01 Dec 2016 08:21:40 -0800 (PST) X-Received: by 10.157.4.119 with SMTP id 110mr2599333otc.11.1480609300300; Thu, 01 Dec 2016 08:21:40 -0800 (PST) Path: csiph.com!weretis.net!feeder6.news.weretis.net!news.glorb.com!n6no836293qtd.0!news-out.google.com!j8ni3127qtc.0!nntp.google.com!n6no836291qtd.0!postnews.google.com!glegroupsg2000goo.googlegroups.com!not-for-mail Newsgroups: microsoft.public.sqlserver.programming Date: Thu, 1 Dec 2016 08:21:39 -0800 (PST) Complaints-To: groups-abuse@google.com Injection-Info: glegroupsg2000goo.googlegroups.com; posting-host=92.207.224.242; posting-account=fmyAHgkAAAChJuFp0RVqnvY9EwN8AFtl NNTP-Posting-Host: 92.207.224.242 User-Agent: G2/1.0 MIME-Version: 1.0 Message-ID: <4e3f4200-45fa-45f6-9ec7-196f884c8d7c@googlegroups.com> Subject: Select last column amongst group of columns where value>0 From: Mary Phelps Injection-Date: Thu, 01 Dec 2016 16:21:40 +0000 Content-Type: text/plain; charset=UTF-8 Xref: csiph.com microsoft.public.sqlserver.programming:31310 Here is data in my table SiteId Week1A Week1B Week2A Week2B Week3A Week3B Week4A Week4B Week5A Week5B 101 0 8 7 9 0 6 0 9 0 9 288 8 8 0 7 0 7 0 8 6 6 468 1 8 6 9 8 6 0 9 0 9 768 0 8 0 7 0 7 6 8 0 6 I want a query that will look at all week1a,week2a,week3a,week4a,week5a and so forth then figure out which is the last column which has value greater than 0 for example SiteId 101 It is Week2A. SiteId 288 it is Week5A. SiteId 468 week3A. then after the last column is figured out sum up values of weeks remaing after that so for example siteid101 last column is week2a so i want a sum of week3b,week4b and week5b.