Get individual values from comma seperated values in the table [duplicate] - sql

This question already has answers here:
Replace comma separated master data with description in a column
(3 answers)
Closed 4 years ago.
I have a table with reference values and another table as selected reference values. In this table the selected reference value are comma separated. How can I get individual values from comma separated values?
CREATE TABLE [DBO].[TBL_REFERENCE]
(
/* REFERENCE TABLE */
REF_ID BIGINT IDENTITY(1, 1) NOT NULL
,REF_NUMBER INT NOT NULL
,REF_NAME NVARCHAR(20)
,REF_VALUE NVARCHAR(25)
,CONSTRAINT PK_REFERENCE PRIMARY KEY (REF_ID)
)
INSERT INTO TBL_REFERENCE (ref_number, ref_name, ref_value)
VALUES (1, 'communication', 'mobile'),
(1, 'communication', 'laptop'),
(1, 'communication', 'PDA'),
(1, 'communication', 'tabs'),
(1, 'communication', 'iphone'),
(1, 'communication', 'smart phone'),
(1, 'communication', 'others');
Another table
CREATE TABLE [DBO].[TBL_FACILITY]
(
/* FACILITY TABLE */
FACILITY_ID BIGINT IDENTITY(1, 1) NOT NULL
,FAMILY_HEAD_ID BIGINT NOT NULL
,FACILITY_SELECTED INT --- REF_NAME
,FACILITY_USED NVARCHAR(25) --- ref_value
,CONSTRAINT PK_FACILITY PRIMARY KEY (FACILITY_ID)
)
INSERT INTO TBL_FACILITY (FAMILY_HEAD_ID, FACILITY_SELECTED, FACILITY_USED)
VALUES (121, 1, '2,6'), (122, 1, '5'), (123, 1, '5,6');
The format of final result:
121 communication laptop
121 communication smart phone
122 communication iphone
123 communication iphone
123 communication smart phone
How can I get this result?

Presuming ,as an identity column, tbl_reference.ref_number columns' values are 1,2,3,4,5,6,7 in downwards order , by the contribution of SUBSTRING and CHARINDEX string operators, you may use the following SQL :
SELECT family_head_id, ref_name+' '+ref_value reference
FROM TBL_FACILITY INNER JOIN TBL_REFERENCE
ON ( ref_number = SUBSTRING(facility_used,CHARINDEX(',', facility_used)-1,1) )
UNION ALL
SELECT family_head_id, ref_name+' '+ref_value reference
FROM TBL_FACILITY INNER JOIN TBL_REFERENCE
ON ( ref_number = SUBSTRING(facility_used,CHARINDEX(',', facility_used)+1,1) )
ORDER BY 1, 2;
SQL Fiddle Demo

Related

INSERT INTO table_name VALUES (value1, value2, value3, ...);How to transfer a data of a single column in a single table row

Hi guys I'm working on a vanilla system where we can add many systems in it with data sets and fields. we can add fields and data of each system at run time without changing the structure of database.
CREATE TABLE Type(
ID int,
Name varchar(255),
PRIMARY KEY (ID)
);
INSERT INTO Type VALUES (1, 'cust_obj_7');
INSERT INTO Type VALUES (2, 'cust_obj_8');
CREATE TABLE Object(
ID int,
Name varchar(255),
Description varchar(255),
TypeID int,
PRIMARY KEY (ID),
FOREIGN KEY (TypeID ) REFERENCES Type(ID)
);
INSERT INTO Object VALUES (1, 'First', 'First_desc', 1);
INSERT INTO Object VALUES (2, 'Second', 'Second_desc', 1);
CREATE TABLE TypeFields(
ID int,
Name varchar(255),
NameType varchar(255),
TypeID int,
PRIMARY KEY (ID),
FOREIGN KEY (TypeID ) REFERENCES Type(ID)
);
INSERT INTO TypeFields VALUES (1, 'First', 'str', 1);
INSERT INTO TypeFields VALUES (2, 'Seond', 'str', 1);
INSERT INTO TypeFields VALUES (3, 'Third', 'int', 1);
CREATE TABLE FieldsData(
ID int,
ObjectID int,
FieldID int,
FieldName varchar(255),
TypeID int,
value varchar(255),
PRIMARY KEY (ID),
FOREIGN KEY (TypeID ) REFERENCES Type(ID),
FOREIGN KEY (ObjectID ) REFERENCES Object(ID),
FOREIGN KEY (FieldID ) REFERENCES TypeFields(ID)
);
INSERT INTO VALUES (1, 1, 1, 'First', 1, "a");
INSERT INTO VALUES (2, 1, 2, 'Second', 1, "b");
INSERT INTO VALUES (3, 1, 3, 'Third', 1, "120");
INSERT INTO VALUES (4, 2, 1, 'First', 1, "c");
INSERT INTO VALUES (5, 2, 2, 'Second', 1, "d");
INSERT INTO VALUES (6, 2, 3, 'Third', 1, "130");
CREATE TABLE FinalTable(
ObjID int,
ObjName varchar(255),
ObjDesc varchar(255),
First varchar(255),
Second varchar(255),
Third int
);
Data will look like this
Insert Into FinalTable
select *
from object, fieldsdata
where object.iD = fieldsdata.objectiD
Values(object.ID , object.Name, object.Description, ....... );
I want to create a single data set form these tables as finaltable as shown above. I can get all the data using keys but I'm having issue to write that data to a single row from a column of different rows. I'm stuck that how can I achieve this using sql in sas-base.
First, "My custom table" should be a view originating from "Fields data". "Fields data" is a bit of a misnomer because it contains the values of the fields. So in an abstraction that distinction is important for understanding within the framework you are building.
A row in "My custom table" appears to be for projecting TypeID=1. You don't show other TypeID values, but I presume they would be for different custom tables.
The custom tables should really be views, otherwise each object is a potential +1 to a multiplier regarding storage requirements
In your sample image the second row of "My custom table" has ObjID=2, yet shows values from the "Fields data" that correspond to ObjID=2. I will presume a typo.
The "Object types" name is not rendered in the Custom table, thus I would consider it simply a catalog of types.
The design you present is not a normal form yet. Not sure why FieldName is replicated in "Fields data", you have the FieldID that refers to a record with the name. Not sure why TypeID is present in "Fields data" because TypeID is essentially a catalog item selector for the desired projection of the "Fields data"
This kind of design can cause a lot of reinvention and will take huge amounts of time to flesh out for different value types such as dates, value rendering formats, multivalued types, etc...
Regardless, the projection of the values data as an object type to a custom table is essentially a transposition of a join that combines object, type fields, and fields data. SAS SQL does not have a PIVOT operator (such as is found in MS SQL Server). The 'old school' way to pivot in SQL involves aggregating case statements over a by group. Search SO
There are some designs for an 'arbitrarium' that simply kill all the middle men and have a single enormous values monolith with say 20 ID fields, 1,000 numeric fields, 1,000 character fields and 100 date fields and each 'object' is a use-case SQL view against it.
** EDIT - ADDED BELOW **
Added code demonstrating 'old school' pivot:
PROC SQL;
CREATE TABLE Type(
ID int,
Name varchar(255)/*,
PRIMARY KEY (ID)*/
);
INSERT INTO Type VALUES (1, 'cust_obj_7');
INSERT INTO Type VALUES (2, 'cust_obj_8');
CREATE TABLE Object(
ID int,
Name varchar(255),
Description varchar(255),
TypeID int/*,
PRIMARY KEY (ID),
FOREIGN KEY (TypeID ) REFERENCES Type(ID);*/
);
INSERT INTO Object VALUES (1, 'First', 'First_desc', 1);
INSERT INTO Object VALUES (2, 'Second', 'Second_desc', 1);
CREATE TABLE TypeFields(
ID int,
Name varchar(255),
NameType varchar(255),
TypeID int/*,
PRIMARY KEY (ID),
FOREIGN KEY (TypeID ) REFERENCES Type(ID) */
);
INSERT INTO TypeFields VALUES (1, 'First', 'str', 1);
INSERT INTO TypeFields VALUES (2, 'Seond', 'str', 1);
INSERT INTO TypeFields VALUES (3, 'Third', 'int', 1);
CREATE TABLE FieldsData(
ID int,
ObjectID int,
FieldID int,
FieldName varchar(255),
TypeID int,
value varchar(255)/*,
PRIMARY KEY (ID),
FOREIGN KEY (TypeID ) REFERENCES Type(ID),
FOREIGN KEY (ObjectID ) REFERENCES Object(ID),
FOREIGN KEY (FieldID ) REFERENCES TypeFields(ID)*/
);
INSERT INTO FieldsData VALUES (1, 1, 1, 'First', 1, "a");
INSERT INTO FieldsData VALUES (2, 1, 2, 'Second', 1, "b");
INSERT INTO FieldsData VALUES (3, 1, 3, 'Third', 1, "120");
INSERT INTO FieldsData VALUES (4, 2, 1, 'First', 1, "c");
INSERT INTO FieldsData VALUES (5, 2, 2, 'Second', 1, "d");
INSERT INTO FieldsData VALUES (6, 2, 3, 'Third', 1, "130");
CREATE TABLE FinalTable(
ObjID int PRIMARY KEY,
ObjName varchar(255),
ObjDesc varchar(255),
First varchar(255),
Second varchar(255),
Third int
);
create view example_type1_realized as
select
FieldsData.ObjectID as ObjID
, max(Object.Name) as ObjName
, max(Object.Description) as ObjDesc
, max(case when FieldID=1 then Value end) as First
, max(case when FieldID=2 then Value end) as Second
, max(case when FieldID=3 then input(Value,best12.) end) as Third
from FieldsData
join Object
on FieldsData.ObjectID = Object.ID
where TypeID = 1
group by ObjID
;
Now, for each TypeID you will need to construct a code generator that can create the source code wallpaper ... max(case when ... construct. from the TypeFields data.
Here is one way:
* Now a codegener macro that can produce the example realization: ;
%macro realize (TypeID=, out=);
%local wallpaper;
proc sql noprint;
select cat
(
', max(case when FieldID=', cats(ID), ' then '
, case
when NameType='str' then 'Value'
when NameType='int' then 'input(Value,12.)'
else 'cats(Value) || " (' || NameType || ') unhandled"'
end
, ' end)'
, ' as ', Name
) length=32000
into :wallpaper separated by ' '
from TypeFields
where TypeID = &TypeID
;
%put NOTE: wallpaper=%SUPERQ(wallpaper);
create &out as
select
FieldsData.ObjectID as ObjID
, max(Object.Name) as ObjName
, max(Object.Description) as ObjDesc
&wallpaper
from FieldsData
join Object
on FieldsData.ObjectID = Object.ID
group by ObjID
;
quit;
%mend;
options mprint;
%realize(TypeID=1, out=table type1_replicate)
You can modify the codegener so that it will insert rows into an out= instead of creating anew.
You should see that your system is workable but needs alot of attention to become generic. Each FieldType might get format, informat, length, date handling, error handling for out-of-range values for type, field sequence, etc...

SQL performance width versus depth

I have a customers table that holds information about customer prefrences like if he wants to receive a newsletter and so on. If he/she wants to receive a newsletter, the value is stored in the column "customerNewsletter" and set to true. However I have a couple of these bit values and parameters that are in a column of there own. I store dates, true/false, integers and tekst like this for each customer.
I find that about 80% of my customers wants to receive a newsletter and that makes that 80% of the values is set to true. I now store a value for each customer set to false or true. What if I only should have to store the 20% set to false ??
There is a list of about 20 of these parameters that I could include as a column (they are now), but I was wondering if there is a better way.
So I create 3 tables to hold these parameter values, a param table holding the actual value, a paramsNames table, that holds the names of the values and a params table that connects the parameters to a customerID
SELECT
customerParamsName as [Name],
customerParamText as [Text],
customerParamINT as [int],
customerParamsDateTime as [Date]
FROM db14.customerParams
INNER JOIN db14.customerParam ON customerParamsChildID = customerParamID
INNER JOIN db14.customerParamsNames ON customerParamNameID = customerParamsNameID
This would give me
Name Text int Date
Phonenumber NULL 615164898 2013-09-20 00:00:00.000
Can anyone tell me if this is a good way to go, or are there more common ways of storing Multi-Type parameters more efficiently ?
AFTER some MORE consideration
I have created 2 tables
customerParam
paramID paramNameID ParamParentID paramChildID paramText paramINT paramDate
INT TINYINT INT INT varchar(24) INT DATETIME
PRIMARY INDEXED
customerParamNames
paramNameID paramName
TINYINT VARCHAR(24)
PRIMARY
1 'FirstName'
2 'LastName'
3 'Email Address'
4 'Phonenumber'
5 etc..
Let's say I want to store the firstName and LastName
I create records in customerParam for both values ;
paramID paramNameID ParamParentID paramChildID paramText paramINT paramDate
17456 1 'John'
17467 2 'Doo'
17468 1 752 17456
17469 2 752 17467
As I expect more occurrences for the name ‘John’ I am storing it as an independent value, then joining it using the parentID/ChildID combination.
and for the phoneNumber
17470 4 752 31615164899
17471 5 752 'me#here.com'
The phonenumber is very explicit to this customer, I am using the parentID to join it straight to the customer. The same goes for the emailaddress.
At this time this solution looks like the way to go... I am also still looking at the xml approach but I don’t have a good understanding on how to use XQuery and xmlDocuments stored in a database.
And It seems like a lot of overhead.
I will move forward with the solution above... until someone gives me a better one.
Example SQL
DECLARE #paramNames TABLE (paramNameID TINYINT, paramName varchar(24))
DECLARE #param TABLE (paramID INT, paramNameID TINYINT, paramParentID INT, paramChildID INT, paramText varchar(24), paramINT INT, paramDate datetime)
INSERT INTO #paramNames VALUES ( 1, 'firstname')
INSERT INTO #paramNames VALUES ( 2, 'lastname')
INSERT INTO #paramNames VALUES ( 3, 'emailaddress')
INSERT INTO #paramNames VALUES ( 4, 'phonenumber')
select * from #paramNames
INSERT INTO #param VALUES (1, 1, Null, Null, 'John' , Null, Null)
INSERT INTO #param VALUES (2, 2, Null, Null, 'Doo' , Null, Null)
INSERT INTO #param VALUES (3, 1, 752, 1, Null , Null, Null)
INSERT INTO #param VALUES (4, 2, 752, 2, Null , Null, Null)
INSERT INTO #param VALUES (5, 4, 752, Null, Null , 615164899, Null)
INSERT INTO #param VALUES (5, 3, 752, Null, 'me#here.com' , Null, Null)
select
a.paramParentID, b.paramName, c.paramText, c.paramINT, c.paramDate
from #param a
inner join #paramNames b on a.paramNameID = b.paramNameID
inner join #param c on a.paramChildID = c.paramID
UNION ALL
select
a.paramParentID, b.paramName, a.paramText, a.paramINT, a.paramDate
from #param a
inner join #paramNames b on a.paramNameID = b.paramNameID
WHERE paramParentID IS NOT NULL
AND paramChildID IS NULL
giving the result
paramParentID paramName paramText paramINT paramDate
752 firstname John NULL NULL
752 lastname Doo NULL NULL
752 phonenumber NULL 615164899 NULL
752 emailaddress me#here.com NULL NULL
I would approach this a little differently if you have performance and flexibility in mind.
USE Test;
CREATE TABLE Customers
(
CustomerID INT NOT NULL CONSTRAINT PK_Customers
PRIMARY KEY CLUSTERED IDENTITY(1,1)
, CustomerName NVARCHAR(255)
);
CREATE TABLE CustomersReceivingEmails
(
CustomerID INT NOT NULL CONSTRAINT FK_CustomerID
FOREIGN KEY REFERENCES Customers (CustomerID)
ON DELETE CASCADE ON UPDATE CASCADE
, EmailAddress NVARCHAR(255) NOT NULL
CONSTRAINT PK_CustomersReceivingEmails
PRIMARY KEY CLUSTERED (CustomerID, EmailAddress)
);
INSERT INTO Customers (CustomerName) VALUES ('Max');
INSERT INTO Customers (CustomerName) VALUES ('Mike');
INSERT INTO CustomersReceivingEmails (CustomerID, EmailAddress)
VALUES (1, 'us#them.com');
INSERT INTO CustomersReceivingEmails (CustomerID, EmailAddress)
VALUES (1, 'us#me.com');
/* ALL Customers */
SELECT * FROM Customers;
/* Only customers who wish to receive Emails, allows a given customer
to have multiple email addresses */
SELECT C.CustomerName, E.EmailAddress
FROM Customers C
INNER JOIN CustomersReceivingEmails E ON C.CustomerID = E.CustomerID
ORDER BY C.CustomerName, E.EmailAddress;
The SELECT returns rows like this:
This allows the Customers table to contain all customers regardless of their preference for emails.
The CustomersReceivingEmails table has a foreign key to Customers.CustomerID for customers who want to receive emails.
Your second solution is a variant of what is commonly known as Entity-Attribute-Value data model. This approach appears to be flexible. However, it essentially generates a schema within schema and is very slow to query as the number of attributes increases
If you're storing a lot of identical values, have a look at columnstore indexes. They work well in scenarios where selectivity is low (lots of rows & only a small number of distinct values)

How to create an "on-the-fly" mapping table within a SELECT statement in Postgresql

I'm creating a select statement that combines two tables, zone and output,
based on a referenced device table and on a mapping of zone_number to output_type_id.
The mapping of zone_number to output_type_id doesn't appear
anywhere in the database, and I would like to create it "on-the-fly" within the select
statement. Below is my schema:
CREATE TABLE output_type (
id INTEGER NOT NULL,
name TEXT,
PRIMARY KEY (id)
);
CREATE TABLE device (
id INTEGER NOT NULL,
name TEXT,
PRIMARY KEY (id)
);
CREATE TABLE zone (
id SERIAL NOT NULL,
device_id INTEGER NOT NULL REFERENCES device(id),
zone_number INTEGER NOT NULL,
PRIMARY KEY (id),
UNIQUE (zone_number)
);
CREATE TABLE output (
id SERIAL NOT NULL,
device_id INTEGER NOT NULL REFERENCES device(id),
output_type_id INTEGER NOT NULL REFERENCES output_type(id),
enabled BOOLEAN NOT NULL,
PRIMARY KEY (id)
);
And here is some example data:
INSERT INTO output_type (id, name) VALUES
(101, 'Output 1'),
(202, 'Output 2'),
(303, 'Output 3'),
(404, 'Output 4');
INSERT INTO device (id, name) VALUES
(1, 'Test Device');
INSERT INTO zone (device_id, zone_number) VALUES
(1, 1),
(1, 2),
(1, 3),
(1, 4);
INSERT INTO output (device_id, output_type_id, enabled) VALUES
(1, 101, TRUE),
(1, 202, FALSE),
(1, 303, FALSE),
(1, 404, TRUE);
I need to get the associated enabled field from the output table for each zone for a given device.
Each zone_number maps to an output_type_id. For this example:
zone_number | output_type_id
----------------------------
1 | 101
2 | 202
3 | 303
4 | 404
One way to handle the mapping would be to create a new table
CREATE TABLE zone_output_type_map (
zone_number INTEGER,
output_type_id INTEGER NOT NULL REFERENCES output_type(id)
);
INSERT INTO zone_output_type_map (zone_number, output_type_id) VALUES
(1, 101),
(2, 202),
(3, 303),
(4, 404);
And use the following SQL to get all zones, plus the enabled flag, for device 1:
SELECT zone.*, output.enabled
FROM zone
JOIN output
ON output.device_id = zone.device_id
JOIN zone_output_type_map map
ON map.zone_number = zone.zone_number
AND map.output_type_id = output.output_type_id
AND zone.device_id = 1
However, I'm looking for a way to create the mapping of zone nunbers to output
types without creating a new table and without piecing together a bunch of AND/OR
statements. Is there an elegant way to create a mapping between the two fields
within the select statement? Something like:
SELECT zone.*, output.enabled
FROM zone
JOIN output
ON output.device_id = zone.device_id
JOIN (
SELECT (
1 => 101,
2 => 202,
3 => 303,
4 => 404
) (zone_number, output_type_id)
) as map
ON map.zone_number = zone.zone_number
AND map.output_type_id = output.output_type_id
AND zone.device_id = 1
Disclaimer: I know that ideally the enabled field would exist in the zone
table. However, I don't have control over that piece. I'm just looking for the
most elegant solution from the application side. Thanks!
You can use VALUES as an inline table and JOIN to it, you just need to give it an alias and column names:
join (values (1, 101), (2, 202), (3, 303), (4, 304)) as map(zone_number, output_type_id)
on ...
From the fine manual:
VALUES can also be used where a sub-SELECT might be written, for
example in a FROM clause:
SELECT f.*
FROM films f, (VALUES('MGM', 'Horror'), ('UA', 'Sci-Fi')) AS t (studio, kind)
WHERE f.studio = t.studio AND f.kind = t.kind;
UPDATE employees SET salary = salary * v.increase
FROM (VALUES(1, 200000, 1.2), (2, 400000, 1.4)) AS v (depno, target, increase)
WHERE employees.depno = v.depno AND employees.sales >= v.target;
So just to complement the accepted answer, the following code is a valid, self-contained Postgresql expression which will evaluate to an 'inline' relation with columns (zone_number, output_type_id):
SELECT * FROM
(VALUES
(1, 101),
(2, 202),
(3, 303),
(4, 304)
) as i(zone_number, output_type_id)
(The (VALUES ... AS ...) part alone will not make a valid expression, which is why I added the SELECT * FROM.)
JOIN
(SELECT 1 zone_number, 101 as output_type_id
UNION ALL
SELECT 2 zone_number, 202 as output_type_id
UNION ALL
SELECT 3 zone_number, 303 as output_type_id
) mappings on mappings.zone_number = zone.zone_number

SQL Server field calculation based on multiple condition

Here is my scenario:
I have a Person table with following fields.
create table Person(PersonID int primary key identity(1,1),
Age int,
height decimal(4,2),
weight decimal(6,2)
);
insert into Person(Age,height,weight) values (60,6.2,169); -- 1
insert into Person(Age,height,weight) values (15,5.1,100); -- 2
insert into Person(Age,height,weight) values (10,4.5,50); -- 3
What I need to do is,
if the person Age >= 18 and height >= 6 then calculationValue = 20
if the person Age >= 18 and height < 6 then calculationValue = 15
if the person Age < 18 and weight >= 60 then calculationValue = 10
if the person Age < 18 and weight < 60 then calculationValue = 5
based on these condition I need to find the calculationValue and do some math.
I tried to make a flexible model so in future it would be easier to add any more conditions and can easily change the constant values (like 18, 6, 60 etc)
I created couple of tables as below:
create table condTable(condTableID int primary key identity(1,1),
condCol varchar(20),
startValue int,
endValue int
);
insert into condTable(condCol,startValue,endValue) values ('Age',18,999) -- 1
insert into condTable(condCol,startValue,endValue) values ('Height',6,99) -- 2
insert into condTable(condCol,startValue,endValue) values ('Height',0,5.99) -- 3
insert into condTable(condCol,startValue,endValue) values ('Age',0,17) -- 4
insert into condTable(condCol,startValue,endValue) values ('Weight',60,999) -- 5
insert into condTable(condCol,startValue,endValue) values ('Weight',0,59) -- 6
I join two condition to make it one in the following table as given by the requirement.(ie. if age >=18 and height >=6 then calculationValue = 20. etc)
create table CondJoin(CondJoin int,condTableID int,CalculationValue int)
insert into CondJoin values (1,1,20)
insert into CondJoin values (1,2,20)
insert into CondJoin values (2,1,15)
insert into CondJoin values (2,3,15)
insert into CondJoin values (3,4,10)
insert into CondJoin values (3,5,10)
insert into CondJoin values (4,4,5)
insert into CondJoin values (4,6,5)
I think this model will provide the flexibility of adding more conditions in future. But I am having difficulties on implementing it in SQL Server 2005. Anyone can write a sql that process in set basis and compare the value in Person table with CondJoin table and provide the corresponding calculationvalue. For eg. for person ID 1 it should look at CondJoin table and give the calculationValue 20 since his age is greater than 18 and height is greater than 6.
this looks like you are headed towards dynamic sql generation.
i think maybe you would be better off with a row for each column and cutoff values for the ranges, and a value if true ... maybe something like:
age_condition
-----------------
min_age
max_age
value
this is something that you could populate and then query without some dynamic generation.
The following is extremely rough but it should get the point across. It normalizes the data and moves towards a semi-object oriented (attribute/value/attribute value) structure. I'll leave it up to you to reinforce referential integrity, but the following is flexible and will return the results you want:
CREATE TABLE Person (
PersonID INT PRIMARY KEY IDENTITY(1,1)
,Name NVARCHAR(255)
);
GO
CREATE TABLE PersonAttribute (
PersonID INT
,CondAttributeID INT
,Value NVARCHAR(255)
);
GO
CREATE TABLE CondAttribute (
AttributeID INT PRIMARY KEY IDENTITY(1,1)
,Attribute NVARCHAR(255));
GO
CREATE TABLE CondTable (
CondTableID INT PRIMARY KEY IDENTITY(1,1)
,CondAttributeID INT
,StartValue MONEY
,EndValue MONEY
);
GO
CREATE TABLE CalculationValues (
CalculationID INT PRIMARY KEY IDENTITY(1,1)
,CalculationValue INT
);
GO
CREATE TABLE CondCalculation (
CondTableID INT
,CalculationID INT
);
INSERT Person (Name)
VALUES ('Joe')
,('Bob')
,('Tom');
INSERT PersonAttribute (
PersonID
,CondAttributeID
,Value
)
VALUES (1, 1, '60')
,(1, 2, '6.2')
,(1, 3, '169')
,(2, 1, '15')
,(2, 2, '5.1')
,(2, 3, '100')
,(3, 1, '10')
,(3, 2, '4.5')
,(3, 3, '50');
INSERT CondAttribute (Attribute)
VALUES ('Age')
,('height')
,('weight');
INSERT CondTable (
CondAttributeID
,StartValue
,EndValue)
VALUES (1,18,999) --Age
,(2,6,99) --Height
,(2,0,5.99) -- Height
,(1,0,17) -- Age
,(3,60,999) -- Weight
,(3,0,59); -- Weight
INSERT CalculationValues (CalculationValue)
VALUES (5)
,(10)
,(15)
,(20);
INSERT CondCalculation (CondTableID, CalculationID)
VALUES (1,4)
,(2,4)
,(1,3)
,(3,3)
,(4,2)
,(5,2)
,(5,1)
,(6,1);
SELECT *
FROM Person AS p
JOIN PersonAttribute AS pa ON p.PersonID = pa.PersonID
JOIN CondAttribute AS ca ON pa.CondAttributeID = ca.AttributeID
JOIN CondTable AS ct ON ca.AttributeID = ct.CondAttributeID
AND CONVERT(money,pa.Value) BETWEEN ct.StartValue AND ct.EndValue
JOIN CondCalculation AS cc ON cc.CondTableID = ct.CondTableID
JOIN CalculationValues AS c ON cc.CalculationID = c.CalculationID
WHERE p.PersonID = 1
The following solution uses PIVOT (twice) to transform the combination of CondJoin and condTable into a chart, then joins the chart to the Person table to calculate the target value. I believe, a series of CASE expressions could be used instead just as well. Anyway...
All the tables have been turned into table variables, for easier testing. So first, DDL and data preparation:
declare #Person table(PersonID int primary key identity(1,1),
Age int,
height decimal(4,2),
weight decimal(6,2)
);
insert into #Person(Age,height,weight) values (60,6.2,169); -- 1
insert into #Person(Age,height,weight) values (15,5.1,100); -- 2
insert into #Person(Age,height,weight) values (10,4.5,50); -- 3
declare #condTable table(condTableID int primary key identity(1,1),
condCol varchar(20),
startValue int,
endValue int
);
insert into #condTable(condCol,startValue,endValue) values ('Age',18,999) -- 1
insert into #condTable(condCol,startValue,endValue) values ('Height',6,99) -- 2
insert into #condTable(condCol,startValue,endValue) values ('Height',0,5.99) -- 3
insert into #condTable(condCol,startValue,endValue) values ('Age',0,17) -- 4
insert into #condTable(condCol,startValue,endValue) values ('Weight',60,999) -- 5
insert into #condTable(condCol,startValue,endValue) values ('Weight',0,59) -- 6
declare #CondJoin table(CondJoin int,condTableID int,CalculationValue int);
insert into #CondJoin values (1,1,20)
insert into #CondJoin values (1,2,20)
insert into #CondJoin values (2,1,15)
insert into #CondJoin values (2,3,15)
insert into #CondJoin values (3,4,10)
insert into #CondJoin values (3,5,10)
insert into #CondJoin values (4,4,5)
insert into #CondJoin values (4,6,5)
And now the query:
;with startValues as (
select
CondJoin,
Age,
Height,
Weight,
CalculationValue
from (
select
j.CondJoin,
j.CalculationValue,
t.condCol,
t.startValue
from #CondJoin j
inner join #condTable t on j.condTableID = t.condTableID
) j
pivot (
max(startValue) for condCol in (Age, Height, Weight)
) p
),
endValues as (
select
CondJoin,
Age,
Height,
Weight,
CalculationValue
from (
select
j.CondJoin,
j.CalculationValue,
t.condCol,
t.endValue
from #CondJoin j
inner join #condTable t on j.condTableID = t.condTableID
) j
pivot (
max(endValue) for condCol in (Age, Height, Weight)
) p
),
combinedChart as (
select
s.CondJoin,
AgeFrom = s.Age,
AgeTo = e.Age,
HeightFrom = s.Height,
HeightTo = e.Height,
WeightFrom = s.Weight,
WeightTo = e.Weight,
s.CalculationValue
from startValues s
inner join endValues e on s.CondJoin = e.CondJoin
)
select
p.*,
c.CalculationValue
from #Person p
left join combinedChart c
on (c.AgeFrom is null or p.Age between c.AgeFrom and c.AgeTo)
and (c.HeightFrom is null or p.Height between c.HeightFrom and c.HeightTo)
and (c.WeightFrom is null or p.Weight between c.WeightFrom and c.WeightTo)

Database auto-increment column

Is it possible to create a Database which has 1 column (but not the column of primary key) to be auto-increment? So that when I insert value to the database, i don't need to fill in the value myself, and DB will fill in that value for that column for me (and increment every time I do a new insert)?
Thank you.
Yes, of course it is possible. Just make this column a unique key (not a primary key) and it has to be declared with a special attribute: "IDENTITY" for SQL Server, and
"AUTO_INCREMENT" for MySQL (see the example below) . And another column can be a primary key.
On MySQL database the table could be declared like this:
CREATE TABLE `mytable` (
`Name` VARCHAR(50) NOT NULL,
`My_autoincrement_column` INTEGER(11) NOT NULL AUTO_INCREMENT,
PRIMARY KEY (`Name`),
UNIQUE KEY `My_autoincrement_column` (`My_autoincrement_column`)
);
Yes, you can do this. Here is a sample for SQL Server using IDENTITY:
CREATE TABLE MyTable (
PrimaryKey varchar(10) PRIMARY KEY,
IdentityColumn int IDENTITY(1,1) NOT NULL,
DefaultColumn CHAR(1) NOT NULL DEFAULT ('N')
)
INSERT INTO MyTable (PrimaryKey) VALUES ('A')
INSERT INTO MyTable (PrimaryKey) VALUES ('B')
INSERT INTO MyTable (PrimaryKey, DefaultColumn) VALUES ('C', 'Y')
INSERT INTO MyTable (PrimaryKey, DefaultColumn) VALUES ('D', 'Y')
INSERT INTO MyTable (PrimaryKey, DefaultColumn) VALUES ('E', DEFAULT)
--INSERT INTO MyTable (PrimaryKey, DefaultColumn) VALUES ('F', NULL) -- ERROR
--> Cannot insert the value NULL into column 'DefaultColumn', table 'tempdb.dbo.MyTable'; column does not allow nulls. INSERT fails.
SELECT * FROM MyTable
Here is an example using SQL Server using functions to roll-your-own incrementing column. This is by means not fault tolerant or the way I would do it. (I'd use the identity feature.) However, it is good to know that you can use functions to return default values.
DROP TABLE MyTable
GO
DROP FUNCTION get_default_for_mytable
GO
CREATE FUNCTION get_default_for_mytable
()
RETURNS INT
AS
BEGIN
-- Declare the return variable here
DECLARE #ResultVar int
-- Add the T-SQL statements to compute the return value here
SET #ResultVar = COALESCE((SELECT MAX(HomeBrewedIdentityColumn) FROM MyTable),0) + 1
-- Return the result of the function
RETURN #ResultVar
END
GO
CREATE TABLE MyTable (
PrimaryKey varchar(10) PRIMARY KEY,
IdentityColumn int IDENTITY(1,1) NOT NULL,
DefaultColumn CHAR(1) NOT NULL DEFAULT ('N'),
HomeBrewedIdentityColumn int NOT NULL DEFAULT(dbo.get_default_for_mytable())
)
GO
INSERT INTO MyTable (PrimaryKey) VALUES ('A')
INSERT INTO MyTable (PrimaryKey) VALUES ('B')
INSERT INTO MyTable (PrimaryKey, DefaultColumn) VALUES ('C', 'Y')
INSERT INTO MyTable (PrimaryKey, DefaultColumn) VALUES ('D', 'Y')
INSERT INTO MyTable (PrimaryKey, DefaultColumn) VALUES ('E', DEFAULT)
--INSERT INTO MyTable (PrimaryKey, DefaultColumn) VALUES ('F', NULL) -- ERRROR
--> Cannot insert the value NULL into column 'DefaultColumn', table 'tempdb.dbo.MyTable'; column does not allow nulls. INSERT fails.
SELECT * FROM MyTable
Results
PrimaryKey IdentityColumn DefaultColumn HomeBrewedIdentityColumn
---------- -------------- ------------- ------------------------
A 1 N 1
B 2 N 2
C 3 Y 3
D 4 Y 4
E 5 N 5
I think you can have only 1 identity autoincrement column per table, this columns doesn't have to be the primary key but it would mean you have to insert the primary key yourself.
If you already have a primary key which is auto increment then I would try and use this if possible.
If you are trying to get an row ID to range on for querying then I would look at creating a view which has the row ID in it (not SQL 2000 or below).
Could you add in what your primary key is and what you intend to use the auto increment column for and it might help come up with a solution
On sql server this is called an identity column
Oracle and DB2 have sequence but I think you are looking for identity and all major dbms (mysql, sql server, db2, oracle) support it.