Does number span exists in database - sql-server-2005

I hope you can understand my question, if not, please let me know...
In my form I have eight textboxes, like so (user wrote '25' to first textbox and '35' to second one):
Code1From: _25____ Code1To:_35____
Code2From: _______ Code2To:_______
Code3From: _______ Code3To:_______
Code4From: _______ Code4To:_______
My table looks like this:
create table MyTable (
Id int identity(1,1),
Code1From bigint, Code1To bigint,
Code2From bigint, Code2To bigint,
Code3From bigint, Code3To bigint,
Code4From bigint, Code4To bigint
)
Now I'd like to prevent inserting data, that is allready inserted. For example:
Data in MyTable:
Id, Code1From, Code1To, Code2From, Code2To, Code3From, Code3To, Code4From, Code4To
1, 1, 10, null, null, null, null, null, null
2, 11, 20, null, null, null, null, null, null
3, 21, 30, null, null, null, null, null, null
4, 31, 40, null, null, null, null, null, null
5, 41, 50, null, null, null, null, null, null
If user wants to insert for Code1 (or Code2, Code3 or Code4) from 25 to 35, I should raise an error
(because span from 25 to 35 is allready in the database - id 3 and 4).
However, user can insert span from 51 to 55 for example.
How can I determine, if a span is allready in my database? Now I could do something like this:
select *
from MyTable
where
#code1From between Code1From and Code1To
or #code1From between Code2From and Code2To
or #code1From between Code3From and Code3To
or #code1From between Code4From and Code4To
--
or #code1To between Code1From and Code1To
or #code1To between Code2From and Code2To
or #code1To between Code3From and Code3To
or #code1To between Code4From and Code4To
--
... and another 24 or statements
Is there any easier way to accomplish this?

If i understand you correctly, this might help you
DECLARE #Table TABLE(
FromVal1 FLOAT,
ToVal1 FLOAT,
FromVal2 FLOAT,
ToVal2 FLOAT,
FromVal3 FLOAT,
ToVal3 FLOAT,
FromVal4 FLOAT,
ToVal4 FLOAT
)
INSERT INTO #Table (FromVal1,ToVal1,FromVal2,ToVal2) SELECT 1, 10, 51, 60
INSERT INTO #Table (FromVal2,ToVal2) SELECT 11, 20
INSERT INTO #Table (FromVal3,ToVal3) SELECT 21, 30
INSERT INTO #Table (FromVal4,ToVal4) SELECT 31, 40
INSERT INTO #Table (FromVal1,ToVal1) SELECT 41, 50
DECLARE #FromVal FLOAT,
#ToVal FLOAT
SELECT #FromVal = 25,
#ToVal = 35
SELECT *
FROM #Table
WHERE NOT(FromVal1 > #ToVal OR ToVal1 < #FromVal)
OR NOT(FromVal2 > #ToVal OR ToVal2 < #FromVal)
OR NOT(FromVal3 > #ToVal OR ToVal3 < #FromVal)
OR NOT(FromVal4 > #ToVal OR ToVal4 < #FromVal)

Related

Change "money" symbol on Beekeeper SQL

I'm student of SQL using Beekeeper Studio/Postgres, and I need to create a column using money.
But when I do SELECT * FROM produto, shows me "R$", my local monetary symbol.
Can I change the R$ to ʛ not only in my Beekeeper, but to everyone?
CREATE TABLE produto(
id SERIAL PRIMARY KEY,
codigo_produto int NOT NULL,
nome_produto VARCHAR(100) NOT NULL,
descricao_produto VARCHAR(100) NOT NULL,
estoque_produto int NOT NULL,
fabricacao_produto date NOT NULL,
valor_produto money NOT NULL
);
INSERT INTO produto (codigo_produto, nome_produto, descricao_produto, estoque_produto, fabricacao_produto, valor_produto) values
(1, 'firebolt', 'vassoura randolph spudmore', 1, '1993-02-08', '2000'),
(2, 'cleansweep eleven', 'vassoura companhia de vassouras cleansweep', 15, '1995-08-01', '500');
SELECT * FROM produto

Insert only unique records in SQL

I ran the below query in SQL to insert records (this is just a snippet)
insert into list_member (list_id, list_int_value , list_float_value , list_decimal_value , list_varchar_value, list_datetime_value, modified_by, asof_time, do_not_audit)
select 42, null, null, security_id, null, null, 10, null, null
from security
where not exists(select user_id_4 from list_member.user_id_4 where list_member.user_id_4 = security.user_id_4)
and deleted = 0 and user_id_4 in
(
'ES0125220311',
'ES0132105018',
'ES0167050915'
)
Now I have another list to insert, but only want to insert new records.
I'm unsure where to insert the additional 'where' clause so that it doesn't insert duplicates. I've come up with the below (which in theory should only add the final record), but the additional where clause in bold is likely wrong ...
insert into list_member (list_id, list_int_value , list_float_value , list_decimal_value , list_varchar_value, list_datetime_value, modified_by, asof_time, do_not_audit)
select 42, null, null, security_id, null, null, 10, null, null
from security
**where not exists(select user_id_4 from list_member.user_id_4 where list_member.user_id_4 = security.user_id_4)**
and deleted = 0 and user_id_4 in
(
'ES0125220311',
'ES0132105018',
'ES0167050915',
'ES0123456789'
)
Anyone able to assist?
I changed it to this instead
left join list_member
on list_member.list_id = 42 and list_member.list_decimal_value = security.security_id
Sorry for the trouble.
You can use corelated query as follows:
insert into list_member (list_id, list_int_value , list_float_value , list_decimal_value , list_varchar_value, list_datetime_value, modified_by, asof_time, do_not_audit)
select 42, null, null, security_id, null, null, 10, null, null
from security s
where not exists
(select 1 from list_member l where l.user_id_4 = s.user_id_4)
and deleted = 0 and user_id_4 in
(
'ES0125220311',
'ES0132105018',
'ES0167050915',
'ES0123456789'
)

Get dynamic variable for SQL insert

I have a query like this:
INSERT INTO my_table
VALUES (SPECIAL_ID, 62, 0, 1, -1, NULL, NULL, -1)
WHERE sp_id IN (SELECT id = SPECIAL_ID
FROM foo
WHERE lock IS NULL)
SPECIAL_ID is not yet defined, but it should be equal to the id that comes from the inner SELECT statement from foo.
Just dont use values, use a select
INSERT INTO my_table (need To declare list of column names for table insert)
Select SPECIAL_ID, 62, 0, 1, -1, NULL, NULL, -1
From Table
WHERE sp_id IN(
SELECT id = SPECIAL_ID
FROM foo
WHERE lock IS NULL
)
If there is no table for the sp_id to be in, you could get rid of that where caluse and move the sub select up like this:
INSERT INTO my_table (need To declare list of column names for table insert)
Select SPECIAL_ID, 62, 0, 1, -1, NULL, NULL, -1
FROM foo
WHERE lock IS NULL
SELECT SPECIAL_ID, 62, 0, 1, -1, NULL, NULL, -1
INTO my_table
FROM your_source_table
WHERE sp_id in (SELECT id = SPECIAL_ID
FROM foo
WHERE lock IS NULL)
Listing column names is not necessary unless their ordinal position is different or you are only applying this to certain columns, not all of them

How to count null columns in a row?

How to count all the columns in a table that have a null value?
The table having a large number of columns and the method should iterate over the columns in a dynamic manner.
In any given row (selected by an identifier), count the null cells.
Select count(number of null value cells) where id=1 from table
e.g:
I have a table consisting of 200 columns I want to know how many null
cells does the row with id=1 have
Basically, you need to check every column in a selected row if it's null or not.
Since you have 200+ columns in your table, manual approach seems tedious, so you can automate it a little bit and construct the query dynamically by querying user_tab_columns:
-- set up
create table t1(
rid number primary key,
c1 varchar2(17),
c2 date,
c3 timestamp,
c4 number
);
insert into t1
valueS(1, 'string', null, systimestamp, null);
commit ;
-- going to use DECODE function - doesnt require type consistency.
select 'decode('||column_name||', null, 1, 0)+' as res
from user_tab_columns
where table_name = 'T1'
Result:
RES
------------------------------
decode(RID, null, 1, 0)+
decode(C1, null, 1, 0)+
decode(C2, null, 1, 0)+
decode(C3, null, 1, 0)+
decode(C4, null, 1, 0)+
And final query:
select decode(C4, null, 1, 0)+
decode(C3, null, 1, 0)+
decode(C2, null, 1, 0)+
decode(C1, null, 1, 0)+
decode(RID, null, 1, 0) as num_of_nulls
from t1
where rid = 1
Result:
NUM_OF_NULLS
--------------
2
Try this. You can pass any ID and get the number of columns with NULL value.
CREATE TABLE TEST (ID NUMBER, B VARCHAR2(20), C NUMBER, D VARCHAR2(200));
INSERT INTO TEST VALUES (1,NULL,NULL,'XX');
SELECT COUNT(NULL_COLS)
FROM (
SELECT
to_number(extractvalue(xmltype(dbms_xmlgen.getxml('SELECT CASE WHEN '||COLUMN_NAME||' IS NULL THEN 0 ELSE NULL END COL_VAL FROM '||TABLE_NAME||' WHERE ID=&VALUE')),'/ROWSET/ROW/COL_VAL')) NULL_COLS
FROM USER_TAB_COLUMNS
WHERE TABLE_NAME='TEST');

Account Hierarchy ? Parent account has children in which child could become a parent

I have 2 tables. One table contains all parent accounts, top level of hierarchy. Second table has all children accounts, that may or may not have a match to a parent account in the parent table. The goal is to create a query (SQL Server 2008, recursive or non) that finds all child accounts that match to the parent in addition to the fact that the child could itself be a parent to other child accounts.
In simpler terms, once a match has been made on a parent to child, need to check to make sure that the child in the match is not itself a parent to other child accounts. A mouthful I understand and I hope it makes sense. I also do not know the depth of which the hierarchy would extend.
CREATE TABLE dbo.Parent_Accounts
(Parent_Account_Key_Lookup varchar(28) NOT NULL,
Account_Number bigint NOT NULL,
Reference_Account_Number_1 bigint NOT NULL,
Reference_Account_Number_2 bigint NOT NULL,
OpenDate int NOT NULL,
Status char(1) NOT NULL,
Record_Created smalldatetime NOT NULL,
Active bit NOT NULL)
GO
CREATE TABLE dbo.Child_Accounts
(Child_Account_Key_Lookup varchar(28) NOT NULL,
Account_Number bigint NOT NULL,
Reference_Account_Number_1 bigint NOT NULL,
Reference_Account_Number_2 bigint NOT NULL,
OpenDate int NOT NULL,
Status char(1) NOT NULL,
Record_Created smalldatetime NOT NULL,
Active bit NOT NULL)
GO
WITH cte_Recursive
AS (SELECT parent.Account_Number,
parent.Parent_Account_Key_Lookup,
parent.Reference_Account_Number_1,
parent.Reference_Account_Number_2,
parent.OpenDate,
parent.[Status],
parent.Record_Created,
parent.Active,
1 AS Hierarchy_Level
FROM dbo.Parent_Accounts parent
WHERE parent.Account_Number = 4498481055218674
UNION ALL
SELECT child.Account_Number,
child.Child_Account_Key_Lookup,
child.Reference_Account_Number_1,
child.Reference_Account_Number_2,
child.OpenDate,
child.[Status],
child.Record_Created,
child.Active,
cte.Hierarchy_Level + 1
FROM cte_Recursive cte
INNER JOIN dbo.Child_Accounts child
ON cte.Parent_Account_Key_Lookup = child.Child_Account_Key_Lookup)
--SELECT * FROM cte_Recursive
SELECT TOP 2 * FROM cte_Recursive
INSERT INTO dbo.Parent_Accounts
(Parent_Account_Key_Lookup,
Account_Number,
Reference_Account_Number_1,
Reference_Account_Number_2,
OpenDate,
[Status],
Record_Created,
Active)
VALUES ('222248105521867419970702', 2222481055218674, 2222481060975466, 0, 19970702, 'U', '2010-11-18 12:46:00', 0)
INSERT INTO dbo.Child_Accounts
(Child_Account_Key_Lookup,
Account_Number,
Reference_Account_Number_1,
Reference_Account_Number_2,
OpenDate,
[Status],
Record_Created,
Active)
VALUES ('222248105521867419970702', 2222481060975466, 2222481055218674, 2222481055218674, 19970702, 'L', '2010-11-19 08:33:00', 0),
('222248106097546619970702', 2222481060982900, 2222481060989137, 2222481060975466, 19970702, 'U', '2010-11-19 16:54:00', 0),
('222248106098290019970702', 2222481060989137, 0, 2222481060982900, 19970702, ' ', '2010-11-21 01:52:00', 1)
The problem appears to be an inability to specify how to determine that a child has children combined with a lack of identity for children. If the ON clause in the CTE could prevent a child joining to itself it would break the infinite recursion chain.
Sample output and answers to the comments should make it clear whether you simply need another condition in the ON clause or a UNION to handle childrens' children.
This at least stops recursing by limiting the hierarchy level and provides a testbed that anyone can execute without cluttering their database:
declare #Parent_Accounts as table (
Parent_Account_Key_Lookup varchar(28) NOT NULL,
Account_Number bigint NOT NULL,
Reference_Account_Number_1 bigint NOT NULL,
Reference_Account_Number_2 bigint NOT NULL,
OpenDate int NOT NULL,
Status char(1) NOT NULL,
Record_Created smalldatetime NOT NULL,
Active bit NOT NULL)
declare #Child_Accounts as table (
Child_Account_Key_Lookup varchar(28) NOT NULL,
Account_Number bigint NOT NULL,
Reference_Account_Number_1 bigint NOT NULL,
Reference_Account_Number_2 bigint NOT NULL,
OpenDate int NOT NULL,
Status char(1) NOT NULL,
Record_Created smalldatetime NOT NULL,
Active bit NOT NULL)
INSERT INTO #Parent_Accounts
(Parent_Account_Key_Lookup, Account_Number, Reference_Account_Number_1, Reference_Account_Number_2, OpenDate, [Status], Record_Created, Active) VALUES
('222248105521867419970702', 2222481055218674, 2222481060975466, 0, 19970702, 'U', '2010-11-18 12:46:00', 0)
INSERT INTO #Child_Accounts
(Child_Account_Key_Lookup, Account_Number, Reference_Account_Number_1, Reference_Account_Number_2, OpenDate, [Status], Record_Created, Active) VALUES
('222248105521867419970702', 2222481060975466, 2222481055218674, 2222481055218674, 19970702, 'L', '2010-11-19 08:33:00', 0),
('222248106097546619970702', 2222481060982900, 2222481060989137, 2222481060975466, 19970702, 'U', '2010-11-19 16:54:00', 0),
('222248106098290019970702', 2222481060989137, 0, 2222481060982900, 19970702, ' ', '2010-11-21 01:52:00', 1)
; WITH cte_Recursive
AS (SELECT parent.Account_Number,
parent.Parent_Account_Key_Lookup,
parent.Reference_Account_Number_1,
parent.Reference_Account_Number_2,
parent.OpenDate,
parent.[Status],
parent.Record_Created,
parent.Active,
1 AS Hierarchy_Level
FROM #Parent_Accounts parent
WHERE parent.Account_Number = 2222481055218674
UNION ALL
SELECT child.Account_Number,
child.Child_Account_Key_Lookup,
child.Reference_Account_Number_1,
child.Reference_Account_Number_2,
child.OpenDate,
child.[Status],
child.Record_Created,
child.Active,
cte.Hierarchy_Level + 1
FROM cte_Recursive cte
INNER JOIN #Child_Accounts child
ON cte.Parent_Account_Key_Lookup = child.Child_Account_Key_Lookup and cte.Hierarchy_Level < 2)
SELECT * FROM cte_Recursive