我有列的表格 - ID,X1,X2,X3,X4SQL查詢來計算重量或發佈基於價值/重量空
如果我有值
1,y,y,y,y
2,y,null,n,null
3,y,null,null,null
然後說,我想計算(1)/ 和顯示行(Y的數量)爲
1,.25,.25,.25,.25
2,1,0,0,0
3,1,0,0,0
是否有可能做到這一點使用SQL查詢?
我有列的表格 - ID,X1,X2,X3,X4SQL查詢來計算重量或發佈基於價值/重量空
如果我有值
1,y,y,y,y
2,y,null,n,null
3,y,null,null,null
然後說,我想計算(1)/ 和顯示行(Y的數量)爲
1,.25,.25,.25,.25
2,1,0,0,0
3,1,0,0,0
是否有可能做到這一點使用SQL查詢?
下面是一個使用子查詢另一個蠻力攻擊。設置一些測試數據:
CREATE TABLE MyTable
(
Id int not null
,Col1 char(1) null
,Col2 char(1) null
,Col3 char(1) null
,Col4 char(1) null
)
INSERT MyTable
select 1, 'Y', 'Y', 'Y', 'Y'
union all select 2, 'Y', null, 'N', null
union all select 3, 'Y', null, null, null
union all select 4, null, null, null, null
我用目標值的字符數據,但你應該沒有問題,使其數值。我添加了第四行來檢查除以零(謝謝ps!)。所以,
SELECT
mt.ID
,case mt.Col1 when 'Y' then xx.distrib else 0 end
,case mt.Col2 when 'Y' then xx.distrib else 0 end
,case mt.Col3 when 'Y' then xx.distrib else 0 end
,case mt.Col4 when 'Y' then xx.distrib else 0 end
from MyTable mt
inner join (select
ID
,1.0/( case Col1 when 'Y' then 1 else 0 end
+ case Col2 when 'Y' then 1 else 0 end
+ case Col3 when 'Y' then 1 else 0 end
+ case Col4 when 'Y' then 1 else 0 end) distrib
from MyTable) xx
on xx.Id = mt.Id
似乎產生所需的結果。 (運行在自己的子查詢,你通過零得到一個鴻溝......但老實說,我不知道爲什麼運行整個查詢沒有。
這裏的天真的版本將是
SELECT
id,
CASE
WHEN x1 IS NULL THEN
0
WHEN f_eval(x1) + f_eval(x2,0) + f_eval(x3,0) + f_eval(x4,0) = 0 THEN
-- default value
ELSE
1/(f_eval(x1) + f_eval(x2,0) + f_eval(x3,0) + f_eval(x4,0))
END,
-- repeat for each column
FROM
Table
其中f_eval被定義爲或多或少如下:
CREATE FUNCTION f_eval(
@x_value varchar(1)
BEGIN
DECLARE @return_val int
SELECT
@return_val =
case
WHEN ISNULL(@x_value, 'n') = 'y' THEN
1
ELSE
0
END
RETURN @return_val
END
是的,我是這麼認爲的,但可以有n值,而不是編輯,以採取非空值考慮空 –
。 –
嘗試foillowing
select
(SUM(case when x1 is null then 0 else 1 end))* 1.0/ ((SUM(case when x1 is null then 0 else 1 end) + SUM(case when x2 is null then 0 else 1 end)+ SUM(case when x3 is null then 0 else 1 end) + + SUM(case when x4 is null then 0 else 1 end)) * 1.0),
(SUM(case when x2 is null then 0 else 1 end))* 1.0/ ((SUM(case when x1 is null then 0 else 1 end) + SUM(case when x2 is null then 0 else 1 end)+ SUM(case when x3 is null then 0 else 1 end) + + SUM(case when x4 is null then 0 else 1 end)) * 1.0),
(SUM(case when x3 is null then 0 else 1 end))* 1.0/ ((SUM(case when x1 is null then 0 else 1 end) + SUM(case when x2 is null then 0 else 1 end)+ SUM(case when x3 is null then 0 else 1 end) + + SUM(case when x4 is null then 0 else 1 end)) * 1.0),
(SUM(case when x4 is null then 0 else 1 end))* 1.0/ ((SUM(case when x1 is null then 0 else 1 end) + SUM(case when x2 is null then 0 else 1 end)+ SUM(case when x3 is null then 0 else 1 end) + + SUM(case when x4 is null then 0 else 1 end)) * 1.0)
from table group by id
你可能已經確保你不要除以零
編輯2
通過添加更多的情況分母時0,那麼1 你也應該改變
case when x1 is null then 0 else 1 end
到
case when x1 = 'y' then 1 else 0 end
試試這個
的樣本數據
declare @t table(Id int,Col1 char(1) null,Col2 char(1) null,Col3 char(1) null,Col4 char(1) null)
INSERT @t select 1, 'Y', 'Y', 'Y', 'Y' union all select 2, 'Y', null, null, null
union all select 3, 'Y', null, null, null
查詢:
select t.id
,case Col1 when 'Y' then LEFT(cast(1.0/x.cntys as varchar(20)),4) else cast(0 as varchar(1)) end x1
,case Col2 when 'Y' then LEFT(cast(1.0/x.cntys as varchar(20)),4) else cast(0 as varchar(1)) end x2
,case Col3 when 'Y' then LEFT(cast(1.0/x.cntys as varchar(20)),4) else cast(0 as varchar(1)) end x3
,case Col4 when 'Y' then LEFT(cast(1.0/x.cntys as varchar(20)),4) else cast(0 as varchar(1)) end x4
from @t t
join(
select ROW_NUMBER() over(order by getdate()) rn,
COUNT(col1) + COUNT(col2) + COUNT(col3) + COUNT(col4) cntys from @t
group by id)X
on t.Id = X.rn
輸出:
id x1 x2 x3 x4
1 0.25 0.25 0.25 0.25
2 1.00 0 0 0
3 1.00 0 0 0
一個與unpivot優雅的東西可能會起作用,但可能是矯枉過正。 –