[Last Call] Learn about multicloud storage options and how to improve your company's cloud strategy. Register Now

x
?
Solved

Derived Query Problem

Posted on 2011-03-03
2
Medium Priority
?
312 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 2000 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

Get your Disaster Recovery as a Service basics

Disaster Recovery as a Service is one go-to solution that revolutionizes DR planning. Implementing DRaaS could be an efficient process, easily accessible to non-DR experts. Learn about monitoring, testing, executing failovers and failbacks to ensure a "healthy" DR environment.

Question has a verified solution.

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

by Mark Wills PIVOT is a great facility and solves many an EAV (Entity - Attribute - Value) type transformation where we need the information held as data within a column to become columns in their own right. Now, in some cases that is relatively…
I've encountered valid database schemas that do not have a primary key.  For example, I use LogParser from Microsoft to push IIS logs into a SQL database table for processing and analysis.  However, occasionally due to user error or a scheduled task…
How to fix incompatible JVM issue while installing Eclipse While installing Eclipse in windows, got one error like above and unable to proceed with the installation. This video describes how to successfully install Eclipse. How to solve incompa…
We’ve all felt that sense of false security before—locking down external access to a database or component and feeling like we’ve done all we need to do to secure company data. But that feeling is fleeting. Attacks these days can happen in many w…

650 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