0

My vendor sent over a piece of SQL code that I'm having a hell of a time getting to function with my existing query.

I have a webpage that is querying a database table for multiple values here's the code I have in the ASP page that queries SQL:

SELECT [Account],
[AccountName],
[ComputerName],
[EMail],
[Telephone],
[Status],
cast(cast([StartDate] as float) as datetime) as 'Start-Date',
cast(cast([LastBackupDate] as float) as datetime) as 'LastBackup-Date',
[TotalFileSize],
[TotalFileCount] 
FROM ExpandedView WHERE loginid = @0

This query works great, but the "Account" field specified above apparently returns only part of the actual account number. Apparently my vendor uses a computation to create the final account number and they've sent it to me:

use <databasenamehere>
select left(
convert(nvarchar,E.Account)
,5)+'-'+convert(nvarchar,
( 50 +((E.Account)%10)
-((E.Account/10)%10)
+((E.Account/100)%10)
-((E.Account/1000)%10)
+((E.Account/10000)%10)
-((E.Account/100000)%10)
+((E.Account/1000000)%10)
-((E.Account/10000000)%10)
+((E.Account/100000000)%10))
%10 )
+right(convert(nvarchar,E.Account),4) 
as 'Account', AccountName from ExpandView E
Inner Join Customer C
on E.Account=C.Account

Now I realize that that this query is selecting the first 5 numbers of the account number, appending the hyphen and then performing a mathematical procedure to the last 4 numbers, however I cannot figure out how to read this. Could I simply append these two statements together with an "AND" clause?

I really would like to understand what the "E.Account" refers to and why they use a "ExpandView E" and "Customer C" in the query. I'm also kind of lost how those relate to the actual tables.

Any assistance would be appreciated. Or if you prefer, post a link and I'll read up as best I can.

Thanks!

4

2 回答 2

0

这应该有效:

SELECT left(
convert(nvarchar,E.Account)
,5)+'-'+convert(nvarchar,
( 50 +((E.Account)%10)
-((E.Account/10)%10)
+((E.Account/100)%10)
-((E.Account/1000)%10)
+((E.Account/10000)%10)
-((E.Account/100000)%10)
+((E.Account/1000000)%10)
-((E.Account/10000000)%10)
+((E.Account/100000000)%10))
%10 )
+right(convert(nvarchar,E.Account),4) 
as 'Account',
[AccountName],
[ComputerName],
[EMail],
[Telephone],
[Status],
cast(cast([StartDate] as float) as datetime) as 'Start-Date',
cast(cast([LastBackupDate] as float) as datetime) as 'LastBackup-Date',
[TotalFileSize],
[TotalFileCount] 
FROM ExpandedView E WHERE loginid = @0
于 2013-01-25T00:07:35.797 回答
0

这是我发现似乎有效的查询:

select left
(convert(nvarchar,E.Account)
,5)+'-'+ convert(nvarchar,
( 50 +((E.Account)%10)
-((E.Account/10)%10)
+((E.Account/100)%10)
-((E.Account/1000)%10)
+((E.Account/10000)%10)
-((E.Account/100000)%10)
+((E.Account/1000000)%10)
-((E.Account/10000000)%10)
+((E.Account/100000000)%10))%10 )
+right(convert(nvarchar,E.Account),4)
as 'Account',
 E.AccountName,
 E.ComputerName,
 E.regloginid,
 E.EMail,
 E.Telephone,
 E.Community,
 E.Status,
 CAST(cast(e.startdate as float) as datetime) as 'Start-Date', 
 CAST(cast(e.lastbackupdate as float) as datetime) as 'LastBackup-Date',
 E.TotalFileSize,
 E.TotalFileCount
 from ExpandedCustomerView E Where E.Status = 'A' and E.regloginid = @0 

似乎他们使用的查询重命名了数据库表,以 E.xxxxx 开头我不确定这是如何工作的。我对 MSSQL 不是很熟悉,无法理解他们为什么在选择中调用参数的重命名。但是,此查询可以正常工作并提供我需要的数据。

也许如果有人可以回答为什么他们会在“FROM EXPANDEDVIEW E”部分使用参数?这似乎是重命名列的地方。

无论如何,我很想知道,但这绝对对我有用,所以我要关闭它。谢谢大家的回答/评论和帮助。

于 2013-01-29T02:10:22.400 回答