Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>In my opinion, this could be a good challenge for you to start with this code:</p> <pre><code>Declare @T Table ( MediaID BigInt Primary Key Identity(1, 1) , MediaTypeID BigInt , MediaTitle Nvarchar(50) , MediaDate DateTime ); Insert @T ( MediaTypeID , MediaTitle , MediaDate ) Select 1 , 'Funny Cat Video' , '2006-01-25 00:00:00.000' Union Select 1 , 'Funny Dog Video' , '2006-01-20 00:00:00.000' Union Select 2 , 'Angry Birds Game' , '2006-03-13 00:00:00.000' Union Select 4 , 'Blonde Joke' , '2006-03-16 00:00:00.000' Union Select 3 , 'Goofy Clown Picture' , '2006-02-27 00:00:00.000' Union Select 2 , 'Racing Game' , '2006-02-10 00:00:00.000' Union Select 1 , 'Star Wars Video' , '2006-07-15 00:00:00.000' ; Select Month.Title , Count(Type01_Result.MediaID) As MediaTypeID_1 , Count(Type02_Result.MediaID) As MediaTypeID_2 , Count(Type03_Result.MediaID) As MediaTypeID_3 , Count(Type04_Result.MediaID) As MediaTypeID_4 From ( Select 1 As Id , 'Jan' As Title Union Select 2 , 'Feb' Union Select 3 , 'March' Union Select 4 , 'April' Union Select 5 , 'May' Union Select 6 , 'June' Union Select 7 , 'July' Union Select 8 , 'Aug' Union Select 9 , 'Sept' Union Select 10 , 'Nov' Union Select 11 , 'Oct' Union Select 12 , 'Dec' ) As Month Left Outer Join @T As Type01_Result On Type01_Result.MediaTypeID = 1 And DatePart(Month, Type01_Result.MediaDate) = Month.Id Left Outer Join @T As Type02_Result On Type02_Result.MediaTypeID = 2 And DatePart(Month, Type02_Result.MediaDate) = Month.Id Left Outer Join @T As Type03_Result On Type03_Result.MediaTypeID = 3 And DatePart(Month, Type03_Result.MediaDate) = Month.Id Left Outer Join @T As Type04_Result On Type04_Result.MediaTypeID = 4 And DatePart(Month, Type04_Result.MediaDate) = Month.Id Group By Month.Title ; </code></pre> <p>The only thing is that you should be carefull about the year value and how many years you wanna include in the output, because this result will give you the summation of all records in each month no matter which year the date of the record is. It may cause some confusion in the result.</p> <p>(Remember that I always set the tab size to 8 characters in the Options of Management Studio, so I suggest you do that to see the correct style of writing T-SQL)</p> <p>Hope it helps you.</p> <p>Cheers</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload