Select two statements in one view - sql

I have View called VW_STOCKOPNAME
CREATE OR REPLACE VIEW VW_STOCKOPNAME AS SELECT
A.M_STOCKCODE_KL_ID,
A.M_STOCKCODE_JB_ID,
A.M_STOCKCODE_NB_ID,
A.M_STOCKCODE_SB_ID,
B."NAME" AS S1_KL,
C."NAME" AS S2_JB,
D."NAME" AS S3_NB,
E."NAME" AS S4_SB,
A.M_ASSETCODE_GOLONGAN_ID,
A.M_ASSETCODE_BIDANG_ID,
A.M_ASSETCODE_KELOMPOK_ID,
A.M_ASSETCODE_SUBKELOMPOK_ID,
A.M_ASSETCODE_SUBSUBKEL_ID,
F."NAME" AS A1_GOLONGAN,
G."NAME" AS A2_BIDANG,
H."NAME" AS A3_KELOMPOK,
I."NAME" AS A4_SUBKELOMPOK,
J."NAME" AS A5_SUBSUBKEL,
A.M_PRODUCT_ID,
K."NAME" AS PRODUCT,
L."NAME" AS UOM,
A.QTYBOOK,
A.QTYCOUNT,
A.DIFFERENCE,
N.M_WAREHOUSE_ID,
O."NAME" AS WAREHOUSE
FROM M_INVENTORYLINE A
LEFT JOIN M_STOCKCODE_KL B ON B.M_STOCKCODE_KL_ID = A.M_STOCKCODE_KL_ID
LEFT JOIN M_STOCKCODE_JB C ON C.M_STOCKCODE_JB_ID = A.M_STOCKCODE_JB_ID
LEFT JOIN M_STOCKCODE_NB D ON D.M_STOCKCODE_NB_ID = A.M_STOCKCODE_NB_ID
LEFT JOIN M_STOCKCODE_SB E ON E.M_STOCKCODE_SB_ID = A.M_STOCKCODE_SB_ID
LEFT JOIN M_ASSETCODE_GOLONGAN F ON F.M_ASSETCODE_GOLONGAN_ID = A.M_ASSETCODE_GOLONGAN_ID
LEFT JOIN M_ASSETCODE_BIDANG G ON G.M_ASSETCODE_BIDANG_ID = A.M_ASSETCODE_BIDANG_ID
LEFT JOIN M_ASSETCODE_KELOMPOK H ON H.M_ASSETCODE_KELOMPOK_ID = A.M_ASSETCODE_KELOMPOK_ID
LEFT JOIN M_ASSETCODE_SUBKELOMPOK I ON I.M_ASSETCODE_SUBKELOMPOK_ID = A.M_ASSETCODE_SUBKELOMPOK_ID
LEFT JOIN M_ASSETCODE_SUBSUBKEL J ON J.M_ASSETCODE_SUBSUBKEL_ID = A.M_ASSETCODE_SUBSUBKEL_ID
LEFT JOIN M_PRODUCT K ON K.M_PRODUCT_ID = A.M_PRODUCT_ID
LEFT JOIN C_UOM L ON L.C_UOM_ID = A.C_UOM_ID
LEFT JOIN M_INVENTORY N ON N.M_INVENTORY_ID = A.M_INVENTORY_ID
LEFT JOIN M_WAREHOUSE O ON O.M_WAREHOUSE_ID = N.M_WAREHOUSE_ID
;
I want to add another column which has command as follows :
SELECT S1_KL ||'-'|| S2_JB ||'-'|| S3_NB ||'-'|| S4_SB AS ASSET_CODE
FROM VW_STOCKOPNAME
It shows group of codes from the columns in the view to make it easier to see.
How can I add it?

Try this
CREATE OR REPLACE VIEW VW_STOCKOPNAME AS SELECT
A.M_STOCKCODE_KL_ID,
A.M_STOCKCODE_JB_ID,
A.M_STOCKCODE_NB_ID,
A.M_STOCKCODE_SB_ID,
B."NAME" AS S1_KL,
C."NAME" AS S2_JB,
D."NAME" AS S3_NB,
E."NAME" AS S4_SB,
A.M_ASSETCODE_GOLONGAN_ID,
A.M_ASSETCODE_BIDANG_ID,
A.M_ASSETCODE_KELOMPOK_ID,
A.M_ASSETCODE_SUBKELOMPOK_ID,
A.M_ASSETCODE_SUBSUBKEL_ID,
F."NAME" AS A1_GOLONGAN,
G."NAME" AS A2_BIDANG,
H."NAME" AS A3_KELOMPOK,
I."NAME" AS A4_SUBKELOMPOK,
J."NAME" AS A5_SUBSUBKEL,
A.M_PRODUCT_ID,
K."NAME" AS PRODUCT,
L."NAME" AS UOM,
A.QTYBOOK,
A.QTYCOUNT,
A.DIFFERENCE,
N.M_WAREHOUSE_ID,
O."NAME" AS WAREHOUSE,
B."NAME" ||'-'|| C."NAME" ||'-'|| D."NAME" ||'-'|| E."NAME" AS ASSET_CODE
FROM M_INVENTORYLINE A
LEFT JOIN M_STOCKCODE_KL B ON B.M_STOCKCODE_KL_ID = A.M_STOCKCODE_KL_ID
LEFT JOIN M_STOCKCODE_JB C ON C.M_STOCKCODE_JB_ID = A.M_STOCKCODE_JB_ID
LEFT JOIN M_STOCKCODE_NB D ON D.M_STOCKCODE_NB_ID = A.M_STOCKCODE_NB_ID
LEFT JOIN M_STOCKCODE_SB E ON E.M_STOCKCODE_SB_ID = A.M_STOCKCODE_SB_ID
LEFT JOIN M_ASSETCODE_GOLONGAN F ON F.M_ASSETCODE_GOLONGAN_ID = A.M_ASSETCODE_GOLONGAN_ID
LEFT JOIN M_ASSETCODE_BIDANG G ON G.M_ASSETCODE_BIDANG_ID = A.M_ASSETCODE_BIDANG_ID
LEFT JOIN M_ASSETCODE_KELOMPOK H ON H.M_ASSETCODE_KELOMPOK_ID = A.M_ASSETCODE_KELOMPOK_ID
LEFT JOIN M_ASSETCODE_SUBKELOMPOK I ON I.M_ASSETCODE_SUBKELOMPOK_ID = A.M_ASSETCODE_SUBKELOMPOK_ID
LEFT JOIN M_ASSETCODE_SUBSUBKEL J ON J.M_ASSETCODE_SUBSUBKEL_ID = A.M_ASSETCODE_SUBSUBKEL_ID
LEFT JOIN M_PRODUCT K ON K.M_PRODUCT_ID = A.M_PRODUCT_ID
LEFT JOIN C_UOM L ON L.C_UOM_ID = A.C_UOM_ID
LEFT JOIN M_INVENTORY N ON N.M_INVENTORY_ID = A.M_INVENTORY_ID
LEFT JOIN M_WAREHOUSE O ON O.M_WAREHOUSE_ID = N.M_WAREHOUSE_ID
;

Related

How to get rid of duplicating data in every row?

SELECT distinct AD.ReferenceNumber, AD.ProjectTitle, Z.ZoneCode, C.CompanyName,SS.AssignedTo, ZG.ZoneGroupName,au.Amount
FROM ApplicationDetails AD
LEFT JOIN ApplicationFormsDetails AS b ON (AD.referencenumber = b.referencenumber)
LEFT JOIN ScheduleSummaries AS SS ON (AD.ReferenceNumber = SS.ReferenceNo)
INNER JOIN AppTypes as at on ss.ItemCode = at.Category
INNER JOIN Companies AS C ON (AD.CompanyId = C.CompanyID)
INNER JOIN Zones Z ON (C.ZoneCode = Z.ZoneCode)
INNER JOIN ZoneGroups ZG ON (Z.ZoneGroup = ZG.ZoneGroupId)
LEFT JOIN AssessmentUsedItems au on ah.AssessmentHeaderId = au.HeaderId
WHERE AD.ApplicationDate BETWEEN '2017-10-01' AND '2017-10-31' AND ZG.ZoneGroupCode = 'HO' and ah.referencenumber = 'N-101317-A1-02'
GROUP BY AD.ReferenceNumber, AD.ProjectTitle, Z.ZoneCode, C.CompanyName,SS.AssignedTo, ZG.ZoneGroupName,au.Amount--, ah.ApplicationForm,au.Amount
The output of this query is its duplicating the amount for every AssignTO.
Output :
Maybe you want to try using SUM(ISNULL(au.amount, 0)) AS amount instead of au.amount and remove au.amount from the GROUP BY as well...
Try this query:
SELECT AD.ReferenceNumber,
AD.ProjectTitle,
Z.ZoneCode,
C.CompanyName,
SS.AssignedTo,
ZG.ZoneGroupName,
SUM(COALESCE(au.Amount,0)) AS Amount
FROM ApplicationDetails AD
LEFT JOIN ApplicationFormsDetails AS b
ON (AD.referencenumber = b.referencenumber)
LEFT JOIN ScheduleSummaries AS SS
ON (AD.ReferenceNumber = SS.ReferenceNo)
INNER JOIN AppTypes AS at
ON ss.ItemCode = at.Category
INNER JOIN Companies AS C
ON (AD.CompanyId = C.CompanyID)
INNER JOIN Zones Z
ON (C.ZoneCode = Z.ZoneCode)
INNER JOIN ZoneGroups ZG
ON (Z.ZoneGroup = ZG.ZoneGroupId)
LEFT JOIN AssessmentUsedItems au
ON ah.AssessmentHeaderId = au.HeaderId
WHERE AD.ApplicationDate BETWEEN '2017-10-01' AND '2017-10-31'
AND ZG.ZoneGroupCode = 'HO'
AND ah.referencenumber = 'N-101317-A1-02'
GROUP BY
AD.ReferenceNumber,
AD.ProjectTitle,
Z.ZoneCode,
C.CompanyName,
SS.AssignedTo,
ZG.ZoneGroupName

SQL Query - Using Information From One Inquiry In Another

Here is my query. I removed the table names along with other things I am selecting just to make it easier to read.
Select
b.Key1,
b.Key2
From
b
left join c
on c.MailingKey = b.MailingKey
left join d
on d.OrderLineKey = c.OrderLineKey
left join e
on e.ShipKey = d.ShipKey
left join g
on d.ShipKey5 = g.ShipKey5
left join a
on a.DocKey = b.OrderKey
left join f
on f.pMethKey = b.MethKey
I need to use Key1 and Key2 that I select in another query to get more information about that key (location, ID, etc.) from another table. I am thinking I will need to make 2 loops within this original query and join them all up. So something like this:
Select b.Key1, b.Key2, h.*, i.*
From b left join
c on c.MailingKey = b.MailingKey left join
d on d.OrderLineKey = c.OrderLineKey left join
e on e.ShipKey = d.ShipKey left join
g on d.ShipKey5 = g.ShipKey5 left join
a on a.DocKey = b.OrderKey left join
f on f.pMethKey = b.MethKey left join
(
Select*
From table l
Where ID = b.Key1
) h left join (
Select*
From table l
Where ID = b.Key2
) i
I am just not sure how to write equal to b.Key1 and b.Key2.
I am using Microsoft Sql Server Management.
Common table expression:
with cte as (
Select b.Key1, b.Key2
From b left join
c on c.MailingKey = b.MailingKey left join
d on d.OrderLineKey = c.OrderLineKey left join
e on e.ShipKey = d.ShipKey left join
g on d.ShipKey5 = g.ShipKey5 left join
a on a.DocKey = b.OrderKey left join
f on f.pMethKey = b.MethKey)
Select * from CTE
INNER JOIN othertable o
on b.key1=o.key1
and b.key2=o.key2
or do you mean...
Inline view:
Select *
from otherTable
INNER JOIN (
Select b.Key1, b.Key2
From b left join
c on c.MailingKey = b.MailingKey left join
d on d.OrderLineKey = c.OrderLineKey left join
e on e.ShipKey = d.ShipKey left join
g on d.ShipKey5 = g.ShipKey5 left join
a on a.DocKey = b.OrderKey left join
f on f.pMethKey = b.MethKey) CTE
on b.key1=o.key1
and b.key2=o.key2
Or do you mean.... something else?
Perhaps:... using an OR statement...
Select *
from otherTable o
INNER JOIN (
Select b.Key1, b.Key2
From b left join
c on c.MailingKey = b.MailingKey left join
d on d.OrderLineKey = c.OrderLineKey left join
e on e.ShipKey = d.ShipKey left join
g on d.ShipKey5 = g.ShipKey5 left join
a on a.DocKey = b.OrderKey left join
f on f.pMethKey = b.MethKey) CTE
on o.key1=cte.key1
OR o.key2=cte.key2
or maybe... an in statement (within this set)
Select *
from otherTable o
INNER JOIN (
Select b.Key1, b.Key2
From b left join
c on c.MailingKey = b.MailingKey left join
d on d.OrderLineKey = c.OrderLineKey left join
e on e.ShipKey = d.ShipKey left join
g on d.ShipKey5 = g.ShipKey5 left join
a on a.DocKey = b.OrderKey left join
f on f.pMethKey = b.MethKey) CTE
on o.key1 IN (cte.key1, cte.key2)

Subquery in from clause, Invalid Identifier in Where clause

select * from iiasa_inventory.inv_device d
join iiasa_inventory.inv_type ty on d.type_id = ty.id
join iiasa_inventory.inv_category c on ty.category_id = c.id
join iiasa_inventory.inv_device_2_barcode b on b.device_id = d.id
join iiasa_inventory.inv_barcodes bc on b.barcode_id = bc.id
join iiasa_inventory.inv_status s on d.status = s.id
join iiasa_inventory.inv_brand br on ty.brand_id = br.id
left join iiasa_inventory.inv_supplier su on su.id = d.supplier_id
left join iiasa_inventory.inv_supplier sup on sup.id = d.maintenance_with
left join (select distinct device_id from
iiasa_inventory.inv_device_2_persons_cc) dp
on dp.device_id = d.id
where dp.active = 1
I am trying to select my data but the where-clause says that "dp.active" is an INVALID Identifier. This is probably because the table dp is in the subquery. I have tried to give it an alias name and some other things I found while browsing stackoverflow, but I cant seem to find a solution. Any idea?
This is Oracle PL/SQL.
Put the check for active = 1 in the subquery as shown below.
select * from iiasa_inventory.inv_device d
join iiasa_inventory.inv_type ty on d.type_id = ty.id
join iiasa_inventory.inv_category c on ty.category_id = c.id
join iiasa_inventory.inv_device_2_barcode b on b.device_id = d.id
join iiasa_inventory.inv_barcodes bc on b.barcode_id = bc.id
join iiasa_inventory.inv_status s on d.status = s.id
join iiasa_inventory.inv_brand br on ty.brand_id = br.id
left join iiasa_inventory.inv_supplier su on su.id = d.supplier_id
left join iiasa_inventory.inv_supplier sup on sup.id = d.maintenance_with
left join (select distinct device_id from iiasa_inventory.inv_device_2_persons_cc where active = 1) dp on dp.device_id = d.id
That is because you are not selecting active column in dp.
select * from iiasa_inventory.inv_device d
join iiasa_inventory.inv_type ty on d.type_id = ty.id
join iiasa_inventory.inv_category c on ty.category_id = c.id
join iiasa_inventory.inv_device_2_barcode b on b.device_id = d.id
join iiasa_inventory.inv_barcodes bc on b.barcode_id = bc.id
join iiasa_inventory.inv_status s on d.status = s.id
join iiasa_inventory.inv_brand br on ty.brand_id = br.id
left join iiasa_inventory.inv_supplier su on su.id = d.supplier_id
left join iiasa_inventory.inv_supplier sup on sup.id = d.maintenance_with
left join (select distinct device_id,active from iiasa_inventory.inv_device_2_persons_cc) dp on dp.device_id = d.id
where dp.active = 1
OR you can just filter from the subquery itself. Like:
left join (select distinct device_id
from iiasa_inventory.inv_device_2_persons_cc
where active=1) dp on dp.device_id = d.id

How can I use the group by Clause in a subquery

I just need to select one more field in my query that is the date... but it's a subquery and i'm using a count field... and because of it i need to use the GROUP BY Clause... But i can't group by my subquery and the query is returning errors...
SELECT
X.NROF,
Z.NMGUERFORN,
C.CDCOMPRADO,
C.CDCOORDENA,
E.CDFUP,
count(*) AS ocorrencias
--(select TOP 1 DTPROGENTR from CMPENL0 C (NOLOCK) where X.NRPEDICOMP = C.NRPEDICOMP AND X.NRITEMPECO = C.NRITEMPECO) AS DTPROGENTR
FROM CMPCIL0 X (NOLOCK)
inner join CMPCCL0 Y (NOLOCK) on X.NRPEDICOMP = Y.NRPEDICOMP
inner join CMFRNL0 Z (NOLOCK) on Y.CDFORNECED1 = Z.CDFORNECED1
inner join CMSCPL0 M (NOLOCK) on X.NRSOLICOMP = M.NRSOLICOMP AND X.NRITEMSC = M.NRITEMSC
inner join CMPENL0 N (NOLOCK) on X.NRPEDICOMP = N.NRPEDICOMP AND X.NRITEMPECO = N.NRITEMPECO
inner join CMMATL0 A (NOLOCK) on X.CDMATERIAL = A.CDMATERIAL
inner join cmcomL0 c (NOLOCK) on c.cdcomprado = y.cdcomprado
LEFT JOIN AMCSPL0 D (NOLOCK) ON D.SCPE_NRSOLICOMP = X.NRSOLICOMP AND D.SCPE_NRITEMSC=X.NRITEMSC
LEFT JOIN CMFUPL0 E (NOLOCK) ON E.CDFORNECED1 = Z.CDFORNECED1
LEFT JOIN CMPFIL0 H ON Z.CDFORNECED1 = H.CDFORNECED1 AND X.NRIDENTIFI = H.NRIDENTIFI and H.DTVALIDADE > Y.DTEFETPECO
LEFT JOIN CMPENL0 F ON (X.NRPEDICOMP = F.NRPEDICOMP AND X.NRITEMPECO = F.NRITEMPECO)
WHERE X.CDSTATUS = 'P' and X.NROF <> 0
group by X.NROF,Z.NMGUERFORN,C.CDCOMPRADO,C.CDCOORDENA,E.CDFUP--,DTPROGENTR
order by 6 desc
The commented parts are the code im trying to include to select the field, but it is giving me errors..
The problem is that you use aliases in your where clause of select sub-query. Aliases cannot be referenced on the same level in the query. You should use full qualified names and the sub-query should look something like that:
(select TOP 1 DTPROGENTR from CMPENL0 C (NOLOCK) where X.NRPEDICOMP = CMPENL0.NRPEDICOMP AND X.NRITEMPECO = CMPENL0.NRITEMPECO) AS DTPROGENTR
If your sql-server supports common table expressions you can try that:
;WITH cte AS
(
SELECT ROW_NUMBER() OVER(PARTITION BY NRPEDICOMP, NRITEMPECO ORDER BY NRPEDICOMP) row_num
,DTPROGENTR
,NRPEDICOMP
,NRITEMPECO
FROM CMPENL0 (NOLOCK)
)
SELECT
X.NROF,
Z.NMGUERFORN,
C.CDCOMPRADO,
C.CDCOORDENA,
E.CDFUP,
count(*) AS ocorrencias
cte.DTPROGENTR
FROM CMPCIL0 X (NOLOCK)
inner join CMPCCL0 Y (NOLOCK) on X.NRPEDICOMP = Y.NRPEDICOMP
inner join CMFRNL0 Z (NOLOCK) on Y.CDFORNECED1 = Z.CDFORNECED1
inner join CMSCPL0 M (NOLOCK) on X.NRSOLICOMP = M.NRSOLICOMP AND X.NRITEMSC = M.NRITEMSC
inner join CMPENL0 N (NOLOCK) on X.NRPEDICOMP = N.NRPEDICOMP AND X.NRITEMPECO = N.NRITEMPECO
inner join CMMATL0 A (NOLOCK) on X.CDMATERIAL = A.CDMATERIAL
inner join cmcomL0 c (NOLOCK) on c.cdcomprado = y.cdcomprado
LEFT JOIN AMCSPL0 D (NOLOCK) ON D.SCPE_NRSOLICOMP = X.NRSOLICOMP AND D.SCPE_NRITEMSC=X.NRITEMSC
LEFT JOIN CMFUPL0 E (NOLOCK) ON E.CDFORNECED1 = Z.CDFORNECED1
LEFT JOIN CMPFIL0 H ON Z.CDFORNECED1 = H.CDFORNECED1 AND X.NRIDENTIFI = H.NRIDENTIFI and H.DTVALIDADE > Y.DTEFETPECO
LEFT JOIN CMPENL0 F ON X.NRPEDICOMP = F.NRPEDICOMP AND X.NRITEMPECO = F.NRITEMPECO
LEFT JOIN cte ON X.NRPEDICOMP = cte.NRPEDICOMP AND X.NRITEMPECO = cte.NRITEMPECO AND cte.row_num = 1
WHERE X.CDSTATUS = 'P' and X.NROF <> 0
group by X.NROF,Z.NMGUERFORN,C.CDCOMPRADO,C.CDCOORDENA,E.CDFUP, cte.DTPROGENTR
order by 6 desc

MSSQL How do I get average of four records from different tables?

I have four tables and I need to find the average of each score for a particular id. I do not need the ENTIRE Column average, but the average of each record with the same id in each table.
I have tried this:
SELECT DISTINCT M.system_id, S.name, SUM(A.Score + WAL.score + F.score + WIN.score) / 4
AS avgScore
FROM dbo.T3_MovementSystemJoin AS M
INNER JOIN dbo.T3_systems AS S ON M.system_id = S.id
INNER JOIN T3_ApplicationSystemJoin AS A ON A.Application_id = #application_id
INNER JOIN T3_WallTypeSystemJoin AS WAL ON WAL.wall_id = #wall_id
INNER JOIN T3_FenestrationSystemJoin AS F ON F.fenestration_id = #fen_id
INNER JOIN T3_WindowOrientation_System AS WIN ON WIN.window_id = #window_id
INNER JOIN T3_ConstructionSystemJoin AS C ON C.contruction_id = #construction_id
INNER JOIN T3_JointDepthSystemJoin AS J ON J.JointDepth_id = #JointDepth_id
INNER JOIN T3_JointGapSystemJoin AS JG ON JG.JointGap_id = #JointGap_id
WHERE (M.movement_id = #movement_id)
GROUP BY M.System_id, S.name
:
Thanks for your help!
No Sum needed (and no grouping too)
SELECT DISTINCT M.system_id, S.name, (IsNull(A.Score, 0) + IsNull(WAL.score, 0) + IsNull(F.score, 0) + IsNull(WIN.score, 0)) /4
as avgscore
FROM dbo.T3_MovementSystemJoin AS M
INNER JOIN dbo.T3_systems AS S ON M.system_id = S.id
INNER JOIN T3_ApplicationSystemJoin AS A ON A.Application_id = #application_id
INNER JOIN T3_WallTypeSystemJoin AS WAL ON WAL.wall_id = #wall_id
INNER JOIN T3_FenestrationSystemJoin AS F ON F.fenestration_id = #fen_id
INNER JOIN T3_WindowOrientation_System AS WIN ON WIN.window_id = #window_id
INNER JOIN T3_ConstructionSystemJoin AS C ON C.contruction_id = #construction_id
INNER JOIN T3_JointDepthSystemJoin AS J ON J.JointDepth_id = #JointDepth_id
INNER JOIN T3_JointGapSystemJoin AS JG ON JG.JointGap_id = #JointGap_id
WHERE (M.movement_id = #movement_id)
SELECT DISTINCT M.system_id
,S.name
,(ISNULL(A.Score,0) + ISNULL(WAL.score,0) + ISNULL(F.score,0) + ISNULL(WIN.score,0)) /4 as 'AvgScore'
FROM dbo.T3_MovementSystemJoin AS M
INNER JOIN dbo.T3_systems AS S ON M.system_id = S.id
INNER JOIN T3_ApplicationSystemJoin AS A ON A.Application_id = #application_id
INNER JOIN T3_WallTypeSystemJoin AS WAL ON WAL.wall_id = #wall_id
INNER JOIN T3_FenestrationSystemJoin AS F ON F.fenestration_id = #fen_id
INNER JOIN T3_WindowOrientation_System AS WIN ON WIN.window_id = #window_id
INNER JOIN T3_ConstructionSystemJoin AS C ON C.contruction_id = #construction_id
INNER JOIN T3_JointDepthSystemJoin AS J ON J.JointDepth_id = #JointDepth_id
INNER JOIN T3_JointGapSystemJoin AS JG ON JG.JointGap_id = #JointGap_id
WHERE (M.movement_id = #movement_id)
If you don't want NULL values to become zeros and included in the average:
SELECT DISTINCT M.system_id, S.name, X.avgScore
FROM dbo.T3_MovementSystemJoin AS M
INNER JOIN dbo.T3_systems AS S ON M.system_id = S.id
INNER JOIN T3_ApplicationSystemJoin AS A ON A.Application_id = #application_id
INNER JOIN T3_WallTypeSystemJoin AS WAL ON WAL.wall_id = #wall_id
INNER JOIN T3_FenestrationSystemJoin AS F ON F.fenestration_id = #fen_id
INNER JOIN T3_WindowOrientation_System AS WIN ON WIN.window_id = #window_id
INNER JOIN T3_ConstructionSystemJoin AS C ON C.contruction_id = #construction_id
INNER JOIN T3_JointDepthSystemJoin AS J ON J.JointDepth_id = #JointDepth_id
INNER JOIN T3_JointGapSystemJoin AS JG ON JG.JointGap_id = #JointGap_id
CROSS APPLY ( SELECT AVG(s) FROM (VALUES (A.Score),(WAL.score),(F.score),(WIN.score) ) scores(s) ) X(avgScore)
WHERE (M.movement_id = #movement_id)
GROUP BY M.System_id, S.name