Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Sql query - getting rid of hard-coded values

Tags:

sql

I have the following query:

Select Name,
       case when charindex('I',a.S_Data) > 0 then 1 else 0 end as Illustrated,
       case when charindex('FP',a.S_Data) > 0 then 1 else 0 end as FrontPage,
       case when charindex('BP',a.S_Data) > 0 then 1 else 0 end as BackPage,
       case when charindex('ELP',a.S_Data) > 0 then 1 else 0 end as EDLP,
       case when charindex('PR',a.S_Data) > 0 then 1 else 0 end as SpecialPromo
From Table1

What I would like to do is to store those filter values in some sort of lookup table or a settings table.

I am struggling with how to draw the values from a lookup table to use with this query.

like image 273
Perplexed Avatar asked Jan 11 '12 15:01

Perplexed


People also ask

What is hardcoded value in SQL?

Hard coding (also, hard-coding or hardcoding) refers to the software development practice of embedding what may, perhaps only in retrospect, be regarded as input or configuration data directly into the source code of a program or other executable object, or fixed formatting of the data, instead of obtaining that data ...

How do you exclude a value in SQL?

SQL Except Set Operator. The SQL EXCEPT operator is used to exclude like rows that are found in one query but not another. It returns rows that are unique to one result. To use the EXCEPT operator, both queries must return the same number of columns and those columns must be of compatible data types.


1 Answers

I can think of at least two options...

CREATE TABLE constants (
  id               AS INT,
  Illustrated      AS VARCHAR(3),
  FrontPage        AS VARCHAR(3),
  BackPage         AS VARCHAR(3),
  EDLP             AS VARCHAR(3),
  SpecialPromo     AS VARCHAR(3)
)

INSERT INTO constants SELECT 1, 'I', 'FP', 'BP', 'ELP', 'PR'

SELECT
  Name,
  CASE WHEN CHARINDEX(constants.Illustrated, data.S_Data) > 0 THEN 1 ELSE 0 END   AS Illustrated,
  etc, etc
FROM
  data
INNER JOIN
  constants
    ON constants.id = 1

Or...

CREATE TABLE constants (
  constant_set_id  AS INT,
  constant_name    AS VARCHAR(16),
  value            AS AS VARCHAR(3)
)

INSERT INTO constants SELECT 1, 'Illustrated',  'I'
INSERT INTO constants SELECT 1, 'FrontPage',    'FP'
INSERT INTO constants SELECT 1, 'BackPage',     'BP'
INSERT INTO constants SELECT 1, 'EDLP',         'ELP'
INSERT INTO constants SELECT 1, 'SpecialPromo', 'PR'

SELECT
  Name,
  MAX(CASE WHEN constants.constant_name = 'Illustrated' AND CHARINDEX(constants.value, data.S_Data) > 0 THEN 1 ELSE 0 END)   AS Illustrated,
  etc, etc
FROM
  data
INNER JOIN
  constants
    ON constants.constant_set_id = 1
GROUP BY
  data.name

Both let you have multiple different sets of constants. One is expandable without changing the schema, though the query still would need to change.

The main advantage of either approach is that you can re-use the constants else where, but store them once in a centralised location. Which is only relevant if/when the values in the constants needs updating. Re-use through indirection.

like image 91
MatBailie Avatar answered Oct 14 '22 01:10

MatBailie