How calculate TRIMMEAN in SQL Server 2012? - sql

How can I calculate the TRIMMEAN in SQL Server?
Executing the TRIMMEAN in Excel for the example data below and percent of 0.35 my result is: 0.10
I've tried so many ways but not success.
example data
CREATE TABLE [dbo].[YourTable](
[Data] [decimal](18, 8) NOT NULL
);
WITH Nums AS
(
SELECT number
FROM master..spt_values
WHERE type='P' AND number > 0
), Vals As
(
SELECT *
FROM (VALUES
(2,0.03555556),
(1,0.05777778),
(1,0.05888889),
(1,0.05916667),
(2,0.05944444),
(2,0.06000000),
(3,0.06027778),
(1,0.06055556),
(2,0.06083333),
(2,0.06111111),
(2,0.06138889),
(2,0.06166667),
(2,0.06194444),
(2,0.06222222),
(3,0.06250000),
(1,0.06277778),
(2,0.06305556),
(3,0.06361111),
(7,0.06388889),
(4,0.06416667),
(3,0.06444444),
(8,0.06472222),
(4,0.06500000),
(5,0.06527778),
(3,0.06555556),
(9,0.06583333),
(8,0.06611111),
(8,0.06638889),
(5,0.06666667),
(10,0.06694444),
(6,0.06722222),
(16,0.06750000),
(13,0.06777778),
(10,0.06805556),
(4,0.06833333),
(7,0.06861111),
(10,0.06888889),
(8,0.06916667),
(10,0.06944444),
(14,0.06972222),
(14,0.07000000),
(15,0.07027778),
(15,0.07055556),
(21,0.07083333),
(19,0.07111111),
(20,0.07138889),
(20,0.07166667),
(18,0.07194444),
(24,0.07222222),
(19,0.07250000),
(17,0.07277778),
(15,0.07305556),
(23,0.07333333),
(25,0.07361111),
(26,0.07388889),
(22,0.07416667),
(22,0.07444444),
(24,0.07472222),
(19,0.07500000),
(20,0.07527778),
(29,0.07555556),
(33,0.07583333),
(16,0.07611111),
(30,0.07638889),
(20,0.07666667),
(25,0.07694444),
(37,0.07722222),
(32,0.07750000),
(25,0.07777778),
(25,0.07805556),
(34,0.07833333),
(28,0.07861111),
(22,0.07888889),
(25,0.07916667),
(27,0.07944444),
(24,0.07972222),
(28,0.08000000),
(31,0.08027778),
(34,0.08055556),
(21,0.08083333),
(23,0.08111111),
(23,0.08138889),
(18,0.08166667),
(22,0.08194444),
(18,0.08222222),
(30,0.08250000),
(26,0.08277778),
(31,0.08305556),
(24,0.08333333),
(20,0.08361111),
(21,0.08388889),
(26,0.08416667),
(29,0.08444444),
(39,0.08472222),
(20,0.08500000),
(25,0.08527778),
(28,0.08555556),
(31,0.08583333),
(29,0.08611111),
(21,0.08638889),
(16,0.08666667),
(27,0.08694444),
(21,0.08722222),
(16,0.08750000),
(23,0.08777778),
(21,0.08805556),
(24,0.08833333),
(24,0.08861111),
(21,0.08888889),
(23,0.08916667),
(23,0.08944444),
(15,0.08972222),
(21,0.09000000),
(18,0.09027778),
(19,0.09055556),
(15,0.09083333),
(19,0.09111111),
(17,0.09138889),
(18,0.09166667),
(21,0.09194444),
(14,0.09222222),
(19,0.09250000),
(21,0.09277778),
(25,0.09305556),
(18,0.09333333),
(17,0.09361111),
(20,0.09388889),
(25,0.09416667),
(18,0.09444444),
(16,0.09472222),
(21,0.09500000),
(9,0.09527778),
(16,0.09555556),
(16,0.09583333),
(16,0.09611111),
(14,0.09638889),
(15,0.09666667),
(18,0.09694444),
(15,0.09722222),
(13,0.09750000),
(20,0.09777778),
(18,0.09805556),
(19,0.09833333),
(24,0.09861111),
(17,0.09888889),
(10,0.09916667),
(10,0.09944444),
(17,0.09972222),
(16,0.10000000),
(17,0.10027778),
(15,0.10055556),
(10,0.10083333),
(17,0.10111111),
(19,0.10138889),
(14,0.10166667),
(9,0.10194444),
(15,0.10222222),
(14,0.10250000),
(15,0.10277778),
(14,0.10305556),
(7,0.10333333),
(13,0.10361111),
(19,0.10388889),
(10,0.10416667),
(9,0.10444444),
(9,0.10472222),
(16,0.10500000),
(16,0.10527778),
(8,0.10555556),
(10,0.10583333),
(15,0.10611111),
(12,0.10638889),
(10,0.10666667),
(9,0.10694444),
(12,0.10722222),
(9,0.10750000),
(17,0.10777778),
(8,0.10805556),
(14,0.10833333),
(9,0.10861111),
(9,0.10888889),
(8,0.10916667),
(13,0.10944444),
(16,0.10972222),
(10,0.11000000),
(12,0.11027778),
(10,0.11055556),
(11,0.11083333),
(9,0.11111111),
(12,0.11138889),
(10,0.11166667),
(8,0.11194444),
(15,0.11222222),
(7,0.11250000),
(9,0.11277778),
(10,0.11305556),
(7,0.11333333),
(12,0.11361111),
(8,0.11388889),
(5,0.11416667),
(13,0.11444444),
(8,0.11472222),
(9,0.11500000),
(5,0.11527778),
(6,0.11555556),
(6,0.11583333),
(9,0.11611111),
(12,0.11638889),
(4,0.11666667),
(8,0.11694444),
(7,0.11722222),
(12,0.11750000),
(9,0.11777778),
(8,0.11805556),
(8,0.11833333),
(5,0.11861111),
(7,0.11888889),
(4,0.11916667),
(5,0.11944444),
(5,0.11972222),
(11,0.12000000),
(1,0.12027778),
(8,0.12055556),
(7,0.12083333),
(8,0.12111111),
(9,0.12138889),
(7,0.12166667),
(3,0.12194444),
(11,0.12222222),
(9,0.12250000),
(8,0.12277778),
(9,0.12305556),
(4,0.12333333),
(5,0.12361111),
(3,0.12388889),
(3,0.12416667),
(13,0.12444444),
(7,0.12472222),
(8,0.12500000),
(8,0.12527778),
(5,0.12555556),
(6,0.12583333),
(6,0.12611111),
(3,0.12638889),
(6,0.12666667),
(5,0.12694444),
(9,0.12722222),
(4,0.12750000),
(6,0.12777778),
(7,0.12805556),
(8,0.12833333),
(7,0.12861111),
(8,0.12888889),
(9,0.12916667),
(8,0.12944444),
(4,0.12972222),
(2,0.13000000),
(5,0.13027778),
(3,0.13055556),
(8,0.13083333),
(4,0.13111111),
(1,0.13138889),
(3,0.13166667),
(9,0.13194444),
(12,0.13222222),
(8,0.13250000),
(4,0.13277778),
(5,0.13305556),
(4,0.13333333),
(11,0.13361111),
(7,0.13388889),
(8,0.13416667),
(2,0.13444444),
(10,0.13472222),
(5,0.13500000),
(5,0.13527778),
(5,0.13583333),
(10,0.13611111),
(2,0.13638889),
(8,0.13666667),
(3,0.13694444),
(8,0.13722222),
(5,0.13750000),
(4,0.13777778),
(5,0.13805556),
(8,0.13833333),
(2,0.13861111),
(2,0.13888889),
(7,0.13916667),
(5,0.13972222),
(10,0.14000000),
(4,0.14027778),
(4,0.14055556),
(6,0.14083333),
(5,0.14111111),
(3,0.14138889),
(5,0.14166667),
(4,0.14194444),
(7,0.14222222),
(5,0.14250000),
(6,0.14277778),
(5,0.14305556),
(7,0.14333333),
(6,0.14361111),
(9,0.14388889),
(7,0.14416667),
(3,0.14444444),
(4,0.14472222),
(5,0.14500000),
(4,0.14527778),
(7,0.14555556),
(5,0.14583333),
(4,0.14611111),
(5,0.14638889),
(4,0.14666667),
(2,0.14694444),
(8,0.14722222),
(5,0.14750000),
(3,0.14777778),
(5,0.14805556),
(6,0.14833333),
(3,0.14861111),
(3,0.14888889),
(8,0.14916667),
(4,0.14944444),
(6,0.14972222),
(5,0.15000000),
(4,0.15027778),
(3,0.15055556),
(1,0.15083333),
(1,0.15111111),
(3,0.15138889),
(6,0.15166667),
(2,0.15194444),
(3,0.15222222),
(2,0.15250000),
(2,0.15277778),
(1,0.15305556),
(2,0.15333333),
(2,0.15361111),
(2,0.15388889),
(1,0.15416667),
(8,0.15444444),
(5,0.15472222),
(2,0.15500000),
(1,0.15527778),
(4,0.15555556),
(2,0.15583333),
(3,0.15611111),
(1,0.15666667),
(1,0.15722222),
(2,0.15750000),
(3,0.15777778),
(2,0.15805556),
(3,0.15833333),
(2,0.15861111),
(4,0.15888889),
(3,0.15916667),
(2,0.15944444),
(3,0.15972222),
(4,0.16000000),
(1,0.16055556),
(2,0.16083333),
(4,0.16111111),
(4,0.16138889),
(4,0.16166667),
(2,0.16194444),
(5,0.16222222),
(1,0.16250000),
(3,0.16333333),
(1,0.16388889),
(2,0.16416667),
(1,0.16444444),
(1,0.16472222),
(1,0.16500000),
(2,0.16527778),
(1,0.16555556),
(1,0.16611111),
(1,0.16638889),
(4,0.16666667),
(3,0.16694444),
(3,0.16722222),
(1,0.16750000),
(1,0.16777778),
(2,0.16833333),
(4,0.16861111),
(2,0.16888889),
(1,0.16916667),
(1,0.16972222),
(2,0.17000000),
(2,0.17027778),
(3,0.17055556),
(2,0.17083333),
(2,0.17111111),
(3,0.17138889),
(2,0.17166667),
(1,0.17194444),
(2,0.17222222),
(2,0.17250000),
(5,0.17277778),
(5,0.17305556),
(3,0.17333333),
(2,0.17361111),
(3,0.17388889),
(3,0.17416667),
(2,0.17444444),
(2,0.17472222),
(1,0.17527778),
(3,0.17555556),
(1,0.17583333),
(2,0.17611111),
(1,0.17666667),
(3,0.17722222),
(3,0.17750000),
(1,0.17777778),
(1,0.17805556),
(3,0.17833333),
(5,0.17861111),
(4,0.17888889),
(2,0.17916667),
(1,0.17944444),
(2,0.18000000),
(1,0.18027778),
(2,0.18055556),
(2,0.18083333),
(1,0.18111111),
(2,0.18138889),
(4,0.18166667),
(2,0.18194444),
(1,0.18222222),
(1,0.18250000),
(1,0.18277778),
(2,0.18361111),
(1,0.18388889),
(1,0.18416667),
(3,0.18444444),
(2,0.18472222),
(4,0.18500000),
(2,0.18527778),
(2,0.18583333),
(3,0.18611111),
(1,0.18638889),
(1,0.18666667),
(1,0.18694444),
(2,0.18722222),
(1,0.18861111),
(4,0.18888889),
(1,0.18916667),
(1,0.18944444),
(1,0.18972222),
(2,0.19000000),
(1,0.19027778),
(1,0.19083333),
(1,0.19111111),
(1,0.19194444),
(2,0.19222222),
(1,0.19250000),
(1,0.19361111),
(1,0.19388889),
(1,0.19416667),
(1,0.19444444),
(1,0.19472222),
(1,0.19500000),
(1,0.19555556),
(3,0.19583333),
(2,0.19638889),
(3,0.19666667),
(2,0.19722222),
(1,0.19777778),
(2,0.19805556),
(1,0.19833333),
(2,0.19861111),
(2,0.19888889),
(1,0.19916667),
(1,0.19944444),
(2,0.20027778),
(1,0.20055556),
(2,0.20083333),
(2,0.20138889),
(1,0.20194444),
(2,0.20222222),
(1,0.20250000),
(1,0.20277778),
(1,0.20333333),
(1,0.20361111),
(1,0.20388889),
(1,0.20444444),
(1,0.20500000),
(2,0.20527778),
(3,0.20555556),
(1,0.20583333),
(1,0.20611111),
(3,0.20638889),
(2,0.20666667),
(2,0.20694444),
(1,0.20750000),
(1,0.20805556),
(3,0.20833333),
(1,0.20888889),
(1,0.20916667),
(1,0.20944444),
(2,0.20972222),
(1,0.21000000),
(1,0.21027778),
(1,0.21055556),
(2,0.21083333),
(1,0.21111111),
(2,0.21138889),
(2,0.21166667),
(1,0.21194444),
(3,0.21250000),
(2,0.21277778),
(1,0.21305556),
(2,0.21333333),
(2,0.21361111),
(2,0.21388889),
(2,0.21416667),
(1,0.21472222),
(1,0.21500000),
(1,0.21694444),
(1,0.21750000),
(4,0.21777778),
(2,0.21861111),
(1,0.21888889),
(3,0.21916667),
(1,0.21944444),
(2,0.21972222),
(1,0.22000000),
(1,0.22027778),
(1,0.22055556),
(1,0.22083333),
(1,0.22111111),
(1,0.22138889),
(1,0.22166667),
(1,0.22194444),
(3,0.22222222),
(1,0.22277778),
(3,0.22333333),
(1,0.22416667),
(1,0.22555556),
(2,0.22583333),
(1,0.22666667),
(1,0.22722222),
(1,0.22750000),
(5,0.22777778),
(1,0.22805556),
(2,0.22833333),
(3,0.22916667),
(1,0.22944444),
(1,0.22972222),
(1,0.23000000),
(2,0.23027778),
(1,0.23083333),
(1,0.23166667),
(1,0.23444444),
(1,0.23500000),
(1,0.23583333),
(1,0.23638889),
(2,0.23694444),
(1,0.23722222),
(1,0.23750000),
(1,0.23805556),
(1,0.23861111),
(2,0.23888889),
(1,0.23916667),
(1,0.23944444),
(1,0.23972222),
(2,0.24000000),
(2,0.24055556),
(3,0.24083333),
(1,0.24166667),
(1,0.24194444),
(1,0.24250000),
(2,0.24388889),
(1,0.24416667),
(1,0.24555556),
(1,0.24583333),
(4,0.24611111),
(2,0.24777778),
(1,0.24833333),
(1,0.24916667),
(1,0.24972222),
(1,0.25000000),
(1,0.25083333),
(1,0.25138889),
(1,0.25166667),
(2,0.25222222),
(3,0.25250000),
(1,0.25305556),
(2,0.25361111),
(2,0.25416667),
(1,0.25444444),
(2,0.25555556),
(2,0.25638889),
(1,0.25666667),
(1,0.25777778),
(1,0.25805556),
(1,0.25833333),
(2,0.25861111),
(1,0.25916667),
(1,0.25944444),
(1,0.26000000),
(2,0.26111111),
(1,0.26138889),
(2,0.26194444),
(3,0.26222222),
(2,0.26277778),
(2,0.26388889),
(1,0.26416667),
(1,0.26444444),
(1,0.26472222),
(1,0.26583333),
(1,0.26611111),
(4,0.26638889),
(1,0.26694444),
(1,0.26750000),
(1,0.26777778),
(1,0.26861111),
(1,0.26888889),
(1,0.26944444),
(1,0.26972222),
(1,0.27027778),
(1,0.27083333),
(1,0.27111111),
(1,0.27166667),
(1,0.27361111),
(1,0.27527778),
(1,0.27666667),
(1,0.27694444),
(2,0.27722222),
(1,0.27750000),
(1,0.27805556),
(1,0.27833333),
(1,0.28000000),
(2,0.28027778),
(1,0.28055556),
(2,0.28083333),
(1,0.28166667),
(1,0.28194444),
(1,0.28277778),
(1,0.28305556),
(1,0.28388889),
(1,0.28555556),
(1,0.28611111),
(1,0.28638889),
(1,0.28694444),
(1,0.28750000),
(1,0.28777778),
(1,0.28805556),
(1,0.28861111),
(1,0.28888889),
(1,0.28944444),
(1,0.29083333),
(1,0.29111111),
(1,0.29222222),
(1,0.29250000),
(1,0.29277778),
(1,0.29388889),
(1,0.29416667),
(1,0.29444444),
(1,0.29472222),
(1,0.29500000),
(2,0.29583333),
(2,0.29611111),
(2,0.29638889),
(1,0.29666667),
(1,0.29694444),
(1,0.29750000),
(1,0.29805556),
(1,0.30000000),
(1,0.30027778),
(1,0.30055556),
(2,0.30111111),
(2,0.30166667),
(1,0.30222222),
(1,0.30250000),
(2,0.30277778),
(1,0.30361111),
(2,0.30444444),
(2,0.30500000),
(1,0.30555556),
(1,0.30583333),
(1,0.30638889),
(1,0.30694444),
(1,0.30722222),
(1,0.30777778),
(1,0.30888889),
(2,0.31000000),
(1,0.31027778),
(2,0.31055556),
(1,0.31138889),
(2,0.31222222),
(1,0.31250000),
(1,0.31388889),
(1,0.31416667),
(2,0.31527778),
(1,0.31583333),
(2,0.31638889),
(1,0.31722222),
(1,0.31777778),
(1,0.31861111),
(1,0.31888889),
(1,0.32027778),
(1,0.32083333),
(1,0.32111111),
(1,0.32222222),
(1,0.32277778),
(1,0.32388889),
(2,0.32416667),
(1,0.32444444),
(1,0.32527778),
(1,0.32583333),
(1,0.32638889),
(1,0.32694444),
(1,0.32805556),
(2,0.32833333),
(1,0.32888889),
(1,0.32972222),
(1,0.33055556),
(1,0.33111111),
(1,0.33166667),
(1,0.33333333),
(1,0.33527778),
(1,0.33611111),
(2,0.33638889),
(1,0.33666667),
(3,0.33833333),
(1,0.34000000),
(1,0.34027778),
(1,0.34166667),
(1,0.34333333),
(1,0.34388889),
(1,0.34416667),
(1,0.34444444),
(1,0.34472222),
(2,0.34555556),
(1,0.34638889),
(1,0.34666667),
(1,0.34750000),
(1,0.34777778),
(1,0.34805556),
(1,0.34833333),
(1,0.34972222),
(1,0.35055556),
(1,0.35083333),
(1,0.35111111),
(1,0.35194444),
(1,0.35277778),
(1,0.35361111),
(2,0.35444444),
(1,0.35583333),
(1,0.35777778),
(1,0.35833333),
(1,0.35916667),
(1,0.36111111),
(1,0.36222222),
(1,0.36305556),
(2,0.36555556),
(1,0.36694444),
(2,0.36722222),
(1,0.36777778),
(1,0.36944444),
(1,0.37027778),
(1,0.37111111),
(1,0.37194444),
(1,0.37416667),
(1,0.37666667),
(1,0.37750000),
(1,0.37805556),
(1,0.38222222),
(1,0.38416667),
(1,0.38555556),
(1,0.38666667),
(1,0.38805556),
(1,0.39166667),
(1,0.39194444),
(1,0.39416667),
(1,0.39666667),
(1,0.39694444),
(1,0.40055556),
(1,0.40166667),
(1,0.40388889),
(1,0.40416667),
(1,0.40444444),
(1,0.40750000),
(1,0.40805556),
(1,0.41250000),
(2,0.41361111),
(1,0.41722222),
(1,0.41916667),
(1,0.42027778),
(1,0.42194444),
(1,0.42361111),
(1,0.42388889),
(1,0.42972222),
(1,0.43222222),
(1,0.43277778),
(1,0.43750000),
(1,0.43777778),
(1,0.43833333),
(1,0.44611111),
(1,0.44638889),
(1,0.44694444),
(1,0.45027778),
(1,0.45166667),
(1,0.45500000),
(1,0.45833333),
(1,0.46083333),
(1,0.46555556),
(1,0.46722222),
(2,0.46888889),
(1,0.47083333),
(1,0.47111111),
(1,0.47416667),
(1,0.47500000),
(1,0.47694444),
(2,0.48000000),
(2,0.48027778),
(1,0.48083333),
(1,0.48111111),
(1,0.48500000),
(1,0.48611111),
(1,0.49444444),
(1,0.49555556),
(1,0.50000000),
(1,0.51000000),
(1,0.51472222),
(1,0.51916667),
(1,0.52638889),
(1,0.52833333),
(1,0.53861111),
(1,0.55055556),
(1,0.55722222),
(1,0.56111111),
(1,0.56750000),
(1,0.56833333),
(1,0.56944444),
(1,0.59500000),
(1,0.59861111),
(1,0.60472222),
(1,0.60750000),
(1,0.61000000),
(1,0.61027778),
(1,0.61138889),
(1,0.61888889),
(1,0.62472222),
(1,0.62916667),
(1,0.63000000),
(1,0.63055556),
(1,0.63138889),
(1,0.63527778),
(1,0.63666667),
(1,0.64277778),
(1,0.64666667),
(1,0.66333333),
(1,0.67277778),
(1,0.68500000),
(1,0.69083333),
(1,0.69916667),
(1,0.70166667),
(1,0.71833333),
(1,0.72527778),
(1,0.72638889),
(1,0.73861111),
(2,0.73944444),
(1,0.75944444),
(1,0.76166667),
(1,0.76194444),
(1,0.77500000),
(1,0.77888889),
(1,0.78888889),
(1,0.80083333),
(1,0.80416667),
(1,0.80611111),
(2,0.80638889),
(1,0.81027778),
(1,0.81277778),
(1,0.81722222),
(1,0.81833333),
(1,0.83138889),
(1,0.84555556),
(1,0.84833333),
(1,0.85138889),
(1,0.85277778),
(1,0.85472222),
(1,0.86444444),
(1,0.86527778),
(1,0.90027778),
(1,0.90500000),
(1,0.92305556),
(1,0.93361111),
(1,0.93527778),
(1,0.94861111),
(2,0.95055556),
(1,0.95222222),
(1,0.96000000),
(1,0.96472222),
(1,0.96527778),
(1,0.97472222),
(1,0.98166667),
(1,1.00555556),
(1,1.00666667),
(1,1.00777778),
(1,1.01250000),
(1,1.01638889),
(1,1.01861111),
(1,1.02111111),
(1,1.02305556),
(1,1.02500000),
(1,1.02666667),
(1,1.02694444),
(1,1.02805556),
(1,1.03000000),
(2,1.03638889),
(1,1.04111111),
(1,1.04222222),
(1,1.04527778),
(1,1.04583333),
(1,1.04750000),
(1,1.04805556),
(1,1.04944444),
(1,1.05138889),
(1,1.05583333),
(1,1.05638889),
(1,1.05944444),
(1,1.05972222),
(1,1.06138889),
(1,1.06250000),
(1,1.06305556),
(1,1.06500000),
(1,1.06666667),
(1,1.06750000),
(1,1.07083333),
(1,1.07277778),
(1,1.07388889),
(1,1.08361111),
(1,1.08666667),
(1,1.09055556),
(1,1.09222222),
(1,1.09805556),
(1,1.11527778),
(1,1.11805556),
(1,1.12777778),
(1,1.13222222),
(1,1.13250000),
(1,1.13361111),
(1,1.13388889),
(1,1.15027778),
(1,1.15194444),
(1,1.15444444),
(1,1.15916667),
(1,1.16111111),
(1,1.16666667),
(1,1.16750000),
(1,1.17222222),
(1,1.17694444),
(1,1.18166667),
(1,1.19305556),
(1,1.20138889),
(1,1.23777778),
(1,1.27861111),
(1,1.29916667),
(1,1.33638889),
(1,1.39750000),
(1,1.42250000),
(1,1.42611111),
(1,1.50111111),
(1,1.61583333),
(1,1.62166667),
(1,1.62500000),
(1,1.63361111),
(1,1.65916667),
(1,1.68666667),
(1,1.68722222),
(1,1.71638889),
(1,1.71861111),
(1,2.10222222),
(1,2.12611111),
(1,2.19416667),
(1,2.20055556),
(1,2.53527778),
(1,2.74638889),
(1,2.79361111),
(1,2.99055556),
(1,3.06277778),
(1,4.24166667),
(1,5.93750000),
(1,8.91694444),
(1,13.53277778),
(1,14.47305556),
(1,14.52361111),
(1,14.55805556),
(1,14.56611111),
(1,14.57138889),
(1,14.60777778),
(1,14.60972222),
(1,14.61722222),
(1,14.61888889),
(1,14.63277778),
(1,14.64305556),
(1,14.66527778),
(1,14.75722222),
(1,14.76000000),
(1,14.91777778),
(1,14.92305556),
(1,15.09722222),
(1,16.23472222),
(1,16.24388889),
(1,16.24722222),
(1,16.29055556),
(1,16.29333333),
(1,59.44916667),
(1,59.71000000),
(1,59.71583333),
(1,59.82138889),
(1,60.05555556),
(1,61.05055556),
(1,61.05694444),
(1,61.11000000),
(1,61.11222222),
(1,61.11527778),
(1,61.11555556),
(1,61.12750000),
(1,61.12972222),
(1,61.13000000),
(1,62.50416667),
(1,62.51166667),
(1,62.52833333),
(1,62.55138889),
(1,62.55750000),
(1,62.56861111),
(1,62.58777778),
(1,62.58833333),
(1,62.59472222),
(1,62.60194444),
(1,62.61416667),
(1,62.64250000),
(1,62.68222222),
(1,62.68805556),
(1,63.12638889),
(1,63.15666667),
(1,63.27472222)
) V(Count,Data))
INSERT INTO [dbo].[YourTable]
SELECT Data
FROM Vals
JOIN Nums ON number <= Count

This is a simplified version of #MartinSmith's answer which avoids calculating two ROW_NUMBERs with different order:
WITH T AS
(
SELECT [Data],
COUNT(*) OVER() AS cnt,
CAST(#Percent * COUNT(*) OVER() AS INT)/2 AS NumToExclude,
ROW_NUMBER() OVER (ORDER BY [Data] ASC) AS RN
FROM [dbo].[YourTable]
WHERE [Data] IS NOT NULL
)
SELECT AVG([Data])
FROM T
WHERE RN > NumToExclude AND RN <= cnt - NumToExclude;

The documentation states
TRIMMEAN(array, percent)
The TRIMMEAN function syntax has the following arguments:
Array Required. The array or range of values to trim and average.
Percent Required. The fractional number of data points to exclude
from the calculation. For example, if percent = 0.2, 4 points are
trimmed from a data set of 20 points (20 x 0.2): 2 from the top and 2
from the bottom of the set.
TRIMMEAN rounds the number of excluded data points down to the nearest
multiple of 2. If percent = 0.1, 10 percent of 30 data points equals 3
points. For symmetry, TRIMMEAN excludes a single value from the top
and bottom of the data set.
Something like the following should simulate the Excel function (demo).
For 0.2 the query returns 0.11107474 which is the same as Excel (and 0.35 returns 0,10455721 which is also the same).
DECLARE #Percent DECIMAL(2,2) = 0.2;
WITH T AS
(
SELECT [Data],
CAST(#Percent * COUNT(*) OVER() AS INT)/2 AS NumToExclude,
ROW_NUMBER() OVER (ORDER BY [Data] ASC) AS RNAsc,
ROW_NUMBER() OVER (ORDER BY [Data] DESC) AS RNDesc
FROM [dbo].[YourTable]
WHERE [Data] IS NOT NULL
)
SELECT AVG([Data])
FROM T
WHERE RNAsc > NumToExclude AND RNDesc > NumToExclude;

Related

sql server, replace chars in string with values in table

how can i replace values in string with values that are in a table?
for example
select *
into #t
from
(
select 'bla'c1,'' c2 union all
select 'table'c1,'TABLE' c2 union all
select 'value'c1,'000' c2 union all
select '...'c1,'' c2
)t1
declare #s nvarchaR(max)='this my string and i want to replace all values that are in table #t'
i have some values in my table and i want to replace C1 with C2 in my string.
the results should be
this my string and i want to replace all 000 that are in TABLE #t
UPDATE:
i solved with a CLR
using System;
using Microsoft.SqlServer.Server;
using System.Data.SqlTypes;
using System.Data.Linq;
namespace ReplaceValues
{
public partial class Functions
{
[SqlFunction
(
//DataAccess = DataAccessKind.Read,
SystemDataAccess = SystemDataAccessKind.Read
)
]
public static string ReplaceValues(string row, string delimitator, string values, string replace/*, bool CaseSensitive*/)
{
//return row;
string[] tmp_values = values.Split(new string[] { delimitator }, StringSplitOptions.None);
string[] tmp_replace = replace.Split(new string[] { delimitator }, StringSplitOptions.None);
row = row.ToUpper();
for (int i = 0; i < Math.Min(tmp_values.Length, tmp_replace.Length); i++)
{
row = row.Replace(tmp_values[i].ToUpper(), tmp_replace[i]);
}
return row;
}
}
}
and then
select *
into #t
from
(
select 'value1'OldValue,'one'NewValue union all
select 'value2'OldValue,'two'NewValue union all
select 'value3'OldValue,'three'NewValue union all
select 'value4'OldValue,'four'NewValue
)t1
select dbo.ReplaceValues(t1.column,'|',t2.v,t2.r)
from MyTable t1
cross apply
(
select dbo.inlineaggr(i1.OldValue,'|',1,1)v,
dbo.inlineaggr(i1.NewValue,'|',1,1)r
from #t i1
)t2
i have to improved it to manage better the case sensitive, but performance are not bad.
(also 'inlineaggr' is a CLR i wrote years ago)
You can do this via recursion. Assuming you have a table of find-replace pairs, you can number the rows and then use recursive cte:
create table #t(c1 nvarchar(100), c2 nvarchar(100));
insert into #t(c1, c2) values
('bla', ''),
('table', 'table'),
('value', '000'),
('...', '');
declare #s nvarchar(max) = 'this my string and i want to replace all values that are in table #t';
with ncte as (
select row_number() over (order by (select null)) as rn, *
from #t
), rcte as (
select rn, replace(#s, c1, c2) as newstr
from ncte
where rn = 1
union all
select ncte.rn, replace(rcte.newstr, ncte.c1, ncte.c2)
from ncte
join rcte on ncte.rn = rcte.rn + 1
)
select *
from rcte
where rn = 4

SQL Query with Concatenated Values. How To Combine Duplicates and Add Identifier?

I can't find an answer to this anywhere – hoping there is one! I'd like to streamline outputted concatenated values where they're the same while retaining a distinct identifier column.
I have a number of different item groups in a table that have associated margin titles and margin percentages set in another table. I can get a full list of all the items using the below code:
SELECT DISTINCT
dbo.OITM.itmsgrpcod AS 'ItemGroupCode',
CONVERT(VARCHAR(8000),
SUBSTRING(
(
SELECT 'itemgroup_' + [dbo].[#FLN_ITG_DISC_HD].[U_FLN_ITGP] + '_' +
[dbo].[#FLN_ITG_DISC_LN].[U_FLN_DSLV]
+ '_' + [dbo].[#FLN_ITG_DISC_LN].[U_FLN_PERC] + ', '
AS 'data()'
FROM [dbo].[#FLN_ITG_DISC_HD] INNER JOIN [dbo].[#FLN_ITG_DISC_LN] ON [dbo].[#FLN_ITG_DISC_HD].[DocEntry] = [dbo].[#FLN_ITG_DISC_LN].[DocEntry]
WHERE [dbo].[#FLN_ITG_DISC_HD].[U_FLN_ITGP] = dbo.OITM.itmsgrpcod
ORDER BY [dbo].[#FLN_ITG_DISC_HD].[U_FLN_ITGP]
FOR XML PATH ('')
), 1, 9999999)
) AS 'PRODUCTTAGS'
FROM
dbo.OITM
INNER JOIN [dbo].[#FLN_ITG_DISC_HD] ON dbo.OITM.itmsgrpcod = [dbo].[#FLN_ITG_DISC_HD].[U_FLN_ITGP]
INNER JOIN [dbo].[#FLN_ITG_DISC_LN] ON [dbo].[#FLN_ITG_DISC_HD].[DocEntry] = [dbo].[#FLN_ITG_DISC_LN].[DocEntry]
Here is the script to clipboard info for the tables:
USE [XXXX]
GO
SELECT [Code]
,[Name]
,[DocEntry]
,[Canceled]
,[Object]
,[LogInst]
,[UserSign]
,[Transfered]
,[CreateDate]
,[CreateTime]
,[UpdateDate]
,[UpdateTime]
,[DataSource]
,[U_FLN_ITGP]
,[U_FLN_IGDC]
FROM [dbo].[#FLN_BP_DISC_ITG_HD]
GO
USE [XXXX]
GO
SELECT [Code]
,[LineId]
,[Object]
,[LogInst]
,[U_FLN_DSLV]
,[U_FLN_DSPR]
FROM [dbo].[#FLN_BP_DISC_ITG_LN]
GO
USE [XXXXXX]
GO
SELECT [ItemCode]
,[ItemName]
,[FrgnName]
,[ItmsGrpCod]
,[CstGrpCode]
,[VatGourpSa]
,[CodeBars]
,[VATLiable]
,[PrchseItem]
,[SellItem]
,[InvntItem]
,[OnHand]
,[IsCommited]
,[OnOrder]
,[IncomeAcct]
,[ExmptIncom]
,[MaxLevel]
,[DfltWH]
,[CardCode]
,[SuppCatNum]
,[BuyUnitMsr]
,[NumInBuy]
,[ReorderQty]
,[MinLevel]
,[LstEvlPric]
,[LstEvlDate]
,[CustomPer]
,[Canceled]
,[MnufctTime]
,[WholSlsTax]
,[RetilrTax]
,[SpcialDisc]
,[DscountCod]
,[TrackSales]
,[SalUnitMsr]
,[NumInSale]
,[Consig]
,[QueryGroup]
,[Counted]
,[OpenBlnc]
,[EvalSystem]
,[UserSign]
,[FREE]
,[PicturName]
,[Transfered]
,[BlncTrnsfr]
,[UserText]
,[SerialNum]
,[CommisPcnt]
,[CommisSum]
,[CommisGrp]
,[TreeType]
,[TreeQty]
,[LastPurPrc]
,[LastPurCur]
,[LastPurDat]
,[ExitCur]
,[ExitPrice]
,[ExitWH]
,[AssetItem]
,[WasCounted]
,[ManSerNum]
,[SHeight1]
,[SHght1Unit]
,[SHeight2]
,[SHght2Unit]
,[SWidth1]
,[SWdth1Unit]
,[SWidth2]
,[SWdth2Unit]
,[SLength1]
,[SLen1Unit]
,[Slength2]
,[SLen2Unit]
,[SVolume]
,[SVolUnit]
,[SWeight1]
,[SWght1Unit]
,[SWeight2]
,[SWght2Unit]
,[BHeight1]
,[BHght1Unit]
,[BHeight2]
,[BHght2Unit]
,[BWidth1]
,[BWdth1Unit]
,[BWidth2]
,[BWdth2Unit]
,[BLength1]
,[BLen1Unit]
,[Blength2]
,[BLen2Unit]
,[BVolume]
,[BVolUnit]
,[BWeight1]
,[BWght1Unit]
,[BWeight2]
,[BWght2Unit]
,[FixCurrCms]
,[FirmCode]
,[LstSalDate]
,[QryGroup1]
,[QryGroup2]
,[QryGroup3]
,[QryGroup4]
,[QryGroup5]
,[QryGroup6]
,[QryGroup7]
,[QryGroup8]
,[QryGroup9]
,[QryGroup10]
,[QryGroup11]
,[QryGroup12]
,[QryGroup13]
,[QryGroup14]
,[QryGroup15]
,[QryGroup16]
,[QryGroup17]
,[QryGroup18]
,[QryGroup19]
,[QryGroup20]
,[QryGroup21]
,[QryGroup22]
,[QryGroup23]
,[QryGroup24]
,[QryGroup25]
,[QryGroup26]
,[QryGroup27]
,[QryGroup28]
,[QryGroup29]
,[QryGroup30]
,[QryGroup31]
,[QryGroup32]
,[QryGroup33]
,[QryGroup34]
,[QryGroup35]
,[QryGroup36]
,[QryGroup37]
,[QryGroup38]
,[QryGroup39]
,[QryGroup40]
,[QryGroup41]
,[QryGroup42]
,[QryGroup43]
,[QryGroup44]
,[QryGroup45]
,[QryGroup46]
,[QryGroup47]
,[QryGroup48]
,[QryGroup49]
,[QryGroup50]
,[QryGroup51]
,[QryGroup52]
,[QryGroup53]
,[QryGroup54]
,[QryGroup55]
,[QryGroup56]
,[QryGroup57]
,[QryGroup58]
,[QryGroup59]
,[QryGroup60]
,[QryGroup61]
,[QryGroup62]
,[QryGroup63]
,[QryGroup64]
,[CreateDate]
,[UpdateDate]
,[ExportCode]
,[SalFactor1]
,[SalFactor2]
,[SalFactor3]
,[SalFactor4]
,[PurFactor1]
,[PurFactor2]
,[PurFactor3]
,[PurFactor4]
,[SalFormula]
,[PurFormula]
,[VatGroupPu]
,[AvgPrice]
,[PurPackMsr]
,[PurPackUn]
,[SalPackMsr]
,[SalPackUn]
,[SCNCounter]
,[ManBtchNum]
,[ManOutOnly]
,[DataSource]
,[validFor]
,[validFrom]
,[validTo]
,[frozenFor]
,[frozenFrom]
,[frozenTo]
,[BlockOut]
,[ValidComm]
,[FrozenComm]
,[LogInstanc]
,[ObjType]
,[SWW]
,[Deleted]
,[DocEntry]
,[ExpensAcct]
,[FrgnInAcct]
,[ShipType]
,[GLMethod]
,[ECInAcct]
,[FrgnExpAcc]
,[ECExpAcc]
,[TaxType]
,[ByWh]
,[WTLiable]
,[ItemType]
,[WarrntTmpl]
,[BaseUnit]
,[CountryOrg]
,[StockValue]
,[Phantom]
,[IssueMthd]
,[FREE1]
,[PricingPrc]
,[MngMethod]
,[ReorderPnt]
,[InvntryUom]
,[PlaningSys]
,[PrcrmntMtd]
,[OrdrIntrvl]
,[OrdrMulti]
,[MinOrdrQty]
,[LeadTime]
,[IndirctTax]
,[TaxCodeAR]
,[TaxCodeAP]
,[OSvcCode]
,[ISvcCode]
,[ServiceGrp]
,[NCMCode]
,[MatType]
,[MatGrp]
,[ProductSrc]
,[ServiceCtg]
,[ItemClass]
,[Excisable]
,[ChapterID]
,[NotifyASN]
,[ProAssNum]
,[AssblValue]
,[DNFEntry]
,[UserSign2]
,[Spec]
,[TaxCtg]
,[Series]
,[Number]
,[FuelCode]
,[BeverTblC]
,[BeverGrpC]
,[BeverTM]
,[Attachment]
,[AtcEntry]
,[ToleranDay]
,[UgpEntry]
,[PUoMEntry]
,[SUoMEntry]
,[IUoMEntry]
,[IssuePriBy]
,[AssetClass]
,[AssetGroup]
,[InventryNo]
,[Technician]
,[Employee]
,[Location]
,[StatAsset]
,[Cession]
,[DeacAftUL]
,[AsstStatus]
,[CapDate]
,[AcqDate]
,[RetDate]
,[GLPickMeth]
,[NoDiscount]
,[MgrByQty]
,[AssetRmk1]
,[AssetRmk2]
,[AssetAmnt1]
,[AssetAmnt2]
,[DeprGroup]
,[AssetSerNo]
,[CntUnitMsr]
,[NumInCnt]
,[INUoMEntry]
,[OneBOneRec]
,[RuleCode]
,[ScsCode]
,[SpProdType]
,[IWeight1]
,[IWght1Unit]
,[IWeight2]
,[IWght2Unit]
,[CompoWH]
,[CreateTS]
,[UpdateTS]
,[VirtAstItm]
,[SouVirAsst]
,[InCostRoll]
,[PrdStdCst]
,[EnAstSeri]
,[LinkRsc]
,[OnHldPert]
,[onHldLimt]
,[PriceUnit]
,[GSTRelevnt]
,[SACEntry]
,[GstTaxCtg]
,[AssVal4WTR]
,[ExcImpQUoM]
,[ExcFixAmnt]
,[ExcRate]
,[SOIExc]
,[TNVED]
,[Imported]
,[AutoBatch]
,[U_AZU_COMM]
,[U_AZU_SUPP]
,[U_AZU_SUPPCF]
,[U_SDB_WHLO]
,[U_SDB_OFLO]
,[U_BA_IsFA]
,[U_BA_TypID]
,[U_BA_NumID]
,[U_BA_LVAFrom]
,[U_BA_LVA]
,[U_BXPArea]
,[U_BXPInvTu]
,[U_BXPLMSIn]
,[U_BXPIsMlt]
,[U_BXPIsSgB]
,[U_BXPCrdCd]
,[U_BXPPmInv]
,[U_BXPPkEmQ]
,[U_BXPPkLcN]
,[U_BXPSLReq]
,[U_BOY_TB_0]
,[U_DisCont]
,[U_DiscRoad]
,[U_DiscMTB]
,[U_DiscTrack]
,[U_DiscAll]
,[U_ParentDescription]
,[U_WebEnabled]
,[U_DiscCX]
,[U_DiscTRI]
,[U_Composition]
,[U_ParentSKUCode]
,[U_Col1Code]
,[U_Col2Code]
,[U_Col3Code]
,[U_Col4Code]
,[U_Col5Code]
,[U_Size]
,[U_Season]
,[U_AttributeGroup]
,[U_Col1Desc]
,[U_Col2Desc]
,[U_Col3Desc]
,[U_Col4Desc]
,[U_Col5Desc]
,[U_Gender]
,[U_DiscHybrid]
,[U_DiscBMX]
,[U_Discontinued]
,[U_Colour]
,[U_ExcludeLoyaltyProm]
,[U_IsB2CSaleable]
,[U_Merchandising]
,[U_U_ShopifyEnabled]
,[U_U_ShopifyHandle]
,[U_U_ShopifyTitle]
,[U_U_ShopifyVendor]
,[U_Exclusive]
,[U_Outlet]
,[U_SyncFlag]
,[U_SyncB2C]
,[U_ParentSKU]
,[U_VariantWebID]
,[U_VariantAdded]
,[U_InventoryItemId]
,[U_Range]
,[U_FLN_TC_TaskId]
,[U_FLN_TC_Date]
,[U_FLN_TC_Time]
,[U_PMX_HBBD]
,[U_PMX_SLID]
,[U_PMX_RETR]
,[U_PMX_ICRI]
,[U_PMX_LOCA]
,[U_PMX_LOUN]
,[U_PMX_ILRP]
,[U_PMX_ILRC]
,[U_PMX_NRSN]
,[U_PMX_NRSR]
,[U_PMX_QSPR]
,[U_PMX_QSCR]
,[U_PMX_QSRC]
,[U_PMX_RQSR]
,[U_PMX_QSSR]
,[U_PMX_SBCP]
,[U_PMX_BCTY]
,[U_PMX_EXDP]
,[U_PMX_EXDR]
,[U_PMX_PBCT]
,[U_PMX_PBCO]
,[U_PMX_SBCT]
,[U_PMX_SBCO]
,[U_PMX_PILR]
,[U_PMX_UOMD]
,[U_PMX_UOM2]
,[U_PMX_UM2D]
,[U_PMX_DQLU]
,[U_PMX_DQU2]
,[U_PMX_HBN2]
,[U_PMX_SEVE]
,[U_PMX_SQTY]
,[U_PMX_ALUS]
,[U_PMX_PREM]
,[U_PMX_PIMG]
,[U_PMX_PURM]
,[U_PMX_PURP]
,[U_PMX_SARM]
,[U_PMX_SARP]
,[U_PMX_VIDE]
,[U_PMX_CUDE]
,[U_PMX_HNVA]
,[U_PMX_LSPT]
,[U_PMX_AMBS]
,[U_PMX_PRMR]
,[U_PMX_DLCS]
,[U_PMX_DLCP]
,[U_PMX_SLDR]
,[U_PMX_HSER]
,[U_PMX_TLSN]
,[U_PMX_ASLA]
,[U_PMX_APPP]
,[U_PMX_APDP]
,[U_PMX_DPQT]
,[U_PMX_PILP]
,[U_PMX_RQPL]
,[U_PMX_ESQM]
,[U_PMX_PITY]
,[U_PMX_SNFO]
,[U_PMX_PTY2]
,[U_PMX_UFTR]
,[U_PMX_AQIL]
,[U_PMX_UWAF]
,[U_PMX_BUPQ]
,[U_PMX_CSOR]
,[U_PMX_3PIP]
,[U_PMX_3POP]
,[U_PMX_PIBP]
,[U_PMX_NIOP]
,[U_PMX_FSNC]
,[U_PMX_ISLT]
,[U_PMX_WOST]
,[U_PMX_WCRE]
,[U_PMX_ICAW]
,[U_PMX_UMMT]
,[U_PMX_U2MT]
,[U_PMX_UMIT]
,[U_PMX_UMPU]
,[U_PMX_UMSA]
,[U_PMX_CSUM]
,[U_PMX_CSU2]
,[U_PMX_DQUM]
,[U_PMX_CWTO]
,[U_PMX_PCSA]
,[U_PMX_PCPU]
,[U_PMX_CWSC]
,[U_PMX_CWQC]
,[U_PMX_PRWD]
,[U_SyncB2B]
,[U_Clearance]
,[U_SpecialOrder]
,[U_ExpCommCode]
,[U_NotForResale]
,[U_Merch_Hierachy]
,[U_Current]
,[U_SyncB2BUK]
,[U_Composition_1]
,[U_Composition_2]
,[U_Composition_3]
,[U_Composition_4]
,[U_Composition_6]
,[U_Composition_7]
,[U_Composition_8]
,[U_Composition_9]
,[U_Composition_10]
,[U_Composition_11]
,[U_Composition_12]
,[U_Composition_13]
,[U_Composition_14]
,[U_Composition_15]
,[U_Composition_17]
,[U_Composition_18]
,[U_Composition_19]
,[U_Composition_20]
,[U_Composition_5]
,[U_Composition_16]
,[U_ImpCommCode]
,[U_SyncB2BIE]
,[U_WarrantyItem]
,[U_EmbargoDate]
,[U_LaunchDate]
,[U_SMUItem]
,[U_ManSize]
,[U_ShopifyProdType]
,[U_AgeRange]
,[U_SeasonType]
,[U_DiscGravel]
FROM [dbo].[OITM]
GO
This outputs data in two columns with all the distinct item groups and corresponding concatenation of margin levels like this:
However, while all the ItemGroupCodes are distinct, some rows in the ProductTags column are identical to others with the exception of the ItemGroup number. Where this is the case, I'd like to replace this number with a common identifier. So for example, where ItemGroups 100, 101 and 108 have all the same margin values, the number in the concatenated values would be replaced with GROUPA, so the output will become:
The reasoning is that I can then put this data into a temp table and call it into another query listing all SKUs based on the itemgroup number.

DAX expression for ROW_NUMBER() PARTITION BY ORDER BY equivalent

I have a SQL statement like this:
(ROW_NUMBER() OVER (PARTITION BY a.[market], [MEASURE_TYPE]
ORDER BY AM, REP, ORDER_KEY)) AS ORDER_KEY
I want to write a DAX to implement the above SQL statement.
This is not as simple in DAX as in SQL. Here is an example:
Order Key Within Partition =
VAR CurrentMarket = [Market]
VAR CurrentMeasureType = [MeasureType]
VAR CurrentAM = [AM]
VAR CurrentREP = [REP]
VAR CurrentOrderKey = [OrderKey]
VAR CurrentPartition = FILTER (
a, -- the table name
[Market] = CurrentMarket
&& [MeasureType] = CurrentMeasureType
)
RETURN SUMX (
CurrentPartition,
IF (
ISONORAFTER (
CurrentAM, [AM], ASC,
CurrentREP, [REP], ASC,
CurrentOrderKey, [OrderKey], ASC
),
1
)
)
EDIT: Power Query would be better to achieve this.
let
/* Steps so far */
Source = ...,
...
a = ...,
/* End of steps so far */
/* Add steps below to add Order Key Within Partition column */
Partitions = Table.Group(
a,
{"Market", "MeasureType"}, {"Partition", each _}
)[Partition],
AddedOrderKeys = List.Transform(
Partitions,
each Table.AddIndexColumn(
Table.Sort(_, {"AM", "REP", "OrderKey"}),
"Order Key Within Partition",
1
)
),
Result = Table.Combine(AddedOrderKeys)
in
Result
I contribute with an alternative solution to the RANKX. The answer containing the Power Query is the correct one because avoid using calculated columns.
Sales[Sequence by Customer] =
VAR CurrentDate = Sales[Date]
VAR CurrentTime = Sales[Time]
RETURN COUNTROWS (
    FILTER (
        CALCULATETABLE (
            Sales,
            ALLEXCEPT ( Sales, Sales[Customer] )
        ),
        Sales[Date] < CurrentDate
          || ( Sales[Date] = CurrentDate
               && Sales[Time] <= CurrentTime )
    )
)
Source

Convert SQL to Linq select in where

I have three table below:
TABLE_PRODUCT (IdProduct, ProductName, ProductUnit)
TABLE_STORE_HOUSE (IdContain, IdProduct, ProductNumber, TimeInput)
TABLE_SELL (IdSell, IdContain, ProductNumberSell, TimeSell)
Current, How to using LinQ query get TABLE_STORE_HOUSE.IdProduct witch condition TABLE_STORE_HOUSE.ProductNumber - Sum(TABLE_SELL.ProductNumberSell) > 0 and TABLE_STORE_HOUSE.TimeInput is smallest
Help me convert Sql to Linq..............
select top 1 IdContain
from
TABLE_STORE_HOUSE
where IdProduct = '6'
and
ProductNumber - (select sum(ProductNumber)
from TABLE_SELL
Where TABLE_SELL.IdContain = IdContain)> 0
order by TimeInput desc;
Can you try this?
from t in TABLE_STORE_HOUSEs
let TSell = (
from s in TABLE_SELLs
where s.IdContain == t.IdContain
orderby s.ProductNumber
select new {
s.ProductNumber
}
)
where t.IdProduct == 6 && (t.ProductNumber - TSell.Sum(si => si.ProductNumber)) > 0
select new { t.IdContain }
for top 1 you can use Take() function.

How to transform path/string by collapsing repeated elements?

There is a filed in my table that represents pathways like below:
Item1->Item1->Item2-> Item3->Item3->Item3->Item1
In most cases this is quite looong sequence with many instances of same consecutive Items.
How I can shorted above path to something like below? in BigQuery!
Item1(x2)->Item2->Item3(x3)->Item1
I wanted to convince myself that this was possible just through array manipulation (using standard SQL), and I came up with a solution. An alternate way to solve the problem would be to use analytic functions, where you could detect changes in item along the path.
CREATE TEMPORARY FUNCTION PartsToString(
parts_and_offsets ARRAY<STRUCT<part STRING, off INT64>>) AS ((
SELECT
STRING_AGG(
CONCAT(part_and_offset.part,
IF(parts_and_offsets[OFFSET(off + 1)].off - part_and_offset.off = 1,
"",
CONCAT("(x", CAST(parts_and_offsets[OFFSET(off + 1)].off - part_and_offset.off AS STRING), ")"))))
FROM UNNEST(parts_and_offsets) AS part_and_offset WITH OFFSET off
WHERE off + 1 < ARRAY_LENGTH(parts_and_offsets)
));
CREATE TEMPORARY FUNCTION PathwayToParts(pathway STRING) AS ((
SELECT
ARRAY_CONCAT(
ARRAY_AGG(
STRUCT(part, off)),
[STRUCT("" AS part, ARRAY_LENGTH(ANY_VALUE(parts)) AS off)]) AS parts_and_offsets
FROM (SELECT SPLIT(pathway, "->") AS parts),
UNNEST(parts) AS part WITH OFFSET off
WHERE off = 0 OR part != parts[OFFSET(off - 1)]
));
WITH YourTable AS (
SELECT "Item1->Item2->Item2->Item2->Item3->Item1->Item1->Item1->Item1->Item1->Item1->Item1->Item2->Item3->Item3->Item2->Item2->Item2->Item2->Item2->Item2->Item2->Item2->Item2->Item2->Item2->Item2->Item2->Item2->Item2->Item2->Item2->Item2->Item2->Item2->Item2->Item2->Item2->Item2->Item2->Item2->Item2->Item2->Item2->Item2->Item2->Item2->Item2->Item2->Item2->Item2->Item2->Item2->Item2->Item2->Item2->Item2->Item2->Item2->Item2->Item2->Item2->Item2->Item2->Item2->Item2->Item2->Item2->Item2->Item2->Item2->Item2->Item2->Item2->Item2->Item2->Item2->Item2->Item2->Item1->Item1->Item1->Item1->Item1->Item1->Item1->Item1->Item1->Item1->Item1->Item1->Item1->Item1->Item1->Item1->Item1->Item4" AS pathway
UNION ALL SELECT "Item1->Item2->Item2->Item3->Item1->Item1->Item1->Item2->Item3->Item3->Item2->Item2->Item2->Item1->Item4" AS pathway
UNION ALL SELECT "Item1->Item1->Item1" AS pathway
UNION ALL SELECT "Item1->Item2->Item2" AS pathway
UNION ALL SELECT "Item1->Item1->Item2" AS pathway
UNION ALL SELECT "Item1->Item2->Item3" AS pathway
)
SELECT PartsToString(PathwayToParts(pathway)) AS parts_string
FROM YourTable;
Using Scalar JS UDF (Standard SQL) <-- would be my choice
CREATE TEMPORARY FUNCTION collapse_repeated(pathway STRING)
RETURNS STRING LANGUAGE js AS """
var items = pathway.split('->');
short = ''; elem = items[0]; count = 0;
for (var i = 0; i < items.length; i++) {
if (items[i] !== elem) {
if (short.length > 0) {short += '->'}
short += elem; if (count > 1) {short += '(x' + count.toString() + ')';}
elem = items[i]; count = 1;
} else {
count++;
}
}
if (short.length > 0) {short += '->'}
short += elem; if (count > 1) {short += '(x' + count.toString() + ')';}
return short;
""";
WITH YourTable AS (
SELECT "Item1->Item2->Item2->Item2->Item3->Item1->Item1->Item1->Item1->Item1->Item1->Item1->Item2->Item3->Item3->Item2->Item2->Item2->Item2->Item2->Item2->Item2->Item2->Item2->Item2->Item2->Item2->Item2->Item2->Item2->Item2->Item2->Item2->Item2->Item2->Item2->Item2->Item2->Item2->Item2->Item2->Item2->Item2->Item2->Item2->Item2->Item2->Item2->Item2->Item2->Item2->Item2->Item2->Item2->Item2->Item2->Item2->Item2->Item2->Item2->Item2->Item2->Item2->Item2->Item2->Item2->Item2->Item2->Item2->Item2->Item2->Item2->Item2->Item2->Item2->Item2->Item2->Item2->Item2->Item1->Item1->Item1->Item1->Item1->Item1->Item1->Item1->Item1->Item1->Item1->Item1->Item1->Item1->Item1->Item1->Item1->Item4" AS pathway
UNION ALL SELECT "Item1->Item2->Item2->Item3->Item1->Item1->Item1->Item2->Item3->Item3->Item2->Item2->Item2->Item1->Item4" AS pathway
UNION ALL SELECT "Item1->Item1->Item1" AS pathway
UNION ALL SELECT "Item1->Item2->Item2" AS pathway
)
SELECT collapse_repeated(pathway) AS shorten_pathway, pathway
FROM YourTable
Note: Same JS can be easily “translated” to JS UDF in Legacy SQL
Using Window Functions (Legacy SQL)
SELECT GROUP_CONCAT_UNQUOTED(IF(repeats=1, item, CONCAT(item, "(x", STRING(repeats), ")")), "->"), pathway
FROM (
SELECT MIN(pos) AS ord, MIN(item) AS item, COUNT(1) AS repeats, pathway
FROM (
SELECT item, pos, IFNULL(grp, 0)AS grp, pathway FROM (
SELECT item, pos, SUM(change) OVER(PARTITION BY pathway ORDER BY pos ROWS BETWEEN UNBOUNDED PRECEDING AND 1 PRECEDING) AS grp, pathway
FROM (
SELECT item, pos, IF(item=next_item, 0, 1) AS change, pathway FROM (
SELECT item, pos, LEAD(item) OVER(PARTITION BY pathway ORDER BY pos) AS next_item, pathway
FROM (
SELECT item, POSITION(item) AS pos, pathway FROM (
SELECT SPLIT(pathway, "->") AS item, pathway FROM
(SELECT "Item1->Item2->Item2->Item2->Item3->Item1->Item1->Item1->Item1->Item1->Item1->Item1->Item2->Item3->Item3->Item2->Item2->Item2->Item2->Item2->Item2->Item2->Item2->Item2->Item2->Item2->Item2->Item2->Item2->Item2->Item2->Item2->Item2->Item2->Item2->Item2->Item2->Item2->Item2->Item2->Item2->Item2->Item2->Item2->Item2->Item2->Item2->Item2->Item2->Item2->Item2->Item2->Item2->Item2->Item2->Item2->Item2->Item2->Item2->Item2->Item2->Item2->Item2->Item2->Item2->Item2->Item2->Item2->Item2->Item2->Item2->Item2->Item2->Item2->Item2->Item2->Item2->Item2->Item2->Item1->Item1->Item1->Item1->Item1->Item1->Item1->Item1->Item1->Item1->Item1->Item1->Item1->Item1->Item1->Item1->Item1->Item4" AS pathway),
(SELECT "Item1->Item2->Item2->Item3->Item1->Item1->Item1->Item2->Item3->Item3->Item2->Item2->Item2->Item1->Item4" AS pathway),
(SELECT "Item1->Item1->Item1" AS pathway),
(SELECT "Item1->Item2->Item2" AS pathway)
)
)
)
)
)
)
GROUP BY grp, pathway
ORDER BY ord
)
GROUP BY pathway