Solved

Cross tab Report Query

Posted on 2015-02-19
12
243 Views
Last Modified: 2015-02-26
Hello All,
I have the below code.Iam  trying to put together a cross tab report with the below query for
columns NSN,SPEEDEX_ID and row will be each month total.
Please help me  with modifying the below query so that I can integrate this into the cross tab report.
Should I calculate each month's total one by one or is there a way I can modify the query to use pivot to pull the data like pivot.Please help with pivot if that could be an option

WITH DUAL as
(
	select 
    CONVERT(Varchar(10),DATEADD(MONTH, DATEDIFF(MONTH, 0, GETDATE()) -24, 0) ,101)  as sdate, 
    CONVERT(Varchar(10),DATEADD(MONTH, DATEDIFF(MONTH, 0, GETDATE()) -23, 0) ,101)  as sdate2, 
    CONVERT(Varchar(10),DATEADD(MONTH, DATEDIFF(MONTH, 0, GETDATE()) -22, 0) ,101)  as sdate3, 
    CONVERT(Varchar(10),DATEADD(MONTH, DATEDIFF(MONTH, 0, GETDATE()) -21, 0) ,101)  as sdate4, 
    CONVERT(Varchar(10),DATEADD(MONTH, DATEDIFF(MONTH, 0, GETDATE()) -20, 0) ,101)  as sdate5, 
    CONVERT(Varchar(10),DATEADD(MONTH, DATEDIFF(MONTH, 0, GETDATE()) -19, 0) ,101)  as sdate6, 
    CONVERT(Varchar(10),DATEADD(MONTH, DATEDIFF(MONTH, 0, GETDATE()) -18, 0) ,101)  as sdate7, 
    CONVERT(Varchar(10),DATEADD(MONTH, DATEDIFF(MONTH, 0, GETDATE()) -17, 0) ,101)  as sdate8, 
    CONVERT(Varchar(10),DATEADD(MONTH, DATEDIFF(MONTH, 0, GETDATE()) -16, 0) ,101)  as sdate9, 
    CONVERT(Varchar(10),DATEADD(MONTH, DATEDIFF(MONTH, 0, GETDATE()) -15, 0) ,101)  as sdate10, 
    CONVERT(Varchar(10),DATEADD(MONTH, DATEDIFF(MONTH, 0, GETDATE()) -14, 0) ,101)  as sdate11, 
    CONVERT(Varchar(10),DATEADD(MONTH, DATEDIFF(MONTH, 0, GETDATE()) -13, 0) ,101)  as sdate12, 
    CONVERT(Varchar(10),DATEADD(MONTH, DATEDIFF(MONTH, 0, GETDATE()) -12, 0) ,101)  as sdate13, 
    CONVERT(Varchar(10),DATEADD(MONTH, DATEDIFF(MONTH, 0, GETDATE()) -11, 0) ,101)  as sdate14, 
    CONVERT(Varchar(10),DATEADD(MONTH, DATEDIFF(MONTH, 0, GETDATE()) -10, 0) ,101)  as sdate15, 
    CONVERT(Varchar(10),DATEADD(MONTH, DATEDIFF(MONTH, 0, GETDATE()) -9, 0) ,101)  as sdate16, 
    CONVERT(Varchar(10),DATEADD(MONTH, DATEDIFF(MONTH, 0, GETDATE()) -8, 0) ,101)  as sdate17, 
    CONVERT(Varchar(10),DATEADD(MONTH, DATEDIFF(MONTH, 0, GETDATE()) -7, 0) ,101)  as sdate18, 
    CONVERT(Varchar(10),DATEADD(MONTH, DATEDIFF(MONTH, 0, GETDATE()) -6, 0) ,101)  as sdate19, 
    CONVERT(Varchar(10),DATEADD(MONTH, DATEDIFF(MONTH, 0, GETDATE()) -5, 0) ,101)  as sdate20, 
    CONVERT(Varchar(10),DATEADD(MONTH, DATEDIFF(MONTH, 0, GETDATE()) -4, 0) ,101)  as sdate21, 
    CONVERT(Varchar(10),DATEADD(MONTH, DATEDIFF(MONTH, 0, GETDATE()) -3, 0) ,101)  as sdate22, 
    CONVERT(Varchar(10),DATEADD(MONTH, DATEDIFF(MONTH, 0, GETDATE()) -2, 0) ,101)  as sdate23, 
    CONVERT(Varchar(10),DATEADD(MONTH, DATEDIFF(MONTH, 0, GETDATE()) -1, 0) ,101)  as sdate24, 
	CONVERT(varchar(10),DATEADD(s,-1,DATEADD(mm, DATEDIFF(m,0,GETDATE()),0)),101) as edate
),
--select *  from DUAL

ALL_NSN_INFO AS (
select 
nsn,
speedex_id,
sum(qty_required) as total_qty
from d_item1, dual
where date_create >= sdate and date_create <= edate
and status between '01' and '79'
and program in ('770','772','773','774','775','776','778','779')
group by nsn, speedex_id
),
--select * from ALL_NSN_INFO order by nsn, speedex_id

MONTH_1 AS (
select 
nsn,
sum(qty_required) as month1_total
from d_item1, dual
where date_create >= sdate and date_create < sdate2
and status between '01' and '79'
and program in ('770','772','773','774','775','776','778','779')
group by nsn
),
--select * from MONTH_1 order by nsn

MONTH_2 AS (
select 
nsn,
sum(qty_required) as month2_total
from d_item1, dual
where date_create >= sdate2 and date_create < sdate3
and status between '01' and '79'
and program in ('770','772','773','774','775','776','778','779')
group by nsn
)
--select * from MONTH_2 order by nsn

select al.nsn
, al.speedex_id
, m1.month1_total
,m2.month2_total
, al.total_qty AS TOTAL
,(al.total_qty/24) AS DEMAND
from ALL_NSN_INFO al
left outer join MONTH_1 m1 on al.nsn = m1.nsn
left outer join MONTH_2 m2 on al.nsn = m2.nsn
order by al.nsn      

Open in new window

0
Comment
Question by:Star79
  • 5
  • 4
  • 3
12 Comments
 
LVL 100

Expert Comment

by:mlmcc
ID: 40619695
Are you building a report in Crystal?

Does that query produce the data you desire?

If so you should be able to use it as a COMMAND datasource in Crystal to produce a cross tab.

There may be an easier way to do it in Crystal.  What data are you trying to show?

mlmcc
0
 

Author Comment

by:Star79
ID: 40622601
I've requested that this question be deleted for the following reason:

Re-submitting another question
0
 

Author Comment

by:Star79
ID: 40619730
The screen shot attached is the data from sql.
Iam trying to display this in a crystal report.I guess a cross tab will help.
I have generated data for month1, month2, but it needs to go on till month24.
The report sample is atatched.I have to provide all the 24 months data along with the total.
Not sure if I need to modify the sql or can I play around in the cross tab.
Please advise.
Capture.PNG
reportSample.PNG
0
 

Author Comment

by:Star79
ID: 40619733
Sorry I would like to continue with the question
0
 
LVL 100

Expert Comment

by:mlmcc
ID: 40622602
Requestor wants to continue the discussion

mlmcc
0
 
LVL 34

Assisted Solution

by:James0628
James0628 earned 250 total points
ID: 40622932
You're trying to create a column for each month, so each record contains all 24 months.  That won't really work for a cross-tab, because a cross-tab uses groups (one, or more, groups for the rows, and one, or more, groups for the columns), and each record in a report can only appear once in any given group.  You would need each record to be included in all 24 columns (ie. in each group), and CR just doesn't work like that.

 Then again, if you have a separate column for each month, it doesn't seem like you need a cross-tab.  You could just put each month field (month1_total, month2_total, etc.) on the report.

 However, a better way to handle this might be to put each month in a separate record (I guess this may essentially be the "pivot" option you asked about).  Your DUAL CTE has a separate column for each month (sdate, sdate2, etc.).  You could put each month in a separate row instead.  Something like:
WITH DUAL as
(
	SELECT 1 AS MONTH_COUNT,
    CONVERT(Varchar(10),DATEADD(MONTH, DATEDIFF(MONTH, 0, GETDATE()) -24, 0) ,101)  as sdate,
    CONVERT(Varchar(10),DATEADD(MONTH, DATEDIFF(MONTH, 0, GETDATE()) -23, 0) ,101)  as edate
        UNION
        SELECT 2 AS MONTH_COUNT, 
    CONVERT(Varchar(10),DATEADD(MONTH, DATEDIFF(MONTH, 0, GETDATE()) -23, 0) ,101)  as sdate,
    CONVERT(Varchar(10),DATEADD(MONTH, DATEDIFF(MONTH, 0, GETDATE()) -22, 0) ,101)  as edate
        UNION
 <and so on>
        SELECT 24 AS MONTH_COUNT,
    CONVERT(Varchar(10),DATEADD(MONTH, DATEDIFF(MONTH, 0, GETDATE()) -1, 0) ,101)  as sdate,
    CONVERT(varchar(10),DATEADD(s,-1,DATEADD(mm, DATEDIFF(m,0,GETDATE()),0)),101) as edate
),

Open in new window

I added the MONTH_COUNT column just for reference.

 Then you'd use date_create to Join d_item1 with DUAL, to get a row for each month.  You could include sdate in the query results, and then the report could have a cross-tab, using sdate for the columns (grouped by day).  Or you could include date_create in the query results, and use that for the cross-tab columns (grouped by month).

 One big advantage to the "separate row for each month" approach is that you'd just have one CTE for the months (DUAL), which would be Joined with your main data, and that's it.  With your approach, you'd presumably end up with 24 CTE's (one for each month, like MONTH_1 and MONTH_2), all of which would have to be Joined with your main data.


 However, I'm not sure how ALL_NSN_INFO and speedex_id figure into this.  ALL_NSN_INFO is grouped by nsn and speedex_id, but the month CTE's are only grouped by nsn, and don't reference speedex_id at all.  Are ALL_NSN_INFO and the month CTE's grouping on two different levels?  Or is the relationship between nsn and speedex_id unique, so grouping by nsn is the same as grouping by nsn and speedex_id ?

 If the grouping is really the same, I think you could just do the summary in the report, and don't really need ALL_NSN_INFO at all.  If you use a cross-tab, it will give you a total for each row.  Or, using your original approach, you could have a formula add the month columns (month1_total, month2_total, etc.) together.

 James
0
Best Practices: Disaster Recovery Testing

Besides backup, any IT division should have a disaster recovery plan. You will find a few tips below relating to the development of such a plan and to what issues one should pay special attention in the course of backup planning.

 
LVL 100

Accepted Solution

by:
mlmcc earned 250 total points
ID: 40623163
WHy don't you let Crystal do the summarizing and just provide the raw data.

Try this query

select
nsn,
speedex_id,
sum(qty_required) as total_qty
from d_item1 where date_create >=  DateAdd(24, 'M', CurrentDate) and status between '01' and '79'
and program in ('770','772','773','774','775','776','778','779')

You can then use the cross tab in Crystal to create the appropriate columns and rows.
For the date columns just set the group to be by MONTH

mlmcc
0
 
LVL 34

Expert Comment

by:James0628
ID: 40623706
That's a very good point.  The DUAL CTE may not be necessary at all.

 James
0
 
LVL 100

Expert Comment

by:mlmcc
ID: 40623714
The only reason I see for that is to ensure there is data for all months in the time frame.

mlmcc
0
 
LVL 34

Expert Comment

by:James0628
ID: 40623732
Yeah.  I got the idea to put the months in separate rows, but I was so focused on the old code, it didn't occur to me that they may not need the CTE at all.

 James
0
 

Author Comment

by:Star79
ID: 40628640
Still trying various options.thanks for all the options
0
 

Author Comment

by:Star79
ID: 40634161
We are using a different reporting tool called Pentaho which can handle dynamic cross tabs.I used that to acieve it.
But were it crytal the cross tab would have worked too.Closing the question
0

Featured Post

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

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

Hello everyone, Hope you find this as helpful as we did. We have on the company I work for an application built in Delphi V with Crystal Reports 8. We all know that Crystal & Delphi can be temperamental sometimes and the worst thing is, nearly…
This article explains how to reset the password of the sa account on a Microsoft SQL Server.  The steps in this article work in SQL 2005, 2008, 2008 R2, 2012, 2014 and 2016.
Many functions in Excel can make decisions. The most simple of these is the IF function: it returns a value depending on whether a condition you describe is true or false. Once you get the hang of using the IF function, you will find it easier to us…
Both in life and business – not all partnerships are created equal. As the demand for cloud services increases, so do the number of self-proclaimed cloud partners. Asking the right questions up front in the partnership, will enable both parties …

863 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

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now