Solved

Passing value from nested select sub query to upper select

Posted on 2008-10-13
2
741 Views
Last Modified: 2012-05-05
Hi,
I need to get a value from a subquery, to the topmost level.
For instance select sum of sales for a week period, plus select sum of sales for only a day.

Something like the following:
Select sum(TableA.nValue), sum(TableC.nValue) from TableA --this is where the problem is !!!
join TableB on TableA.PK = TableB.PK
where TableA.PK in
      ( Select PK from TableA TableC
        join TableB TableD on TableC.PK = TableD.PK
        where TableC.date => '2008-10-13' and TableC.date < '2008-10-20'
)
and TableA.Date <= '2008-10-13' and TableA.date < '2008-10-14'

How can I do this without needing all sorts of vars etc ? Can I reference it directly ?
0
Comment
Question by:wilcosw
2 Comments
 
LVL 142

Accepted Solution

by:
Guy Hengel [angelIII / a3] earned 500 total points
ID: 22701161
this approach should do:
Select sum(CASE WHEN TableA.Date <= '2008-10-13' and TableA.date < '2008-10-14' THEN TableA.nValue ELSE 0 END)  sum_day
     , sum(TableA.nValue) sum_week
from TableA 
join TableB 
  on TableA.PK = TableB.PK
where TableA.date => '2008-10-13' 
  and TableA.date < '2008-10-20'

Open in new window

0
 
LVL 1

Author Comment

by:wilcosw
ID: 22701632
Great,
Thanks for the quick response !
0

Featured Post

Free Gift Card with Acronis Backup Purchase!

Backup any data in any location: local and remote systems, physical and virtual servers, private and public clouds, Macs and PCs, tablets and mobile devices, & more! For limited time only, buy any Acronis backup products and get a FREE Amazon/Best Buy gift card worth up to $200!

Join & Write a Comment

Introduction SQL Server Integration Services can read XML files, that’s known by every BI developer.  (If you didn’t, don’t worry, I’m aiming this article at newcomers as well.) But how far can you go?  When does the XML Source component become …
Introduction In my previous article (http://www.experts-exchange.com/Microsoft/Development/MS-SQL-Server/SSIS/A_9150-Loading-XML-Using-SSIS.html) I showed you how the XML Source component can be used to load XML files into a SQL Server database, us…
Using examples as well as descriptions, and references to Books Online, show the different Recovery Models available in SQL Server and explain, as well as show how full, differential and transaction log backups are performed
Viewers will learn how the fundamental information of how to create a table.

744 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now