Click here to Skip to main content
15,911,848 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Dear All,
Below is my SQL query in which I have used UNION All In my outer query. so I want to select data onwards particular date . Is it possible? if yes please reply.

[CODE]--------------------------------------------------------------------------------------------------
--KNOW-HOWS
-------------------
	SELECT KnowHowID AS Id,
			FirstName + ' '+ LastName AS PostedBY,--PostedByID
			Title AS Title,
			ImagePath AS PhotoGraph,
			PostedDateTime AS DateTime,
			'knowHows' AS FeatureName,
			'' AS cnt
	FROM uvw_KnowHows 
	WHERE Active=1 AND PostedByID IN( --This query is for all friend of this user
						SELECT FriendID 
						FROM tblFriends 
						WHERE UserID=422973513 AND ISApproved = 1
					UNION ALL
						SELECT UserID
						FROM tblFriends 
						WHERE FriendID =422973513 AND ISApproved = 1
					UNION ALL
						 --This query is for All members in common network with this user
						SELECT FK_UserID 
						FROM tblNetworkMembers 
						WHERE FK_NetworkId IN (
												SELECT FK_NetworkId 
												FROM tblNetworkMembers 
												WHERE FK_UserID=422973513 
											   )
						 )
UNION All
---------------------------------------------------------------------------------------------------------------------
--NEWS-VIEWS
-------------------
	SELECT NewsViewsID AS Id,
			FirstName + ' '+ LastName AS PostedBy,
			Title AS Title,
			ImagePath AS PhotoGraph,
			PostedDateTime AS DateTime, 
			'NewsViews' AS FeatureName,
			'' AS cnt 
	FROM uvw_NewsViews 
	WHERE Active=1 AND PostedByID IN(
						  --This query is for all friend of this user
						SELECT FriendID 
						FROM tblFriends 
						WHERE UserID=422973513 AND ISApproved = 1
					UNION ALL
						SELECT UserID
						FROM tblFriends 
						WHERE FriendID =422973513 AND ISApproved = 1
					UNION ALL
						 --This query is for All members in common network with this user
						SELECT FK_UserID 
						FROM tblNetworkMembers 
						WHERE FK_NetworkId IN (
												SELECT FK_NetworkId 
												FROM tblNetworkMembers 
												WHERE FK_UserID=422973513 
											   )
						) 
UNION All
---------------------------------------------------------------------------------------------------------------------
--PUBLIC IMAGE
-------------------
	SELECT PhotographID AS Id,
			FirstName + ' '+ LastName AS PostedBy,
			Title AS Title,
			[Path] AS PhotoGraph,
			PostedDateTime AS DateTime,
			'PublicImage' AS FeatureName ,
			'' AS cnt
	FROM uvw_PublicAlbum  
	WHERE Active=1 AND UserID IN (
						 --This query is for all friend of this user
						SELECT FriendID 
						FROM tblFriends 
						WHERE UserID=422973513 AND ISApproved = 1
					UNION ALL
						SELECT UserID
						FROM tblFriends 
						WHERE FriendID =422973513 AND ISApproved = 1
					UNION ALL
						 --This query is for All members in common network with this user
						SELECT FK_UserID 
						FROM tblNetworkMembers 
						WHERE FK_NetworkId IN (
												SELECT FK_NetworkId 
												FROM tblNetworkMembers 
												WHERE FK_UserID=422973513 
											   )
					 )
Posted
Comments
Sandeep Mewara 25-Mar-11 10:12am    
Not clear.

1 solution

Sure, you can wrap your whole query above in another select statement and then filter on the date:
Select Id, PostedBy, Title, PhotoGraph, DateTime, FeatureName, cnt 
From 
( 
[your query here]
) 

where DateTime > '1/1/2011'


This would be easier to write, however, it may be more efficient if you limited each sub select with the date range. This would depend on how your server optimizes the query.
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900