Question

I need to have 2 columns for a result. 1 source table/view has the field called "Date_Entered" which is yyyy-mm-dd hh.mm.ss for each entry (full date/time stamp) I need a query that builds 2 columns of results. The results are the total number of records per month per year. Would look like this ...

Year_2012..... Year_2013
498................. 132
134................. 564
787................. 342

and so on for 12 results per year(if applicable) what I have so far is this.. which nets the first column of 12 recods (1 per month) with total number of rows within each month.

SELECT  COUNT(DATEPART(mm, Date_Entered)) AS Year_2012
FROM    cwwebapp_oti.dbo.v_cbi_All_Tickets
WHERE   (Company_Name IN ('Company, Inc.', 'Business LLC')) AND 
        (DATEPART(yyyy, Date_Entered) = '2012')
GROUP BY DATEPART(mm, Date_Entered)
ORDER BY DATEPART(mm, Date_Entered)

Year_2012
518
452
593
810
etc...

I have researched and not found how to get the second column build from the same data source but only showing Year_2013 of which there are 5 rows to date of course.

Thanks in advance for any help you can lend!

~Coog

Was it helpful?

Solution

Here's a possible solution. It's limited as it only works for years 2012 and 2013 and would need modified if you wanted to include additional years. It's also not very elegant and I'm sure some of the SQL gurus on this site will be able to simplify it.

SELECT NVL(SUM(DECODE(date_entered_year, '2012', 1, 0)), 0) year_2012,
       NVL(SUM(DECODE(date_entered_year, '2013', 1, 0)), 0) year_2013
  FROM (SELECT TO_CHAR(date_entered, 'YYYY-MM') date_entered_month,
               TO_CHAR(date_entered, 'YYYY')    date_entered_year
          FROM cwwebapp_oti.dbo.v_cbi_All_Tickets
         WHERE company_name IN ('Company, Inc.', 'Business LLC')
       ),
       (SELECT TO_CHAR(ADD_MONTHS(TO_DATE('2012-01','yyyy-mm'), rownum -1), 'YYYY-MM') year_month,
               TO_CHAR(ADD_MONTHS(TO_DATE('2012-01','yyyy-mm'), rownum -1), 'MM') month
          FROM cwwebapp_oti.dbo.v_cbi_All_Tickets
         WHERE ROWNUM <= MONTHS_BETWEEN(TO_DATE('2013-12','yyyy-mm'),
                                        TO_DATE('2012-01','yyyy-mm'))+1
       ) list_of_months
 WHERE year_month = date_entered_month
 GROUP BY month
 ORDER BY month

The list_of_months subquery is used just to get the list of months between Jan 2012 and Dec 2013. This could be any table, with the only requirement being that it contains as many rows in it as you need months to report. In this case we assume the table has at least 24 rows.

Also I have an Oracle background and it looks like you are in an SQL Server environment, so you'd need convert the Oracle functions into SQL functions. I hope this helps.

UPDATE I did a little Google-ing on SQL Server and I think the following query should work. Again, I'm sure someone more versed in SQL Server will be able to simplify it.

SELECT SUM(CASE date_entered_year
             WHEN '2012' THEN 1
             ELSE 0
           END) year_2012,
       SUM(CASE date_entered_year
             WHEN '2013' THEN 1
             ELSE 0
           END) year_2013
  FROM (SELECT CAST(DATEPART(year, DATEADD(mm,rn-1, CONVERT(date, '20120101'))) AS VARCHAR)+'-'+
                CAST(DATEPART(month, DATEADD(mm,rn-1, CONVERT(date, '20120101'))) AS VARCHAR) y1,
               CAST(DATEPART(month, DATEADD(mm,rn-1, CONVERT(date, '20120101'))) AS VARCHAR) m1
          FROM (SELECT row_number() OVER (ORDER BY id) as rn
                  FROM cwwebapp_oti.dbo.v_cbi_All_Tickets) sub1
         WHERE rn <= DATEDIFF(mm, CONVERT(date, '20120101'), 
                                  CONVERT(date, '20131231'))+1
       ) list_of_months LEFT OUTER JOIN 
       (SELECT CAST(DATEPART(year, date_entered) AS VARCHAR)+'-'+
                CAST(DATEPART(month, date_entered) AS VARCHAR) date_entered_month,
               CAST(DATEPART(year, date_entered) AS VARCHAR) date_entered_year
          FROM cwwebapp_oti.dbo.v_cbi_All_Tickets
         WHERE company_name IN ('Company, Inc.', 'Business LLC')
       ) company_data ON list_of_months.y1 = company_data.date_entered_month
 GROUP BY m1
 ORDER BY m1
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top