Apache derby Double datatype - and trying to default it to "null" - default

Well, I'm a bit puzzeled here, yet it seems to be a really dumb question.
I'm trying to create a table that has a double column that is null by default. What i get is a table with a double column that is 0 by default, and I have no idea why.
The relevant part of the code:
CREATE_TABLE_STATEMENT = "create table " + TABLE_NAME + "
(id int not null generated always as identity constraint pk primary key,
parentid int default null, title varchar(50), minimum double default null,
maximum double default null, timeUnit double default null,
comment varchar(150) default null)";
And the corresponding "insert":
"insert into " + TABLE_NAME + " " + "(parentid, title) values (1, 'first')"

Seems to work fine on my copy of Derby (see below).
What makes you think the default of null isn't working properly?
ij> create table xxx (id int not null generated always as identity constraint pk primary key,
parentid int default null, title varchar(50), minimum double default null,
maximum double default null, timeUnit double default null,
comment varchar(150) default null) ;
0 rows inserted/updated/deleted
ij> describe xxx;
COLUMN_NAME |TYPE_NAME|DEC&|NUM&|COLUM&|COLUMN_DEF|CHAR_OCTE&|IS_NULL&
------------------------------------------------------------------------------
ID |INTEGER |0 |10 |10 |AUTOINCRE&|NULL |NO
PARENTID |INTEGER |0 |10 |10 |NULL |NULL |YES
TITLE |VARCHAR |NULL|NULL|50 |NULL |100 |YES
MINIMUM |DOUBLE |NULL|2 |52 |NULL |NULL |YES
MAXIMUM |DOUBLE |NULL|2 |52 |NULL |NULL |YES
TIMEUNIT |DOUBLE |NULL|2 |52 |NULL |NULL |YES
COMMENT |VARCHAR |NULL|NULL|150 |NULL |300 |YES
7 rows selected
ij> insert into xxx (parentid, title) values (1, 'first');
1 row inserted/updated/deleted
ij> select * from xxx;
ID |PARENTID |TITLE |MINIMUM |MAXIMUM |TIMEUNIT |COMMENT
--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
1 |1 |first |NULL |NULL |NULL |NULL
1 row selected

Related

Multiple table CTE with numbering

I am struggling with Common Table Expression (CTE) for the database I have.
tblDistricts
DistrictID (pk)
District
tblVillage
VillageID (pk)
DistrictID
Village
tblWard
WardID (pk)
VillageID
DistrictID
Village
Below is how to create the sample database:
Create Table tblDistricts(
[DistrictID] [int] IDENTITY(1,1) NOT NULL Primary Key Clustered,
[District] [nvarchar](200) NOT NULL
)
GO
Create Table tblVillage(
[VillageID] [int] IDENTITY(1,1) NOT NULL Primary Key Clustered,
[DistrictID] [int] NOT NULL,
[Village] [nvarchar](200) NOT NULL
)
GO
Create Table tblWards(
[WardID] [int] IDENTITY(1,1) NOT NULL Primary Key Clustered,
[DistrictID] [int] NOT NULL,
[VillageID] [int] NOT NULL,
[Ward] [nvarchar](200) NOT NULL
)
GO
Insert into tblDistricts Values ('Barolong')
Insert into tblDistricts Values ('Central_Bobonong')
Insert into tblDistricts Values ('Central_Boteti')
Insert into tblDistricts Values ('Central_Mahalapye')
Insert into tblDistricts Values ('Central_Tutume')
Insert into tblDistricts Values ('Chobe')
GO
Insert into tblVillage Values (1,'Bethel')
Insert into tblVillage Values (1,'Borobadilepe')
Insert into tblVillage Values (1,'Digawana')
Insert into tblVillage Values (1,'Dikhukhung')
Insert into tblVillage Values (1,'Dinatshana')
GO
Insert into tblVillage Values (2,'Bobonong')
Insert into tblVillage Values (2,'Damochojena')
Insert into tblVillage Values (2,'Kobojango')
GO
Insert into tblVillage Values (5,'Shakwe')
Insert into tblVillage Values (5,'Shoshong')
Insert into tblVillage Values (5,'Taupye')
Insert into tblVillage Values (5,'Tewane')
GO
Insert into tblWards Values (1,3,'Shakwe')
Insert into tblWards Values (1,3,'Shoshong')
Insert into tblWards Values (2,6,'Taupye')
Insert into tblWards Values (2,8,'Tewane')
Insert into tblWards Values (5,9,'Shakwe')
GO
I would like to have an output that looks like this and not sure if CTE's are the only option:
| ID| ParéntID | Item | DistrictID | VillageID | WardID |
| 1 | 0 | District 1 |1 |0 |0 |
| 2 | 1 | Village 1 |1 |1 |0 |
| 3 | 1 | Village 2 |1 |2 |0 |
| 4 | 2 | Ward 1 |1 |1 |1 |
| 5 | 0 | District 2 |2 |0 |0 |
| 6 | 5 | Village 3 |2 |3 |0 |
| 7 | 6 | Ward 2 |2 |3 |2 |
I hope that I have provided sufficient information to my problem.
KK

Join 2 Table with same column and display the same column to one single column

I have 3 tables:
DATA_IZIN_BODY
DATA_IZIN_DETAIL
DATA_IZIN_DETAILPC
Here is the sample data :
DATA_IZIN_BODY//UPDATE
|ID_B|NIK |PERMIT TYPE|REASON |NAME |SUBMISSION DATE |STATUS |
|----|-----|-----------|-------------|------|-----------------|--------|
|N1 |1070 |ABSENT |SICK |John |9/5/2019 |PENDING |
|N2 |1088 |LEAVE |LATE |Laura |8/6/2019 |APPROVED|
|N3 |1009 |ABSENT |CANNT ATTND |Emmet |8/8/2019 |APPROVED|
DATA_IZIN_DETAIL*//UPDATE***
|ID |ID_B |NIK |DETAIL DATE |DETAIL HOUR|STATUS |FLAG|
|----|-----|-----|-------------|-----------|--------|----|
|001 |N1 |1070 |10/5/2019 |08.00 |NULL |1 |
|002 |N1 |1070 |11/5/2019 |07.00 |NULL |1 |
|003 |N1 |1070 |12/6/2019 |08.00 |NULL |1 |
|004 |N3 |1009 |9/8/2019 |09.00 |NULL |1 |
|005 |N3 |1088 |10/6/2019 |10.00 |NULL |1 |
|006 |N3 |1009 |11/8/2019 |11.00 |NULL |1 |
DATA_IZIN_DETAILPC*//UPDATE***
|ID |ID_B|NIK |DETAIL DATE |STATUS |FLAG |
|----|----|-----|-------------|--------|------|
|001 |N1 |1070 |13/5/2019 |NULL |2 |
|002 |N1 |1070 |14/6/2019 |NULL |2 |
|003 |N3 |1009 |12/8/2019 |NULL |2 |
THE GOAL
|ID |ID_B|NIK |NAME |PERMIT TYPE|REASON |DETAIL DATE |STATUS |FLAG|
|----|----|-----|------|-----------|-----------|-------------|--------|----|
|001 |N1 |1070 |John |ABSENT |SICK |13/5/2019 |NULL |2 |
|002 |N1 |1070 |John |ABSENT |SICK |14/6/2019 |NULL |2 |
|003 |N3 |1009 |Emmet |ABSENT |CANNT ATTND|12/8/2019 |NULL |2 |
|001 |N1 |1070 |John |ABSENT |SICK |10/5/2019 |NULL |1 |
|002 |N1 |1070 |John |ABSENT |SICK |11/5/2019 |NULL |1 |
|003 |N1 |1070 |John |ABSENT |SICK |12/6/2019 |NULL |1 |
|004 |N3 |1009 |Emmet |ABSENT |CANNT ATTND|9/8/2019 |NULL |1 |
|005 |N3 |1088 |Emmet |ABSENT |CANNT ATTND|10/6/2019 |NULL |1 |
|006 |N3 |1009 |Emmet |ABSENT |CANNT ATTND|11/8/2019 |NULL |1 |
And here is my query, tried with case but won't work as expected:
select
b.izin_id, b.IZIN_NIK
/*case
when pc.Flag = 2 then 'PC'
when d.Flag = 1 then 'DT'end
as Flag*/
from DATA_IZIN_BODY b
inner join DATA_IZIN_DETAIL d on d.IZIN_ID = b.IZIN_ID
inner join DATA_IZIN_DETAILPC pc on pc.IZIN_ID = b.IZIN_ID
but how do we combine:
DATA_IZIN_DETAIL.izin_id & DATA_IZIN_DETAILPC.izin_id into 1 column as ID ?
DATA_IZIN_DETAIL.Flag & DATA_IZIN_DETAILPC.Flag into 1 column as Flag?
update
ON GOAL : COLUMN ID & FLAG CONTAIN VALUE OF DATA_IZIN_DETAILPC.ID & DATA_IZIN_DETAIL.ID, DATA_IZIN_DETAILPC.FLAG & DATA_IZIN_DETAIL.FLAG
update 2
Data_izin_body
1. ID_B : nvarchar
2. NIK : nvarchar
3. Permit_Type : nvarchar
4. Reason : nvarchar
5. Name : nvarchar
6. Submission_Date : smalldatetime
7. Status : nvarchar
Data_izin_detail
1. ID : nvarchar
2. ID_B : nvarchar
2. NIK : nvarchar
3. Detail_Date : smalldatetime
4. Detail_Hour : nvarchar
5. Status : nvarchar
6. Flag : int
Data_izin_detailpc
1. ID : nvarchar
2. ID_B : nvarchar
2. NIK : nvarchar
3. Detail_Date : smalldatetime
4. Status : nvarchar
5. Flag : int
So the UNION ALL combines the 2 secondary tables. Then the main table joins to that.
SELECT Details.ID,
b.ID_B,
b.IZIN_NIK AS NIK,
b.Name,
b.[Permit Type],
b.Reason,
Details.[Detail Date],
Details.Status,
Details.Source,
Details.Flag
FROM DATA_IZIN_BODY b
JOIN
(SELECT ID, ID_B, [DETAIL DATE], STATUS, 'DT' AS Source, FLAG
FROM DATA_IZIN_DETAIL
UNION ALL
SELECT ID, ID_B, [DETAIL DATE], STATUS, 'PC' AS Source, FLAG
FROM DATA_IZIN_DETAILPC) AS Details
ON b.ID_B = Details.ID_B
ORDER BY b.NAME,
Details.[DETAIL DATE]
Not sure you still need the FLAG column if you have the Source column...
It seems that tables DATA_IZIN_DETAIL and DATA_IZIN_DETAILPC have the same field structure. If that is intended, you can perform a UNION on two SELECT queries:
SELECT [ID], [ID_B], [NIK], [NAME], [DETAIL DATE], [STATUS], [FLAG] FROM [DATA_IZIN_DETAILPC]
UNION ALL
SELECT [ID], [ID_B], [NIK], [NAME], [DETAIL DATE], [STATUS], [FLAG] FROM [DATA_IZIN_DETAIL]
Edit:
Based on the recent information in your updated question, I would suggest the following query to get your desired results:
SELECT
D.[ID],
D.[ID_B],
D.[NIK],
B.[Name],
B.[Permit_Type],
B.[Reason],
D.[Detail_Date],
D.[Status],
D.[Flag]
FROM
[DATA_IZIN_DETAILPC] AS D
INNER JOIN [DATA_IZIN_BODY] AS B ON B.[ID_B] = D.[ID_B] --AND B.[NIK] = D.[NIK]
UNION ALL
SELECT
D.[ID],
D.[ID_B],
D.[NIK],
B.[Name],
B.[Permit_Type],
B.[Reason],
D.[Detail_Date],
D.[Status],
D.[Flag]
FROM
[DATA_IZIN_DETAIL] AS D
INNER JOIN [DATA_IZIN_BODY] AS B ON B.[ID_B] = D.[ID_B] --AND B.[NIK] = D.[NIK]

How to update column with another column in the same table?

I have data like this :
+--+----------+--------+------+
|Id|class_name|class_id|medals|
+--+----------+--------+------+
|1 |7IPA1 |7 |3 |
|2 |7IPA2 |7 |2 |
|3 |7IPA3 |7 |5 |
|4 |8IPA1 |8 |1 |
|5 |8IPA2 |8 |7 |
|6 |8IPA3 |8 |3 |
+--+----------+--------+------+
I want data on class_id be 7IPA & 8IPA (4 first character from class_name).
You have to use substring function:
UPDATE MYTABLE SET CLASS_ID=SUBSTRING(CLASS_NAME,1,4)
Another way is using LEFT string function
Select LEFT(CLASS_NAME,4) from yourtable
Looks like you want a new column and not to update existing column, I will suggest you to create a computed column
alter table yourtable add new_class_id as (left(class_name,4)) persisted

Update table with While loop reading id first

I just wanted to ask something, I want to update a table, but this table 300k rows, it is taking about 1.8 - 2.3 seconds per row this is the table I need to update, if name match with a column i need to set the value of column All,
ID |cexp_cod |All |name |New |Normal |Vip
1 |13 |9 |Vip |NULL |NULL |NULL
2 |16 |9 |Vip |NULL |NULL |NULL
3 |16 |6 |Normal |NULL |NULL |NULL
4 |18 |9 |Vip |NULL |NULL |NULL
5 |23 |9 |New |NULL |NULL |NULL
6 |25 |9 |New |NULL |NULL |NULL
7 |26 |9 |Vip |NULL |NULL |NULL
8 |27 |9 |New |NULL |NULL |NULL
9 |40 |9 |Vip |NULL |NULL |NULL
10 |43 |9 |Vip |NULL |NULL |NULL
11 |43 |6 |Normal |NULL |NULL |NULL
and this is my query:
DECLARE #loop smallint = 0
,#v_xp_cod varchar(max) = 0
,#v_name varchar(MAX) = NULL
,#v_pond varchar(max) = 0
,#v_id bigint = 0
WHILE #loop = 0
BEGIN
SELECT TOP 1 #v_xp_cod = query01.xp_cod
,#v_name = query01.name
,#v_pond = query01.All
,#v_id = query01.ID
FROM (
SELECT xp_cod
, name
, All
, ID
FROM temp WITH(NOLOCK)
) AS query01
WHERE query01.ID > #v_id
ORDER BY query01.ID ASC
IF ##rowcount=0
BEGIN
SET #loop=1
END
ELSE -- update
BEGIN
DECLARE #i_update nvarchar(MAX)=null
SELECT #i_update = 'UPDATE temp
SET [' +#v_name+ '] = '+#v_pond+'
WHERE xp_cod = ' + #v_xp_cod
EXECUTE sp_executesql #i_update
END
END
Perhaps try rewriting the query using a different approach or format:
UPDATE MyTable
SET MyColumnName =
( SELECT table2.Name
FROM table2
WHERE table2.my_id = MyTable.id)
WHERE EXISTS
( SELECT table2.Name
FROM table2
WHERE table2.my_id = MyTable.id);

How can i get all childs row with entity commands?

i used this SQL Command to get all child and childs of child users and i dont know how can i write it with entities?
WITH ParentUser AS (
SELECT *, 1 AS nthLevel
FROM [OnlinePage].[dbo].[Users] usr1
WHERE ID = #UserID
UNION ALL
SELECT usr2.*,
nthLevel + 1 AS nthLevel
FROM ParentUser pu INNER JOIN
[OnlinePage].[dbo].[Users] usr2 ON pu.ID = usr2.UserParentID
)
SELECT * FROM ParentUser WHERE 1 < nthLevel
and my users table is:
CREATE TABLE [dbo].[Users](
[ID] [int] IDENTITY(1,1) NOT NULL,
[Username] [nvarchar](50) NOT NULL,
[UserParentID] [int] NOT NULL
)
it`s something like this:
|--------------------------------------|
|ID |Username |UserParentID |
|-----|-----------|--------------------|
|1 |admin |0 |
|2 |reseler1 |1 |
|3 |user1 |1 |
|4 |reseler2 |2 |
|5 |user2 |4 |
|6 |user3 |5 |
|--------------------------------------|
and if i want to get all childs of reseler1 with upper sql command i change #UserID with 2 and my result is:
|--------------------------------------|
|ID |Username |UserParentID |
|-----|-----------|--------------------|
|2 |reseler1 |1 |
|3 |user1 |1 |
|4 |reseler2 |2 |
|5 |user2 |4 |
|6 |user3 |5 |
|--------------------------------------|
now i need entity model of this sql command,
i search it on google and i found something in stackoverflow(Querying child entities) like down code, but not work correctly and only get one level of childs:
var query = from m in dbSet.Where(x => x.ID == 1)
join s in dbSet
on m.ID equals s.UserParentID into masterSlaves
from ms in masterSlaves.DefaultIfEmpty()
select ms;
Entity framework and Linq-to-entities does not support hierarchical queries. You must use your SQL query and call it with dbSet.SqlQuery or dbContext.Database.SqlQuery. With EF 5 / .NET 4.5 and database first approach you would also be able to create SQL function wrapping your query and map it (so you would be able to use that function from Linq-to-entities query) but that is not available in earlier versions and in code first.

Resources