﻿<?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 2005 / Development  / Cross Joins / 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>Tue, 21 May 2013 04:22:24 GMT</lastBuildDate><ttl>20</ttl><item><title>RE: Cross Joins</title><link>http://www.sqlservercentral.com/Forums/Topic733499-145-1.aspx</link><description>Please provide table structure, sample data and expected result set based on the sample as described in the link in my signature.Reason: It looks like you don't need the cross join at all.Basis idea: using a CTE/subquery with the Group By function on your table dbo.tblPurchase to get the weeks where a purchase has been made and do an right outer join to a calendar table. If you don't have a calendar table by now you might want to look into it. It also could be created on the fly within the CTE, if this query is not heavily used.</description><pubDate>Fri, 12 Jun 2009 03:24:41 GMT</pubDate><dc:creator>LutzM</dc:creator></item><item><title>Cross Joins</title><link>http://www.sqlservercentral.com/Forums/Topic733499-145-1.aspx</link><description>How can I avoid a cross join for the following query?SELECT a.Week, a.StartDate, CASE WHEN purchasedate BETWEEN a.startdate AND a.nextdate THEN 'Yes' ELSE 'No' END AS [Purchase Done]FROM dbo.tblPurchase CROSS JOIN(SELECT number AS Week, DATEADD(wk, number, DATEADD(DD, 1 - DATEPART(DW, '08-Jan-09'), '08-Jan-09')) AS StartDate, DATEADD(wk, number + 1, DATEADD(DD, 1 - DATEPART(DW, '08-Jan-09'), '08-Jan-09')) AS NextDateFROM master.dbo.spt_values AS vWHERE (type = 'P') AND (number BETWEEN 0 AND 51)) AS aAll I am trying to get is, for all the weeks starting on Mondays, I am trying to figure out if a purchase has been made during that week.The outer query returns the week numbers and start week of the dates for this year.But since it is a cross join, it is returning a cartesian product and I just want week numbers, week start date and whether or not a purchase has been made.The output should be something likeWeek # Week Start Purchase Done1              05/01       Yes2              12/01       No3              19/01       Yesand so on....Thanks for your time.</description><pubDate>Thu, 11 Jun 2009 22:24:42 GMT</pubDate><dc:creator>balars_2000</dc:creator></item></channel></rss>