Solved

sql server query

Posted on 2014-03-10
2
231 Views
Last Modified: 2014-03-11
Query 1


I have a query that looks like this:

query 1
If you notice in the FROM clause of the case statement, it's doing the select from TestTable that is aliased as tt2

When I run this query it works just fine.

SELECT Q1.[Col1]
      ,Q1.[Col2]
      ,Q1.[Col3]
      ,TotalCompensation
      ,CASE WHEN [Col1] = 0 THEN 0 ELSE AnnualCompensation - (
           SELECT [TotalGross] + [TotalBenefits]
           FROM [TestDatabase].[dbo].[TestTable] tt2
           WHERE
               tt2.[Col1] = Q1.[Col1] AND
               tt2.[Col2] = Q1.[Col2] AND
               tt2.[Col3] = Q1.[Col3] - 1
           ) END AS TotalCompDiff
FROM FROM [TestDatabase].[dbo].[TestTable] tt2
CROSS APPLY (
    SELECT [TotalGross] + [TotalBenefits] AS AnnualCompensation
) AS cross_apply_1

Open in new window



Query 2

Now I'm using a very similar query that looks like this:

my query 2
SELECT Q1.[Col1]
      ,Q1.[Col2]
      ,Q1.[Col3]
      ,TotalCompensation
    ,CASE WHEN [Col1] = 0 THEN 0 ELSE AnnualCompensation - (
           SELECT [TotalGross] + [TotalBenefits]
           FROM Q1
           WHERE
               tt2.[Col1] = Q1.[Col1] AND
               tt2.[Col2] = Q1.[Col2] AND
               tt2.[Col3] = Q1.[Col3] - 1
           ) END AS TotalCompDiff
FROM
(
--- start of query 1 -------------------------------------------


--- end of query 1 ---------------------------------------------
) Q1
CROSS APPLY (
    SELECT [TotalGross] + [TotalBenefits] AS AnnualCompensation
) AS cross_apply_1

Open in new window


In this query, my main select statement gets its data from a query that goes in between the comments lines I'm showing in green. I didn't show the query in between the comment lines for simplicity.

Now in my case statement in this query my select statement is grabbing the column from my query which I called Q1.

When I run this query it didn't work. I don't think I'm writing the from clause in my cases statement correctly to reference my query Q1.

Right now I have this:

           SELECT [TotalGross] + [TotalBenefits]
           FROM Q1


Anyone know how I should correctly be referencing my query in my Case Statement?
p1.jpg
p2.jpg
0
Comment
Question by:maqskywalker
2 Comments
 
LVL 6

Assisted Solution

by:c1nmo
c1nmo earned 250 total points
ID: 39918970
If they are projected (i.e. need to be in SELECT list) from Q1 then:

SELECT Q1.[TotalGross] + Q1.[TotalBenefits]
           FROM Q1
0
 
LVL 69

Accepted Solution

by:
ScottPletcher earned 250 total points
ID: 39919251
"tt2" is likely no longer defined.  Therefore, SQL can't process a comparison against that alias.
0

Featured Post

How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

Join & Write a Comment

Suggested Solutions

If you have heard of RFC822 date formats, they can be quite a challenge in SQL Server. RFC822 is an Internet standard format for email message headers, including all dates within those headers. The RFC822 protocols are available in detail at:   ht…
Ever needed a SQL 2008 Database replicated/mirrored/log shipped on another server but you can't take the downtime inflicted by initial snapshot or disconnect while T-logs are restored or mirror applied? You can use SQL Server Initialize from Backup…
Familiarize people with the process of retrieving data from SQL Server using an Access pass-thru query. Microsoft Access is a very powerful client/server development tool. One of the ways that you can retrieve data from a SQL Server is by using a pa…
This video shows, step by step, how to configure Oracle Heterogeneous Services via the Generic Gateway Agent in order to make a connection from an Oracle session and access a remote SQL Server database table.

758 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

21 Experts available now in Live!

Get 1:1 Help Now