Please start any new threads on our new
site at https://forums.sqlteam.com. We've got lots of great SQL Server
experts to answer whatever question you can come up with.
| Author |
Topic |
|
sqlns
Starting Member
3 Posts |
Posted - 2009-11-17 : 15:40:23
|
| Hi, I am relatively new to SQL and very new to SQL Server. I am trying to write a query (below), that will display a list of page urls, date accessed and the number of people that have accessed the page each day. I know my query here is wrong but I can't for the life of me work it out.Current output (using this query) is: 02 Jun 2009 resume.aspx 103 Jan 2009 contact.aspx 104 Mar 2009 skills.aspx 116 Jul 2009 about.aspx 216 Nov 2009 about.aspx 116 May 2009 contact.aspx 116 Aug 2009 contact.aspx 116 Oct 2009 contact.aspx 116 Aug 2009 resume.aspx 116 Nov 2009 resume.aspx 116 Nov 2009 resume.aspx 116 Nov 2009 resume.aspx 116 Sep 2009 skills.aspx 1As you can see, the pages aren't being grouped by day. I'm not sure whether this is enough info but can anyone please help??SELECT CONVERT(CHAR(11), accessedDate, 106) as Date, pageName, COUNT(accessedDate) as TimesAccessed FROM pageAccessed LEFT JOIN page ON page.pageID = pageAccessed.pageID GROUP BY DATEPART(dd, accessedDate), pageName, accessedDateTable 1:Table Name: page pageID pageName pageContent1 resume.aspx Resume page content2 skills.aspx Skills page content3 contact.aspx Contact page content4 about.aspx About page contentTable 2 Info:Table Name: pageAccessed accessedID pageID accessDate1 1 16/11/2009 20:352 4 16/11/2009 20:363 1 02/06/2009 20:384 3 16/05/2009 20:385 3 03/01/2009 20:386 2 04/03/2009 20:387 3 16/08/2009 20:388 1 16/11/2009 20:3810 4 16/07/2009 20:3911 4 16/07/2009 20:3912 1 16/08/2009 20:3913 2 16/09/2009 20:3914 3 16/10/2009 20:3915 1 16/11/2009 20:39 |
|
|
X002548
Not Just a Number
15586 Posts |
|
|
jhocutt
Constraint Violating Yak Guru
385 Posts |
Posted - 2009-11-17 : 15:46:15
|
| SELECT CONVERT(CHAR(11), accessedDate, 106) as Date, pageName, COUNT(accessedDate) as TimesAccessed FROM pageAccessed LEFT JOIN page ON page.pageID = pageAccessed.pageID GROUP BY convert(char(11), accessedDate, 106), pageName"God does not play dice" -- Albert Einstein"Not only does God play dice, but he sometimes throws them where they cannot be seen." -- Stephen Hawking |
 |
|
|
sqlns
Starting Member
3 Posts |
Posted - 2009-11-17 : 16:26:49
|
| Super fast! Thank you!!! Can you help with another? I'm having a similar (but different) problem:For this one, I'm trying to display a list of all contact email addresses, the number of times that have tried to email and how many failed(0) or succeeded(1).Current output (using this query) is:company1@owner.com 2 1 1company1@owner.com 1 1 0company1@owner.com 3 1 2company2@owner.com 4 1 3company2@owner.com 1 0 1company3@owner.com 1 1 0As yo can see, I want similar email addresses grouped, but they aren't. (I know this isn't great table design but the point here is that I figure out the grouping functions).SELECT e.emailAddress, count(*) AS [Total Attempts],(SELECT COUNT(sent) FROM emailAttempt ea1 WHERE sent = 1 AND ea1.emailID = ea.emailID ) AS Succeeded,(SELECT COUNT(sent) FROM emailAttempt ea2 WHERE sent = 0 AND ea2.emailID = ea.emailID ) AS FailedFROM emailAttempt ea LEFT JOIN email e ON e.emailID = ea.emailID GROUP BY e.emailAddress, ea.emailIDORDER BY e.emailAddress Table 1:email emailID emailFName emailLName emailAddress1 Company1 LName1 company1@owner.com2 Company2 LName2 company2@owner.com3 Company3 LName3 company3@owner.com4 Company1 LName1 company1@owner.com5 Company1 LName1 company1@owner.com6 Company6 LName6 company6@owner.com7 Company3 LName3 company3@owner.com8 Company1 LName1 company1@owner.com12 Company2 LName2 company2@owner.comTable 2:emailAttempt attemptID emailID sent1 1 02 1 13 2 04 2 05 2 06 2 17 3 18 4 19 5 010 5 011 5 112 12 0 |
 |
|
|
jhocutt
Constraint Violating Yak Guru
385 Posts |
Posted - 2009-11-18 : 08:50:39
|
| SELECT e.emailAddress, count(*) AS [Total Attempts], sum(sent) as Succeeded, sum( case(sent) when 1 then 0 else 1 end )AS FailedFROM emailAttempt ea LEFT JOIN email e ON e.emailID = ea.emailID GROUP BY e.emailAddressORDER BY e.emailAddress "God does not play dice" -- Albert Einstein"Not only does God play dice, but he sometimes throws them where they cannot be seen." -- Stephen Hawking |
 |
|
|
sqlns
Starting Member
3 Posts |
Posted - 2009-11-18 : 15:07:15
|
| THANK YOU jhocutt!!!!!!!!!!! |
 |
|
|
|
|
|
|
|