2016-01-28 34 views
0

可有人請引導我在Azure的數據湖書寫SQL爲U-SQL查詢

select tt.userId, count(tt.userId) from (SELECT userId,count(userId) as cou 
    FROM [dbo].[users] 

    where createdTime> DATEADD(wk,-1,GETDATE()) 

    group by userId,DATEPART(minute,createdTime)/5) tt group by tt.userId 

使用我不覺得以U-SQL中DATEPART函數U-SQL語言寫這篇以下SQL。 Azure數據分析作業給我錯誤。

回答

2

U-SQL不提供除少數(如LIKE)之外的T-SQL內部函數。請參閱https://msdn.microsoft.com/en-us/library/azure/mt621343.aspx以獲取列表。

那麼你如何做DateTime操作?您只需使用C#函數和方法!

所以DATEADD(wk, -1, GETDATE())是像DateTime.Now.AddDays(-7)DATEPART(minute,createdTime)/5(有你行額外))有點像createdTime.Minute/5(也許你需要,如果你想非整數值將其轉換爲雙)。

+1

這個很完美。謝謝邁克爾 –

2

對於任何正在尋找邁克爾提到的實現的人。就像下面那樣

@records = 
    EXTRACT userId string,      
      createdTime DateTime    
    FROM "/datalake/input/data.tsv" 
    USING Extractors.Tsv(); 

@result = 
    SELECT 
     userId,  
     COUNT(createdTime) AS userCount 
    FROM @records 
    WHERE createdTime > DateTime.Now.AddDays(-30) 
GROUP BY userId,createdTime.Minute/5; 

@result2= SELECT userId,COUNT(userId) AS TotalCount 
FROM @result 
GROUP BY userId; 

OUTPUT @result2 
    TO "/datalake/output/data.csv" 
USING Outputters.Csv();