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

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.

Related

How to store multiple circles in a single PostGIS geometry column?

I need to store multiple circles in a single geometry column in PostGIS.
So far I can store only one like this:
UPDATE element SET geo = ST_Buffer(ST_MakePoint(-71.1043443253471, 42.3150676015829), 6, 'quad_segs=8') WHERE id = 1;
Is there a way to use GEOMETRYCOLLECTION here?
Thanks in advance.
Aggregate your buffers using ST_Union and then create the collection with ST_ForceCollection, e.g. to append a buffer to an existing geometry:
UPDATE element
SET geo =
ST_ForceCollection(
ST_Union(geo, ST_Buffer(ST_MakePoint(-80.10, 45.31), 3) ))
WHERE id = 1;
Demo: db<>fiddle
SELECT
ST_ForceCollection(
ST_Union(
ST_Buffer(ST_MakePoint(-71.10, 42.31), 6),
ST_Buffer(ST_MakePoint(-80.10, 45.31), 3)
));
st_astext
------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
GEOMETRYCOLLECTION(POLYGON((-77.1576441587903 44.72472903395162,-77.32836140246613 44.161949702904735,-77.60559116309236 43.6432893009412,-77.97867965644035 43.188679656440364,-78.43328930094118 42.81559116309237,-78.95194970290473 42.538361402466144,-79.51472903395161 42.36764415879031,-80.1 42.31,-80.68527096604838 42.36764415879031,-81.24805029709526 42.53836140246614,-81.76671069905879 42.815591163092364,-82.22132034355963 43.18867965644036,-82.59440883690763 43.64328930094119,-82.87163859753385 44.16194970290473,-83.04235584120968 44.72472903395161,-83.1 45.309999999999995,-83.04235584120968 45.89527096604838,-82.87163859753386 46.45805029709526,-82.59440883690763 46.9767106990588,-82.22132034355964 47.431320343559634,-81.7667106990588 47.80440883690763,-81.24805029709528 48.08163859753386,-80.68527096604839 48.25235584120969,-80.10000000000001 48.31,-79.51472903395162 48.2523558412097,-78.95194970290474 48.08163859753387,-78.4332893009412 47.80440883690765,-77.97867965644036 47.431320343559655,-77.60559116309237 46.97671069905883,-77.32836140246614 46.45805029709529,-77.1576441587903 45.89527096604841,-77.1 45.31,-77.1576441587903 44.72472903395162)),POLYGON((-65.21528831758062 41.139458067903234,-65.55672280493228 40.01389940580947,-66.11118232618472 38.976578601882395,-66.8573593128807 38.06735931288072,-67.76657860188237 37.32118232618473,-68.80389940580945 36.766722804932286,-69.92945806790321 36.42528831758062,-71.09999999999998 36.31,-72.27054193209675 36.425288317580616,-73.39610059419053 36.76672280493228,-74.4334213981176 37.321182326184726,-75.34264068711927 38.06735931288071,-76.08881767381526 38.97657860188238,-76.64327719506771 40.01389940580945,-76.98471168241937 41.13945806790321,-77.1 42.30999999999998,-76.98471168241937 43.48054193209675,-76.64327719506772 44.60610059419052,-76.08881767381529 45.643421398117596,-75.3426406871193 46.55264068711927,-74.43342139811763 47.29881767381526,-73.39610059419056 47.85327719506771,-72.27054193209679 48.19471168241938,-71.10000000000002 48.31,-69.92945806790325 48.19471168241939,-68.80389940580949 47.85327719506773,-67.76657860188242 47.29881767381529,-66.85735931288073 46.55264068711931,-66.11118232618475 45.643421398117646,-65.55672280493229 44.60610059419057,-65.21528831758062 43.48054193209681,-65.1 42.31,-65.21528831758062 41.139458067903234)))
(1 row)
Or using a CTE
WITH j (geom) AS (
VALUES (ST_Buffer(ST_MakePoint(-71.10, 42.31), 6)),
(ST_Buffer(ST_MakePoint(-80.10, 45.31), 3))
)
SELECT ST_ForceCollection(ST_Union(geom)) FROM j;
st_astext
------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
GEOMETRYCOLLECTION(POLYGON((-77.1576441587903 44.72472903395162,-77.32836140246613 44.161949702904735,-77.60559116309236 43.6432893009412,-77.97867965644035 43.188679656440364,-78.43328930094118 42.81559116309237,-78.95194970290473 42.538361402466144,-79.51472903395161 42.36764415879031,-80.1 42.31,-80.68527096604838 42.36764415879031,-81.24805029709526 42.53836140246614,-81.76671069905879 42.815591163092364,-82.22132034355963 43.18867965644036,-82.59440883690763 43.64328930094119,-82.87163859753385 44.16194970290473,-83.04235584120968 44.72472903395161,-83.1 45.309999999999995,-83.04235584120968 45.89527096604838,-82.87163859753386 46.45805029709526,-82.59440883690763 46.9767106990588,-82.22132034355964 47.431320343559634,-81.7667106990588 47.80440883690763,-81.24805029709528 48.08163859753386,-80.68527096604839 48.25235584120969,-80.10000000000001 48.31,-79.51472903395162 48.2523558412097,-78.95194970290474 48.08163859753387,-78.4332893009412 47.80440883690765,-77.97867965644036 47.431320343559655,-77.60559116309237 46.97671069905883,-77.32836140246614 46.45805029709529,-77.1576441587903 45.89527096604841,-77.1 45.31,-77.1576441587903 44.72472903395162)),POLYGON((-65.21528831758062 41.139458067903234,-65.55672280493228 40.01389940580947,-66.11118232618472 38.976578601882395,-66.8573593128807 38.06735931288072,-67.76657860188237 37.32118232618473,-68.80389940580945 36.766722804932286,-69.92945806790321 36.42528831758062,-71.09999999999998 36.31,-72.27054193209675 36.425288317580616,-73.39610059419053 36.76672280493228,-74.4334213981176 37.321182326184726,-75.34264068711927 38.06735931288071,-76.08881767381526 38.97657860188238,-76.64327719506771 40.01389940580945,-76.98471168241937 41.13945806790321,-77.1 42.30999999999998,-76.98471168241937 43.48054193209675,-76.64327719506772 44.60610059419052,-76.08881767381529 45.643421398117596,-75.3426406871193 46.55264068711927,-74.43342139811763 47.29881767381526,-73.39610059419056 47.85327719506771,-72.27054193209679 48.19471168241938,-71.10000000000002 48.31,-69.92945806790325 48.19471168241939,-68.80389940580949 47.85327719506773,-67.76657860188242 47.29881767381529,-66.85735931288073 46.55264068711931,-66.11118232618475 45.643421398117646,-65.55672280493229 44.60610059419057,-65.21528831758062 43.48054193209681,-65.1 42.31,-65.21528831758062 41.139458067903234)))
(1 row)

How to get other column value in different table into the query?

I had searching application, finding personal information which had been filtered by some criteria (category, years of experience etc)
I had problem with the last filter, 'tempoh perkhidmatan by negeri'. I need to calculate the number of working experience by state(negeri). For example, when searching for people of 5 years in the state(negeri) 'x', the sql will sum years of experience of each person in the state selected.
This is the full code of SQL searching by criteria:
$query = DB::table('itemregistrations')
->join('sections', 'itemregistrations.SectionID', '=', 'sections.SectionID')
->join('categories', 'itemregistrations.CategoryID', '=', 'categories.CategoryID')
->join('operasi', 'itemregistrations.OperasiID', '=', 'operasi.OperasiID')
->join('negeri', 'itemregistrations.NegeriID', '=', 'negeri.NegeriID')
->join('gred', 'itemregistrations.GredID', '=', 'gred.GredID')
->where('itemregistrations.statusProID', '=', 1)
->select('itemregistrations.name','sections.sectionname', 'categories.categoryname', 'operasi.operasiname', 'itemregistrations.Nobadan', 'itemregistrations.lahir_yy', 'itemregistrations.pdrm_yy', 'gred.namagred', 'itemregistrations.itemRegistrationID', '');
if($request->input('negeri_lahir') != ''){
$query->where('itemregistrations.NegeriID', $request->input('negeri_lahir'));
}
if($request->input('kategori') != '') {
$query->where('itemregistrations.CategoryID', $request->input('kategori'));
}
if($request->input('pangkat') != '') {
$query->where('itemregistrations.OperasiID', $request->input('pangkat'));
}
if(request('umur')) {
$query->whereRaw('YEAR(CURDATE()) - lahir_yy >= ?', [request('umur')]);
}
if($request->input('gred') != '') {
$query->where('itemregistrations.GredID', $request->input('gred'));
}
if(request('tempoh')) {
$query->whereRaw('YEAR(CURDATE()) - pdrm_yy >= ?', [request('tempoh')]);
}
if($request->input('negeri_perkhidmatan') != '') {
$query->join('itemregistrationpangkat', 'itemregistrationpangkat.itemRegistrationID', '=', 'itemregistrations.itemRegistrationID')
->where('itemregistrationpangkat.NegeriID', $request->input('negeri_perkhidmatan'));
}
if(request('tempoh_negeri')) {
$query->select(DB::raw('m.itemRegistrationID, sum(m.duration)'))
->from(DB::raw('(SELECT itemRegistrationID, NegeriID, yeartamatkhidmat - yearmulakhidmat as duration FROM itemregistrationpangkat) AS m
RIGHT JOIN itemregistrations ON itemregistrations.itemRegistrationID=m.itemRegistrationID'))
->distinct()
->groupBy('m.itemRegistrationID');
}
$newitem = $query->get();
return response::json($newitem);
The code involve to be solve is this(the last filter):
if(request('tempoh_negeri')) {
$query->select(DB::raw('m.itemRegistrationID, m.NegeriID, sum(distinct m.duration)'))
->from(DB::raw('(SELECT itemRegistrationID, NegeriID, yeartamatkhidmat - yearmulakhidmat as duration FROM itemregistrationpangkat) AS m
RIGHT JOIN itemregistrations ON itemregistrations.itemRegistrationID=m.itemRegistrationID'))
->groupBy('m.itemRegistrationID', 'm.NegeriID');
}
The problem is I need to get name column, sectionID column, CategoryID, OperasiID, NegeriID, GredID, from itemregistrations table from the $query statement. How to combine the last query filter in 'tempoh_negeri' with the previous one?
I didn't know about Laravel in particular, so I had much trouble trying to understand how your query was built, but this syntax seems to enable people to write a request by adding chunks, but not necessarily in the right order. So here's what I believe your query is supposed to do, for SQL speakers:
SELECT itemregistrations .name,
sections .sectionname,
categories .categoryname,
operasi .operasiname,
itemregistrations .Nobadan,
itemregistrations .lahir_yy,
itemregistrations .pdrm_yy,
gred .namagred,
itemregistrations .itemRegistrationID
-- if($tempoh_negeri) (request)
,m .itemRegistrationID,
sum(m.duration)
FROM itemregistrations
-- if($tempoh_negeri) (request)
,(SELECT DISTINCT itemRegistrationID,
NegeriID,
yeartamatkhidmat - yearmulakhidmat as duration
FROM itemregistrationpangkat) AS m
RIGHT JOIN itemregistrations
ON itemregistrations.itemRegistrationID = m.itemRegistrationID
JOIN sections
ON itemregistrations.SectionID = sections.SectionID
JOIN categories
ON itemregistrations.CategoryID = categories.CategoryID
JOIN operasi
ON itemregistrations.OperasiID = operasi.OperasiID
JOIN negeri
ON itemregistrations.NegeriID = negeri.NegeriID
JOIN gred
ON itemregistrations.GredID = gred.GredID
-- if($negeri_perkhidmatan)
JOIN itemregistrationpangkat
ON itemregistrationpangkat.itemRegistrationID = itemregistrations.itemRegistrationID
WHERE itemregistrations.statusProID = 1
-- if($negeri_lahir) (WHERE)
AND itemregistrations.NegeriID = $negeri_lahir
-- if($kategori) (WHERE)
AND itemregistrations.CategoryID = $kategori
-- if($pangkat) (WHERE)
AND itemregistrations.OperasiID = $pangkat
-- if(umur) (WHERERAW) (request)
AND YEAR(CURDATE()) - lahir_yy >= umur
-- if($gred) (WHERE)
AND itemregistrations.GredID = $gred
-- if($tempoh) (WHERERAW) (request)
AND YEAR(CURDATE()) - pdrm_yy >= tempoh
-- if($negeri_perkhidmatan)
AND itemregistrationpangkat.NegeriID = $negeri_perkhidmatan
-- if($tempoh_negeri) (request)
GROUP BY m.itemRegistrationID
If it's so, you cannot do what you want following that way (including main columns into the subquery) because your subquery will be evaluated BEFORE the main one is.
Instead, you need to write a proper filter at main query level, that is : among the others "JOIN…ON" clauses already in place. Which would give:
LEFT JOIN itemregistrationpangkat
ON itemregistrationpangkat.itemRegistrationID = itemregistrations.itemRegistrationID
… then specify the substraction directly in your sum() function
sum(yeartamatkhidmat - yearmulakhidma)
As regards Lavarel, this probably would give something like:
if(request('tempoh_negeri')) {
$query->leftjoin('itemregistrationpangkat','itemRegistrationID','=','itemregistrations.itemRegistrationID');
$query->select(DB:raw('sum(yeartamatkhidmat - yearmulakhidmat'));
}

How calculate TRIMMEAN in SQL Server 2012?

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;

MDX - multiple filters on different dimension with OR condition

I have a problem with MDX querying.
I have one measure WEIGHT and two dimensions DESTINATION and SOURCE with the same attributes: NAME and TYPE.
I want to return:
SUM of WEIGHT
where
DESTINATION.TYPE="A"
**OR**
SOURCE.TYPE="B"
**AND**
(DESTINATION.TYPE **<>** SOURCE.TYPE)
If try this:
SELECT NON EMPTY {
[Measures].[Weight]
}
ON COLUMNS,
NON EMPTY {
([Source].[Name].[Name].ALLMEMBERS * [Destination].[Name].[Name].ALLMEMBERS )
}
ON ROWS
FROM
( SELECT ( { [Source].[Type].&[A] } ) ON COLUMNS FROM ( SELECT ( { [Destination].[Type].&[B] } )
ON COLUMNS FROM [CUBE])) WHERE ( [Destination].[Type].&[B], [Source].[Type].&[A] )
But it doesn't work.
In SQL it look like
Select source.name, destination.name, sum(weight) from cube
where
(source.type = "A" or destination.type = "b")
and
(source.type <> destination.type)
group by source.name, destination.name, source.type, destination.type
Your From section is a bit messy. Try the following:
SELECT
NON EMPTY { [Measures].[Weight] } ON COLUMNS,
NON EMPTY { [Source].[Name].[Name].ALLMEMBERS * [Destination].[Name].[Name].ALLMEMBERS } ON ROWS
FROM [CUBE]
WHERE ( {[Destination].[Type].&[B]} * {[Source].[Type].[All]} + {[Destination].[Type].[All]} * {[Source].[Type].&[A]} )

How To Split Pipe-Delimited Column and insert each value into new table Once?

I have an old database with a gazillion records (more or less) that have a single tags column (with tags being pipe-delimited) that looks like so:
Breakfast
Breakfast|Brunch|Buffet|Burger|Cakes|Crepes|Deli|Dessert|Dim Sum|Fast Food|Fine Wine|Spirits|Kebab|Noodles|Organic|Pizza|Salad|Seafood|Steakhouse|Sushi|Tapas|Vegetarian
Breakfast|Brunch|Buffet|Burger|Deli|Dessert|Fast Food|Fine Wine|Spirits|Noodles|Pizza|Salad|Seafood|Steakhouse|Vegetarian
Breakfast|Brunch|Buffet|Cakes|Crepes|Dessert|Fine Wine|Spirits|Salad|Seafood|Steakhouse|Tapas|Teahouse
Breakfast|Brunch|Burger|Crepes|Salad
Breakfast|Brunch|Cakes|Dessert|Dim Sum|Noodles|Pizza|Salad|Seafood|Steakhouse|Vegetarian
Breakfast|Brunch|Cakes|Dessert|Dim Sum|Noodles|Pizza|Salad|Seafood|Vegetarian
Breakfast|Brunch|Deli|Dessert|Organic|Salad
Breakfast|Brunch|Dessert|Dim Sum|Hot Pot|Seafood
Breakfast|Brunch|Dessert|Dim Sum|Seafood
Breakfast|Brunch|Dessert|Fine Wine|Spirits|Noodles|Pizza|Salad|Seafood
Breakfast|Brunch|Dessert|Fine Wine|Spirits|Salad|Vegetarian
Is there a way one could retrieve each tag and insert it into a new table tag_id | tag_nm using MySQL only?
Here is my attempt which uses PHP..., I imagine this could be more efficient with a clever MySQL query. I've placed the relationship part of it there too. There's no escaping and error checking.
$rs = mysql_query('SELECT `venue_id`, `tag` FROM `venue` AS a');
while ($row = mysql_fetch_array($rs)) {
$tag_array = explode('|',$row['tag']);
$venueid = $row['venue_id'];
foreach ($tag_array as $tag) {
$rs2 = mysql_query("SELECT `tag_id` FROM `tag` WHERE tag_nm = '$tag'");
$tagid = 0;
while ($row2 = mysql_fetch_array($rs2)) $tagid = $row2['tag_id'];
if (!$tagid) {
mysql_execute("INSERT INTO `tag` (`tag_nm`) VALUES ('$tag')");
$tagid = mysql_insert_id;
}
mysql_execute("INSERT INTO `venue_tag_rel` (`venue_id`, `tag_id`) VALUES ($venueid, $tagid)");
}
}
After finding there is no official split function I've solved the issue using only MySQL like so:
1: I created the function strSplit
CREATE FUNCTION strSplit(x varchar(21845), delim varchar(255), pos int) returns varchar(255)
return replace(
replace(
substring_index(x, delim, pos),
substring_index(x, delim, pos - 1),
''
),
delim,
''
);
Second I inserted the new tags into my new table (real names and collumns changed, to keep it simple)
INSERT IGNORE INTO tag (SELECT null, strSplit(`Tag`,'|',1) AS T FROM `old_venue` GROUP BY T)
Rinse and repeat increasing the pos by one for each collumn (in this case I had a maximum of 8 seperators)
Third to get the relationship
INSERT INTO `venue_tag_rel`
(Select a.`venue_id`, b.`tag_id` from `old_venue` a, `tag` b
WHERE
(
a.`Tag` LIKE CONCAT('%|',b.`tag_nm`)
OR a.`Tag` LIKE CONCAT(b.`tag_nm`,'|%')
OR a.`Tag` LIKE CONCAT(CONCAT('%|',b.`tag_nm`),'|%')
OR a.`Tag` LIKE b.`tag_nm`
)
)