?
Solved

Displaying totals for data puled from January thru December

Posted on 2013-12-03
3
Medium Priority
?
249 Views
Last Modified: 2013-12-04
I have a complete annual  report minus totals at end. I attempted to use another sql query to generate totals for  'Literal14.Text = GenerateTotal()  (see attached) without success. I deleted it out.  I attempted to create temp variable table (sql 2008) - but not sure I can delcare and insert into @temp ???) - - Tried but got sql unhandled exceptions.

Is there a way to use datarows, data columns, or any other technique to store the counts for each dataRow I defined for Jan- dec and then display at end?
vb3.doc
0
Comment
Question by:mahpog
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
3 Comments
 
LVL 49

Accepted Solution

by:
PortletPaul earned 2000 total points
ID: 39694443
I would suggest a rethinking of the SQL query.

It's possible to arrange the results into 12 monthly columns using case expressions. Plus, this can be performed as a "common table expression" (CTE) which allows reuse of the calculations performed; and we reuse them to produce the column totals.

I have also replaced the use of "between" with a better method,  see: "Beware of Between"
WITH
      CTE
      AS (
                  SELECT
                        a.srp_no
                      , @selyear                                                  AS selyear
                      , COUNT(CASE WHEN MONTH(a.entry_dt) = 1 THEN a.srp_no END)  AS month_1
                      , COUNT(CASE WHEN MONTH(a.entry_dt) = 2 THEN a.srp_no END)  AS month_2
                      , COUNT(CASE WHEN MONTH(a.entry_dt) = 3 THEN a.srp_no END)  AS month_3
                      , COUNT(CASE WHEN MONTH(a.entry_dt) = 4 THEN a.srp_no END)  AS month_4
                      , COUNT(CASE WHEN MONTH(a.entry_dt) = 5 THEN a.srp_no END)  AS month_5
                      , COUNT(CASE WHEN MONTH(a.entry_dt) = 6 THEN a.srp_no END)  AS month_6
                      , COUNT(CASE WHEN MONTH(a.entry_dt) = 7 THEN a.srp_no END)  AS month_7
                      , COUNT(CASE WHEN MONTH(a.entry_dt) = 8 THEN a.srp_no END)  AS month_8
                      , COUNT(CASE WHEN MONTH(a.entry_dt) = 9 THEN a.srp_no END)  AS month_9
                      , COUNT(CASE WHEN MONTH(a.entry_dt) = 10 THEN a.srp_no END) AS month_10
                      , COUNT(CASE WHEN MONTH(a.entry_dt) = 11 THEN a.srp_no END) AS month_11
                      , COUNT(CASE WHEN MONTH(a.entry_dt) = 12 THEN a.srp_no END) AS month_12
                  FROM asrp_usage a
                  WHERE a.frp_ror = '1'
                        AND a.bus_type_gc = 'G'
                        AND a.location IN (
                              SELECT
                                    Value
                              FROM fn_Split(@locationlist, ',')
                        )
                        AND ( a.entry_dt >= DATEADD(YEAR, (@selyear - 1900), 0)
                        AND   a.entry_dt <  DATEADD(YEAR, (@selyear - 1899), 0)  )
                  GROUP BY
                        a.srp_no
            )
SELECT
      0 AS sort_by
    , a.srp_no
    , selyear
    , month_1
    , month_2
    , month_3
    , month_4
    , month_5
    , month_6
    , month_7
    , month_8
    , month_9
    , month_10
    , month_11
    , month_12
FROM CTE
UNION ALL
      SELECT
            1             AS sort_by
          , NULL          AS srp_no
          , NULL          AS selyear
          , SUM(month_1)  AS month_1
          , SUM(month_2)  AS month_2
          , SUM(month_3)  AS month_3
          , SUM(month_4)  AS month_4
          , SUM(month_5)  AS month_5
          , SUM(month_6)  AS month_6
          , SUM(month_7)  AS month_7
          , SUM(month_8)  AS month_8
          , SUM(month_9)  AS month_9
          , SUM(month_10) AS month_10
          , SUM(month_11) AS month_11
          , SUM(month_12) AS month_12
      FROM CTE
ORDER BY
      sort_by
    , srp_no
;

Open in new window

Note instead of '01/01/@selyear' etc. I use of dateadd() ;Those calculations produce Jan 1 of the chosen year, and Jan 1 of the next year, like so:
declare @selyear int = 2012

select
     dateadd(year,(@selyear-1900),0)
  ,  dateadd(year,(@selyear-1899),0)


|                       COLUMN_0 |                       COLUMN_1 |
|--------------------------------|--------------------------------|
| January, 01 2012 00:00:00+0000 | January, 01 2013 00:00:00+0000 |

Open in new window

By the way, it isn't necessary to also do this "AND YEAR(a.entry_dt) = r.selyear" if you are choosing only dates >= "Jan 1 chosen year" and < "Jan 1 next year"

I hope the above makes sense. I guess I should point out that the output of the suggested query mimics the 12 column layout, so your ASP code would need to be amended to suit.
0
 

Author Comment

by:mahpog
ID: 39695737
wow, that is some code. I know about CTE, but wow.   Date range was good technique. Lot to learn from this. going to take some time.
0
 

Author Closing Comment

by:mahpog
ID: 39695753
Argh... thorough.
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction This article shows how to use the open source plupload control to upload multiple images. The images are resized on the client side before uploading and the upload is done in chunks. Background I had to provide a way for user…
The article shows the basic steps of integrating an HTML theme template into an ASP.NET MVC project
NetCrunch network monitor is a highly extensive platform for network monitoring and alert generation. In this video you'll see a live demo of NetCrunch with most notable features explained in a walk-through manner. You'll also get to know the philos…
In this video, Percona Solution Engineer Dimitri Vanoverbeke discusses why you want to use at least three nodes in a database cluster. To discuss how Percona Consulting can help with your design and architecture needs for your database and infras…
Suggested Courses

743 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question