?
Solved

Derived Query Problem

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

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

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…
When writing XML code a very difficult part is when we like to remove all the elements or attributes from the XML that have no data. I would like to share a set of recursive MSSQL stored procedures that I have made to remove those elements from …
In this video you will find out how to export Office 365 mailboxes using the built in eDiscovery tool. Bear in mind that although this method might be useful in some cases, using PST files as Office 365 backup is troublesome in a long run (more on t…
In this video, Percona Solution Engineer Dimitri Vanoverbeke discusses why you want to use at least three nodes in a database cluster. To discuss how Percona Consulting can help with your design and architecture needs for your database and infras…

770 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