Parse a simple JSON string - sql-server

I have the following JSON String stored in table
create table #t1 (Caseno int, CaseText text)
insert into #t1 values
(1,'[{"RowType":"KV","s1":0,"s2":0,"s3":0,"TableNo":0,"FieldName":"PCNForEnrollmentCreationDesc","DisplayName":"Header","FieldSchemaName":null,"FieldValue":"","IsDynamic":true,"IsDependent":false,"DependentKey":null,"IsRecurrence":false,"RecurrenceKey":null,"Section":null,"Table":null,"DataType":null,"UIOrder":2,"LookupSelectField":null,"LookupFilterField":null,"col3":null,"col4":null,"col5":null,"col6":null,"col7":null,"col8":null,"col9":null,"col10":null,"col11":null,"col12":null,"col13":null,"FieldType":null,"IsRequired":null},{"RowType":"H2","s1":0,"s2":0,"s3":0,"TableNo":0,"FieldName":"PrimaryEntityInformation","DisplayName":"Primary Entity Information","FieldSchemaName":null,"FieldValue":"","IsDynamic":false,"IsDependent":false,"DependentKey":null,"IsRecurrence":false,"RecurrenceKey":null,"Section":null,"Table":null,"DataType":null,"UIOrder":2,"LookupSelectField":null,"LookupFilterField":null,"col3":null,"col4":null,"col5":null,"col6":null,"col7":null,"col8":null,"col9":null,"col10":null,"col11":null,"col12":null,"col13":null,"FieldType":null,"IsRequired":null},{"RowType":"KV","s1":0,"s2":0,"s3":0,"TableNo":0,"FieldName":"EnrollPrimaryEntityName","DisplayName":"Primary Entity Name","FieldSchemaName":null,"FieldValue":"Mau \"Informatsionnye tekhnologii\"","IsDynamic":true,"IsDependent":false,"DependentKey":null,"IsRecurrence":false,"RecurrenceKey":null,"Section":null,"Table":null,"DataType":null,"UIOrder":2,"LookupSelectField":null,"LookupFilterField":null,"col3":null,"col4":null,"col5":null,"col6":null,"col7":null,"col8":null,"col9":null,"col10":null,"col11":null,"col12":null,"col13":null,"FieldType":null,"IsRequired":null},{"RowType":"KV","s1":0,"s2":0,"s3":0,"TableNo":0,"FieldName":"EnrollPrimaryEntityAddress","DisplayName":"Primary Entity Address","FieldSchemaName":null,"FieldValue":"123 Main St, ","IsDynamic":true,"IsDependent":false,"DependentKey":null,"IsRecurrence":false,"RecurrenceKey":null,"Section":null,"Table":null,"DataType":null,"UIOrder":2,"LookupSelectField":null,"LookupFilterField":null,"col3":null,"col4":null,"col5":null,"col6":null,"col7":null,"col8":null,"col9":null,"col10":null,"col11":null,"col12":null,"col13":null,"FieldType":null,"IsRequired":null},{"RowType":"KV","s1":0,"s2":0,"s3":0,"TableNo":0,"FieldName":"EnrollPrimaryEntityCity","DisplayName":"Primary Entity City","FieldSchemaName":null,"FieldValue":"Kaliningrad","IsDynamic":true,"IsDependent":false,"DependentKey":null,"IsRecurrence":false,"RecurrenceKey":null,"Section":null,"Table":null,"DataType":null,"UIOrder":2,"LookupSelectField":null,"LookupFilterField":null,"col3":null,"col4":null,"col5":null,"col6":null,"col7":null,"col8":null,"col9":null,"col10":null,"col11":null,"col12":null,"col13":null,"FieldType":null,"IsRequired":null},{"RowType":"KV","s1":0,"s2":0,"s3":0,"TableNo":0,"FieldName":"EnrollPrimaryEntityPhone","DisplayName":"Primary Entity Phone","FieldSchemaName":null,"FieldValue":"772 322-977","IsDynamic":true,"IsDependent":false,"DependentKey":null,"IsRecurrence":false,"RecurrenceKey":null,"Section":null,"Table":null,"DataType":null,"UIOrder":2,"LookupSelectField":null,"LookupFilterField":null,"col3":null,"col4":null,"col5":null,"col6":null,"col7":null,"col8":null,"col9":null,"col10":null,"col11":null,"col12":null,"col13":null,"FieldType":null,"IsRequired":null},{"RowType":"KV","s1":0,"s2":0,"s3":0,"TableNo":0,"FieldName":"EnrollPrimaryEntityPostalCode","DisplayName":"Primary Entity Postal Code","FieldSchemaName":null,"FieldValue":"236029","IsDynamic":true,"IsDependent":false,"DependentKey":null,"IsRecurrence":false,"RecurrenceKey":null,"Section":null,"Table":null,"DataType":null,"UIOrder":2,"LookupSelectField":null,"LookupFilterField":null,"col3":null,"col4":null,"col5":null,"col6":null,"col7":null,"col8":null,"col9":null,"col10":null,"col11":null,"col12":null,"col13":null,"FieldType":null,"IsRequired":null},{"RowType":"KV","s1":0,"s2":0,"s3":0,"TableNo":0,"FieldName":"EnrollPrimaryEntityCountry","DisplayName":"Primary Entity Country","FieldSchemaName":null,"FieldValue":"Russia","IsDynamic":true,"IsDependent":false,"DependentKey":null,"IsRecurrence":false,"RecurrenceKey":null,"Section":null,"Table":null,"DataType":null,"UIOrder":2,"LookupSelectField":null,"LookupFilterField":null,"col3":null,"col4":null,"col5":null,"col6":null,"col7":null,"col8":null,"col9":null,"col10":null,"col11":null,"col12":null,"col13":null,"FieldType":null,"IsRequired":null},{"RowType":"H2","s1":0,"s2":0,"s3":0,"TableNo":0,"FieldName":"PrimaryContactInformation","DisplayName":"Primary Contact Information","FieldSchemaName":null,"FieldValue":"","IsDynamic":false,"IsDependent":false,"DependentKey":null,"IsRecurrence":false,"RecurrenceKey":null,"Section":null,"Table":null,"DataType":null,"UIOrder":2,"LookupSelectField":null,"LookupFilterField":null,"col3":null,"col4":null,"col5":null,"col6":null,"col7":null,"col8":null,"col9":null,"col10":null,"col11":null,"col12":null,"col13":null,"FieldType":null,"IsRequired":null},{"RowType":"KV","s1":0,"s2":0,"s3":0,"TableNo":0,"FieldName":"EnrollContactTypeIndividual","DisplayName":"Contact Type","FieldSchemaName":null,"FieldValue":"Individual","IsDynamic":false,"IsDependent":false,"DependentKey":null,"IsRecurrence":false,"RecurrenceKey":null,"Section":null,"Table":null,"DataType":null,"UIOrder":2,"LookupSelectField":null,"LookupFilterField":null,"col3":null,"col4":null,"col5":null,"col6":null,"col7":null,"col8":null,"col9":null,"col10":null,"col11":null,"col12":null,"col13":null,"FieldType":null,"IsRequired":null},{"RowType":"KV","s1":0,"s2":0,"s3":0,"TableNo":0,"FieldName":"EnrollPriContactFirstName","DisplayName":"Given Name: ","FieldSchemaName":null,"FieldValue":"Elena","IsDynamic":true,"IsDependent":false,"DependentKey":null,"IsRecurrence":false,"RecurrenceKey":null,"Section":null,"Table":null,"DataType":null,"UIOrder":2,"LookupSelectField":null,"LookupFilterField":null,"col3":null,"col4":null,"col5":null,"col6":null,"col7":null,"col8":null,"col9":null,"col10":null,"col11":null,"col12":null,"col13":null,"FieldType":null,"IsRequired":null},{"RowType":"KV","s1":0,"s2":0,"s3":0,"TableNo":0,"FieldName":"EnrollPriContactLastName","DisplayName":"Family Name: ","FieldSchemaName":null,"FieldValue":"Andre","IsDynamic":true,"IsDependent":false,"DependentKey":null,"IsRecurrence":false,"RecurrenceKey":null,"Section":null,"Table":null,"DataType":null,"UIOrder":2,"LookupSelectField":null,"LookupFilterField":null,"col3":null,"col4":null,"col5":null,"col6":null,"col7":null,"col8":null,"col9":null,"col10":null,"col11":null,"col12":null,"col13":null,"FieldType":null,"IsRequired":null},{"RowType":"KV","s1":0,"s2":0,"s3":0,"TableNo":0,"FieldName":"EnrollPriContPrimaryEntityCountry","DisplayName":"Primary Entity Country","FieldSchemaName":null,"FieldValue":"Russia","IsDynamic":true,"IsDependent":false,"DependentKey":null,"IsRecurrence":false,"RecurrenceKey":null,"Section":null,"Table":null,"DataType":null,"UIOrder":2,"LookupSelectField":null,"LookupFilterField":null,"col3":null,"col4":null,"col5":null,"col6":null,"col7":null,"col8":null,"col9":null,"col10":null,"col11":null,"col12":null,"col13":null,"FieldType":null,"IsRequired":null},{"RowType":"KV","s1":0,"s2":0,"s3":0,"TableNo":0,"FieldName":"EnrollPriContPrimaryEntityAddress","DisplayName":"Primary Entity Address","FieldSchemaName":null,"FieldValue":"123 Main St, ","IsDynamic":true,"IsDependent":false,"DependentKey":null,"IsRecurrence":false,"RecurrenceKey":null,"Section":null,"Table":null,"DataType":null,"UIOrder":2,"LookupSelectField":null,"LookupFilterField":null,"col3":null,"col4":null,"col5":null,"col6":null,"col7":null,"col8":null,"col9":null,"col10":null,"col11":null,"col12":null,"col13":null,"FieldType":null,"IsRequired":null},{"RowType":"KV","s1":0,"s2":0,"s3":0,"TableNo":0,"FieldName":"EnrollPriContPrimaryEntityCity","DisplayName":"Primary Entity City","FieldSchemaName":null,"FieldValue":"Kaliningrad","IsDynamic":true,"IsDependent":false,"DependentKey":null,"IsRecurrence":false,"RecurrenceKey":null,"Section":null,"Table":null,"DataType":null,"UIOrder":2,"LookupSelectField":null,"LookupFilterField":null,"col3":null,"col4":null,"col5":null,"col6":null,"col7":null,"col8":null,"col9":null,"col10":null,"col11":null,"col12":null,"col13":null,"FieldType":null,"IsRequired":null},{"RowType":"KV","s1":0,"s2":0,"s3":0,"TableNo":0,"FieldName":"EnrollPriContPrimaryEntityPhone","DisplayName":"Primary Entity Phone","FieldSchemaName":null,"FieldValue":"772 322-977","IsDynamic":true,"IsDependent":false,"DependentKey":null,"IsRecurrence":false,"RecurrenceKey":null,"Section":null,"Table":null,"DataType":null,"UIOrder":2,"LookupSelectField":null,"LookupFilterField":null,"col3":null,"col4":null,"col5":null,"col6":null,"col7":null,"col8":null,"col9":null,"col10":null,"col11":null,"col12":null,"col13":null,"FieldType":null,"IsRequired":null},{"RowType":"KV","s1":0,"s2":0,"s3":0,"TableNo":0,"FieldName":"EnrollPriContEmailAddress","DisplayName":"Email Address","FieldSchemaName":null,"FieldValue":"info#us.me","IsDynamic":true,"IsDependent":false,"DependentKey":null,"IsRecurrence":false,"RecurrenceKey":null,"Section":null,"Table":null,"DataType":null,"UIOrder":2,"LookupSelectField":null,"LookupFilterField":null,"col3":null,"col4":null,"col5":null,"col6":null,"col7":null,"col8":null,"col9":null,"col10":null,"col11":null,"col12":null,"col13":null,"FieldType":null,"IsRequired":null},{"RowType":"KV","s1":0,"s2":0,"s3":0,"TableNo":0,"FieldName":"EnrollPriContDataLanguage","DisplayName":"Data Language","FieldSchemaName":null,"FieldValue":"Russkij","IsDynamic":true,"IsDependent":false,"DependentKey":null,"IsRecurrence":false,"RecurrenceKey":null,"Section":null,"Table":null,"DataType":null,"UIOrder":2,"LookupSelectField":null,"LookupFilterField":null,"col3":null,"col4":null,"col5":null,"col6":null,"col7":null,"col8":null,"col9":null,"col10":null,"col11":null,"col12":null,"col13":null,"FieldType":null,"IsRequired":null},{"RowType":"KV","s1":0,"s2":0,"s3":0,"TableNo":0,"FieldName":"PCNforEnrollPrimaryEntity","DisplayName":"PCN for Enrollment Primary Entity","FieldSchemaName":null,"FieldValue":"123455","IsDynamic":false,"IsDependent":false,"DependentKey":null,"IsRecurrence":false,"RecurrenceKey":null,"Section":null,"Table":null,"DataType":null,"UIOrder":2,"LookupSelectField":null,"LookupFilterField":null,"col3":null,"col4":null,"col5":null,"col6":null,"col7":null,"col8":null,"col9":null,"col10":null,"col11":null,"col12":null,"col13":null,"FieldType":"FreeText","IsRequired":"true"},{"RowType":"H2","s1":0,"s2":0,"s3":0,"TableNo":0,"FieldName":"NoticesEntityInformation"')
I want to extract the value from the string and present this way:
Caseno FieldName FieldValue
1 Header
1 Primary Entity Information
1 Primary Entity Name Mau \"Informatsionnye tekhnologii
1 Primary Entity Address 123 Main St,
1 Primary Entity City Kaliningrad
1 Primary Entity Phone 772 322-977
1 Primary Entity Postal Code 236029
1 Primary Entity Country Russia
1 Primary Contact Information
1 Contact Type Individual
1 Given Name: Elena
1 Family Name: Andre
1 Primary Entity Country Russia
1 Primary Entity Address 123 Main St,
1 Primary Entity City Kaliningrad
1 Primary Entity Phone 772 322-977
1 Email Address info#us.me
1 Data Language Russkij
1 PCN for Enrollment Primary Entity 123455
Basically I want to extract DisplayName and FieldValues.
Example String: "DisplayName":"Primary Entity Name","FieldSchemaName":null,"FieldValue":"Mau \"Informatsionnye tekhnologii\""

If you use SQL Server 2016+, just parse the JSON with OPENJSON() using explicit schema (WITH clause). The data in the question is truncated (and the JSON is not valid), but the following example is a working solution:
Table and JSON (fixed and simplified):
create table #t1 (Caseno int, CaseText text)
insert into #t1 values
(1,'[
{
"RowType":"KV",
"s1":0,
"s2":0,
"s3":0,
"TableNo":0,
"FieldName":"PCNForEnrollmentCreationDesc",
"DisplayName":"Header",
"FieldSchemaName":null,
"FieldValue":"",
"IsDynamic":true,
"IsDependent":false,
"DependentKey":null,
"IsRecurrence":false,
"RecurrenceKey":null,
"Section":null,
"Table":null,
"DataType":null,
"UIOrder":2,
"LookupSelectField":null,
"LookupFilterField":null,
"col3":null,
"col4":null,
"col5":null,
"col6":null,
"col7":null,
"col8":null,
"col9":null,
"col10":null,
"col11":null,
"col12":null,
"col13":null,
"FieldType":null,
"IsRequired":null
},
{
"RowType":"H2",
"s1":0,
"s2":0,
"s3":0,
"TableNo":0,
"FieldName":"PrimaryEntityInformation",
"DisplayName":"Primary Entity Information",
"FieldSchemaName":null,
"FieldValue":"",
"IsDynamic":false,
"IsDependent":false,
"DependentKey":null,
"IsRecurrence":false,
"RecurrenceKey":null,
"Section":null,
"Table":null,
"DataType":null,
"UIOrder":2,
"LookupSelectField":null,
"LookupFilterField":null,
"col3":null,
"col4":null,
"col5":null,
"col6":null,
"col7":null,
"col8":null,
"col9":null,
"col10":null,
"col11":null,
"col12":null,
"col13":null,
"FieldType":null,
"IsRequired":null
},
{
"RowType":"KV",
"s1":0,
"s2":0,
"s3":0,
"TableNo":0,
"FieldName":"EnrollPrimaryEntityName",
"DisplayName":"Primary Entity Name",
"FieldSchemaName":null,
"FieldValue":"Mau \"Informatsionnye tekhnologii\"",
"IsDynamic":true,
"IsDependent":false,
"DependentKey":null,
"IsRecurrence":false,
"RecurrenceKey":null,
"Section":null,
"Table":null,
"DataType":null,
"UIOrder":2,
"LookupSelectField":null,
"LookupFilterField":null,
"col3":null,
"col4":null,
"col5":null,
"col6":null,
"col7":null,
"col8":null,
"col9":null,
"col10":null,
"col11":null,
"col12":null,
"col13":null,
"FieldType":null,
"IsRequired":null
}
]')
Statement:
SELECT t.CaseNo, j.*
FROM #t1 t
CROSS APPLY OPENJSON(t.CaseText) WITH (
DisplaydName varchar(100) '$.DisplayName',
FieldValue varchar(100) '$.FieldValue'
) j
Result:
CaseNo DisplaydName FieldValue
1 Header
1 Primary Entity Information
1 Primary Entity Name Mau "Informatsionnye tekhnologii"
1 Primary Entity Address 123 Main St,
1 Primary Entity City Kaliningrad
1 Primary Entity Phone 772 322-977
1 Primary Entity Postal Code 236029
1 Primary Entity Country Russia
1 Primary Contact Information
1 Contact Type Individual
1 Given Name: Elena
1 Family Name: Andre
1 Primary Entity Country Russia
1 Primary Entity Address 123 Main St,
1 Primary Entity City Kaliningrad
1 Primary Entity Phone 772 322-977
1 Email Address info#us.me
1 Data Language Russkij
1 PCN for Enrollment Primary Entity 123455

Related

How create procedure to retrieve data from three tables with user inserted data

I have three tables DEPARTMENTS, SEMESTER and SUBJECTS
CREATE TABLE DEPARTMENTS
(
D_ID INT PRIMARY KEY identity(1, 1),
department_name VARCHAR(50) NOT NULL
);
CREATE TABLE SEMESTER
(
D_ID INT FOREIGN KEY REFERENCES departments(D_ID),
sem_id INT PRIMARY KEY identity(1, 1),
semester INT CHECK
(semester BETWEEN 1 AND 8) NOT NULL
);
CREATE TABLE SUBJECTS
(
D_ID INT FOREIGN KEY REFERENCES departments(D_ID),
sem_id INT FOREIGN KEY REFERENCES semester(sem_id),
sub_id INT PRIMARY KEY identity(1, 1),
sub_name VARCHAR(50),
syllabus VARBINARY(MAX),
exam_format VARBINARY(MAX)
);
If user insert department_name in DEPARTMENTS table and semester from semester table then how to get sub_name from subject
Firstly, your design is wrong. you don't need to have a reference from Subject to Departments because Department is accessible through Semester and the D_Id in Subject is redundant, IMHO.
Second, what is your criteria for retrieving subject?
Select Sub_Name
from Subjects as sbj
inner join Departments as dpt on sbj.D_Id = dpt.D_Id
inner join Semester as smt on sbj.sem_id = smt.sem_id
where (YOUR CRITERIA)
This is basically the general correlation among your tables,
so you could specify the conditions and list of fields to be retrieved.
However it's best to rectify your design first and then access department
through Semester :
inner join Departments as dpt on smt.D_Id = dpt.D_Id
thereby you're gonna use the dependency in semester rather than subjects!
p.s.
if the department in subject is different from the on in semester
then your layout is good as it is, but if they're same thing then
you can get rid of the latter
You need to use join here
select sub.sub_name,dep.department_name,sem.semester
from subjects sub
join departments dep on dep.d_id = sub.d_id
join semester sem on sem.sem_id = sub.sem_id;

Choice of Clustered and Non-Clustered Index in SQL Server

I have two standalone tables Person and Bank in one database. Moreover I have a reference table Customers in another database.
Table: Person - Database #1
PersonId (PK) Name
_________________________________
1 Ram
2 Raj
3 John
4 Emma
Table: Bank - Database #1
BankId (PK) Name
_________________________________
1 ICICI
2 HDFC
3 SBI
Table: Customers - Database #2
CustomerId (PK) BankId PersonId UserName PIN
_____________________________________________________________
1 1 1 person1 7456bb
2 1 4 person4 NULL
3 2 1 person1 5691io
4 3 2 person2 7892yh
5 3 4 person4 1596pl
I need to execute the following queries efficiently, without INDEX it will do a FULL COLUMN search:
SELECT
c.CustomerId, p.Name, b.Name, c.UserName, c.PIN
FROM
DB2.Customers c
INNER JOIN
DB1.Person p ON p.PersonId = c.PersonId
INNER JOIN
DB1.Bank b ON b.BankId = c.BankId
SELECT *
FROM DB2.Customers c
WHERE c.UserName = 'person1'
SELECT *
FROM DB2.Customers c
WHERE c.UserName = 'person2' AND c.PIN = '7892yh'
So, I need a CREATE table SQL query for the table Customers. The following are the constraints
CustomerId int NOT NULL primary key
BankId int NOT NULL -- needs index and non-unique
PersonId int NOT NULL -- needs index and non-unique
UserName varchar(25) NOT NULL -- needs index and non-unique
PIN varchar(10) NULL -- needs index and non-unique
The said table don't have a FK relationship because the said table is in different database server. So, I need a efficient table structure to fetch records using JOIN. I don't know the which INDEX is efficient in this scenario either clustered nor non-clustered index.

Create BD structure for multiple stats

my cuestion is simple but there are so many possibilities...
Imagine you have a car, the car has a state (available, rented, in workshop, sold,...) and each state has different attributes:
available: we want to keep the employee has established the state and date.
rented: employee, client(company or person), start date, end date and price
in workshop: workshop, date.
sold: client(company or person), employee, date, price.
and finally, we want to keep a record of states of each car.
Easy. This is one entity (car) that has a multi-valued attribute (state). Let's use status, the word state has other more obvious meanings. However, the entity has other associated attributes that only apply according to the status, so the status attribute can be treated as if it were an entity. This the status of a car is represented as a relationship with the status "entity." Your other requirement that the status be recorded is also easy. Be aware, though, that easy does not necessarily mean simple.
create table Cars(
ID int auto_generated primary key,
... ... -- other attributes
);
The status has a base-class, sub-class format
create table Status(
ID char( 1 ) not null primary key, -- 'A', 'R', 'W', 'S'
Label varchar not null - 'Available', 'Rented', 'In Workshop', 'Sold'
constraint UQ_StatusLabel unique( ID, Label )
);
Now the tables that will contain the actual status information for the cars.
create table AStatus(
A_ID char( 1 ) check( A_ID = 'A' ),
EstablishedBy int not null references Employees( ID ),
);
create table RStatus(
R_ID char( 1 ) check( R_ID = 'R' ),
RentedBy int not null references Employees( ID ),
RentedTo int not null references Clients( ID ),
Price currency not null
);
create table WStatus(
W_ID char( 1 ) check( W_ID = 'W' ),
WS_ID int not null references Workshops( ID )
);
create table SStatus(
S_ID char( 1 ) check( S_ID = 'S' ),
SoldBy int not null references Employees( ID ),
SoldTo int not null references Clients( ID ),
Price currency not null
);
Finally, the intersection table that ties these all together.
create table CarStatus(
CarID int not null references Cars( ID ),
EffDate date not null default Now(),
StatusID char( 1 ) not null references Status( ID ),
constraint PK_CarStatus primary key( CarID, EffDate )
);
Let's follow a car as it goes into the workshop, becomes available, is rented out, returned (becomes available again) and then sold. All these changes take place (conveniently enough) on the first date of successive months.
CarStatus xStatus
1 1/1/16 W W 17 -- Jan 1, Car #1 goes to workshop 17
1 2/1/16 A A 2 -- Feb 1, Employee #2 makes if available
1 3/1/16 R R 2 12 24 -- Mar 1, Employee #2 rents to client #12 for $24
1 4/1/16 A A 3 -- Apr 1, Employee #3 returns car to Available
1 5/1/16 S S 3 13 20000 -- May 1, Employee #3 sells car to client #13 for $20000
Note that the table rows do not replace each other. So the CarStatus table contains the five rows as shown, AStatus contains two rows, and each of the other xStatus tables contain one row each.
To read the history of what has happened to car # 1:
select c.id CarID, s.Label Status, cs.EffDate "Effective Date"
from CarStatus cs
join Cars c
on c.ID = cs.CarID
join Status s
on s.ID = cs.StatusID
where cs.CarID = 1
order by cs.EffDate;
CarID Status Effective Date
1 In Workshop 1/1/16
1 Available 2/1/16
1 Rented 3/1/16
1 Available 4/1/16
1 Sold 5/1/16
Note there is only one date associated with a rental. The return date is the date the car next becomes available. If there is some length of time between a return and when it can become available again, such as for cleaning, then add another status. Something like, oh, Cleaning. (Or you could add a "ReturnDate" field to the RStatus table, but that opens you up for the possibility of anomalous data so you have to be extra vigilant.)
To show the current state of a car is a little more involved. But I'm using Version Normal Form (search for it under my name for more details) which is a standard pattern I use for maintaining a history of data changes. So the query also fits a standard pattern which is quickly learned.
select c.id CarID, s.Label Status, cs.EffDate "Effective Date"
from CarStatus cs
join CarStatus cs2
on cs2.CarID = cs.CarID
and cs2.EffDate =(
select Max( cs3.EffDate )
from CarStatus cs3
where cs3.EffDate <= Now() )
join Cars c
on c.ID = cs.CarID
join Status s
on s.ID = cs.StatusID
where cs.CarID = 1
order by cs.EffDate;
This will show the last row of the result set above because that is the last status entered. You can remove the WHERE and ORDER BY clauses and use this query to create a view which shows the current status of all the cars.
Here's the cool part. Suppose you want to see the status of the car as of 15 Mar. Just change the "Now()" in the query above to "to_date('3/15/16')" and you will get the third row in the result set above: Rented. So you can look at the current status or the past status on any particular date using the same query.

ORA-00911: invalid character on oracle, but works with H2

I have the following SQL command, it works with H2 database, but when i try to run it on Oracle XE, it gets the " ORA-00911: invalid character " error.
create table EMPLOYEE (
EMPLOYEE_KEY NUMBER(10) not null,
SALARY NUMBER(10,2),
LAST_NAME VARCHAR2(132),
FIRST_NAME VARCHAR2(132),
SUPERVISOR_KEY NUMBER(10),
constraint EMPLOYEE_PK primary key (EMPLOYEE_KEY)
);
create unique index EMPLOYEE_PK on EMPLOYEE(EMPLOYEE_KEY);
With this example, when you created the primary key constraint, you also created an index with the same name as the index you are trying to create, EMPLOYEE_PK.
Tom Kytes states on his ask tom site:
A primary key or unique constraint is not guaranteed to create a new
index, nor is the index they create guaranteed to be a unique index.
Therefore, if you desire a unique index to be created for query
performance issues, you should explicitly create one.
Oddly, enough when I run your DDL, I receive the ORA-00955 error and not the ORA-00911 (invalid character error).
Often times the ORA-00911 error occurs when one is copying from one editor to another and you copy some non-printable characters.
Below, I ran the first DDL statement provided and then I checked the indices created. If you look closely, you will see that EMPLOYEE_PK index was created as a consequence of the primary key constraint which you created.
SCOTT#dev> create table EMPLOYEE (
2 EMPLOYEE_KEY NUMBER(10) not null,
3 SALARY NUMBER(10,2),
4 LAST_NAME VARCHAR2(132),
5 FIRST_NAME VARCHAR2(132),
6 SUPERVISOR_KEY NUMBER(10),
7 constraint EMPLOYEE_PK primary key (EMPLOYEE_KEY)
8 );
Table created.
SCOTT#dev> SELECT ind.index_name,
2 ind.index_type,
3 ind.table_owner,
4 ind.table_name
5 FROM all_indexes ind
6 JOIN all_ind_columns icol
7 ON ind.owner = icol.index_owner
8 AND ind.table_name = icol.table_name
9 AND ind.index_name = icol.index_name
10 WHERE 1 = 1
11 AND ind.table_name = 'EMPLOYEE'
12 /
INDEX_NAME INDEX_TYPE TABLE_OWNER TABLE_NAME
============================== =========================== ============================== ==============================
EMPLOYEE_PK NORMAL SCOTT EMPLOYEE
If you desire to create a unique index on EMPLOYEE_KEY separately, a number of approaches could be taken. Here is one:
--create the table
SCOTT#dev> create table EMPLOYEE (
2 EMPLOYEE_KEY NUMBER(10) not null,
3 SALARY NUMBER(10,2),
4 LAST_NAME VARCHAR2(132),
5 FIRST_NAME VARCHAR2(132),
6 SUPERVISOR_KEY NUMBER(10)
7 );
Table created.
--create the unique index
SCOTT#dev> CREATE UNIQUE INDEX EMPLOYEE_PK ON EMPLOYEE (EMPLOYEE_KEY);
Index created.
--add the primary key
SCOTT#dev> alter table EMPLOYEE add
2 constraint EMPLOYEE_PK primary key (EMPLOYEE_KEY)
3 /
Table altered.
SCOTT#dev> SELECT ind.index_name,
2 ind.index_type,
3 ind.table_owner,
4 ind.table_name
5 FROM all_indexes ind
6 JOIN all_ind_columns icol
7 ON ind.owner = icol.index_owner
8 AND ind.table_name = icol.table_name
9 AND ind.index_name = icol.index_name
10 WHERE 1 = 1
11 AND ind.table_name = 'EMPLOYEE'
12 /
INDEX_NAME INDEX_TYPE TABLE_OWNER TABLE_NAME
============================== =========================== ============================== ==============================
EMPLOYEE_PK NORMAL SCOTT EMPLOYEE
SCOTT#dev>
SCOTT#dev> SELECT cons.constraint_name
2 FROM all_constraints cons
3 JOIN all_cons_columns conc
4 ON conc.table_name = 'EMPLOYEE'
5 AND cons.owner = conc.owner
6 AND cons.table_name = conc.table_name
7 WHERE 1 = 1
8 AND cons.constraint_name = conc.constraint_name
9 /
CONSTRAINT_NAME
=============================
EMPLOYEE_PK
I won' insist on the redundancy between a primary key and an unique index, but, to answer only to your question as it is titled:
when i try to run it on Oracle XE, it gets the " ORA-00911: invalid character " error.
It chops on ; as, as far as I know, you can only issue one SQL command at a time through the "SQL Workshop > SQL Command" page of Oracle application Express (you can send a PL/SQL bloc too)
However you can select your commands one by one and hit run. When there is a selection, only the sectioned part is executed. For example, in the following screen capture, only the first DDL statement will be executed by clicking on the "run" button:

How to dynamically set up TreeView

Using WPF I would like to have a TreeView that displays data from a Compact Database variably. That means I would like to change the view based on how the user would like it sorted.
Assuming that the database has the following tables and schema, how can I set up my views? As I said, they should be variable, but I would consider multiple HierarchialDataTemplates if that is possible. A few potential views that come to mind are listed below.
Thanks for any help offered!
Employee
ID - Int primary Unique AutoIncrement
Name – string
Department - int (foreign relation to Department. ID)
Supervisor - int (ID of Employee in this table)
Division
ID - Int primary Unique AutoIncrement
Name – Unique string
DivisionLeader - int (foreign relation to Employee. ID)
Department
ID - Int primary Unique AutoIncrement
Name – Unique string
DepartmentLeader - int (foreign relation to Employee. ID)
Division – int (foreign relation to Division. ID)
CustomerSupportDepartments
ID - Int primary Unique AutoIncrement
Name – string
Department - int (foreign relation to Department. ID)
Continent - int (foreign relation to Continent. ID)
Country - int (foreign relation to Country. ID)
State - int (foreign relation to State. ID)
City - int (foreign relation to City. ID)
Customer
ID - Int primary Unique AutoIncrement
Name – string
SalesRep - (foreign relation to Employee. ID)
Revenue . int
Continent - int (foreign relation to Continent. ID)
Country - int (foreign relation to Country. ID)
State - int (foreign relation to State. ID)
City - int (foreign relation to City. ID)
Continent
ID - Int primary Unique AutoIncrement
Name – Unique string
Country
ID - Int primary Unique AutoIncrement
Name – Unique string
State
ID - Int primary Unique AutoIncrement
Name – Unique string
City
ID - Int primary Unique AutoIncrement
Name – Unique string
Potential Views
View 1
Employees
Employee Name 1
Supervisor
Name
Department
Name
Customers
Name 1
Name 2
Name 3
Employee Name 2
Supervisor
Name
Department
Name
Customers
Name 1
Name 2
Name 3
View 2
Departments
Department Name 1
Division
Name
Supervisor
Name
Employees
Employee Name 1
Employee Name 2
Department Name 2
Division
Name
Supervisor
Name
Employees
Employee Name 1
Employee Name 2
View 3
Divisions
Division Name 1
Departments
Department Name 1
Department Name 2
Division Name 2
Departments
Department Name 1
Department Name 2
View 4
Continents
Continent Name 1
Country Name 1
City Name 1
Customers
Customer Name 1
Sales Representative
Employee Name
Revenue
Sum
Customer Name 2
Sales Representative
Employee Name
Revenue
Sum
Country Name 2
City Name 1
Customers
Customer Name 1
Sales Representative
Employee Name 1
Revenue
Sum
City Name 2
Customers
Customer Name 1
Sales Representative
Employee Name 1
Revenue
Sum
Customer Name 2
Sales Representative
Employee Name
Revenue
Sum
View 5
Customers
Customer Name 1
Sales Representative
Employee Name
Sales Representative’s Supervisor
Employee Name
Location
Continent
Country
State
City
Revenue
Sum
Customer Name 2
Sales Representative
Employee Name
Sales Representative’s Supervisor
Employee Name
Location
Continent
Country
State
City
Revenue
Sum
I wound up using a generic object.

Resources