?
Solved

Passing value from nested select sub query to upper select

Posted on 2008-10-13
2
Medium Priority
?
782 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
2 Comments
 
LVL 143

Accepted Solution

by:
Guy Hengel [angelIII / a3] earned 1500 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

 [eBook] Windows Nano Server

Download this FREE eBook and learn all you need to get started with Windows Nano Server, including deployment options, remote management
and troubleshooting tips and tricks

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Load balancing is the method of dividing the total amount of work performed by one computer between two or more computers. Its aim is to get more work done in the same amount of time, ensuring that all the users get served faster.
International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
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
Via a live example, show how to extract insert data into a SQL Server database table using the Import/Export option and Bulk Insert.
Suggested Courses

770 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