Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

how do i get the sum of this?

Posted on 2006-04-10
4
Medium Priority
?
181 Views
Last Modified: 2012-08-13
here's my query

SELECT DISTINCT tbl_filedetails.vfilepathclient, tbl_filedetails.iactualsize
FROM         tbl_filedetails INNER JOIN
                      tbl_userdetails ON tbl_filedetails.vuserid = tbl_userdetails.vuserid
WHERE     (tbl_userdetails.vuname = 'april10test') AND (NOT (tbl_filedetails.vbkpk IN
                          (SELECT     vbkpk
                            FROM          tbl_deletedfiledetails
                            WHERE      vuserid = tbl_userdetails.vuserid)))

How do i modify this so i get the sum of all the iactualsize records this returns?
0
Comment
Question by:kenshaw
4 Comments
 
LVL 93

Expert Comment

by:Patrick Matthews
ID: 16423329
Hi kenshaw,

SELECT tbl_filedetails.vfilepathclient, SUM(tbl_filedetails.iactualsize) AS SumOfSize
FROM         tbl_filedetails INNER JOIN
                      tbl_userdetails ON tbl_filedetails.vuserid = tbl_userdetails.vuserid
WHERE     (tbl_userdetails.vuname = 'april10test') AND (NOT (tbl_filedetails.vbkpk IN
                          (SELECT     vbkpk
                            FROM          tbl_deletedfiledetails
                            WHERE      vuserid = tbl_userdetails.vuserid)))
GROUP BY tbl_filedetails.vfilepathclient

Regards,

Patrick
0
 
LVL 28

Expert Comment

by:imran_fast
ID: 16424101
SELECT  tbl_filedetails.vfilepathclient, sum(distinct tbl_filedetails.iactualsize)
FROM         tbl_filedetails INNER JOIN
                      tbl_userdetails ON tbl_filedetails.vuserid = tbl_userdetails.vuserid
WHERE     (tbl_userdetails.vuname = 'april10test') AND (NOT (tbl_filedetails.vbkpk IN
                          (SELECT     vbkpk
                            FROM          tbl_deletedfiledetails
                            WHERE      vuserid = tbl_userdetails.vuserid)))
group by tbl_filedetails.vfilepathclient
0
 
LVL 50

Expert Comment

by:Lowfatspread
ID: 16424290
if you have multiple filedetails for a user in a filepath....
how do you want the filedetails to be selected ?

do you have the last accessed date or something similar?

e.g.

select f.vfilepathclient,sum(f.iactualsize)
  from tbl_fieldetails as f
 inner join tbluserdetails as u
     on u.vuserid=f.vuserid
 where u.vuname='april10test'
     and not exists (select vbkpk
                              from tbl_deletedfiledetails as x
                             where x.vuserid=u.vuserid)
    and f.dateaccessed = (select max(y.dateaccessed)
                                        from tbl_fieldetails as y
                                       where y.vfilepathclient=f.vfilepathclient
                                           and y.vuserrid=u.vuserid
                                    )

note exists syntax can perform better than in syntax


hth  
0
 
LVL 42

Accepted Solution

by:
dqmq earned 2000 total points
ID: 16424383
This gives the sum along with the detail, but in a separate column:


SELECT DISTINCT tbl_filedetails.vfilepathclient, tbl_filedetails.iactualsize,  sum.iactualsizesum as total
FROM         tbl_filedetails
 INNER JOIN   tbl_userdetails ON tbl_filedetails.vuserid = tbl_userdetails.vuserid
 INNER JOIN                      
(
SELECT dis.vfilepathclient, sum(dis.iactualsize) as iactualsizesum
(
SELECT  distinct f.vfilepathclient, f.iactualsize
FROM         tbl_filedetails as f INNER JOIN
                      tbl_userdetails as u ON f.vuserid = u.vuserid
WHERE     (u.vuname = 'april10test') AND (NOT (f.vbkpk IN
                          (SELECT     d.vbkpk
                            FROM          tbl_deletedfiledetails as d
                            WHERE      d.vuserid = u.vuserid)))
group by f.vfilepathclient
) as dis
) as sum
WHERE     (tbl_userdetails.vuname = 'april10test') AND (NOT (tbl_filedetails.vbkpk IN
                          (SELECT     vbkpk
                            FROM          tbl_deletedfiledetails
                            WHERE      vuserid = tbl_userdetails.vuserid)))
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Why is this different from all of the other step by step guides?  Because I make a living as a DBA and not as a writer and I lived through this experience. Defining the name: When I talk to people they say different names on this subject stuff l…
Recently we ran in to an issue while running some SQL jobs where we were trying to process the cubes.  We got an error saying failure stating 'NT SERVICE\SQLSERVERAGENT does not have access to Analysis Services. So this is a way to automate that wit…
Via a live example, show how to shrink a transaction log file down to a reasonable size.
Viewers will learn how to use the SELECT statement in SQL and will be exposed to the many uses the SELECT statement has.
Suggested Courses

810 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question