1

我从以下代码中获取了 sql server 数据库中的值,

SELECT [Zone Name]
      ,[Zone Count]
      ,[Phase Name]
  FROM [Interface].[dbo].[VwZoneCount]
  where [Zone Name] IN ('EB2GFNMZ','EB2GFSMZ','EB2GFNZ1','EB2GFSZ1','EB21FNZ1','EB21FSMZ','EB2IFSZ1','EB22FNZ1','EB22FSZ1','EB22FSMZ','EB23FNMZ','EB23FNZ1','EB23FNZ2','EB23FNZ3','EB23FSMZ','EB23FSZ1','EB23FSZ2','EB24FNMZ','EB24FNZ1','EB24FSMZ','EB24FSZ1','EB25FNMZ','EB25FNZ1','EB25FSMZ','EB25FSZ1','EB26FNMZ','EB26FNZ1','EB26FSMZ','EB26FSZ1','EB27FNZ1','EB27FSMZ')
GO

上述查询的输出是

Zone Name   Zone Count

EB24FNZ1    160
EB24FSMZ    10
EB24FSZ1    87
EB25FNMZ    82
EB25FNZ1    82
EB25FSMZ    12
EB25FSZ1    123
EB26FNMZ    4
EB26FNZ1    92
EB26FSMZ    23
EB26FSZ1    91
EB27FNZ1    1
EB27FSMZ    64
EB2GFNMZ    12
EB2GFNZ1    152
EB2GFSMZ    36
EB2GFSZ1    212

但我需要通过对一些行值求和来输出。我需要将具有“EB2GFN%”的值组合成一个具有不同名称的值,“EB21FN%”..类似地需要组合其他行。任何机构都可以建议我如何做到这一点。

期望的输出:-

Zone Name    Zone Count

    EB24F_NORTH_WING    160
    EB24F_SOUTH_WING    10+87
    EB25F_NORTH_WING    82+82
    EB25F_SOUTH_WING    12+123
    EB26F_NORTH_WING    4+92
    EB26F_SOUTH_WING    23+91
    EB27F_NORTH_WING    1
    EB27F_SOUTH_WING    64
    EB2GF_NORTH_WING    12+152
    EB2GF_SOUTH_WING    36+212
4

2 回答 2

0

试试这个,它将为您提供前 6 个共同字母的组的总和:

SELECT  LEFT(Zone_Prefix, 5) + CASE WHEN RIGHT(Zone_Prefix, 1) = 'N' THEN '_NORTH_WING' ELSE '_SOUTH_WING' END AS [Zone Name],
        Cnt AS [Zone Count]
FROM
(
    SELECT  LEFT([Zone Name], 6) AS Zone_Prefix
            ,SUM([Zone Count]) Cnt
    FROM    [Interface].[dbo].[VwZoneCount]
    WHERE   [Zone Name] IN ('EB2GFNMZ','EB2GFSMZ','EB2GFNZ1','EB2GFSZ1','EB21FNZ1','EB21FSMZ','EB2IFSZ1','EB22FNZ1','EB22FSZ1','EB22FSMZ','EB23FNMZ','EB23FNZ1','EB23FNZ2','EB23FNZ3','EB23FSMZ','EB23FSZ1','EB23FSZ2','EB24FNMZ','EB24FNZ1','EB24FSMZ','EB24FSZ1','EB25FNMZ','EB25FNZ1','EB25FSMZ','EB25FSZ1','EB26FNMZ','EB26FNZ1','EB26FSMZ','EB26FSZ1','EB27FNZ1','EB27FSMZ')
    GROUP BY 
            LEFT([Zone Name], 6)

) tbl

这是一个SQL 小提琴

于 2012-12-17T11:14:40.300 回答
0

你可以这样做:

;WITH AllZones
AS
(
  SELECT * FROM YourQuery
), WithGroupedZones
AS
(
  SELECT
    ZoneName,
    ZoneCount,
    LEFT(ZoneName, 2) Eb,
    SUBSTRING(ZoneName, 3, 1) EbNumber,
    SUBSTRING(ZoneName, 4, 3) F,
    SUBSTRING(ZoneName, 8, 1) FNumber
  FROM AllZones
) 
SELECT
  ZoneName,
  (SELECT SUM(t2.ZoneCount) 
   FROM WithGroupedZones t2
   WHERE t1.Eb = t2.Eb 
     AND t1.F = t2.F
     AND t1.EBNumber= t2.EBnumber
  ) ZonesCount
FROM WithGroupedZones t1;

SQL 小提琴演示

这会给你:

| ZONENAME | ZONESCOUNT |
-------------------------
| EB24FNZ1 |        160 |
| EB24FSMZ |         97 |
| EB24FSZ1 |         97 |
| EB25FNMZ |        164 |
| EB25FNZ1 |        164 |
| EB25FSMZ |        135 |
| EB25FSZ1 |        135 |
| EB26FNMZ |         96 |
| EB26FNZ1 |         96 |
| EB26FSMZ |        114 |
| EB26FSZ1 |        114 |
| EB27FNZ1 |          1 |
| EB27FSMZ |         64 |
| EB2GFNMZ |        164 |
| EB2GFNZ1 |        164 |
| EB2GFSMZ |        248 |
| EB2GFSZ1 |        248 |

请注意:这可能与您要查找的结果集不同。但是您可以修改条件,我在查询中使用:

     t1.Eb = t2.Eb 
 AND t1.F = t2.F
 AND t1.EBNumber= t2.EBnumber

得到你想要的输出。另请注意,区域的名称分为:

| ZONENAME | ZONECOUNT | EB | EBNUMBER |   F | FNUMBER |
--------------------------------------------------------
| EB24FNZ1 |       160 | EB |        2 | 4FN |       1 |
| EB24FSMZ |        10 | EB |        2 | 4FS |       Z |
| EB24FSZ1 |        87 | EB |        2 | 4FS |       1 |
| EB25FNMZ |        82 | EB |        2 | 5FN |       Z |
| EB25FNZ1 |        82 | EB |        2 | 5FN |       1 |
| EB25FSMZ |        12 | EB |        2 | 5FS |       Z |
| EB25FSZ1 |       123 | EB |        2 | 5FS |       1 |
| EB26FNMZ |         4 | EB |        2 | 6FN |       Z |
| EB26FNZ1 |        92 | EB |        2 | 6FN |       1 |
| EB26FSMZ |        23 | EB |        2 | 6FS |       Z |
| EB26FSZ1 |        91 | EB |        2 | 6FS |       1 |
| EB27FNZ1 |         1 | EB |        2 | 7FN |       1 |
| EB27FSMZ |        64 | EB |        2 | 7FS |       Z |
| EB2GFNMZ |        12 | EB |        2 | GFN |       Z |
| EB2GFNZ1 |       152 | EB |        2 | GFN |       1 |
| EB2GFSMZ |        36 | EB |        2 | GFS |       Z |
| EB2GFSZ1 |       212 | EB |        2 | GFS |       1 |

然后你可以比较ZoneName使用这些组EB, EBNUMBER, F, FNUMBER

于 2012-12-17T11:28:33.137 回答