2014-03-03 41 views
1

真的只是從更有經驗的SQL用戶或您認爲可行的解決方案尋找指導。卡在SQL Pickle中

我需要顯示顧問的名單,並在他們的名字旁邊,我需要顯示3總計。事情是......這些總數需要將他們的名字作爲變量傳入以計算出他/她的金額。

DECLARE @lastMonthDate VARCHAR(10) 
DECLARE @currentMonthDate VARCHAR(10) 
DECLARE @Year VARCHAR(10) 
DECLARE @TotalExpected VARCHAR (MAX) 
DECLARE @TotalPaid VARCHAR (MAX) 
DECLARE @TotalUnpaid VARCHAR (MAX) 

SET @lastMonthDate = CONVERT(VARCHAR(4), DATEPART(YEAR, DATEADD(MONTH, -1, GETDATE())))+'-'+CONVERT(VARCHAR(2), DATEPART(MONTH, DATEADD(MONTH, -1, GETDATE())))+'-21' 
SET @currentMonthDate = CONVERT(VARCHAR(4), DATEPART(YEAR, GETDATE()))+'-'+CONVERT(VARCHAR(2), DATEPART(MONTH, GETDATE()))+'-20' 
SET @Year = DATEPART(YEAR, GETDATE()) 

SELECT @TotalExpected = SUM(CONVERT(DECIMAL(10,2), budg_do1_total)) 
FROM Company 
JOIN Opportunity on Company.Comp_CompanyId = Opportunity.Oppo_PrimaryCompanyId 
JOIN Budget on Opportunity.Oppo_OpportunityId = Budget.budg_OpportunityId 
JOIN Users on company.Comp_PrimaryUserId = User_UserId 
Join Channel on User_PrimaryChannelId = Channel.Chan_ChannelId 
WHERE CONVERT(DATE, budg_tempDODate1) BETWEEN CONVERT(DATE, @lastMonthDate) AND CONVERT(DATE, @currentMonthDate) 
    AND User_FirstName = 'Geoffrey' AND User_LastName = 'Wylde' 

其他總計被以類似的方式制定出來,但我想保持這種儘可能短,所以我離開了出來。你會注意到,在@TotalExpected結尾我手動添加了我的顧問的名字和姓氏,但我想動態地做到這一點如何......這就是爲什麼我來找你,因爲我沒有甚至知道從哪裏開始。

說我給你邁克爾,凱文·約翰,金...

我怎麼能夠顯示這樣的事情:

+---------+----------+ 
| Name | Expected | 
+---------+----------+ 
| Michael |  124 | 
| Kevin |  246 | 
| John |  241 | 
| Kim  |  233 | 
+---------+----------+ 

(廣東話得到這個表來正確顯示在這裏)

請讓我知道這是否有意義?

回答

1

你是相當接近上面的SQL(當標籤還只是<SQL>這種反應是書面)。您需要將用戶名添加到SELECT語句,而不是在WHERE子句,然後使用GROUP BY,就像這樣:

SELECT User_FirstName + ' ' + UserLastName, SUM(CONVERT(DECIMAL(10,2), budg_do1_total)) 
FROM Company 
    JOIN Opportunity on Company.Comp_CompanyId = Opportunity.Oppo_PrimaryCompanyId 
    JOIN Budget on Opportunity.Oppo_OpportunityId = Budget.budg_OpportunityId 
    JOIN Users on company.Comp_PrimaryUserId = User_UserId 
    Join Channel on User_PrimaryChannelId = Channel.Chan_ChannelId WHERE CONVERT(DATE, budg_tempDODate1) BETWEEN CONVERT(DATE, @lastMonthDate) AND CONVERT(DATE, @currentMonthDate) 
    GROUP BY User_FirstName + ' ' + UserLastName 

理想情況下,你也將GROUP BY上的用戶ID,而不是第一個名字和姓氏連接在一起。希望這可以幫助!

+0

非常感謝你,約瑟夫,這沒有伎倆:)沒有得到我所有的顧問,但這可能是因爲組織中的其他原因。再次感謝xD – Mike

0

爲了計算總數,並且假設您只能使用標準SQL而不是某種特殊用途的特定於供應商的聚合工具,則必須使用SUM(...)... GROUP BY Name;

您需要三個這樣的表達式來創建三個總計,並且您需要三向連接來創建三個總計並排的單個表(並且要記住單個表不會是保證在他們所有的名字)。

1

,如果你只是刪除限制

AND User_FirstName = 'Geoffrey' AND User_LastName = 'Wylde' 

那麼就應該計算總爲表中的每個用戶(就是你想要的東西,或者你需要限制到一個子集?)

它應該有可能你的3個select語句沿着線合併成一個單一的一個,東西:

select Users.FirstName, Users.SecondName, Total1 = SUM(...), Total2 = SUM(...), Total3 = SUM(...) 
from ... 
join ... 
group by Users.Id 
+0

謝謝格雷厄姆,我想我需要走你所有3條選擇語句的路線。不能完全弄清楚,但生病繼續給它一個bash :)謝謝。 – Mike

0
DECLARE @lastMonthDate VARCHAR(10) 
DECLARE @currentMonthDate VARCHAR(10) 
DECLARE @Year VARCHAR(10) 
DECLARE @TotalExpected VARCHAR (MAX) 
DECLARE @TotalPaid VARCHAR (MAX) 
DECLARE @TotalUnpaid VARCHAR (MAX) 

SET @lastMonthDate = CONVERT(VARCHAR(4), DATEPART(YEAR, DATEADD(MONTH, -1, GETDATE())))+'-'+CONVERT(VARCHAR(2), DATEPART(MONTH, DATEADD(MONTH, -1, GETDATE())))+'-21' 
SET @currentMonthDate = CONVERT(VARCHAR(4), DATEPART(YEAR, GETDATE()))+'-'+CONVERT(VARCHAR(2), DATEPART(MONTH, GETDATE()))+'-20' 
SET @Year = DATEPART(YEAR, GETDATE()) 



**Declare @tbl table 
(
    names nvarchar (50) 
) 
insert into @tbl 
    select 'Michael' 
    Union All 
    select 'Kevin' 
    Union All 
    select 'John'** 

SELECT **Users.consultant**,SUM(CONVERT(DECIMAL(10,2), budg_do1_total)),SUM(CONVERT(DECIMAL(10,2), budg_do2_total)) 
    FROM Company 
JOIN Opportunity on Company.Comp_CompanyId = Opportunity.Oppo_PrimaryCompanyId 
JOIN Budget on Opportunity.Oppo_OpportunityId = Budget.budg_OpportunityId 
JOIN Users on company.Comp_PrimaryUserId = User_UserId 
Join Channel on User_PrimaryChannelId = Channel.Chan_ChannelId 
WHERE CONVERT(DATE, budg_tempDODate1) BETWEEN CONVERT(DATE, @lastMonthDate) AND CONVERT(DATE, @currentMonthDate) 
    **AND User_FirstName in(select names from @tbl) 
    group by Users.consultant** 

我假設你的用戶表包含顧問字段