0

These are my tables:

tbl_fa_asset_reg

  • AssetID
  • AssetGroup
  • BusinessUnit
  • AssetGroup

tbl_a_comp_f_bu

  • Region
  • Districk
  • BusinessUnit

tbl_fa_asset_reg_mas_b_class

  • ClassCode
  • Description

tbl_fa_asset_reg_mas_c_group

  • id
  • AssetGroup
  • AssetClass

This is my query:

SELECT DISTINCT a.ClassCode,a.Description,d.BusinessUnit,d.BUDes,d.Department 
    FROM tbl_fa_asset_reg_mas_b_class a 
    JOIN tbl_fa_asset_reg_mas_c_group b ON a.ClassCode=b.AssetClass 
    JOIN tbl_fa_asset_reg c ON c.AssetGroup=b.AssetGroup 
    JOIN tbl_a_comp_f_bu d ON d.BusinessUnit=c.BusinessUnit
    order by a.ClassCode ASC 

The output I want:

3 Answers 3

2

Seems you just need aggregation:

select a.ClassCode,
    a.Description,
    d.BusinessUnit,
    d.BUDes,
    d.Department,
    count(*) as total
from tbl_fa_asset_reg_mas_b_class a
join tbl_fa_asset_reg_mas_c_group b on a.ClassCode = b.AssetClass
join tbl_fa_asset_reg c on c.AssetGroup = b.AssetGroup
join tbl_a_comp_f_bu d on d.BusinessUnit = c.BusinessUnit
group by a.ClassCode,
    a.Description,
    d.BusinessUnit,
    d.BUDes,
    d.Department
order by ClassCode asc
Sign up to request clarification or add additional context in comments.

Comments

0
SELECT 
    DISTINCT 
      a.ClassCode,
      a.Description, 
      d.BusinessUnit,
      d.BUDes, 
      d.Department, 
      count(*)
FROM tbl_fa_asset_reg_mas_b_class a 
    JOIN tbl_fa_asset_reg_mas_c_group b ON a.ClassCode=b.AssetClass 
    JOIN tbl_fa_asset_reg c ON c.AssetGroup=b.AssetGroup 
    JOIN tbl_a_comp_f_bu d ON d.BusinessUnit =c.BusinessUnit 
Order by a.ClassCode ASC

Comments

0

Thanks but different from the output, there are some different data count, for example the results of calculations 16 actually should result 12

this is the output from

select a.ClassCode, a.Description, d.BusinessUnit, d.BUDes, d.Department, count(*) as total from tbl_fa_asset_reg_mas_b_class a join tbl_fa_asset_reg_mas_c_group b on a.ClassCode = b.AssetClass join tbl_fa_asset_reg c on c.AssetGroup = b.AssetGroup join tbl_a_comp_f_bu d on d.BusinessUnit = c.BusinessUnit group by a.ClassCode, a.Description, d.BusinessUnit, d.BUDes, d.Department order by ClassCode asc

enter image description here

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.