Solved

Derived Query Problem

Posted on 2011-03-03
2
301 Views
Last Modified: 2012-05-11
I have a table (tblReportHits) that contains 3 columns ntid, report, date. I want to show by report and grouped by ntid the report count by ntid (an agent no). I want to do this since the begining and for the past 30 days.The table has been in existence about 4 months. So for each ntid (agent) I can show how many time he/she went to report X since the begining and for the  last 30 days, this is a "hits" calculation.

I thought I could do the above via a derived query. I am using SSMS and in a view when I list my selects in the from clause I end up with the following:

SELECT     derivedtbl_1.report, derivedtbl_1.Expr1, derivedtbl_1.ntid
FROM         (SELECT     report, COUNT(report) AS Expr1, ntid
                       FROM          dbo.tblReportHits
                       WHERE      (lastVisit > GETDATE() - 30)
                       GROUP BY report, ntid) AS derivedtbl_1 CROSS JOIN
                          (SELECT     report, COUNT(report) AS TotalHits, ntid
                            FROM          dbo.tblReportHits AS tblReportHits_1
                            GROUP BY report, ntid) AS derivedtbl_2

Thus I get a cross join. This also happens when I manually write the query

Select t.report, t.count, t.date, m.count
From (Select ntid, count(report) as count, date from  tblReportHits Group by ntid, date) as t,
(Select ntid, count(report) as count, date from  tblReportHits Group by ntid, date) as m

Am I trying to do something that is logically not possible?






0
Comment
Question by:SeTech
2 Comments
 
LVL 15

Accepted Solution

by:
derekkromm earned 500 total points
Comment Utility
so you want the output to be:

report, ntid, count for past month, count since beginning?

select a.report, a.ntid, a.c TotalCount, isnull(b.c, 0) MonthCount
from  (select report,ntid,count(*) c from tblreporthits group by report,ntid) a
left join (select report, ntid, count(*) c from tblreporthits where lastvisit > getdate() - 30 group by report,ntid) b
on a.report=b.report and a.ntid=b.ntid
0
 

Author Closing Comment

by:SeTech
Comment Utility
Perfect, thanks much.
0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

If you having speed problem in loading SQL Server Management Studio, try to uncheck these options in your internet browser (IE -> Internet Options / Advanced / Security):    . Check for publisher's certificate revocation    . Check for server ce…
There are some very powerful Data Management Views (DMV's) introduced with SQL 2005. The two in particular that we are going to discuss are sys.dm_db_index_usage_stats and sys.dm_db_index_operational_stats.   Recently, I was involved in a discu…
This video discusses moving either the default database or any database to a new volume.
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…

728 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

14 Experts available now in Live!

Get 1:1 Help Now