How to get the count of occurrence from comma separated string - sql

I have following two tables in Oracle 8i
For each col1 in T1 table, we need to find out the number of occurrences in T2 table (for matching col3).
QUESTION
What is the best way to achieve this in Oracle using a single query (without creating temporary table)?
Fiddle
EXPECTED RESULT
‘C1’ -- 10 -- 1
‘C1’ -- 8 -- 1
‘C2’ -- 10 -- 1
‘C3’ -- 10 -- 0
‘C4’ -- 10 -- 2
SQL
--Table 1
CREATE TABLE T1 (col1 varchar2(2), col2 varchar(8), col3 NUMBER);
INSERT INTO T1 (col1, col2, col3) VALUES ('C1', 'john',10);
INSERT INTO T1 (col1, col2, col3) VALUES ('C1', 'nishal',8);
INSERT INTO T1 (col1, col2, col3) VALUES ('C2', 'piers',10);
INSERT INTO T1 (col1, col2, col3) VALUES ('C3', 'sara',10);
INSERT INTO T1 (col1, col2, col3) VALUES ('C4', 'lijo',10);
--Table 2
CREATE TABLE T2 (col1 varchar2(2), col2 varchar(8), col3 NUMBER);
INSERT INTO T2 (col1, col2 , col3) VALUES ('R0', 'C1,C4',10);
INSERT INTO T2 (col1, col2 , col3) VALUES ('R1', 'C1',8);
INSERT INTO T2 (col1, col2 , col3) VALUES ('R2', 'C2,C4',10);
Fiddle

select T1.col1
, T1.col3
, count(T2.col1)
from T1
left join
T2
on T1.col3 = T2.col3
and ',' || T2.col2 || ',' like '%,' || T1.col1 || ',%'
group by
T1.col1
, T1.col3
Example at SQL Fiddle.

Related

finding counts assigned to another field's value

Create table t1 (col1 (number), col2 (number), col3 (number);
Insert into t1 values (1,1,1);
Insert into t1 values (1,2,5);
Insert into t1 values (1,3,1);
Insert into t1 values (2,1,1);
Insert into t1 values (2,1,1);
Desired result
col1 col2
1 3
2 2
I need to return the value in col1 and the count of values found in col 2 for each distinct col1 value. Do not need col3
select col1, count(col1) from t1
group by col1

TSQL INSERT INTO SELECT - display rows

Is it possible to display which rows I did inserted via this query:
INSERT INTO dbo.Table (Col1, Col2, Col2)
(SELECT
Col1,
Col2,
'Something modified',
FROM dbo.Table
WHERE Col10 = 66)
It is important to obtain whole new row with it's PrimaryKey value, etc., not only Col1, Col2, Col3
The OUTPUT clause is your friend now:
INSERT INTO steam.DocumentGeographies (Col1, Col2, Col3)
OUTPUT inserted.*
(SELECT
Col1,
Col2,
'Something modified',
FROM dbo.Table
WHERE Col10 = 66)
You can insert the result of the OUTPUT by specifying the destination table with the INTO clause or by wrapping the query as a subquery:
INTO clause
This is useful, when you want to insert the same data into two tables. You can always list the required fileds in the OUTPUT clause (inserted.Col1, inserted.Col2)
INSERT INTO steam.DocumentGeographies (Col1, Col2, Col3)
OUTPUT inserted.* INTO DestinationTableName(Col1, Col2, Col3)
(SELECT
Col1,
Col2,
'Something modified',
FROM dbo.Table
WHERE Col10 = 66)
SUBQUERY
This is useful, when you want to join the OUTPUT to another tables or you want to make calculations (like summing or counting values) and insert those results into another table.
INSERT INTO DestinationTableName
(Col1, Col2, Col3)
SELECT Col1, Col2, Col3 FROM (
INSERT INTO steam.DocumentGeographies (Col1, Col2, Col3)
OUTPUT inserted.*
(SELECT
Col1,
Col2,
'Something modified',
FROM dbo.Table
WHERE Col10 = 66)
) TMP

How to get count on ths query

I'm doing an
INSERT INTO table1...
SELECT...
FROM table2
However, I need to retrieve the identity from a table3 and do an insert into it just before inserting into table1. These two inserts need to occur together, with table3 insert going first. I've tried something like this:
INSERT INTO table1 (col1, col2, col3)
SELECT (
col1=(insert into table3(col1, col2)values(1,1) select SCOPE_IDENTITY(),
col2, col3)
FROM table2
But that doesn't work. table1.col1 does need the identity value from the new insert into table3. Amount of data to insert probably no more than a few 100 rows. Any suggestions?
It looks like you might be able to use the Output Clause.
BEGIN TRANSACTION
DECLARE #MyResults table(col1 int, col2 int, col3 int);
INSERT INTO table3 (col1, col2)
OUTPUT SCOPE_IDENTITY(), table2.col2, table2.col3 INTO #MyResults
SELECT 1, 1 FROM table2
INSERT INTO table1 (col1, col2, col3)
SELECT col1, col2, col3 FROM #MyResults
COMMIT

SQL query to transfer values from rows to columns for subselect groups

Here's the problem:
The dataset is:
Col1 Col2
BK.01.04 A0103
BK.01.04 A0306
BK.01.04 A0309
BK.01.04 A0403
BK.02.01 A1403
BK.02.02 A1403
BK.02.03 A0403
BK.02.03 A0703
BK.02.04 A0103
BK.02.04 A0306
BK.02.04 A0309
BK.02.04 A0403
The required result is:
Col1 Col2 Col3 Col4 Col5
BK.01.04 A0103 A0306 A0309 A0403
BK.02.01 A1403
BK.02.02 A1403
BK.02.04 A0103 A0306 A0309 A0403
Any ideas on how to do this in MS Access/Plain SQL? Any help is greatly appreciated :)
Join the table back to itself:
select
t1.col1,
t1.col2,
t2.col2 as col3,
t3.col2 as col4,
t4.col2 as col5
from mytable t1
left join mytable t2 on t2.col1 = t1.col1 and t2.col2 > t1.col2
left join mytable t3 on t3.col1 = t1.col1 and t3.col2 > t2.col2 and t2.col2 is not null
left join mytable t4 on t4.col1 = t1.col1 and t4.col2 > t3.col2 and t3.col2 is not null
order by 1;
Using left join means you'll get blanks when there aren't matching rows in the joined table. All that crap in the join conditions is to make sure there's a different and ascending value in each column
Not quite exactly what you wanted, but I think you'll find it challenging to have the leftmost column populated with the value you want...
A pivot table using ANSI-SQL:
CREATE TABLE DataSet ( Col1 varchar(20), Col2 varchar(20))
INSERT INTO DataSet ( Col1, Col2 ) VALUES ( 'BK.01.04','A0103')
INSERT INTO DataSet ( Col1, Col2 ) VALUES ( 'BK.01.04','A0306')
INSERT INTO DataSet ( Col1, Col2 ) VALUES ( 'BK.01.04','A0309')
INSERT INTO DataSet ( Col1, Col2 ) VALUES ( 'BK.01.04','A0403')
INSERT INTO DataSet ( Col1, Col2 ) VALUES ( 'BK.02.01','A1403')
INSERT INTO DataSet ( Col1, Col2 ) VALUES ( 'BK.02.02','A1403')
INSERT INTO DataSet ( Col1, Col2 ) VALUES ( 'BK.02.03','A0403')
INSERT INTO DataSet ( Col1, Col2 ) VALUES ( 'BK.02.03','A0703')
INSERT INTO DataSet ( Col1, Col2 ) VALUES ( 'BK.02.04','A0103')
INSERT INTO DataSet ( Col1, Col2 ) VALUES ( 'BK.02.04','A0306')
INSERT INTO DataSet ( Col1, Col2 ) VALUES ( 'BK.02.04','A0309')
INSERT INTO DataSet ( Col1, Col2 ) VALUES ( 'BK.02.04','A0403')
SELECT Col1,
MAX(CASE Col2 WHEN 'A0103' THEN 'A0103' ELSE '' END) Col2,
MAX(CASE Col2 WHEN 'A0306' THEN 'A0306' ELSE '' END) Col3,
MAX(CASE Col2 WHEN 'A0309' THEN 'A0309' ELSE '' END) Col4,
MAX(CASE Col2 WHEN 'A0403' THEN 'A0403' ELSE '' END) Col5,
MAX(CASE Col2 WHEN 'A1403' THEN 'A1403' ELSE '' END) Col6
FROM DataSet
GROUP BY DataSet.Col1
Results Are:
Col1 Col2 Col3 Col4 Col5 Col6
-------------------- ----- ----- ----- ----- -----
BK.01.04 A0103 A0306 A0309 A0403
BK.02.01 A1403
BK.02.02 A1403
BK.02.03 A0403
BK.02.04 A0103 A0306 A0309 A0403
(5 row(s) affected)
If you're using MS-Access, you can accomplish the same thing by using pivot-tables:
http://office.microsoft.com/en-us/access-help/designing-your-first-pivottable-pivotchart-views-in-access-HA001034580.aspx

Stored procedure for inserting a constant plus values from another table

How to modify the following stored procedure if we want in the inserted records the Col3 contain a constant string 'Foo'?
INSERT Table2
(Col1,
Col2,
Col3)
SELECT T1Col1,
T1Col2
FROM Table1
INSERT Table2
(Col1,
Col2,
Col3)
SELECT T1Col1,
T1Col2,
'Something'
FROM Table1
INSERT Table2
(Col1,
Col2,
Col3)
SELECT T1Col1,
T1Col2,
'Foo'
FROM Table1