Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 General SQL Server Forums
 New to SQL Server Programming
 SQL help

Author  Topic 

jim_jim
Constraint Violating Yak Guru

306 Posts

Posted - 2010-09-17 : 16:49:32
Hi Everyone
Need help writing a query in sql

Scenario:I have a table1 Name - Requests
Table 2 Name - Itemid

Requestid is a primary key in the table 1 and foreign key in the table 2.

Every request in table A is associated with 13 items in table B and each item id has completed date associated in table B

I want see data as

Request Id Indicator
134 Yes
135 No

where

indicator feild should check all the completed dates associated with each item for a request and give value = "no" if all the dates are equal to 1/1/1900 and yes if it is somethign other than 1/1/1900


visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2010-09-18 : 09:18:33
[code]
SELECT a.RequestID, CASE WHEN b.Cnt > 0 THEN 'Yes' ELSE 'No' END AS Indicator
FROM TableA a
INNER JOIN (SELECT RequestID, SUM(CASE WHEN Date <> '1900-01-01' THEN 1 ELSE 0 END) AS Cnt
FROM TableB
GROUP BY RequestID) b
ON b.RequestID = a.RequestID
[/code]

------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/

Go to Top of Page
   

- Advertisement -