Solved

Passing value from nested select sub query to upper select

Posted on 2008-10-13
2
759 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

Microsoft Certification Exam 74-409

Veeam® is happy to provide the Microsoft community with a study guide prepared by MVP and MCT, Orin Thomas. This guide will take you through each of the exam objectives, helping you to prepare for and pass the examination.

Question has a verified solution.

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

Use this article to create a batch file to backup a Microsoft SQL Server database to a Windows folder.  The folder can be on the local hard drive or on a network share.  This batch file will query the SQL server to get the current date & time and wi…
This article explains how to reset the password of the sa account on a Microsoft SQL Server.  The steps in this article work in SQL 2005, 2008, 2008 R2, 2012, 2014 and 2016.
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 backup a database, simulate a failure backup the tail of the database transaction log and perform the restore.

777 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