How can I JOIN two tables columns using a wildcard? - sql

I'm using Access 97.
This is what I wanna do:
SELECT *
FROM CodeDIS, ListOfProducts
WHERE CodeDIS.IDNumber LIKE ListOfProducts.IDNumber*;
Sometimes the IDNumber on ListOfProducts has an extra letter indicating some information about the product.
So I have the IDNumber AC244 on CodeDIS but I have AC244P on ListOfProducts.
All I want is to add a wildcard to the end of the condition.
Is this possible?

If the choice is C or P, why not:
SELECT *
FROM CodeDIS, ListOfProducts
WHERE CodeDIS.IDNumber = ListOfProducts.IDNumber & "C"
OR CodeDIS.IDNumber = ListOfProducts.IDNumber & "P"

How about:
SELECT *
FROM CodeDIS, ListOfProducts
WHERE CodeDIS.IDNumber LIKE left(ListOfProducts.IDNumber,5);
or if you like:
SELECT *
FROM CodeDIS a inner join ListOfProducts b on a.IDNumber = left(b.IDNumber,5);

I know this is old, but I have not posted much if ever and thought I would try here.
Why not use a substring function with PATINDEX testing if the last digit is always a character? I'm sure my code is mega-inefficient, but here goes my beginner attempt. I'll bet some could golf my case statement into something smaller.
-- I used the code below
-- It only works if the final digit is lower or uppercase letter of
-- specified language.
DECLARE #CodeDIS TABLE (IdNumber nvarchar(50), BaseRowNum nvarchar(50));
insert into #CodeDIS (IdNumber,BaseRowNum) values ('52352345','1')
insert into #CodeDIS (IdNumber,BaseRowNum) values ('134131','2')
insert into #CodeDIS (IdNumber,BaseRowNum) values ('1343141','3')
insert into #CodeDIS (IdNumber,BaseRowNum) values ('a143321','4')
insert into #CodeDIS (IdNumber,BaseRowNum) values ('c34324','5')
insert into #CodeDIS (IdNumber,BaseRowNum) values ('1343214','6')
insert into #CodeDIS (IdNumber,BaseRowNum) values ('%134324','7')
insert into #CodeDIS (IdNumber,BaseRowNum) values ('a%134324','8')
insert into #CodeDIS (IdNumber,BaseRowNum) values ('1413','9')
;
DECLARE #ListOfProducts TABLE (IdNumber nvarchar(50), UpdateSourceDataColumn nvarchar(50));
insert into #ListOfProducts (IdNumber,UpdateSourceDataColumn) values ('52352345a','11')
insert into #ListOfProducts (IdNumber,UpdateSourceDataColumn) values ('134131','22')
insert into #ListOfProducts (IdNumber,UpdateSourceDataColumn) values ('1343141a','33')
insert into #ListOfProducts (IdNumber,UpdateSourceDataColumn) values ('a143321','44')
insert into #ListOfProducts (IdNumber,UpdateSourceDataColumn) values ('c343245','55')
insert into #ListOfProducts (IdNumber,UpdateSourceDataColumn) values ('1343214Z','66')
insert into #ListOfProducts (IdNumber,UpdateSourceDataColumn) values ('%134324','77')
insert into #ListOfProducts (IdNumber,UpdateSourceDataColumn) values ('a%134324','88')
insert into #ListOfProducts (IdNumber,UpdateSourceDataColumn) values ('1413\','99')
;
SELECT a.*,b.IdNumber as IdNumber2, b.UpdateSourceDataColumn
FROM #CodeDIS a
left join #ListOfProducts b
on a.IdNumber =
case
when PATINDEX(
'[a-zA-Z]',
substring(b.IdNumber,len(b.IdNumber),1)
COLLATE Latin1_General_CS_AS
)=1
then left(b.IdNumber,len(b.IdNumber)-1)
else b.IdNumber
end
;
--IdNumber BaseRowNum IdNumber2 UpdateSourceDataColumn
--52352345 1 52352345a 11
--134131 2 134131 22
--1343141 3 1343141a 33
--a143321 4 a143321 44
--c34324 5 NULL NULL
--1343214 6 1343214Z 66
--%134324 7 %134324 77
--a%134324 8 a%134324 88
--1413 9 NULL NULL

Related

GROUP BY A, B, C, and else for counting data?

I want to try group by with data with certain types and etc.
CREATE TABLE dbo.T_TEST(
TYPE nchar(1) NOT NULL,
Qty int NOT NULL
)
INSERT INTO [T_TEST] VALUES ('A','2');
INSERT INTO [T_TEST] VALUES ('A','1');
INSERT INTO [T_TEST] VALUES ('A','2');
INSERT INTO [T_TEST] VALUES ('B','22');
INSERT INTO [T_TEST] VALUES ('B','21');
INSERT INTO [T_TEST] VALUES ('C','2');
INSERT INTO [T_TEST] VALUES ('C','2');
INSERT INTO [T_TEST] VALUES ('C','2');
INSERT INTO [T_TEST] VALUES ('C','2');
INSERT INTO [T_TEST] VALUES ('D','2');
INSERT INTO [T_TEST] VALUES ('D','2');
INSERT INTO [T_TEST] VALUES ('D','2');
INSERT INTO [T_TEST] VALUES ('E','1');
INSERT INTO [T_TEST] VALUES ('E','1');
INSERT INTO [T_TEST] VALUES ('E','4');
INSERT INTO [T_TEST] VALUES ('F','5');
The result I am wondering is like this.
TYPE Count
A 3
B 2
C 4
etc 7
I could do with get total count and subtract count A,B,C.
But I believe it's not a good logic, Please help me with this.
You may try the following and see working fiddle:
SELECT
CASE WHEN TYPE IN ('A','B','C') THEN TYPE ELSE 'etc' END as [Type],
COUNT(Type) as [Count]
FROM
T_TEST
GROUP BY
CASE WHEN TYPE IN ('A','B','C') THEN TYPE ELSE 'etc' END
GO
Type | Count
:--- | ----:
A | 3
B | 2
C | 4
etc | 7
db<>fiddle here
You need a basic GROUP BY query:
SELECT TYPE, COUNT(*) AS Count
FROM dbo.T_TEST
GROUP BY TYPE
ORDER BY TYPE;

Finding trouble in generating the list of all Students who belongs to the major named as “CS” but not to major named as “EE”

S_id int Primary Key,
S_name varchar(100),
Gpa float ,
Size_hs int
)
Create table Apply (
s_id int ,
C_name varchar(100),
Major varchar(10),
Decision varchar(2)
)
insert into Students values (123,'Amy',3.9,1000)
insert into Students values (234,'Bob',3.6,1500)
insert into Students values (345,'Craig',3.5,500)
insert into Students values (456,'Doug',3.9,1000)
insert into Students values (567,'Edward',2.9,2000)
insert into Students values (678,'Fay',3.8,200)
insert into Students values (789,'Gray',3.4,800)
insert into Students values (987,'Helen',3.7,800)
insert into Students values (876,'Irene',3.9,400)
insert into Students values (765,'Jay',2.9,1500)
insert into Students values (654,'Amy',3.9,1000)
insert into Students values (543,'Craig',3.4,2000)
insert into Apply values (123,'NJIT','CS','Y')
insert into Apply values (123,'NJIT','EE','N')
insert into Apply values (123,'Stoony Brook','CS','Y')
insert into Apply values (123,'Cornell','EE','Y')
insert into Apply values (234,'Stoony Brook','Bio','N')
insert into Apply values (345,'WPI','Bio-Eng','Y')
insert into Apply values (345,'Cornell','Bio-Eng','N')
insert into Apply values (345,'Cornell','CS','Y')
insert into Apply values (345,'Cornell','EE','N')
insert into Apply values (678,'NJIT','Hist','Y')
insert into Apply values (987,'NJIT','CS','Y')
insert into Apply values (987,'Stoony Brook','CS','Y')
insert into Apply values (876,'NJIT','Bio','N')
insert into Apply values (876,'WPI','Marine-Bio','Y')
insert into Apply values (876,'WPI','Hist','N')
insert into Apply values (765,'NJIT','Hist','Y')
insert into Apply values (765,'Cornell','Hist','N')
insert into Apply values (765,'Cornell','Psych','Y')
insert into Apply values (543,'WPI','CS','N')
Create table Students(
S_id int Primary Key,
S_name varchar(100),
Gpa float ,
Size_hs int
)
Create table Apply (
s_id int ,
C_name varchar(100),
Major varchar(10),
Decision varchar(2)
)
insert into Students values (123,'Amy',3.9,1000)
insert into Students values (234,'Bob',3.6,1500)
insert into Students values (345,'Craig',3.5,500)
insert into Students values (456,'Doug',3.9,1000)
insert into Students values (567,'Edward',2.9,2000)
insert into Students values (678,'Fay',3.8,200)
insert into Students values (789,'Gray',3.4,800)
insert into Students values (987,'Helen',3.7,800)
insert into Students values (876,'Irene',3.9,400)
insert into Students values (765,'Jay',2.9,1500)
insert into Students values (654,'Amy',3.9,1000)
insert into Students values (543,'Craig',3.4,2000)
insert into Apply values (123,'NJIT','CS','Y')
insert into Apply values (123,'NJIT','EE','N')
insert into Apply values (123,'Stoony Brook','CS','Y')
insert into Apply values (123,'Cornell','EE','Y')
insert into Apply values (234,'Stoony Brook','Bio','N')
insert into Apply values (345,'WPI','Bio-Eng','Y')
insert into Apply values (345,'Cornell','Bio-Eng','N')
insert into Apply values (345,'Cornell','CS','Y')
insert into Apply values (345,'Cornell','EE','N')
insert into Apply values (678,'NJIT','Hist','Y')
insert into Apply values (987,'NJIT','CS','Y')
insert into Apply values (987,'Stoony Brook','CS','Y')
insert into Apply values (876,'NJIT','Bio','N')
insert into Apply values (876,'WPI','Marine-Bio','Y')
insert into Apply values (876,'WPI','Hist','N')
insert into Apply values (765,'NJIT','Hist','Y')
insert into Apply values (765,'Cornell','Hist','N')
insert into Apply values (765,'Cornell','Psych','Y')
insert into Apply values (543,'WPI','CS','N')
Basically I have to find the list of students id who belongs to major named as 'Cs' but not to the major 'EE'. I tried it by myself but it is not working properly.
Here is the code below:
select * from students
where s_id in (
select s_id
from apply
where major='CS' and Major!='EE'
group by s_id
)```
I would use exists and not exists:
select s.*
from students s
where
exists (select 1 from applies a where a.s_id = s._id and a.major = 'CS')
and not exists (select 1 from applies a where a.s_id = s._id and a.major = 'EE')
With an index on applies(s_id, major), this should be an efficient option.
Another approach is to join, aggregate, and filter with a having clause. This requires listing the columns that you want to show in both the select and group by clauses:
select s.s_id, s.s_name
from students s
inner join applies a on a.s_id = s.s_id
where a.major in ('CS', 'EE')
group by s.s_id, s.s_name
having
max(case when a.major = 'CS' then 1 else 0 end) = 1
and max(case when a.major = 'EE' then 1 else 0 end) = 0
Note: apply is a reserved word in a number a databases (SQL Serer, Oracle, ...), hence not a good choice for a table name. I renamed it to applies in the queries.

Make all word with special character list in each position SQL server

I have a table similar this
CREATE TABLE [dbo].[Test](
[harfno] INT,
[harf] NCHAR(1) NULL
)
and below values row count and harfno is variable
INSERT INTO [dbo].[Test]([harfno],[harf]) VALUES (1,'a')
INSERT INTO [dbo].[Test]([harfno],[harf]) VALUES (2,'b')
INSERT INTO [dbo].[Test]([harfno],[harf]) VALUES (3,'c')
INSERT INTO [dbo].[Test]([harfno],[harf]) VALUES (3,'d')
INSERT INTO [dbo].[Test]([harfno],[harf]) VALUES (4,'e')
INSERT INTO [dbo].[Test]([harfno],[harf]) VALUES (5,'f')
INSERT INTO [dbo].[Test]([harfno],[harf]) VALUES (5,'g')
INSERT INTO [dbo].[Test]([harfno],[harf]) VALUES (5,'h')
I need output like this: all word with special character list in each position ,position is harfno in each harfno maybe I have more than on char
word
abcef
abceg
abceh
abdef
abdeg
abdeh
Naive approach:
SELECT t1.harf + t2.harf+ t3.harf+ t4.harf+ t5.harf AS word
FROM Test t1, Test t2, Test t3, Test t4, Test t5
WHERE t1.harfno=1
AND t2.harfno=2
AND t3.harfno=3
AND t4.harfno=4
AND t5.harfno=5;
DBFidlde Demo
EDIT:
Recursive CTE:
WITH cte(harf, harfno) AS (
SELECT CAST(harf AS NVARCHAR(MAX)), 1 AS harfno
FROM Test
WHERE harfno=1
UNION ALL
SELECT c.harf + t.harf,t.harfno
FROM Test t
JOIN cte c
ON t.harfno = c.harfno+1
)
SELECT *
FROM cte
WHERE LEN(harf)= (SELECT MAX(harfno) FROM cte)
ORDER BY harf
--OPTION(MAXRECURSION nn)
DBFiddle Demo

Updating the parent table considering the values of the child table in oracle

I have table Parent_tbl which consists of 3 columns H_N, Col58 and Type this both the first two columns will be having the same values, only the column type differs.
I have a child table where col58 defines the relationship with the parent but rest of the columns in child_tbl is specific to that table only H_N is the unique column in both of the tables.
I need to update TYPE as EXCHANGE in PARENT_TBL when ever i find the the CHILD_TBL I_STATUS having all the values like S,R and V else the parent_tbl type remains untouched, how can we do this ?
The Parent_tbl.col58 = 1140 that type should be 'EXCHANGE' because child_tbl.col58 = 1140 is having every letter i.e S,R,V.
Here is the DDL for the samples.
CREATE TABLE PARENT_TBL (
H_N number,
col58 number,
TYPE varchar(100)
);
Insert into PARENT_TBL (H_N,COL58,TYPE) values (2,2,'SALE');
Insert into PARENT_TBL (H_N,COL58,TYPE) values (16,16,'SALE');
Insert into PARENT_TBL (H_N,COL58,TYPE) values (20,20,'SALE');
Insert into PARENT_TBL (H_N,COL58,TYPE) values (34,34,'VOID');
Insert into PARENT_TBL (H_N,COL58,TYPE) values (38,38,'SALE');
Insert into PARENT_TBL (H_N,COL58,TYPE) values (102,102,'SALE');
Insert into PARENT_TBL (H_N,COL58,TYPE) values (111,111,'SALE');
Insert into PARENT_TBL (H_N,COL58,TYPE) values (117,117,'SALE');
Insert into PARENT_TBL (H_N,COL58,TYPE) values (1140,1140,'RETURN');
Insert into PARENT_TBL (H_N,COL58,TYPE) values (131,131,'SALE');
commit;
CREATE TABLE CHILD_TBL
(
I_STATUS varchar(100),
H_n number,
col58 number
);
Insert into CHILD_TBL (I_STATUS,H_N,COL58) values ('S',3,2);
Insert into CHILD_TBL (I_STATUS,H_N,COL58) values ('S',5,2);
Insert into CHILD_TBL (I_STATUS,H_N,COL58) values ('S',7,2);
Insert into CHILD_TBL (I_STATUS,H_N,COL58) values ('S',8,2);
Insert into CHILD_TBL (I_STATUS,H_N,COL58) values ('S',10,2);
Insert into CHILD_TBL (I_STATUS,H_N,COL58) values ('S',1141,1140);
Insert into CHILD_TBL (I_STATUS,H_N,COL58) values ('V',1142,1140);
Insert into CHILD_TBL (I_STATUS,H_N,COL58) values ('R',1143,1140);
Insert into CHILD_TBL (I_STATUS,H_N,COL58) values ('R',1144,1140);
Insert into CHILD_TBL (I_STATUS,H_N,COL58) values ('S',1145,1140);
commit;
EXPECTED OUTPUT:
truncate table PARENT_TBL ;
Insert into PARENT_TBL (H_N,COL58,TYPE) values (2,2,'SALE');
Insert into PARENT_TBL (H_N,COL58,TYPE) values (16,16,'SALE');
Insert into PARENT_TBL (H_N,COL58,TYPE) values (20,20,'SALE');
Insert into PARENT_TBL (H_N,COL58,TYPE) values (34,34,'VOID');
Insert into PARENT_TBL (H_N,COL58,TYPE) values (38,38,'SALE');
Insert into PARENT_TBL (H_N,COL58,TYPE) values (102,102,'SALE');
Insert into PARENT_TBL (H_N,COL58,TYPE) values (111,111,'SALE');
Insert into PARENT_TBL (H_N,COL58,TYPE) values (117,117,'SALE');
Insert into PARENT_TBL (H_N,COL58,TYPE) values (1140,1140,**'EXCHANGE'**);
Insert into PARENT_TBL (H_N,COL58,TYPE) values (131,131,'SALE');
Use this
update PARENT_TBL p
set TYPE='EXCHANGE'
where exists
( select 1
from child_tbl c
where
i_status in ('S','R','V')
and c.col58=p.col58
group by col58
having count(distinct(i_status))=3
)
Explanation:
select col58
from child_tbl c
where
i_status in ('S','R','V')
group by col58
having count(distinct(i_status))=3
This will give you the col58 where count(distinct(i_status))=3 after the filter i_status in ('S','R','V'). So it will be 3 only if there are at least 1 each status of 'S','R','V'. Now use this in exists clause and add
a where condition in the above query and c.col58=p.col58 to join it with the parent table while updating.
Please try this first of your test data and try this without committing the original data. Commit only when you are sure that you got expected result.
Find rows in child table (CHILD_TBL) with proper grouping and use merge:
merge into parent_tbl p
using (select col58
from child_tbl
group by col58
having count(decode(i_status, 'S', 1)) > 0
and count(decode(i_status, 'R', 1)) > 0
and count(decode(i_status, 'V', 1)) > 0) c
on (p.col58 = c.col58)
when matched then update set type = 'EXCHANGE'

Insert error in sql cursor

I have this SQL Server query with a cursor:
DECLARE #ids TABLE(id varchar(50))
INSERT INTO #ids VALUES ('1098264', '1098859', '1098860', '1098267', '1098265')
But when I run the code, I get an error:
Insert Error: Column name or number of supplied values does not match table definition.
You have only one column in table i.e id and you are trying to insert 5 column values in table.
Try this
Create table :
DECLARE #ids TABLE(id varchar(50))
Insert values to table :
INSERT INTO #ids VALUES ('1098264')
INSERT INTO #ids VALUES ('1098859')
INSERT INTO #ids VALUES ('1098860')
INSERT INTO #ids VALUES ('1098267')
INSERT INTO #ids VALUES ('1098265')
You need to use a pair of bracket for each row like below:
INSERT INTO #ids VALUES ('1098264'),('1098859'),('1098860'),('1098267'),('1098265')
There are three ways of inserting multiple rows into a table (other than via a select statement)
INSERT INTO FOO (columna,columnb)
VALUES (1,a)
INSERT INTO FOO (columna,columnb)
VALUES (2,b)
INSERT INTO FOO (columna,columnb)
VALUES (3,c)
The second
INSERT INTO FOO (columna,columnb)
select 1,'a'
UNION ALL
select 2,'b'
UNION ALL
select 3,'c'
Thirdly (only works SQLServer 2008 and up)
INSERT INTO FOO (columna,columnb)
VALUES (1,'a'),(2,'b'),(3,'c')
In the example in your question, you were trying to insert multiple fields into one column, which is why you got the error.
You have only 1 column in the table but are trying to insert 5 values.
First declare 5 different columns in the table to hold 5 values
For Example:-
declare #IDS_1 varchar(50)
,#IDS_2 varchar(50)
,#IDS_3 varchar(50)
,#IDS_4 varchar(50)
,#IDS_5 varchar(50)
INSERT INTO #IDS VALUES ('1098264'
,'1098859'
,'1098860'
,'1098267'
,'1098265')