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 |
|
dantesfu
Starting Member
7 Posts |
Posted - 2006-06-24 : 22:31:01
|
| I want to show the count of Unique Books and Chapters edited by Date but I am having a hard time getting the book count not to be counted more than ones but my query is not working. Can you please tell me what I am doing wrong? Thank you!SELECT DATE, COUNT(BookTitle) AS [BooksEdited], COUNT(Chapter) AS [ChaptersEdited]FROM BooksEditedGROUP BY DATE Desired OutputCount of Books and Chapters Edited grouped by DateDate ---- BooksEdited --- ChaptersEdited4/25/2006 --- ---3 --- --- 54/26/2006 --- --- 2 --- --- 44/27/2006 --- --- 1 --- --- 3Here is my data. Table: BooksEditedDate --- --- BookTitle --- --- --- Chapter4/25/2006 --- Crime and Punishment --- one4/25/2006 --- Crime and Punishment --- two4/25/2006 --- The Eastern Code --- one4/25/2006 --- The Eastern Code --- two4/25/2006 --- True Justice --- one4/26/2006 --- White Lies --- one4/26/2006 --- White Lies --- two4/26/2006 --- The Rain Maker --- one4/26/2006 --- The Rain Maker --- two4/27/2006 --- Cold War --- one4/27/2006 --- Cold War --- two4/27/2006 --- Cold War --- three |
|
|
Michael Valentine Jones
Yak DBA Kernel (pronounced Colonel)
7020 Posts |
Posted - 2006-06-24 : 22:42:29
|
| [code]select DATE, count(distinct BookTitle) as [BooksEdited], count(distinct Chapter) as [ChaptersEdited]from BooksEditedgroup by DATEorder by DATE [/code]CODO ERGO SUM |
 |
|
|
suneil23
Starting Member
4 Posts |
Posted - 2006-06-25 : 01:16:24
|
| Mr.Micheal ur solution to the question is incorrect...i tried ur answer but its showing like dis.DATE BOOKSEDITED CHAPTERSEDITED 2006-04-25 00:00:00.000 4 22006-04-26 00:00:00.000 2 22006-04-27 00:00:00.000 2 3UR SOLUTION IS TOTALLY ABSURD...PLS CHECK ME AND LETME KMNOW ASAP...THANX*REGSSQL Novice |
 |
|
|
suneil23
Starting Member
4 Posts |
Posted - 2006-06-25 : 01:30:40
|
| Dear Dantesfu please try dis pal..it surely gives the answer wat u need...select DATE,count(distinct BookTitle) as [BooksEdited],count(Chapter) as [ChaptersEdited] from BooksEditedgroup by dateorder by date; |
 |
|
|
jsmith8858
Dr. Cross Join
7423 Posts |
Posted - 2006-06-25 : 11:20:12
|
| suneil23 --First off, your "new" solution is exactly the same as what MVJ wrote (you know, the "TOTALLY ABSURD" one). Second, this isn't an AOL chatroom, so please try to act like an adult.EDIT: see below. |
 |
|
|
Arnold Fribble
Yak-finder General
1961 Posts |
Posted - 2006-06-25 : 13:22:58
|
Sorry, but they aren't the same, Jeff.MVJ had a distinct "Chapter" count, the other one didn't.We haven't been told that (Date, BookTitle, Chapter) is a candidate key on BooksEdited. If it isn't, neither is right.SELECT Date, COUNT(*) AS BooksEdited, SUM(ct_chapter) AS ChaptersEditedFROM ( SELECT Date, BookTitle, COUNT(DISTINCT Chapter) AS ct_chapter FROM BooksEdited GROUP BY Date, BookTitle ) AS AGROUP BY Date |
 |
|
|
jsmith8858
Dr. Cross Join
7423 Posts |
Posted - 2006-06-25 : 14:10:17
|
| Thanks, Arnold -- I missed that. my fault. Point #2 still holds, though. :) |
 |
|
|
dantesfu
Starting Member
7 Posts |
Posted - 2006-06-25 : 14:36:44
|
| Thank you all Michael Valentine Jones, suneil23, Arnold Fribble. It worked! |
 |
|
|
|
|
|
|
|