dimanche 22 avril 2018

mysql select distinct if condition exists

I have this table :

std_id  std_type  std_dept  target
  1      type-1     ALL       15
  2      type-1     HRD       10
  3      type-2     ALL       1
  4      type-2     ACCTG     5
  5      type-3     ALL       5
  6      type-4     ALL       25

(1)std_dept with value 'ALL' means std_target for each std_type are valid for all dept
(2)std_dept with specific dept value, overide the value form point (1).
So, my problem is lets say iam from HRD dept and i want have result set as follow:

std_id  std_type  std_dept  target
  2      type-1     HRD       10
  3      type-2     ALL       1
  5      type-3     ALL       5
  6      type-4     ALL       25

i really like to show my work, but really i'v got no clue at all, idont know how to distincly select * from std_table (for each std_type) where std_dept='HRD' if exist else get from std_dept='ALL'.


need pointers if not the code, and thank you in advance

Aucun commentaire:

Enregistrer un commentaire