﻿<?xml version='1.0' encoding='UTF-8'?><rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/"><channel><title>SQLServerCentral / SQL Server 2008 / T-SQL (SS2K8)  / Query help / Latest Posts</title><generator>InstantForum.NET v2.9.0</generator><description>SQLServerCentral</description><link>http://www.sqlservercentral.com/Forums/</link><webMaster>notifications@sqlservercentral.com</webMaster><lastBuildDate>Sat, 18 May 2013 17:55:08 GMT</lastBuildDate><ttl>20</ttl><item><title>RE: Query help</title><link>http://www.sqlservercentral.com/Forums/Topic1410579-392-1.aspx</link><description>The kudos belongs to Michael Higgins. He provided the solution. I just suggested a way for you to fit it in with your system. :-D</description><pubDate>Wed, 23 Jan 2013 09:55:00 GMT</pubDate><dc:creator>Sean Lange</dc:creator></item><item><title>RE: Query help</title><link>http://www.sqlservercentral.com/Forums/Topic1410579-392-1.aspx</link><description>Absolutely spot on. Nice one Sean.Thanks,Simon</description><pubDate>Wed, 23 Jan 2013 09:51:18 GMT</pubDate><dc:creator>s_osborne2</dc:creator></item><item><title>RE: Query help</title><link>http://www.sqlservercentral.com/Forums/Topic1410579-392-1.aspx</link><description>[quote][b]s_osborne2 (1/23/2013)[/b][hr]Hi,Thanks for you help.I've been playing with this, and you're right it does work but doesn't give me the solution to the problem i have. I've given @table as an example here to illustrate the problem. In my real world problem @table is actually the result of a complex query and isn't stored in a table (temporary or otherwise). I have many more columns than outlined above and the query returns ~40k rows.I was hoping to be able to do something in-line with a CROSS APPLY type solution but haven't been able to crack it. I could write the result of the query to a temporary or even a persisted table and then use the CTE as you suggest but there's write overhead invovled with that, that i'd like to avoid if possible.Is there a way to do this without a CTE or am I hoping for a solution that simply isn't possible?Thanks,Simon[/quote]Actually the way you describe this a CTE is exactly what you want. If you use a cross apply you would have to requery your data anyway. This is exactly what CTEs were designed for.Here is the same solution but instead of selecting directly from the table variable I setup a CTE. You would just replace the first CTE query with your complex query.[code]DECLARE @table TABLE(	ProposalId INT,	RiskId INT,	RiskTaken BIT)INSERT INTO @tableSELECT 1, 1, 1UNION ALL SELECT 1,2,0UNION ALL SELECT 1,3,0UNION ALL SELECT 2,1,0UNION ALL SELECT 2,2,0UNION ALL SELECT 2,3,0;with CTE_AsQueryReplacement as(	select * from @table --replace this with your complex query),ProposalRisk AS(SELECT DISTINCT ProposalId FROM CTE_AsQueryReplacement WHERE RiskTaken = 1)SELECT 	t.[ProposalId],	t.[RiskId],	t.[RiskTaken],	CASE	WHEN pr.ProposalId IS NOT NULL THEN 1	ELSE	0	END AS ProposalTaken FROM 	CTE_AsQueryReplacement t	LEFT OUTER JOIN ProposalRisk pr ON t.ProposalId = pr.ProposalId	[/code]</description><pubDate>Wed, 23 Jan 2013 09:33:26 GMT</pubDate><dc:creator>Sean Lange</dc:creator></item><item><title>RE: Query help</title><link>http://www.sqlservercentral.com/Forums/Topic1410579-392-1.aspx</link><description>Hi,Thanks for you help.I've been playing with this, and you're right it does work but doesn't give me the solution to the problem i have. I've given @table as an example here to illustrate the problem. In my real world problem @table is actually the result of a complex query and isn't stored in a table (temporary or otherwise). I have many more columns than outlined above and the query returns ~40k rows.I was hoping to be able to do something in-line with a CROSS APPLY type solution but haven't been able to crack it. I could write the result of the query to a temporary or even a persisted table and then use the CTE as you suggest but there's write overhead invovled with that, that i'd like to avoid if possible.Is there a way to do this without a CTE or am I hoping for a solution that simply isn't possible?Thanks,Simon</description><pubDate>Wed, 23 Jan 2013 09:20:56 GMT</pubDate><dc:creator>s_osborne2</dc:creator></item><item><title>RE: Query help</title><link>http://www.sqlservercentral.com/Forums/Topic1410579-392-1.aspx</link><description>[code="sql"]DECLARE @table TABLE(	ProposalId INT,	RiskId INT,	RiskTaken BIT)INSERT INTO @tableSELECT 1, 1, 1UNION ALL SELECT 1,2,0UNION ALL SELECT 1,3,0UNION ALL SELECT 2,1,0UNION ALL SELECT 2,2,0UNION ALL SELECT 2,3,0;WITH ProposalRiskAS(SELECT DISTINCT ProposalId FROM @table WHERE RiskTaken = 1)SELECT 	t.[ProposalId],	t.[RiskId],	t.[RiskTaken],	CASE	WHEN pr.ProposalId IS NOT NULL THEN 1	ELSE	0	END AS ProposalTaken FROM 	@table t	LEFT OUTER JOIN ProposalRisk pr ON t.ProposalId = pr.ProposalId[/code]Looks like it should work</description><pubDate>Wed, 23 Jan 2013 07:48:59 GMT</pubDate><dc:creator>michael.higgins</dc:creator></item><item><title>Query help</title><link>http://www.sqlservercentral.com/Forums/Topic1410579-392-1.aspx</link><description>Hi,I have a query that returns a ProposalId a RiskId and RiskTaken bit flag. I've setup some test data below:[code="sql"]DECLARE @table TABLE(	ProposalId INT,	RiskId INT,	RiskTaken BIT)INSERT INTO @tableSELECT 1, 1, 1UNION ALL SELECT 1,2,0UNION ALL SELECT 1,3,0UNION ALL SELECT 2,1,0UNION ALL SELECT 2,2,0UNION ALL SELECT 2,3,0SELECT 	t.[ProposalId],	t.[RiskId],	t.[RiskTaken]	FROM 	@table t[/code]Now, what'd i'd like is another column called ProposalTaken that should be a 1 where there is a risk for that proposal that has a risktaken flag of 1. Example results shown in the attachment.Is there a way to achieve this?Thanks,Simon</description><pubDate>Wed, 23 Jan 2013 07:39:39 GMT</pubDate><dc:creator>s_osborne2</dc:creator></item></channel></rss>