Click here to Skip to main content
16,013,642 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have table like,

Party Name | Date
AAA              2013-12-01
BBB              2013-12-03
CCC              2013-12-05
DDD             2013-12-06

I want result using select query like

Party Name | Date
AAA              2013-12-01
-                   2013-12-02
BBB              2013-12-03
-                  2013-12-04
CCC             2013-12-05
DDD            2013-12-06
Posted

If you're using SQL Server 2005 or above, you can do it like this (I'm using CTE to generate dates between min and max dates):
SQL
DECLARE @MinDate DATE, @MaxDate DATE;

SET @MinDate = (SELECT MIN([Date]) FROM [TableName]);
SET @MaxDate = (SELECT MAX([Date]) FROM [TableName]);

;WITH Dates
     AS (SELECT [Date] = @MinDate
         UNION ALL
         SELECT [Date] = DATEADD(DAY, 1, [Date])
         FROM Dates
         WHERE [Date] < @MaxDate)
SELECT ISNULL(t.[Party Name], '-') AS [Party Name],
       d.[Date]
FROM   Dates AS d
       LEFT OUTER JOIN [TableName] AS t
                    ON t.[Date] = d.[Date]
ORDER  BY d.[Date]
OPTION (MAXRECURSION 32767);
 
Share this answer
 
Comments
pareshpbhayani 24-Jun-14 1:50am    
Thanks Andrius Leonavicius, it works.
Andrius Leonavicius 24-Jun-14 9:28am    
You're welcome.
A quick way could be like

SQL
SELECT COALESCE(MT.PARTYNAME, TMP.PARTYNAME), TDATE
FROM 
(
   SELECT '2013-12-01' AS TDATE, '-' as PARTYNAME
   UNION ALL
   SELECT '2013-12-02' AS TDATE 
   UNION ALL
   SELECT '2013-12-03' AS TDATE 
   UNION ALL
   SELECT '2013-12-04' AS TDATE 
   UNION ALL
   SELECT '2013-12-05' AS TDATE 
   UNION ALL
   SELECT '2013-12-06' AS TDATE 
) TMP LEFT OUTER JOIN MYTABLE MT ON TMP.TDATE = MT.DATE
 
Share this answer
 
Comments
pareshpbhayani 23-Jun-14 9:44am    
Thanks for giving me reply.
In my table there are lots of data event i don't know amount of data in that case i can't use this query.
Andrius Leonavicius 23-Jun-14 9:48am    
Hi,

What is your version of SQL Server?

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