Click here to monitor SSC
SQLServerCentral is supported by Red Gate Software Ltd.
 
Log in  ::  Register  ::  Not logged in
 
 
 
        
Home       Members    Calendar    Who's On


Add to briefcase

Group by select problem Expand / Collapse
Author
Message
Posted Monday, October 15, 2007 7:39 AM
Forum Newbie

Forum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum Newbie

Group: General Forum Members
Last Login: Thursday, July 8, 2010 9:10 AM
Points: 5, Visits: 69
Hi all
have developed my library project counter problem when I want to get sum
book status (in , out ,lost,damage and removed) of the same title from master Book table vs details Book table in one row.Master book table link with detail table field bookID and their status field on the book details have 1=in,2=out,3=lost,4=damage,5=removed .I tried to use group by method but got sum their status of same title different rows
Please need you help
thank in advanced
Post #410817
Posted Monday, October 15, 2007 7:49 AM


SSCertifiable

SSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiable

Group: General Forum Members
Last Login: 2 days ago @ 8:09 AM
Points: 6,735, Visits: 8,495
Maybe this gets you started...

Select M.BookId, D.[status], count(*) as NumberOfOccurrences
from [master Book table] M
inner join [details Book table] D
on M.bookID = D.BookId
Group by M.BookId, D.Status


Johan


Don't drive faster than your guardian angel can fly ...
but keeping both feet on the ground won't get you anywhere

- How to post Performance Problems
- How to post data/code to get the best help


- How to prevent a sore throat after hours of presenting ppt ?


"press F1 for solution", "press shift+F1 for urgent solution"


Need a bit of Powershell? How about this

Who am I ? Sometimes this is me but most of the time this is me
Post #410820
Posted Monday, October 15, 2007 8:07 AM
Forum Newbie

Forum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum Newbie

Group: General Forum Members
Last Login: Thursday, July 8, 2010 9:10 AM
Points: 5, Visits: 69
thank for you answer
but I hope you
did not get me well I want to get sum of status Book i.e In book,out Book Lost book,Damage and Removed as table fields for each title book
thanks in advance
Post #410843
Posted Monday, October 15, 2007 9:03 AM


SSCertifiable

SSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiable

Group: General Forum Members
Last Login: Today @ 11:35 AM
Points: 7,112, Visits: 15,489
Sounds like you might be looking for a "pivot query" solution. Something like:

Select M.BookId, M.booktitle,
sum(case when d.status=1 then 1 else 0 end) as TotalIn,
sum(case when d.status=2 then 1 else 0 end) as TotalOut,
sum(case when d.status=3 then 1 else 0 end) as TotalLost,
sum(case when d.status=4 then 1 else 0 end) as TotalDamaged,
sum(case when d.status=5 then 1 else 0 end) as TotalRemoved

from [master Book table] M
inner join [details Book table] D
on M.bookID = D.BookId
Group by M.BookId, M.booktitle


----------------------------------------------------------------------------------
Your lack of planning does not constitute an emergency on my part...unless you're my manager...or a director and above...or a really loud-spoken end-user..All right - what was my emergency again?
Post #410874
Posted Tuesday, October 16, 2007 12:06 AM
Forum Newbie

Forum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum Newbie

Group: General Forum Members
Last Login: Thursday, July 8, 2010 9:10 AM
Points: 5, Visits: 69
thanks a lot its work
Post #411127
« Prev Topic | Next Topic »

Add to briefcase

Permissions Expand / Collapse