finding counts assigned to another field's value - sql

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

Related

Need to get the value from a column whose column name is based on a value in another table

Table A has columns ID, COL1, COL2, COL3.
Table B has columns AID, ColumnName.
I need to get the [ColumnName] value in Table A based on the value of [ColumnName] in Table B.
In the example below:
For ID 1, I need to get the value of column COL1 (This is the value of [ColumnName] for AID 1 in Table B).
For ID 2, I need to get the value of column COL3 (This is the value of [ColumnName] for AID 2 in Table B).
Table A
ID COL1 COL2 COL3
1 a aa aaa
2 b bb bbb
Table B
AID ColumnName
1 COL1
2 COL3
Desired Result:
ID VALUE
1 a
2 bbb
How can I do that ?
Thank you.
Unpivot then join
drop table t
go
drop table t1
go
create table t
(ID int, COL1 varchar(10), COL2 varchar(10), COL3 varchar(10))
go
create table t1
(AID int,ColumnName varchar(10));
go
insert into t values
(1 , 'a', 'aa', 'aaa'),
(2 , 'b', 'bb', 'bbb')
go
insert into t1 values
(1 , 'COL1'),
(2 , 'COL3')
go
with cte as
(select id, u.col, u.val
from t
unpivot
(
val
for col in (col1, col2, col3)
) u
)
select cte.id,cte.val
from cte
join t1 on
t1.aid = cte.id and
t1.columnname = cte.col
go
id val
----------- ----------
1 a
2 bbb
(2 row(s) affected)
One possible approach is to unpivot the columns in TableA using VALUES table value constructor and additional APPLY operator:
Tables:
SELECT *
INTO TableA
FROM (VALUES
(1, 'a', 'aa', 'aaa'),
(2, 'b', 'bb', 'bbb')
) v (ID, COL1, COL2, COL3)
SELECT *
INTO TableB
FROM (VALUES
(1, 'COL1'),
(2, 'COL3')
) v (AID, COL)
Statement:
SELECT b.AID, v.VALUE
FROM TableB b
JOIN TableA a ON b.AID = a.ID
CROSS APPLY (VALUES
('COL1', a.COL1),
('COL2', a.COL2),
('COL3', a.COL3)
) v (COL, [VALUE])
WHERE v.COL = b.COL
Result:
AID
VALUE
1
a
2
bbb

Arrange NULL at the end in each column of the table

I have a table that contains two columns col1, col2.
NULL need to be placed at the end of each column.
Data in my table now.
--- ---
col1 col2
--- ---
1 null
2 2
3 null
4 4
null 5
create table mytable (
col1 number(3),
col2 number(3)
);
insert into mytable values(1, null);
insert into mytable values(2, 2);
insert into mytable values(3, null);
insert into mytable values(4, 4);
insert into mytable values(null, 5);
I need to update them in this format.
--- ---
col1 col2
--- ---
1 2
2 4
3 5
4 null
This mostly solves the problem; you'll need to put the final update in a recursive trigger so it executes the proper number of times.
-- remove nulls from col2
delete from mytable where col2 is null;
-- erase col1
update mytable
set col1 = null;
-- add "footer" row
insert into mytable (col1,col2) values ((select count(*)+1 from mytable), null);
-- update col1 for 1 remaining record
update mytable
set col1 = (select count(*) from mytable where col1 is not null)
where col2 = (select min(col2) from mytable where col1 is null);

Get ID of inserted & Selected row after multiple insert [duplicate]

This question already has answers here:
Insert Into... Merge... Select (SQL Server)
(1 answer)
Combine OUTPUT inserted.id with value from selected row
(2 answers)
Closed 4 years ago.
I have a query running in loop which I am trying to optimize as this
INSERT INTO myTable (col1, col2, col3)
OUTPUT inserted.id, SOURCE_ROW_ID_NEEDED_HERE
SELECT col1, col2, col3
FROM myTable
WHERE col2 = 20 --any value
My problem is : col2 = 20 can have N number of rows which get inserted, I need the id of the source row for the new record. For example say there are 3 rows for col2 = 20 and id of them are 11,12,15. The new inserted ID are say 150,151,152.
I would need
11 150
12 151
15 152
Are you looking for something like
CREATE TABLE T1(
Col1 INT IDENTITY(1, 1),
Col2 INT,
Col3 INT
);
CREATE TABLE T2(
Col1 INT IDENTITY(1, 1),
Col2 INT,
Col3 INT
);
INSERT INTO T2(Col2, Col3) VALUES
(11, 150),
(12, 151),
(15, 152);
DECLARE #TT TABLE (ID INT, Col2 INT);
SET IDENTITY_INSERT T1 ON;
INSERT INTO T1 (Col1, Col2, Col3)
OUTPUT INSERTED.Col1,
INSERTED.Col2
INTO #TT (ID, Col2)
SELECT Col1,
Col2,
Col3
FROM T2;
SELECT *
FROM #TT;
SET IDENTITY_INSERT T1 OFF;
Demo

How to get the count of occurrence from comma separated string

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.

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