SQL Clone
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


Group by select problem


Group by select problem

Author
Message
bsasiju
bsasiju
Grasshopper
Grasshopper (11 reputation)Grasshopper (11 reputation)Grasshopper (11 reputation)Grasshopper (11 reputation)Grasshopper (11 reputation)Grasshopper (11 reputation)Grasshopper (11 reputation)Grasshopper (11 reputation)

Group: General Forum Members
Points: 11 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
ALZDBA
ALZDBA
SSC-Dedicated
SSC-Dedicated (30K reputation)SSC-Dedicated (30K reputation)SSC-Dedicated (30K reputation)SSC-Dedicated (30K reputation)SSC-Dedicated (30K reputation)SSC-Dedicated (30K reputation)SSC-Dedicated (30K reputation)SSC-Dedicated (30K reputation)

Group: General Forum Members
Points: 30057 Visits: 8986
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


Dont drive faster than your guardian angel can fly ...
but keeping both feet on the ground wont get you anywhere w00t

- 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 :-D


Need a bit of Powershell? How about this

Who am I ? Sometimes this is me Alien but most of the time this is me Hehe
bsasiju
bsasiju
Grasshopper
Grasshopper (11 reputation)Grasshopper (11 reputation)Grasshopper (11 reputation)Grasshopper (11 reputation)Grasshopper (11 reputation)Grasshopper (11 reputation)Grasshopper (11 reputation)Grasshopper (11 reputation)

Group: General Forum Members
Points: 11 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
Matt Miller (4)
Matt Miller (4)
One Orange Chip
One Orange Chip (29K reputation)One Orange Chip (29K reputation)One Orange Chip (29K reputation)One Orange Chip (29K reputation)One Orange Chip (29K reputation)One Orange Chip (29K reputation)One Orange Chip (29K reputation)One Orange Chip (29K reputation)

Group: General Forum Members
Points: 29677 Visits: 19006
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?
bsasiju
bsasiju
Grasshopper
Grasshopper (11 reputation)Grasshopper (11 reputation)Grasshopper (11 reputation)Grasshopper (11 reputation)Grasshopper (11 reputation)Grasshopper (11 reputation)Grasshopper (11 reputation)Grasshopper (11 reputation)

Group: General Forum Members
Points: 11 Visits: 69
thanks a lot its work
stevennjuki
stevennjuki
Grasshopper
Grasshopper (16 reputation)Grasshopper (16 reputation)Grasshopper (16 reputation)Grasshopper (16 reputation)Grasshopper (16 reputation)Grasshopper (16 reputation)Grasshopper (16 reputation)Grasshopper (16 reputation)

Group: General Forum Members
Points: 16 Visits: 47
Hello,

I have a similar problem. The difference is the number of sums returned is variable, i.e. it is determined by an input parameter.

How do I code this?
pietlinden
pietlinden
SSChampion
SSChampion (14K reputation)SSChampion (14K reputation)SSChampion (14K reputation)SSChampion (14K reputation)SSChampion (14K reputation)SSChampion (14K reputation)SSChampion (14K reputation)SSChampion (14K reputation)

Group: General Forum Members
Points: 14163 Visits: 14135
Could you post this in a new thread and post your table structure and some sample data? hard to tell without some details.
Go


Permissions

You can't post new topics.
You can't post topic replies.
You can't post new polls.
You can't post replies to polls.
You can't edit your own topics.
You can't delete your own topics.
You can't edit other topics.
You can't delete other topics.
You can't edit your own posts.
You can't edit other posts.
You can't delete your own posts.
You can't delete other posts.
You can't post events.
You can't edit your own events.
You can't edit other events.
You can't delete your own events.
You can't delete other events.
You can't send private messages.
You can't send emails.
You can read topics.
You can't vote in polls.
You can't upload attachments.
You can download attachments.
You can't post HTML code.
You can't edit HTML code.
You can't post IFCode.
You can't post JavaScript.
You can post emoticons.
You can't post or upload images.

Select a forum

































































































































































SQLServerCentral


Search