sql中的中位数计算 - 没有结果

时间:2014-08-26 08:24:24

标签: sql-server sql-server-2008

我正在使用以下查询根据PACK_DESCREGION1_CD分区计算中值。

SELECT
    PACK_DESC,
    AVG(PRICE) AS MED_PRICE,
    REGION1_CD                          
FROM (
    SELECT
        PACK_DESC,
        REGION1_CD,
        PRICE,
        ROW_NUMBER() OVER (PARTITION BY CHECKSUM(PACK_DESC,REGION1_CD) ORDER BY PRICE ASC) AS ROWASC,
        ROW_NUMBER() OVER (PARTITION BY CHECKSUM(PACK_DESC,REGION1_CD) ORDER BY PRICE DESC) AS ROWDESC
    FROM prices
) y 
WHERE ROWASC IN (ROWDESC,ROWDESC-1,ROWDESC+1)
GROUP BY PACK_DESC,REGION1_CD

对于其中一个地区,我的ROWASCROWDESC值变化不一,因此我认为我无法获得中值。

下面是生成包含数据的表的脚本

/****** Object:  Table [dbo].[prices]    Script Date: 08/26/2014 13:34:47 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE TABLE [dbo].[prices](
    [REGION1_CD] [float] NULL,
    [REGION2_CD] [float] NULL,
    [REGION3_CD] [nvarchar](255) NULL,
    [PRICE] [float] NULL,
    [CURRENCY_DESC] [nvarchar](255) NULL,
    [PACK_CD] [float] NULL,
    [PACK_DESC] [nvarchar](255) NULL
) ON [PRIMARY]
GO
INSERT [dbo].[prices] ([REGION1_CD], [REGION2_CD], [REGION3_CD], [PRICE], [CURRENCY_DESC], [PACK_CD], [PACK_DESC]) VALUES (61, 88, N'NULL', 15.456592, N'EUR', 1285, N'abc')
INSERT [dbo].[prices] ([REGION1_CD], [REGION2_CD], [REGION3_CD], [PRICE], [CURRENCY_DESC], [PACK_CD], [PACK_DESC]) VALUES (61, 88, N'NULL', 8.481165, N'EUR', 1286, N'abc')
INSERT [dbo].[prices] ([REGION1_CD], [REGION2_CD], [REGION3_CD], [PRICE], [CURRENCY_DESC], [PACK_CD], [PACK_DESC]) VALUES (61, 79, N'NULL', 9.353736, N'EUR', 1287, N'abc')
INSERT [dbo].[prices] ([REGION1_CD], [REGION2_CD], [REGION3_CD], [PRICE], [CURRENCY_DESC], [PACK_CD], [PACK_DESC]) VALUES (72, 80, N'NULL', 4.92, N'EUR', 1288, N'abc')
INSERT [dbo].[prices] ([REGION1_CD], [REGION2_CD], [REGION3_CD], [PRICE], [CURRENCY_DESC], [PACK_CD], [PACK_DESC]) VALUES (75, 90, N'NULL', 6.02, N'EUR', 1289, N'abc')
INSERT [dbo].[prices] ([REGION1_CD], [REGION2_CD], [REGION3_CD], [PRICE], [CURRENCY_DESC], [PACK_CD], [PACK_DESC]) VALUES (75, 77, N'NULL', 4.97, N'EUR', 1290, N'abc')
INSERT [dbo].[prices] ([REGION1_CD], [REGION2_CD], [REGION3_CD], [PRICE], [CURRENCY_DESC], [PACK_CD], [PACK_DESC]) VALUES (61, 88, N'NULL', 26.908139, N'EUR', 1291, N'abc')
INSERT [dbo].[prices] ([REGION1_CD], [REGION2_CD], [REGION3_CD], [PRICE], [CURRENCY_DESC], [PACK_CD], [PACK_DESC]) VALUES (61, 88, N'NULL', 31.1323, N'EUR', 1291, N'abc')
INSERT [dbo].[prices] ([REGION1_CD], [REGION2_CD], [REGION3_CD], [PRICE], [CURRENCY_DESC], [PACK_CD], [PACK_DESC]) VALUES (61, 88, N'NULL', 32.066316, N'EUR', 1291, N'abc')
INSERT [dbo].[prices] ([REGION1_CD], [REGION2_CD], [REGION3_CD], [PRICE], [CURRENCY_DESC], [PACK_CD], [PACK_DESC]) VALUES (61, 88, N'NULL', 32.066316, N'EUR', 1291, N'abc')
INSERT [dbo].[prices] ([REGION1_CD], [REGION2_CD], [REGION3_CD], [PRICE], [CURRENCY_DESC], [PACK_CD], [PACK_DESC]) VALUES (61, 88, N'NULL', 32.066316, N'EUR', 1291, N'abc')
INSERT [dbo].[prices] ([REGION1_CD], [REGION2_CD], [REGION3_CD], [PRICE], [CURRENCY_DESC], [PACK_CD], [PACK_DESC]) VALUES (61, 88, N'NULL', 32.066316, N'EUR', 1291, N'abc')
INSERT [dbo].[prices] ([REGION1_CD], [REGION2_CD], [REGION3_CD], [PRICE], [CURRENCY_DESC], [PACK_CD], [PACK_DESC]) VALUES (61, 88, N'NULL', 32.066316, N'EUR', 1291, N'abc')
INSERT [dbo].[prices] ([REGION1_CD], [REGION2_CD], [REGION3_CD], [PRICE], [CURRENCY_DESC], [PACK_CD], [PACK_DESC]) VALUES (61, 88, N'NULL', 32.066316, N'EUR', 1291, N'abc')
INSERT [dbo].[prices] ([REGION1_CD], [REGION2_CD], [REGION3_CD], [PRICE], [CURRENCY_DESC], [PACK_CD], [PACK_DESC]) VALUES (61, 88, N'NULL', 33.028385, N'EUR', 1291, N'abc')
INSERT [dbo].[prices] ([REGION1_CD], [REGION2_CD], [REGION3_CD], [PRICE], [CURRENCY_DESC], [PACK_CD], [PACK_DESC]) VALUES (61, 88, N'NULL', 33.028385, N'EUR', 1291, N'abc')
INSERT [dbo].[prices] ([REGION1_CD], [REGION2_CD], [REGION3_CD], [PRICE], [CURRENCY_DESC], [PACK_CD], [PACK_DESC]) VALUES (61, 88, N'NULL', 33.028385, N'EUR', 1291, N'abc')
INSERT [dbo].[prices] ([REGION1_CD], [REGION2_CD], [REGION3_CD], [PRICE], [CURRENCY_DESC], [PACK_CD], [PACK_DESC]) VALUES (61, 88, N'NULL', 33.028385, N'EUR', 1291, N'abc')
INSERT [dbo].[prices] ([REGION1_CD], [REGION2_CD], [REGION3_CD], [PRICE], [CURRENCY_DESC], [PACK_CD], [PACK_DESC]) VALUES (61, 88, N'NULL', 33.028385, N'EUR', 1291, N'abc')
INSERT [dbo].[prices] ([REGION1_CD], [REGION2_CD], [REGION3_CD], [PRICE], [CURRENCY_DESC], [PACK_CD], [PACK_DESC]) VALUES (61, 88, N'NULL', 33.028385, N'EUR', 1291, N'abc')
INSERT [dbo].[prices] ([REGION1_CD], [REGION2_CD], [REGION3_CD], [PRICE], [CURRENCY_DESC], [PACK_CD], [PACK_DESC]) VALUES (75, 92, N'NULL', 1.95661, N'EUR', 1292, N'abc')
INSERT [dbo].[prices] ([REGION1_CD], [REGION2_CD], [REGION3_CD], [PRICE], [CURRENCY_DESC], [PACK_CD], [PACK_DESC]) VALUES (61, 88, N'NULL', 8.62848, N'EUR', 1293, N'abc')
INSERT [dbo].[prices] ([REGION1_CD], [REGION2_CD], [REGION3_CD], [PRICE], [CURRENCY_DESC], [PACK_CD], [PACK_DESC]) VALUES (61, 88, N'NULL', 6.624518, N'EUR', 1294, N'abc')
INSERT [dbo].[prices] ([REGION1_CD], [REGION2_CD], [REGION3_CD], [PRICE], [CURRENCY_DESC], [PACK_CD], [PACK_DESC]) VALUES (61, 88, N'NULL', 7.021574, N'EUR', 1294, N'abc')
INSERT [dbo].[prices] ([REGION1_CD], [REGION2_CD], [REGION3_CD], [PRICE], [CURRENCY_DESC], [PACK_CD], [PACK_DESC]) VALUES (75, 81, N'NULL', 5.52, N'EUR', 1295, N'abc')
INSERT [dbo].[prices] ([REGION1_CD], [REGION2_CD], [REGION3_CD], [PRICE], [CURRENCY_DESC], [PACK_CD], [PACK_DESC]) VALUES (75, 81, N'NULL', 5.52, N'EUR', 1295, N'abc')
INSERT [dbo].[prices] ([REGION1_CD], [REGION2_CD], [REGION3_CD], [PRICE], [CURRENCY_DESC], [PACK_CD], [PACK_DESC]) VALUES (75, 81, N'NULL', 7.26, N'EUR', 1296, N'abc')
INSERT [dbo].[prices] ([REGION1_CD], [REGION2_CD], [REGION3_CD], [PRICE], [CURRENCY_DESC], [PACK_CD], [PACK_DESC]) VALUES (75, 81, N'NULL', 7.26, N'EUR', 1296, N'abc')
INSERT [dbo].[prices] ([REGION1_CD], [REGION2_CD], [REGION3_CD], [PRICE], [CURRENCY_DESC], [PACK_CD], [PACK_DESC]) VALUES (75, 77, N'NULL', 2.52865, N'EUR', 1298, N'abc')
INSERT [dbo].[prices] ([REGION1_CD], [REGION2_CD], [REGION3_CD], [PRICE], [CURRENCY_DESC], [PACK_CD], [PACK_DESC]) VALUES (75, 77, N'NULL', 2.52865, N'EUR', 1298, N'abc')
INSERT [dbo].[prices] ([REGION1_CD], [REGION2_CD], [REGION3_CD], [PRICE], [CURRENCY_DESC], [PACK_CD], [PACK_DESC]) VALUES (75, 77, N'NULL', 2.52865, N'EUR', 1298, N'abc')
INSERT [dbo].[prices] ([REGION1_CD], [REGION2_CD], [REGION3_CD], [PRICE], [CURRENCY_DESC], [PACK_CD], [PACK_DESC]) VALUES (75, 90, N'NULL', 2.61619, N'EUR', 1299, N'abc')
INSERT [dbo].[prices] ([REGION1_CD], [REGION2_CD], [REGION3_CD], [PRICE], [CURRENCY_DESC], [PACK_CD], [PACK_DESC]) VALUES (60, 93, N'NULL', 8.63541, N'EUR', 1300, N'abc')
INSERT [dbo].[prices] ([REGION1_CD], [REGION2_CD], [REGION3_CD], [PRICE], [CURRENCY_DESC], [PACK_CD], [PACK_DESC]) VALUES (60, 93, N'NULL', 8.43024, N'EUR', 1301, N'abc')
INSERT [dbo].[prices] ([REGION1_CD], [REGION2_CD], [REGION3_CD], [PRICE], [CURRENCY_DESC], [PACK_CD], [PACK_DESC]) VALUES (60, 93, N'NULL', 14.06793, N'EUR', 1302, N'abc')
INSERT [dbo].[prices] ([REGION1_CD], [REGION2_CD], [REGION3_CD], [PRICE], [CURRENCY_DESC], [PACK_CD], [PACK_DESC]) VALUES (60, 93, N'NULL', 15.282, N'EUR', 1302, N'abc')
INSERT [dbo].[prices] ([REGION1_CD], [REGION2_CD], [REGION3_CD], [PRICE], [CURRENCY_DESC], [PACK_CD], [PACK_DESC]) VALUES (60, 86, N'NULL', 3.45121, N'EUR', 1303, N'abc')
INSERT [dbo].[prices] ([REGION1_CD], [REGION2_CD], [REGION3_CD], [PRICE], [CURRENCY_DESC], [PACK_CD], [PACK_DESC]) VALUES (60, 83, N'NULL', 10.991, N'EUR', 1304, N'abc')
INSERT [dbo].[prices] ([REGION1_CD], [REGION2_CD], [REGION3_CD], [PRICE], [CURRENCY_DESC], [PACK_CD], [PACK_DESC]) VALUES (60, 93, N'NULL', 14.21836, N'EUR', 1305, N'abc')
INSERT [dbo].[prices] ([REGION1_CD], [REGION2_CD], [REGION3_CD], [PRICE], [CURRENCY_DESC], [PACK_CD], [PACK_DESC]) VALUES (60, 83, N'NULL', 5.575551, N'EUR', 1306, N'abc')
INSERT [dbo].[prices] ([REGION1_CD], [REGION2_CD], [REGION3_CD], [PRICE], [CURRENCY_DESC], [PACK_CD], [PACK_DESC]) VALUES (61, 89, N'NULL', 12.81084, N'EUR', 1307, N'abc')
INSERT [dbo].[prices] ([REGION1_CD], [REGION2_CD], [REGION3_CD], [PRICE], [CURRENCY_DESC], [PACK_CD], [PACK_DESC]) VALUES (61, 88, N'NULL', 17.71856, N'EUR', 1308, N'abc')
INSERT [dbo].[prices] ([REGION1_CD], [REGION2_CD], [REGION3_CD], [PRICE], [CURRENCY_DESC], [PACK_CD], [PACK_DESC]) VALUES (61, 88, N'NULL', 39.838612, N'EUR', 1309, N'abc')
INSERT [dbo].[prices] ([REGION1_CD], [REGION2_CD], [REGION3_CD], [PRICE], [CURRENCY_DESC], [PACK_CD], [PACK_DESC]) VALUES (61, 88, N'NULL', 7.71015, N'EUR', 1310, N'abc')
INSERT [dbo].[prices] ([REGION1_CD], [REGION2_CD], [REGION3_CD], [PRICE], [CURRENCY_DESC], [PACK_CD], [PACK_DESC]) VALUES (75, 92, N'NULL', 6.187129, N'EUR', 1311, N'abc')
INSERT [dbo].[prices] ([REGION1_CD], [REGION2_CD], [REGION3_CD], [PRICE], [CURRENCY_DESC], [PACK_CD], [PACK_DESC]) VALUES (75, 92, N'NULL', 6.128287, N'EUR', 1311, N'abc')
INSERT [dbo].[prices] ([REGION1_CD], [REGION2_CD], [REGION3_CD], [PRICE], [CURRENCY_DESC], [PACK_CD], [PACK_DESC]) VALUES (75, 81, N'NULL', 6.09582, N'EUR', 1312, N'abc')
INSERT [dbo].[prices] ([REGION1_CD], [REGION2_CD], [REGION3_CD], [PRICE], [CURRENCY_DESC], [PACK_CD], [PACK_DESC]) VALUES (75, 81, N'NULL', 6.09582, N'EUR', 1312, N'abc')
INSERT [dbo].[prices] ([REGION1_CD], [REGION2_CD], [REGION3_CD], [PRICE], [CURRENCY_DESC], [PACK_CD], [PACK_DESC]) VALUES (75, 81, N'NULL', 6.09582, N'EUR', 1312, N'abc')
INSERT [dbo].[prices] ([REGION1_CD], [REGION2_CD], [REGION3_CD], [PRICE], [CURRENCY_DESC], [PACK_CD], [PACK_DESC]) VALUES (75, 81, N'NULL', 6.09582, N'EUR', 1312, N'abc')
INSERT [dbo].[prices] ([REGION1_CD], [REGION2_CD], [REGION3_CD], [PRICE], [CURRENCY_DESC], [PACK_CD], [PACK_DESC]) VALUES (75, 81, N'NULL', 9.516528, N'EUR', 1313, N'abc')
INSERT [dbo].[prices] ([REGION1_CD], [REGION2_CD], [REGION3_CD], [PRICE], [CURRENCY_DESC], [PACK_CD], [PACK_DESC]) VALUES (75, 81, N'NULL', 9.516528, N'EUR', 1313, N'abc')
INSERT [dbo].[prices] ([REGION1_CD], [REGION2_CD], [REGION3_CD], [PRICE], [CURRENCY_DESC], [PACK_CD], [PACK_DESC]) VALUES (75, 81, N'NULL', 9.516528, N'EUR', 1313, N'abc')
INSERT [dbo].[prices] ([REGION1_CD], [REGION2_CD], [REGION3_CD], [PRICE], [CURRENCY_DESC], [PACK_CD], [PACK_DESC]) VALUES (75, 81, N'NULL', 6.078811, N'EUR', 1314, N'abc')
INSERT [dbo].[prices] ([REGION1_CD], [REGION2_CD], [REGION3_CD], [PRICE], [CURRENCY_DESC], [PACK_CD], [PACK_DESC]) VALUES (75, 81, N'NULL', 6.078811, N'EUR', 1314, N'abc')
INSERT [dbo].[prices] ([REGION1_CD], [REGION2_CD], [REGION3_CD], [PRICE], [CURRENCY_DESC], [PACK_CD], [PACK_DESC]) VALUES (75, 81, N'NULL', 6.53527, N'EUR', 1315, N'abc')
INSERT [dbo].[prices] ([REGION1_CD], [REGION2_CD], [REGION3_CD], [PRICE], [CURRENCY_DESC], [PACK_CD], [PACK_DESC]) VALUES (75, 81, N'NULL', 6.53527, N'EUR', 1315, N'abc')
INSERT [dbo].[prices] ([REGION1_CD], [REGION2_CD], [REGION3_CD], [PRICE], [CURRENCY_DESC], [PACK_CD], [PACK_DESC]) VALUES (75, 81, N'NULL', 6.53527, N'EUR', 1315, N'abc')
INSERT [dbo].[prices] ([REGION1_CD], [REGION2_CD], [REGION3_CD], [PRICE], [CURRENCY_DESC], [PACK_CD], [PACK_DESC]) VALUES (75, 81, N'NULL', 5.62, N'EUR', 1316, N'abc')
INSERT [dbo].[prices] ([REGION1_CD], [REGION2_CD], [REGION3_CD], [PRICE], [CURRENCY_DESC], [PACK_CD], [PACK_DESC]) VALUES (75, 90, N'NULL', 2.05178, N'EUR', 1317, N'abc')
INSERT [dbo].[prices] ([REGION1_CD], [REGION2_CD], [REGION3_CD], [PRICE], [CURRENCY_DESC], [PACK_CD], [PACK_DESC]) VALUES (75, 90, N'NULL', 1.2283, N'EUR', 1317, N'abc')
INSERT [dbo].[prices] ([REGION1_CD], [REGION2_CD], [REGION3_CD], [PRICE], [CURRENCY_DESC], [PACK_CD], [PACK_DESC]) VALUES (75, 90, N'NULL', 1.2283, N'EUR', 1317, N'abc')
INSERT [dbo].[prices] ([REGION1_CD], [REGION2_CD], [REGION3_CD], [PRICE], [CURRENCY_DESC], [PACK_CD], [PACK_DESC]) VALUES (72, 80, N'NULL', 3.379084, N'EUR', 1318, N'abc')
INSERT [dbo].[prices] ([REGION1_CD], [REGION2_CD], [REGION3_CD], [PRICE], [CURRENCY_DESC], [PACK_CD], [PACK_DESC]) VALUES (72, 87, N'NULL', 6.9, N'EUR', 1319, N'abc')
INSERT [dbo].[prices] ([REGION1_CD], [REGION2_CD], [REGION3_CD], [PRICE], [CURRENCY_DESC], [PACK_CD], [PACK_DESC]) VALUES (75, 90, N'NULL', 3.77068, N'EUR', 1320, N'abc')
INSERT [dbo].[prices] ([REGION1_CD], [REGION2_CD], [REGION3_CD], [PRICE], [CURRENCY_DESC], [PACK_CD], [PACK_DESC]) VALUES (75, 77, N'NULL', 3.71, N'EUR', 1321, N'abc')
INSERT [dbo].[prices] ([REGION1_CD], [REGION2_CD], [REGION3_CD], [PRICE], [CURRENCY_DESC], [PACK_CD], [PACK_DESC]) VALUES (75, 77, N'NULL', 3.71, N'EUR', 1321, N'abc')
INSERT [dbo].[prices] ([REGION1_CD], [REGION2_CD], [REGION3_CD], [PRICE], [CURRENCY_DESC], [PACK_CD], [PACK_DESC]) VALUES (75, 77, N'NULL', 3.71, N'EUR', 1321, N'abc')
INSERT [dbo].[prices] ([REGION1_CD], [REGION2_CD], [REGION3_CD], [PRICE], [CURRENCY_DESC], [PACK_CD], [PACK_DESC]) VALUES (75, 77, N'NULL', 3.71, N'EUR', 1321, N'abc')
INSERT [dbo].[prices] ([REGION1_CD], [REGION2_CD], [REGION3_CD], [PRICE], [CURRENCY_DESC], [PACK_CD], [PACK_DESC]) VALUES (75, 77, N'NULL', 3.71, N'EUR', 1321, N'abc')
INSERT [dbo].[prices] ([REGION1_CD], [REGION2_CD], [REGION3_CD], [PRICE], [CURRENCY_DESC], [PACK_CD], [PACK_DESC]) VALUES (75, 77, N'NULL', 5.334797, N'EUR', 1322, N'abc')
INSERT [dbo].[prices] ([REGION1_CD], [REGION2_CD], [REGION3_CD], [PRICE], [CURRENCY_DESC], [PACK_CD], [PACK_DESC]) VALUES (75, 77, N'NULL', 4.9, N'EUR', 1324, N'abc')
INSERT [dbo].[prices] ([REGION1_CD], [REGION2_CD], [REGION3_CD], [PRICE], [CURRENCY_DESC], [PACK_CD], [PACK_DESC]) VALUES (75, 77, N'NULL', 4.86, N'EUR', 1325, N'abc')
INSERT [dbo].[prices] ([REGION1_CD], [REGION2_CD], [REGION3_CD], [PRICE], [CURRENCY_DESC], [PACK_CD], [PACK_DESC]) VALUES (75, 77, N'NULL', 4.47, N'EUR', 1326, N'abc')
INSERT [dbo].[prices] ([REGION1_CD], [REGION2_CD], [REGION3_CD], [PRICE], [CURRENCY_DESC], [PACK_CD], [PACK_DESC]) VALUES (75, 90, N'NULL', 3.36, N'EUR', 1327, N'abc')
INSERT [dbo].[prices] ([REGION1_CD], [REGION2_CD], [REGION3_CD], [PRICE], [CURRENCY_DESC], [PACK_CD], [PACK_DESC]) VALUES (75, 90, N'NULL', 5.35, N'EUR', 1328, N'abc')
INSERT [dbo].[prices] ([REGION1_CD], [REGION2_CD], [REGION3_CD], [PRICE], [CURRENCY_DESC], [PACK_CD], [PACK_DESC]) VALUES (75, 90, N'NULL', 5.42, N'EUR', 1329, N'abc')
INSERT [dbo].[prices] ([REGION1_CD], [REGION2_CD], [REGION3_CD], [PRICE], [CURRENCY_DESC], [PACK_CD], [PACK_DESC]) VALUES (75, 90, N'NULL', 3.26986, N'EUR', 1330, N'abc')
INSERT [dbo].[prices] ([REGION1_CD], [REGION2_CD], [REGION3_CD], [PRICE], [CURRENCY_DESC], [PACK_CD], [PACK_DESC]) VALUES (75, 90, N'NULL', 6.02, N'EUR', 1331, N'abc')
INSERT [dbo].[prices] ([REGION1_CD], [REGION2_CD], [REGION3_CD], [PRICE], [CURRENCY_DESC], [PACK_CD], [PACK_DESC]) VALUES (75, 90, N'NULL', 5.338361, N'EUR', 1332, N'abc')
INSERT [dbo].[prices] ([REGION1_CD], [REGION2_CD], [REGION3_CD], [PRICE], [CURRENCY_DESC], [PACK_CD], [PACK_DESC]) VALUES (75, 90, N'NULL', 5.448506, N'EUR', 1333, N'abc')
INSERT [dbo].[prices] ([REGION1_CD], [REGION2_CD], [REGION3_CD], [PRICE], [CURRENCY_DESC], [PACK_CD], [PACK_DESC]) VALUES (75, 90, N'NULL', 5.57, N'EUR', 1334, N'abc')
INSERT [dbo].[prices] ([REGION1_CD], [REGION2_CD], [REGION3_CD], [PRICE], [CURRENCY_DESC], [PACK_CD], [PACK_DESC]) VALUES (75, 90, N'NULL', 3.36, N'EUR', 1335, N'abc')
INSERT [dbo].[prices] ([REGION1_CD], [REGION2_CD], [REGION3_CD], [PRICE], [CURRENCY_DESC], [PACK_CD], [PACK_DESC]) VALUES (72, 72, N'NULL', 5.18, N'EUR', 1336, N'abc')
INSERT [dbo].[prices] ([REGION1_CD], [REGION2_CD], [REGION3_CD], [PRICE], [CURRENCY_DESC], [PACK_CD], [PACK_DESC]) VALUES (72, 72, N'NULL', 5.04, N'EUR', 1336, N'abc')
INSERT [dbo].[prices] ([REGION1_CD], [REGION2_CD], [REGION3_CD], [PRICE], [CURRENCY_DESC], [PACK_CD], [PACK_DESC]) VALUES (72, 85, N'NULL', 3.82, N'EUR', 1338, N'abc')
INSERT [dbo].[prices] ([REGION1_CD], [REGION2_CD], [REGION3_CD], [PRICE], [CURRENCY_DESC], [PACK_CD], [PACK_DESC]) VALUES (72, 80, N'NULL', 2.17, N'EUR', 1339, N'abc')
INSERT [dbo].[prices] ([REGION1_CD], [REGION2_CD], [REGION3_CD], [PRICE], [CURRENCY_DESC], [PACK_CD], [PACK_DESC]) VALUES (72, 80, N'NULL', 2.02, N'EUR', 1339, N'abc')
INSERT [dbo].[prices] ([REGION1_CD], [REGION2_CD], [REGION3_CD], [PRICE], [CURRENCY_DESC], [PACK_CD], [PACK_DESC]) VALUES (72, 80, N'NULL', 2.02, N'EUR', 1339, N'abc')
INSERT [dbo].[prices] ([REGION1_CD], [REGION2_CD], [REGION3_CD], [PRICE], [CURRENCY_DESC], [PACK_CD], [PACK_DESC]) VALUES (72, 80, N'NULL', 2.02, N'EUR', 1339, N'abc')
INSERT [dbo].[prices] ([REGION1_CD], [REGION2_CD], [REGION3_CD], [PRICE], [CURRENCY_DESC], [PACK_CD], [PACK_DESC]) VALUES (72, 85, N'NULL', 3.21, N'EUR', 1340, N'abc')
INSERT [dbo].[prices] ([REGION1_CD], [REGION2_CD], [REGION3_CD], [PRICE], [CURRENCY_DESC], [PACK_CD], [PACK_DESC]) VALUES (61, 88, N'NULL', 6.02126, N'EUR', 1342, N'abc')
INSERT [dbo].[prices] ([REGION1_CD], [REGION2_CD], [REGION3_CD], [PRICE], [CURRENCY_DESC], [PACK_CD], [PACK_DESC]) VALUES (75, 77, N'NULL', 4.47, N'EUR', 1343, N'abc')
INSERT [dbo].[prices] ([REGION1_CD], [REGION2_CD], [REGION3_CD], [PRICE], [CURRENCY_DESC], [PACK_CD], [PACK_DESC]) VALUES (77, 77, N'NULL', 5.17, N'EUR', 1344, N'abc')
INSERT [dbo].[prices] ([REGION1_CD], [REGION2_CD], [REGION3_CD], [PRICE], [CURRENCY_DESC], [PACK_CD], [PACK_DESC]) VALUES (77, 77, N'NULL', 4.47, N'EUR', 1345, N'abc')

我得到的结果如下图所示

enter image description here

正如你在上面的图片中看到的那样,我没有得到REGION1_CD的任何一行。

请帮助

1 个答案:

答案 0 :(得分:0)

问题来自documentation

  

无法保证查询返回的行使用   每次执行时,ROW_NUMBER()的排序方式都完全相同   除非以下条件成立。

     
      
  • 分区列的值是唯一的。
  •   
  • ORDER BY列的值是唯一的。
  •   
  • 分区列和ORDER BY列的值组合是唯一的。
  •   

这会导致ROWDESCROWASC不完全相反。

以下查询将为您提供正确的值:

SELECT x.PACK_DESC, x.REGION1_CD, AVG(x.price) 
FROM (
    SELECT 
        PACK_DESC,
        REGION1_CD,
        price,
        COUNT(1)
            OVER (PARTITION BY CHECKSUM(PACK_DESC,REGION1_CD))
            AS total_rows, 
        ROW_NUMBER()
            OVER (PARTITION BY CHECKSUM(PACK_DESC,REGION1_CD) ORDER BY price ASC)
            AS row_order 
    FROM prices) x
WHERE  x.row_order IN (
    FLOOR((x.total_rows + 1) / 2.0),
    FLOOR((x.total_rows + 2) / 2.0))
GROUP BY PACK_DESC, REGION1_CD

棘手的部分在底部的row_order比较中:

  • 对于不均匀的行数,例如 5 ,它将评估为:
    x.row_order IN (3, 3)
    因此,只会选择第三行。
  • 对于偶数行,例如 6 ,它将评估为:
    x.row_order IN (3, 4)
    因此,选择了第三行和第四行,并将它们平均在查询的顶部。