脚本如何删除扩展并放入单独的列

时间:2016-12-07 19:01:10

标签: mysql sql sql-server tsql

我需要获取列文件名,然后在'。'之后创建一个名为extension的新列。这需要是一个名为extensions的视图。

此脚本适用于 ACCESS ,但似乎没有 - InStrRev

SELECT [UserProfile12_7-512-1mb].FileName,
       Right([Filename], Len([Filename])-InStrRev([Filename],".")) AS Extension,
       [UserProfile12_7-512-1mb].ComputerName,
       [UserProfile12_7-512-1mb].KB
FROM [UserProfile12_7-512-1mb];

然后我需要使用New View扩展创建另一个脚本,并通过最后一个扩展连接另一个表和组。 在ACCESS中,脚本是这样的:

SELECT ExtensionSheet.Extension, Sum([UserProfile12_7-512-1mb].KB) AS SumOfKB
FROM [UserProfile12_7-512-1mb]
INNER JOIN ExtensionSheet ON [UserProfile12_7-512-1mb].ComputerName = ExtensionSheet.ComputerName
GROUP BY ExtensionSheet.Extension;

以下是表格的屏幕截图:

Table2 Table1

1 个答案:

答案 0 :(得分:0)

您可以使用此表达式。

RIGHT(t.[FileName],IIF(CHARINDEX('.',REVERSE(t.[FileName]))>0,CHARINDEX('.',REVERSE(t.[FileName]))-1,0 ))

整个查询可以是

SELECT t.FileName, 
    RIGHT(t.[FileName],IIF(CHARINDEX('.',REVERSE(t.[FileName]))>0,CHARINDEX('.',REVERSE(t.[FileName]))-1,0 )) AS Extension,
    t.ComputerName,
    t.KB 
FROM [UserProfile12_7-512-1mb] t;

我不知道你是如何创建视图的,但这会给你带来结果。

WITH ExtensionSheet AS (
    SELECT t.FileName, 
        RIGHT(t.[FileName],IIF(CHARINDEX('.',REVERSE(t.[FileName]))>0,CHARINDEX('.',REVERSE(t.[FileName]))-1,0 )) AS Extension
        t.ComputerName,
        t.KB 
    FROM [UserProfile12_7-512-1mb] t
)
SELECT
    e.Extension
    , SUM(t1.KB) AS SUMOfKB
FROM
    ExtensionSheet e
    JOIN [UserProfile12_7-512-1mb] t1 ON e.Extension=RIGHT(t1.[FileName],IIF(CHARINDEX('.',REVERSE(t1.[FileName]))>0,CHARINDEX('.',REVERSE(t1.[FileName]))-1,0 ))
GROUP BY
    e.Extension;

或者这个(如果你已经创建了视图)。

SELECT
    e.Extension
    , SUM(t1.KB) AS SUMOfKB
FROM
    ExtensionSheet e
    JOIN [UserProfile12_7-512-1mb] t1 ON e.Extension=RIGHT(t1.[FileName],IIF(CHARINDEX('.',REVERSE(t1.[FileName]))>0,CHARINDEX('.',REVERSE(t1.[FileName]))-1,0 ))
GROUP BY
    e.Extension;