| Author |
Topic |
|
ljp099
Yak Posting Veteran
79 Posts |
Posted - 2006-04-11 : 12:58:02
|
| Im trying to construct a query which is sorted to show users who have downloaded the most articles first. My table structure is:iditem, articlename, idarticle, iduser, downloaddateHow would I write a query to show me article download history displaying: iduser, articlename, datedownloadedand sorted by the iduser with the most downloads...thanks |
|
|
chiragkhabaria
Master Smack Fu Yak Hacker
1907 Posts |
Posted - 2006-04-11 : 13:02:18
|
| Somthing like this Select Count(1) As Download,IdUser,IdArticle From TblGroup by IdUser,IdArticle order by IduserIf Debugging is the process of removing Bugs then i Guess programming should be process of Adding them. |
 |
|
|
RyanRandall
Master Smack Fu Yak Hacker
1074 Posts |
Posted - 2006-04-11 : 13:07:06
|
--datadeclare @t table (iditem int identity(1, 1), articlename varchar(10), idarticle int, iduser int, downloaddate datetime)insert @t select 'a', 1, 1, '1 jan 2006'union all select 'a', 1, 3, '2 jan 2006'union all select 'b', 2, 1, '3 jan 2006'union all select 'c', 3, 1, '4 jan 2006'union all select 'c', 3, 2, '5 jan 2006'union all select 'd', 4, 3, '6 jan 2006'union all select 'd', 4, 1, '7 jan 2006'--calculationselect a.iduser, a.articlename, a.downloaddate from @t a inner join (select iduser, count(*) as count from @t group by iduser) b on a.iduser = b.iduserorder by b.count desc, articlename Ryan Randallwww.monsoonmalabar.com London-based IT consultancy Solutions are easy. Understanding the problem, now, that's the hard part. |
 |
|
|
Srinika
Master Smack Fu Yak Hacker
1378 Posts |
Posted - 2006-04-11 : 13:10:03
|
| U want to sort by the # of articles downloaded --> each day ?Give some sample data and expected resultsSrinika |
 |
|
|
ljp099
Yak Posting Veteran
79 Posts |
Posted - 2006-04-11 : 14:11:49
|
| In my original example I didnt include all the tables Im actually joining for ease of example. However, I cant get the sql statements you describe to run with the Select statement in the Inner Join and using the group by clause properly. Here is my current statement (Im joining 4 tables, Users, Documents, UserDownloadHistory, Organizations):SELECT O.orgName, REPLACE(CONVERT(CHAR(10),O.licExpirationDate, 110), '-', '/') AS LicExpDate,REPLACE(CONVERT(CHAR(10), O.registrationDate, 110),'-', '/') AS RegDate, U.idUser,U.NoUsers, UDH.idItem, UDH.articleDownloaded,REPLACE(CONVERT(CHAR(10), udh.downloadDateTime, 110),'-', '/') AS DownloadDate, UDH.articleDownloadedID, UDH.uniqueUserID,Documents.TitleFROM Org AS O INNER JOIN Users AS u ON O.idOrg = U.idOrgINNER JOIN UserDownloadHistory AS UDH ON U.NoUsers = UDH.uniqueUserID INNER JOIN Documents ON UDH.articleDownloadedID = Documents.DocumentIDWHERE (UDH.downloadDateTime >= @FromDate) AND (UDH.downloadDateTime <= @ToDate)ORDER BY UDH.downloadDateTime DESCThanks for your help |
 |
|
|
ljp099
Yak Posting Veteran
79 Posts |
Posted - 2006-04-11 : 15:05:48
|
| I want to show the most downloaded articles for a date range (selected by the user). The most downloaded articles would simply be displayed first in the results. Heres a single row of data, i just need to get the sort to show users who have downloaded the most articles first:Org Name Download Date Document Title Document File Name IDUser B 04/11/2006 Best Practices... zant_3_13.pdf premenko |
 |
|
|
PSamsig
Constraint Violating Yak Guru
384 Posts |
Posted - 2006-04-13 : 08:57:12
|
| What about (not checked for typos):SELECT O.orgName, REPLACE(CONVERT(CHAR(10),O.licExpirationDate, 110), '-', '/') AS LicExpDate,REPLACE(CONVERT(CHAR(10), O.registrationDate, 110),'-', '/') AS RegDate, U.idUser,U.NoUsers, UDH.idItem, UDH.articleDownloaded,REPLACE(CONVERT(CHAR(10), udh.downloadDateTime, 110),'-', '/') AS DownloadDate, UDH.articleDownloadedID, UDH.uniqueUserID,Documents.TitleFROM Org AS O INNER JOINUsers AS u ON O.idOrg = U.idOrgINNER JOIN(SELECT uniqueUserID, COUNT(*) AS RankFROM UserDownloadHistoryWHERE (downloadDateTime >= @FromDate) AND (downloadDateTime <= @ToDate)GROUP BY uniqueUserID) AS DTbl ON DTbl.uniqueUserID = U.NoUsersINNER JOINUserDownloadHistory AS UDH ON U.NoUsers = UDH.uniqueUserID AND (UDH.downloadDateTime >= @FromDate) AND (UDH.downloadDateTime <= @ToDate)INNER JOINDocuments ON UDH.articleDownloadedID = Documents.DocumentIDORDER BY DTbl.Rank DESC, UDH.uniqueUserID, UDH.downloadDateTime DESC |
 |
|
|
|
|
|