I am using MS Sql server 2008 R2.
I have a query that gives me output like this
Col1....Col2
CV1.....AV1
CV1.....AV2
CV2.....AV3
CV2.....AV4
The query is
select Tab1.Col1, Tab2.Col2
from Table1 Tab1
JOIN Table2 Tab2 on Tab1.PKID = Tab2.FKID
What I want is one row for each distinct values in Col1 and in Col2 all the values related to col1 with comma or pipeline delimiter
Col1....Col2
CV1.....AV1,AV2
CV2.....AV3,AV4
Can anyone help me on this?
Basically I need something like group_concat that is available in My sql
CREATE TABLE a(
Col1 varchar(50),
Col2 varchar(20));
INSERT INTO a (Col1,Col2) values ('CV1','AV1');
INSERT INTO a (Col1,Col2) values ('CV1','AV2');
INSERT INTO a (Col1,Col2) values ('CV2','AV3');
INSERT INTO a (Col1,Col2) values ('CV2','AV4');
with t as (SELECT Col1,(CAST(Col2 AS nvarchar (12))) as col2 from a )
Select distinct T2.Col1,
substring((Select ',' + T1.col2 AS [text()]
From t T1
Where T1.Col1 = T2.Col1
ORDER BY T1.Col1
For XML PATH ('')),2, 100) [col2]
From t T2
Try this query. I am doing it in sql server. check at sqlfidddle
http://sqlfiddle.com/#!3/7ab28/1
Related
I am trying to create an Migration Script but i need some help:, i had the very basic script to insert an item into a table but i am trying to do it in a way to check for the item first if it exists, then insert it else skip it
here is my code:
use mydatabase;
INSERT INTO dbo.mytable(col1,col2,col3)
SELECT '3','test1','test3/abc.html';
You don't need to repeat the expression just use value construct like that :
INSERT INTO dbo.mytable(col1,col2,col3)
SELECT t.col1, t.col2, t.col3
FROM (values (3, 'test1','test3/abc.html')) t (col1, col2, col3)
WHERE NOT EXISTS (
SELECT 1
FROM dbo.mytable m
WHERE m.col1 = t.col1
AND m.col2 = t.col2
AND m.col3 = t.col3
);
INSERT INTO dbo.mytable(col1,col2,col3)
SELECT '3','test1','test3/abc.html'
WHERE NOT EXISTS (
SELECT 1
FROM dbo.mytable
WHERE col1='3'
AND col2='test1'
AND col3='test3/abc.html'
)
You can change the where depending on what you consider already inserted.
Something like this (but you would need some kind of key to identify if the record exists or not) -
IF NOT EXISTS(SELECT 1 FROM dbo.mytable WHERE col1 = '3' AND col2 = 'test1' AND col3 = 'test3/abc.html')
BEGIN
insert into dbo.mytable(col1,col2,col3)
SELECT '3','test1','test3/abc.html'
END
Use a MERGE:
CREATE TABLE #mytable (col1 nvarchar(20), col2 nvarchar(20), col3 nvarchar(20))
INSERT INTO #mytable(col1,col2,col3)
SELECT '3','test1','test3/abc.html';
Merge #mytable t
USING (SELECT '3','test1','test3/abc.html') s (col1, col2, col3)
ON t.col1 = s.col1
AND t.col2 = s.col2
AND t.col3 = s.col3
when not matched by target then
INSERT (col1,col2,col3) VALUES (s.col1, s.col2, s.col3);
I have a stored procedure with two table variables (#temp and #temp2).
How can I select the values from both temp tables (Both table variables contain one row) and insert them all in one table ?
I tried the following but this didn't work and I got the error that the number of SELECT and INSERT statements does not match.
DECLARE #temp AS TABLE
(
colA datetime,
colB nvarchar(1000),
colC varchar(50)
)
DECLARE #temp2 AS TABLE
(
colD int
)
...
INSERT INTO MyTable
(
col1,
col2,
col3,
col4
)
SELECT colD FROM #temp2,
colA FROM #temp,
colB FROM #temp,
colC FROM #temp
Many thanks for any help with this, Tim.
As both table variables have a single row you can cross join them.
INSERT INTO MyTable
(col1,
col2,
col3,
col4)
SELECT t.colA,
t.colB,
t.colC,
t2.colD
FROM #temp t
CROSS JOIN #temp2 t2
you should use this if you have only single row in both table #temp and #temp2, because this is a cartesian product.
INSERT INTO MyTable(col1,col2,col3,col4)
SELECT t.colA,
t.colB,
t.colC,
t2.colD
FROM #temp t,#temp2 t2
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.
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
There two tables:
Table1
field1 | field2
Table2
field1
“string1”
“string2”
I need to insert concatenation of table2.field1 values into table1, so it looks like
insert into table1(field1, field2) values (1, “string1string2”);
How can I do it? Is there any SQL-standard way to do it?
PS: string1 and string2 are values of the field1 column.
PPS: the main subtask of my question is, how can I get the result of select query into one row? All examples I've seen just use concatenation, but in all your examples SELECT subquery does not return string concatenation for all values of the table2.field1 column.
There is no ANSI standard SQL way to do this.
But in MySQL you can use GROUP_CONCAT
insert into table1 ( field1, field2 )
select 1, group_concat(field1) from table2
In SQL Server 2005 and later you can use XML PATH,
insert into table1 ( field1, field2 )
select 1, (select field1 from table2
for xml path(''), type).value('.','nvarchar(max)')
In Oracle, you can refer to Stack Overflow question How can I combine multiple rows into a comma-delimited list in Oracle?.
INSERT INTO TABLE1 (FIELD1, FILED2) VALUES (1, CONCAT("string1", "string2"))
try this :
insert into table1(field1, field2)
select table2.field1, table2.string1 || table2.string2 from table2;
You can add a where clause to the query to select only some entries from table2 :
insert into table1(field1, field2)
select table2.field1, table2.string1 || table2.string2 from table2
where table2.field = 'whatever';
I'd try with
insert table1 select field1, string1+string2 from table2
tested with MSSQL Server 2008
create table #t1 (n int, s varchar(200))
create table #t2 (n int, s1 varchar(100), s2 varchar(100))
insert #t2 values (1, 'one', 'two') -- worked without into ???
insert #t2 values (2, 'three', 'four') -- worked without into ???
insert #t1 select n, s1+s2 from #t2 -- worked without into ???
select * from #t1
drop table #t1
drop table #t2
After the edit:
No, if you have no way to identify the lines in table2 and sort them the way you want it is impossible. Remember that, in the absence of a order by in the SQL statement, lines can be returned in any order whatsoever
Assuming this is SQL server,
Insert into table1 (field1, field2)
select field1, string1 + string2
from table2
In oracle you will do it as -
Insert into table1 (field1, field2)
select field1, string1 || string2
from table2