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.

 All Forums
 SQL Server 2005 Forums
 Transact-SQL (2005)
 Tricky partitioning & grouping data and Logic

Author  Topic 

modi123p
Starting Member

1 Post

Posted - 2011-01-08 : 11:45:14
Hi,

Im new to SQL Server programming and have stumped across a hurdle with a complicated query to be designed.

I have a view.. which is nothing more than a join of 8 tables.

I need to create a SP (or update View to include 3 additional columns that are computed sections)

Data in View is as follows:
User -- MemberID -- ClubID -- FamilyID -- Mem.Amnt -- AdditionalAmnt
U1 ------ A1234 -------- C1 ------ F1 -------- 100 ---------- 10
U1 ------ A1234 -------- C7 ------ F2 -------- 150 ---------- 10
U1 ------ A2233 -------- C2 ------ F3 -------- 150 ---------- --

U2 ------ A1244 -------- C2 ------ F4 -------- 200 ---------- 20
U2 ------ A1245 -------- C3 ------ F5 -------- 200 ---------- --
U2 ------ A1246 -------- C4 ------ F6 -------- 200 ---------- 10

U3 ------ A1251 -------- C5 ------ F1 -------- 100 ---------- --
U3 ------ A1252 -------- C6 ------ F1 -------- 100 ---------- --


Now For every user,
1. I need to have a TotalAdditionalContribution column that has the sum of Mem.Amnt having AdditionalAmnt (for U1 this would be 250 and for (U2,A1244) --> 200)grouped by MembershipID for User

2. I need to have a TotalContributors coulmn with number of FamilyIds for every MemberID assigned for User (For (U1,A1234) it would be 2 and for (U1, A2233)=0

3. A giftValue column computed on TotalAdditionalContribution
For TotalContribution (001 - 100) -- 01
For TotalContribution (101 - 300) -- 05
For TotalContribution (301 - 500) -- 10
For TotalContribution > 501 -- 20

dataguru1971
Master Smack Fu Yak Hacker

1464 Posts

Posted - 2011-01-08 : 12:10:49
Not sure really..but something using CTE's would work.



Create table #foo
(
[User] char(2),
MemberID char(5),
ClubID char(2),
FamilyID char(2),
MemAmnt money,
AdditionalAmnt money NULL)


INSERT INTO #foo

SELECT 'U1', 'A1234', 'C1', 'F1' ,100, 10 UNION
SELECT 'U1', 'A1234', 'C7', 'F2' ,150, 10 UNION
SELECT 'U1', 'A2233', 'C2', 'F3' ,150, NULL UNION

SELECT 'U2', 'A1244', 'C2', 'F4' ,200, 20 UNION
SELECT 'U2', 'A1245', 'C3', 'F5' ,200, NULL UNION
SELECT 'U2', 'A1246', 'C4', 'F6' ,100, 10 UNION

SELECT 'U3', 'A1251', 'C5', 'F1' ,100, NULL UNION
SELECT 'U3', 'A1252', 'C6', 'F1' ,100, NULL


;WITH t1 AS
(SELECT [USER],[MEMBERID],SUM(MemAmnt) as TotalContribution
FROM #foo
WHERE AdditionalAmnt >0
GROUP BY [USER],[MEMBERID])
, t2 AS
(SELECT [USER],COUNT(*) as NumMembers
FROM #foo
GROUP BY [USER])

SELECT #foo.*
,TotalContribution = COALESCE(t1.TotalContribution,0)
,TotalContributors =t2.NumMembers
,giftValue = CASE WHEN t1.TotalContribution between 1 and 100 then '01'
WHEN t1.TotalContribution between 101 and 300 then '03'
WHEN t1.TotalContribution between 301 and 500 then '10'
WHEn t1.TotalContribution> 500 then '20' ELSE NULL END
FROM #foo LEFT JOIN t1 on #foo.[USER] = t1.[USER] and #foo.MemberID = t1.MemberID
LEFT JOIN t2 on #foo.[USER] = t2.[USER]

Drop Table #foo




Poor planning on your part does not constitute an emergency on my part.

Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2011-01-08 : 13:55:42
somethink like:-


SELECT *,
CASE WHEN TotalAdditionalContribution BETWEEN 1 AND 100 THEN '01'
WHEN TotalAdditionalContribution BETWEEN 101 AND 300 THEN '05'
WHEN TotalAdditionalContribution BETWEEN 301 AND 500 THEN '10'
ELSE '20'
END
FROM
(
SELECT *,
SUM(CASE WHEN AdditionalAmnt > 0 THEN [Mem.Amnt] ELSE 0 END) OVER (PARTITION BY User,MemberID) AS TotalAdditionalContribution,
COUNT(CASE WHEN AdditionalAmnt > 0 THEN FamilyID ELSE 0 END) OVER (PARTITION BY User,MemberID) AS TotalContributors
FROM View
)t



------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/

Go to Top of Page
   

- Advertisement -