gtjr921
Programmer
- Aug 30, 2006
- 115
I am new to Sql Reporting so I will try to explain this as best i can.
I have a sql query that has two unions with the same table
When i run the query I get the expected results.
When i put the fields on the report not all of the data shows up.
1. The data in my first query only displays one row for my results even though there are 5 rows.
The data in my third query for the total calls does not display, I have the field set in the layout. I wanted to just use the reporting to give me the sum of all the calls for each caller, but it would only give me the sum for each month not the year.
SUM(CASE WHEN Direction = '2' and LEN(Targetnum) = 7 THEN 1 ELSE 0 END) AS [TotYRLocalCallCount],
SUM(CASE WHEN Direction = '2' and LEN(Targetnum) >= 10 THEN 1 ELSE 0 END) AS [TotYRLDCalls]
Here's My Query
I have a sql query that has two unions with the same table
When i run the query I get the expected results.
When i put the fields on the report not all of the data shows up.
1. The data in my first query only displays one row for my results even though there are 5 rows.
The data in my third query for the total calls does not display, I have the field set in the layout. I wanted to just use the reporting to give me the sum of all the calls for each caller, but it would only give me the sum for each month not the year.
SUM(CASE WHEN Direction = '2' and LEN(Targetnum) = 7 THEN 1 ELSE 0 END) AS [TotYRLocalCallCount],
SUM(CASE WHEN Direction = '2' and LEN(Targetnum) >= 10 THEN 1 ELSE 0 END) AS [TotYRLDCalls]
Here's My Query
Code:
--query 1 Top 5 Area Codes
select top (5) (substring(Targetnum,1,3)) As [TopAreaCodes],
datepart(Month,convert(datetime,convert (varchar(8), localday))) as [Month],
datepart(Year,convert(datetime,convert (varchar(8), localday))) As [Year],
null as CallerName,null As TalkTimeTot,
null As AvgCallLengthDay,null AS TimeMonthTest,
null AS LocCallCount,null as TotLDCallsPerMonth,
null as AvgLDCallsPerDay,null as [411Ccount],null As AvgTalk,null as [TotYRLocalCallCount], null as [TotYRLDCalls] from cdrmain
where Targetnum like '[2-9]%' and Targetnum not like '800%' or
Targetnum not like '[0-1]%'
and Direction ='2'
And LEN(Targetnum) >= 10
GROUP BY datepart(Month,convert(datetime,convert (varchar(8), localday))),
datepart(Year,convert(datetime,convert (varchar(8), localday))),targetnum
--Query 2 Long Distance Call Details
Union All
SELECT null as [TopAreaCodes],
datepart(Month,convert(datetime,convert (varchar(8), localday))) As [Month],
datepart(Year,convert(datetime,convert (varchar(8), localday))) As [Year],
CallerName,
Sum (TalkDuration) /60 As TalkTimeTot,
sum (TalkDuration) /60 /21.5 As AvgCallLengthDay,
sum (TalkDuration) /60 As TimeMonthTest,
SUM(CASE WHEN Direction = '2' and LEN(Targetnum) = 7 THEN 1 ELSE 0 END) AS [LocalCallCount],
SUM(CASE WHEN Direction = '2' and LEN(Targetnum) >= 10 THEN 1 ELSE 0 END) AS [TotLDCallsPerMonth],
SUM(CASE WHEN Direction = '2' and LEN(Targetnum) >= 10 THEN 1 ELSE 0 END) /21.5 AS [AvgLDCallsPerDay],
SUM(CASE WHEN TargetNum = '411' THEN 1 ELSE 0 END) AS [411Ccount],
Avg (TalkDuration)/60 As AvgTalk,null as [TotYRLocalCallCount], null as [TotYRLDCalls]
FROM CDRMAIN
Where Len (TargetNum) >= 10 or TargetNum ='411' or Len (TargetNum) >= 7
GROUP BY datepart(Month,convert(datetime,convert (varchar(8), localday))),
datepart(Year,convert(datetime,convert (varchar(8), localday))), CallerName
HAVING HAVING
(CallerName = 'CallerName1')Or
(CallerName = 'CallerName2')Or
(CallerName = 'CallerName2')
--year to date query 3 get Total calls
Union All
SELECT null as [TopAreaCodes],
null as [Month],
null As [Year],
CallerName,null As TalkTimeTot,
null As AvgCallLengthDay,null AS TimeMonthTest,
null AS LocCallCount,null as TotLDCallsPerMonth,
null as AvgLDCallsPerDay,null as [411Ccount],null As AvgTalk, SUM(CASE WHEN Direction = '2' and LEN(Targetnum) = 7 THEN 1 ELSE 0 END) AS [TotYRLocalCallCount],
SUM(CASE WHEN Direction = '2' and LEN(Targetnum) >= 10 THEN 1 ELSE 0 END) AS [TotYRLDCalls]
FROM CDRMAIN
Where Len (TargetNum) >= 10 or TargetNum ='411' or Len (TargetNum) >= 7
GROUP BY CallerName
HAVING
(CallerName = 'CallerName1')Or
(CallerName = 'CallerName2')Or
(CallerName = 'CallerName2')
order by datepart(Month,convert(datetime,convert (varchar(8), localday))) Desc,
datepart(Year,convert(datetime,convert (varchar(8), localday))) Desc