请帮我解决这个问题。
表是这样的 -
Type | SubType | Flag | Value
--------------------------------
123 | A1 | Y | 101
--------------------------------
123 | A2 | Y | 102
------------------------------
123 | A3 | Y | 103
------------------------------
124 | A4 | N | 104
------------------------------
124 | A5 | N | 105
------------------------------
124 | A6 | N | 106
------------------------------
125 | A7 | Y | 107
------------------------------
125 | A8 | Y | 108
------------------------------
125 | A9 | N | 109
------------------------------
125 | A10 | N | 110
要求是根据特定条件选择行 - 如果特定类型的所有标志都是Y,那么只选择具有最低子类型的行, 如果特定类型的所有标志都是N,则选择该类型的所有行, 如果一个类型的标志是Y和N的组合,那么为该类型选择2行 - 一个具有标志Y的最低子类型,一个具有标志N的最低子类型。
所以上表的输出应该是 -
Type | SubType | Flag | Value
------------------------------
123 | A1 | Y | 101
------------------------------
124 | A4 | N | 104
------------------------------
124 | A5 | N | 105
------------------------------
124 | A6 | N | 106
------------------------------
125 | A7 | Y | 107
------------------------------
125 | A9 | N | 109
对于糟糕的表格格式感到抱歉,请提前感谢您对此进行调查。
答案 0 :(得分:1)
你可以使用这样的窗口函数:
with your_table (Type , SubType , Flag , Value) as (
select 123 , 'A1' , 'Y' , 101 union all
select 123 , 'A2' , 'Y' , 102 union all
select 123 , 'A3' , 'Y' , 103 union all
select 124 , 'A4' , 'N' , 104 union all
select 124 , 'A5' , 'N' , 105 union all
select 124 , 'A6' , 'N' , 106 union all
select 125 , 'A7' , 'Y' , 107 union all
select 125 , 'A8' , 'Y' , 108 union all
select 125 , 'A9' , 'N' , 109 union all
select 125 , 'A10' , 'N' , 110)
select Type, SubType, Flag, Value
from (
select
t.*,
case when min(flag) over (partition by type) = max(flag) over (partition by type) then 1 else 2 end cnt,
row_number() over (partition by type, flag order by convert(int, substring(subtype,2,len(subtype)))) rn
from your_table t
) t where
(cnt = 1 and (rn = 1 or flag = 'N'))
or (cnt <> 1 and rn = 1);
产地:
答案 1 :(得分:1)
使用CTE为每种类型生成摘要,然后对其进行过滤:
g0 <- [... the rest of the graph ...]
cor.value <- 0.84
c.a <- substitute(~rho == x, list(x=cor.value))
g0 + labs(y="Angekreuzte Option", x=c.a)
在上面的代码中,我们从SubType中提取数字以正确排序。这可确保WITH summary AS (
SELECT [Type]
,SUM(CASE WHEN [Flag] = 'Y' THEN 1 ELSE 0 END) AS Count_Y
,SUM(CASE WHEN [Flag] = 'N' THEN 1 ELSE 0 END) AS Count_N
-- This value will be NULL, if there are no rows with Y flag
,MIN(CASE WHEN [Flag] = 'Y' THEN CAST(SUBSTRING(SubType,2,99) AS INT) END) AS Lowest_Y_SubType
-- This value will be NULL, if there are no rows with N flag
,MIN(CASE WHEN [Flag] = 'N' THEN CAST(SUBSTRING(SubType,2,99) AS INT) END) AS Lowest_N_SubType
FROM dbo.tSO_41657760
GROUP BY [Type]
)
SELECT *
FROM dbo.tSO_41657760 t
WHERE EXISTS (
SELECT 1
FROM summary
-- Filter this inner table using Type from the outer table
WHERE [Type] = t.[Type]
AND
(
-- All rows have N flag
(Count_Y = 0)
-- Select the lowest N and Y rows with lowest SubType values
OR (CAST(SUBSTRING(SubType,2,99) AS INT) IN (Lowest_N_SubType, Lowest_Y_SubType))
)
)
小于 A9
(否则,纯字母排序会产生不正确的结果)。
答案 2 :(得分:1)
这是一种方式
Group.find({})
.populate({
path : 'teachers' ,
match : { username : "bob" }
})
.exec(callback);