2014-01-21 215 views
-1

原文:轉換SQL查詢/子查詢LINQ

select *, (PlanBasedOnDataUseage - CurrentPlanPricePerMonth) as MonthlyOverageInvoiceAmount from  

(  
Select * ,  

(select top 1 PricePerMonth from accounttypes where IsDeleted = 0 and AvailableOnline = 1  
and RecordsPerMonth > 0) as PlanBasedOnDataUseage  
,  
(select top 1 AccountTypeName from accounttypes where IsDeleted = 0 and AvailableOnline = 1  
and RecordsPerMonth > 0) as SuggestPlanBasedOnDataUsage  
from  
(  
select  
ClientID, ClientName, ClientEmail, AccountOwnerName,  
AccountExpiryDate,  
DATEADD(day, DAY(AccountExpiryDate)-1,  
CAST(  
    cast(YEar(GETDATE())as nvarchar(4)) +  
    replace(str(cast(Month(GETDATE())as nvarchar(2)), 2), ' ', '0') + '01'  
    as datetime)  
) as MonthlyRollOverDate  
, AT.RecordsPerMonth as CurrentPlanMentionsMaxPerMonth  
    , AT.AccountTypeName as CurrentPlanAccountType  
, AT.PricePerMonth as CurrentPlanPricePerMonth  
from clients C  
inner join AccountTypes AT on C.AccountTypeID = AT.AccountTypeID  
LEFT OUTER JOIN AccountOwner AO on AO.AccountOwnerID = C.AccountOwnerID 
WHERE  
AT.AccountTypeName not in ('Standard', 'Demo', 'Trial') and  
AccountExpiryDate > getdate()  
    AND  
    (  
    DATEADD(day, DAY(AccountExpiryDate)-1,  
    CAST(  
     cast(YEar(GETDATE())as nvarchar(4)) +  
     replace(str(cast(Month(GETDATE())as nvarchar(2)), 2), ' ', '0') + '01'  
     as datetime)  
    )  

    >=   
    --Monday Last Week  
    dateadd(d, -7, DATEADD(wk, DATEDIFF(wk, 0, GETDATE()), 0))  
    AND  
    --Calc MonthlyRollOverDate  
     DATEADD(day, DAY(AccountExpiryDate)-1,  
     CAST(  
     cast(YEar(GETDATE())as nvarchar(4)) +  
     replace(str(cast(Month(GETDATE())as nvarchar(2)), 2), ' ', '0') + '01'  
     as datetime)  
     )  
    <=  
    -- THis Monthday  
    DATEADD(wk, DATEDIFF(wk, 0, GETDATE()), 0)  



)  
) as ClientsRollingOverThisWeek  

) as ClientsRollingOverThisWeekWithRealPlanAttached  

where (PlanBasedOnDataUseage - CurrentPlanPricePerMonth) > 0  
+0

嘗試http://www.sqltolinq.com/ – Nilesh

+0

至少在請求我們爲你做你的工作之前嘗試自己做... http://whathaveyoutried.com/ – Basic

+3

http://bit.ly/1bMcoe0 – Smartis

回答

-1

您可以使用Linqpad顯示LINQ查詢的SQL輸出。

+0

我嘗試使用LinqPad,但無法將sql轉換爲linq –

+0

@Sourav是正確的。 Linqpad不會將SQL轉換爲LINQ。但是,它確實顯示了LINQ查詢的SQL輸出。 – webworm