Solved

Cross tab Report Query

Posted on 2015-02-19
12
238 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
Comment Utility
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
Comment Utility
I've requested that this question be deleted for the following reason:

Re-submitting another question
0
 

Author Comment

by:Star79
Comment Utility
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
Comment Utility
Sorry I would like to continue with the question
0
 
LVL 100

Expert Comment

by:mlmcc
Comment Utility
Requestor wants to continue the discussion

mlmcc
0
 
LVL 34

Assisted Solution

by:James0628
James0628 earned 250 total points
Comment Utility
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
IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 
LVL 100

Accepted Solution

by:
mlmcc earned 250 total points
Comment Utility
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
Comment Utility
That's a very good point.  The DUAL CTE may not be necessary at all.

 James
0
 
LVL 100

Expert Comment

by:mlmcc
Comment Utility
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
Comment Utility
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
Comment Utility
Still trying various options.thanks for all the options
0
 

Author Comment

by:Star79
Comment Utility
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

Complete Microsoft Windows PC® & Mac Backup

Backup and recovery solutions to protect all your PCs & Mac– on-premises or in remote locations. Acronis backs up entire PC or Mac with patented reliable disk imaging technology and you will be able to restore workstations to a new, dissimilar hardware in minutes.

Join & Write a Comment

In this article I will describe the Copy Database Wizard method as one possible migration process and I will add the extra tasks needed for an upgrade when and where is applied so it will cover all.
Ever needed a SQL 2008 Database replicated/mirrored/log shipped on another server but you can't take the downtime inflicted by initial snapshot or disconnect while T-logs are restored or mirror applied? You can use SQL Server Initialize from Backup…
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…
This video shows how to remove a single email address from the Outlook 2010 Auto Suggestion memory. NOTE: For Outlook 2016 and 2013 perform the exact same steps. Open a new email: Click the New email button in Outlook. Start typing the address: …

772 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

12 Experts available now in Live!

Get 1:1 Help Now