Solved

Passing value from nested select sub query to upper select

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

The Ultimate Checklist to Optimize Your Website

Websites are getting bigger and complicated by the day. Video, images, custom fonts are all great for showcasing your product/service. But the price to pay in terms of reduced page load times and ultimately, decreased sales, can lead to some difficult decisions about what to cut.

Question has a verified solution.

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

I have a large data set and a SSIS package. How can I load this file in multi threading?
A Stored Procedure in Microsoft SQL Server is a powerful feature that it can be used to execute the Data Manipulation Language (DML) or Data Definition Language (DDL). Depending on business requirements, a single Stored Procedure can return differe…
Viewers will learn how to use the SELECT statement in SQL to return specific rows and columns, with various degrees of sorting and limits in place.
Viewers will learn how to use the UPDATE and DELETE statements to change or remove existing data from their tables. Make a table: Update a specific column given a specific row using the UPDATE statement: Remove a set of values using the DELETE s…

690 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