Hi there,
I appreciate if someone can assist me with my SQL queries.
I require assistance in creating a generic query which will work depending on the parameters given.
Here is my following table structure I have to work with:
Table1
MachId
MachCode
MachLinkType(Can be of Type 'ROBOT', 'MINI')
Table2
StatId
Stat_GDU_Code(linked to the Table 3)
Stat_MachCode(linked to Table1)
Table3
GDU_Id
GDU_prefix
GDU_code
GDU_description
Note The GDU_CODE is not unique. The GDU_prefix and GDU_id have to be unique.
Table4
PlacId
Plac_Mach_Code(links to MachCode)
PlacType(e.g FROD, PLACK)
PlacDescription
If the MachLinkType is 'ROBOT' then I look in Table 1 and Table 4 to get the description etc:
SELECT MachId, MachCode, MachLinkType, PlacType, Plac_Description
from Table1 INNER JOIN Table4
ON Table1.MachCode = Table4.Plac_Mach_Code
RTRIM(MachLinkType)= 'ROBOT' AND RTRIM(PlacType)= 'FROD'
Now if the value is not ROBOT then I have to interrogate 3 tables (Table 1, Table 2 and Table 3) but I have 2 issues with this:
Firstly if I include the GDU_Description fields I get duplicate records.
I have tried using 'distinct' on the Table1.MachId but I think that is incorrect way of using it for my query.
I need the description of the record from Table 3 in regards to the output I am trying to generate.
Below is my query if the value provided by the user isn't PLAC:
SELECT distinct(MachId), MachCode, MachLinkType, Stat_GDU_Code, GDU_Description
from Table1 INNER JOIN Table2
ON Table1.MachCode = Table2.Stat_MachCode
INNER JOIN Table2 ON
Table3.GDU_Code = Table2.Stat_GDU_Code
WHERE RTRIM(MachLinkType) = 'MINIBOT' AND RTRIM(Stat_GDU_Code) = '31A'
Secondly, if user provides ROBOT and MINIBOT types how can I combine the two queries above in an appropriate manner so I can retrieve results for both ROBOT and MINIBOT types.
I appreciate if someone can assist me please?
This is the sort of output i want to generate in my user control:
MachineReference:123
MachineCode: EPIS
MachineLinkType: AUTONOMOUS
MachinePartCode: 31A
MachineDescription: 'Test Description'.
Regardless of which table the respective data lives in i would like to use assign the data to those 5 labels.
Thanks,