Solved

Derived Query Problem

Posted on 2011-03-03
2
303 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
ID: 35029576
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
ID: 35030304
Perfect, thanks much.
0

Featured Post

Courses: Start Training Online With Pros, Today

Brush up on the basics or master the advanced techniques required to earn essential industry certifications, with Courses. Enroll in a course and start learning today. Training topics range from Android App Dev to the Xen Virtualization Platform.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
OPENDATASOURCE 8 40
how many extra RAM for SQL server is needed 22 37
Passing value to a stored procedure 8 99
How to simplify my SQL statement? 14 52
Introduction: When running hybrid database environments, you often need to query some data from a remote db of any type, while being connected to your MS SQL Server database. Problems start when you try to combine that with some "user input" pass…
Use this article to create a batch file to backup a Microsoft SQL Server database to a Windows folder.  The folder can be on the local hard drive or on a network share.  This batch file will query the SQL server to get the current date & time and wi…
This Micro Tutorial will teach you how to censor certain areas of your screen. The example in this video will show a little boy's face being blurred. This will be demonstrated using Adobe Premiere Pro CS6.
In a recent question (https://www.experts-exchange.com/questions/28997919/Pagination-in-Adobe-Acrobat.html) here at Experts Exchange, a member asked how to add page numbers to a PDF file using Adobe Acrobat XI Pro. This short video Micro Tutorial sh…

813 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