TECHNOLOGIES
FORUMS
JOBS
BOOKS
EVENTS
INTERVIEWS
Live
MORE
LEARN
Training
CAREER
MEMBERS
VIDEOS
NEWS
BLOGS
Sign Up
Login
No unread comment.
View All Comments
No unread message.
View All Messages
No unread notification.
View All Notifications
Answers
Post
An Article
A Blog
A News
A Video
An EBook
An Interview Question
Ask Question
Forums
Monthly Leaders
Forum guidelines
ahmed elbarbary
NA
1.6k
278.3k
count is wrong for author related book why and how to solve ?
Aug 22 2020 7:58 PM
I work on SQL server 2012 I face issue count is wrong for book s related to author
so why count for books related to author is wrong for author Ahmed
create
table
#books
(
BookId
int
,
BookName nvarchar(200),
AuthorId
int
)
create
table
#booksUpdate
(
BookId
int
,
BookName nvarchar(200),
AuthorId
int
)
insert
into
#booksUpdate
values
(119,
'matlab'
,1),
(120,
'3dmax'
,1),
(121,
'c'
,1)
create
table
#Authors
(
AuthorId
int
,
AuthorName nvarchar(200)
)
insert
into
#Authors
values
(1,
'Ahmed'
),
(2,
'Mohamed'
),
(3,
'Eslam'
)
insert
into
#books
values
(122,
'c#'
,1),
(233,
'Java'
,1),
(555,
'c++'
,1),
(666,
'photoshop'
,2),
(777,
'asp.net'
,2),
(888,
'python'
,2)
select
a.authorName,
count
(b.BookName)
as
countBooks ,
count
(bu.BookName)
as
countBooksUpdate,(
count
(bu.BookName) +
count
(b.BookName))
as
Total
from
#Authors a
left
join
#books b
on
a.AuthorId=b.AuthorId
left
join
#booksUpdate bu
on
a.AuthorId=bu.AuthorId
group
by
a.authorName
authorName countBooks countBooksUpdate Total
Ahmed 9 9 18
Eslam 0 0 0
Mohamed 3 0 3
result is wrong for Author Name Ahmed
it must be
authorName countBooks countBooksUpdate Total
Ahmed 3 3 6
so How to fix query to give correct result
Expected result
is
authorName countBooks countBooksUpdate Total
Ahmed 3 3 6
Eslam 0 0 0
Mohamed 3 0 3
Reply
Answers (
1
)
how to eliminate a blank character
where function cross temp table not return data update status