How do I Output into Self Specified Data like Yes or No into Self Specified Columns Based on the result of a Query SQL Server 2008

El Amen

I have got these data in these 2 tables in SQL Server 2008:

Facility table:

FacilityID      Region          FacilityName           FacilityType
--------------------------------------------------------------------
A-1             Acc            Amen Clinic               Clinic
UW-2            Upp W          Mah Hospital              Gov Hosp
UE-3            Upp E          Mat                       Pr Hosp
A-4             Acc            Dent OPD                  CHPS
BA-5            Br Ah          Class Compl               Gov Hosp

Equipment table:

EquipmentName           FacilityID      
-------------------------------------
BP Apparatus            A-1
Thermometer             A-1
Screen                  A-1
Beds                    A-1
BP Apparatus            UW-2
Thermometer             UW-2
Beds                    UW-2
Vans                    UW-2
Thermometer             UE-3
Screen                  UE-3
BP Apparatus            A-4
Thermometer             A-4

This is my desired output:

acilityID      Region          FacilityName       Bp Apparatus     Thermometer         Screen
-------------------------------------------------------------------------------------------------
A-1             Acc            Amen Clinic        Yes               Yes                 Yes
UW-2            Upp W          Mah Hospital       Yes               Yes                 No
UE-3            Upp E          Mat                No                Yes                 Yes
A-4             Acc            Dent OPD           Yes               Yes                 No
BA-5            Br Ah          Class Compl        No                No                  No
symbiont

the hard part is that 'Bp Apparatus', 'Thermometer' and 'Screen' are not actual columns:

select FacilityID
     , Region
     , FacilityName
     , (case when exists (select * 
                          from Equipment as e 
                          where     e.FacilityID = f.FacilityID 
                                and e.EquipmentName = 'BP Apparatus') 
             then 'Yes' 
             else 'No' 
        end)
       as 'Bp Apparatus'
     , (case when exists (select * 
                          from Equipment as e 
                          where     e.FacilityID = f.FacilityID 
                                and e.EquipmentName = 'Thermometer')
             then 'Yes'
             else 'No'
        end)
       as 'Thermometer'
     , (case when exists (select * 
                          from Equipment as e 
                          where     e.FacilityID = f.FacilityID 
                                and e.EquipmentName = 'Screen')
             then 'Yes'
             else 'No'
        end)
       as 'Screen'
from Facility as f

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

SQL Server pivot specified columns

SQL Server Insert with no specified columns

How to duplicate columns data in SQL Server 2008?

Self-join a table in SQL Server 2008

SQL Server self referencing query

How do I trigger an event like keyup with a specified keycode

How do I self-join the result of an aggregate function in MySQL?

In RStudio -How do I import a csv dataset with no columns specified?

How do I delete rows that are duplicated on specified columns?

How to create a table from select query result in SQL Server 2008

How do I query RealmObject that have RealmList that contains specified value

PHP SQL Server 2008 Error Locating Server/Instance Specified [xFFFFFFFF]

How do I get the percentage of 4 columns of yes/no data in R?

How do i show the specified data when i press the specified list?

SQL Server 2008 - Adding self-referencing foreign key with constraint and getting "Cannot find data type" error

How do I make this complex self-joined query with sequelize?

select the next date based on a specified date SQL 2008

How to use `self` as a function, like `self(**data)` in the example code

How to do IF...ELSE like programming in SQL Server 2008

How do I get the specified node from XML in SQL

How do I produce synthetic data over a specified range in python?

How do I loop a specified region of code to compile data in Javascript?

SQL Server 2008 Creating Table with date column with specified format

How do I create query in sql server 2008 r2

SQL Server 2008, Pivot based on multiple columns

How do i create a graphql schema for a self referencing data hierarchy?

SQL query to capture data with self join

How do i modify the result of this SQL query?

How do I Get this result by SQL query