Solved

Derived Query Problem

Posted on 2011-03-03
2
309 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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

The Eight Noble Truths of Backup and Recovery

How can IT departments tackle the challenges of a Big Data world? This white paper provides a roadmap to success and helps companies ensure that all their data is safe and secure, no matter if it resides on-premise with physical or virtual machines or in the cloud.

Question has a verified solution.

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

INTRODUCTION: While tying your database objects into builds and your enterprise source control system takes a third-party product (like Visual Studio Database Edition or Red-Gate's SQL Source Control), you can achieve some protection using a sing…
Data architecture is an important aspect in Software as a Service (SaaS) delivery model. This article is a study on the database of a single-tenant application that could be extended to support multiple tenants. The application is web-based develope…
Michael from AdRem Software outlines event notifications and Automatic Corrective Actions in network monitoring. Automatic Corrective Actions are scripts, which can automatically run upon discovery of a certain undesirable condition in your network.…
This is my first video review of Microsoft Bookings, I will be doing a part two with a bit more information, but wanted to get this out to you folks.

691 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