I am trying to use a filter on an OUTPUT clause in t-sql.
What I want to do is something like this:
Insert into tbl_1(col1,col2)
Output Inserted.col1 into #tbl_temp
**where col1 > 0**
select col3, col4
from tbl_2
For performance reasons I don't want to use two insert statements.
insert into #tbl_temp
select col1
from
(
insert into tbl_1(col1,col2)
output Inserted.col1
select col3, col4
from tbl_2
) as T
where T.col1 > 0
Related
I am trying to get a pivot result with no aggregation, I tried max and it didn't help, may be I am doing something wrong.
When I run this below query
declare #t table
(
col1 int,
col2 varchar(100),
col3 varchar(100),
col4 varchar(100),
col5 int
)
insert into #t values(1,'test1','p1','v1',1)
insert into #t values(1,'test1','p2','v2',2)
insert into #t values(1, 'test1','p3','v3',3)
insert into #t values(1,'test1','p1','v11',1)
insert into #t values(1,'test1','p1','v12',1)
insert into #t values(1,'test1','p2','v21',2)
insert into #t values(1,'test1','p2','v22',2)
--select * from #t
select col1,
col2,
[p1],
[p2],
[p3]
from
(
select * from #t
) x
pivot
(
Max(col4 )
for col3 in ([p1],[p2],[p3])
) pvt
I get this below result
I am trying to get this below result
It would be great if you could show me a path to achieve this.
You'll still need to use an aggregate function with the PIVOT, but you need some sort of value to return multiple rows based on the combination of col1, col2, and col3. This is where you'd want to use a windowing function like row_number().
If you use the following query you should be able to get the result:
select col1, col2, p1, p2, p3
from
(
select col1, col2, col3, col4,
rn = row_number() over(partition by col1, col2, col3 order by col5, col4)
from #t
) d
pivot
(
max(col4)
for col3 in (p1, p2, p3)
) p;
See SQL Fiddle with Demo
The row_number() function creates a unique sequence that is partitioned by the col1, col2 and col3 values - I then ordered the results by your col5 and col4 values to create the sequence in a specific order. This new value is used when the pivot groups the data which results in multiple rows being returned instead of the single row.
I want to INSERT INTO by more than one value at a time, I did this by doing the following:
INSERT INTO table_1(col_1,col_2,col_3)
SELECT col_1, col2, (SELECT col3 FROM table_3)
FROM table_2
But col3 from table_3 is a datetime format, while col3 from table_1 needs an integer value. I did this by doing the following:
CAST(CONVERT(varchar(10),(SELECT col3 FROM table_3),112)AS int)
When I run this I get a more than one result in a subquery error. I have really no idea whatsoever on how to fix this. Hopefully you do.
Thank you in advance.
INSERT INTO table_1(col_1,col_2,col_3)
SELECT col_1, col2, (CAST(CONVERT(varchar(10),(SELECT top 1 col3 FROM table_3),112)AS int))
FROM table_2
You need to use top 1 to select 1 row
Well, I think the error says it all. You have to limit the inner query somehow with WHERE condition, with TOP or with MAX(col3) for example. Depends WHICH col3 you want.
You need to join table_2 to table_3. Not sure what your database structure is, but it should be something like this:
INSERT INTO table_1(col_1,col_2,col_3)
SELECT t2.col_1, t2.col2, t3.col3
FROM table_2 t2
INNER JOIN table_3 t3 on t3.t2id = t2.id
The alternative is to use TOP 1, to just return 1 record in the sub-query - but I would not recommend this as it may not be the value you want:
INSERT INTO table_1(col_1,col_2,col_3)
SELECT col_1, col2, (SELECT top 1 col3 FROM table_3)
FROM table_2
You can use CTE to prepare your data :
;WITH MyData (col1, col2, col3)
AS
(
SELECT col_1, col2, CAST(CONVERT(varchar(10),(col3),112)AS int)
FROM table_2 JOIN table_3 ON <join condition>
)
INSERT INTO table_1(col_1,col_2,col_3)
SELECT col_1, col2, col3
FROM MyData
Pls try below query :
INSERT INTO table_1(col_1,col_2,col_3)
SELECT col_1, col2, isnull((SELECT TOP 1 cast(col3 as int) FROM table_3),0)
FROM table_2
I am trying to use a filter on an OUTPUT clause in t-sql.
What I want to do is something like this:
Insert into tbl_1(col1,col2)
Output Inserted.col1 into #tbl_temp
**where col1 > 0**
select col3, col4
from tbl_2
For performance reasons I don't want to use two insert statements.
insert into #tbl_temp
select col1
from
(
insert into tbl_1(col1,col2)
output Inserted.col1
select col3, col4
from tbl_2
) as T
where T.col1 > 0
I need to get several columns form sql query. Then I have to filter this answer by the "distinct" values of one column, but in the output I need to have all columns, not only this which values has to be distinct. Can anybody help me? Order by clause is not an answer for me.
A,B,C,D
E,F,G,H
I,J,C,L
M,N,Z,H
Above is a simple rows output. Please have a look onto 3rd column. Let's assume that we don't know how many rows do we have. I need to select only rows which has distinct value in 3rd column. (C,G,Z) - We need to filter anyone from "C" rows.
I've arbitrarily chosen to use col1 to break ties on col3. You can adjust the order by portion of the partition to suit your needs.
/* Set up test data */
declare #test table (
col1 char(1),
col2 char(1),
col3 char(1),
col4 char(1)
)
insert into #test
(col1, col2, col3, col4)
select 'A','B','C','D' union all
select 'E','F','G','H' union all
select 'I','J','C','L' union all
select 'M','N','Z','H'
/* Here's the query */
;with cteRowNumber as (
select col1, col2, col3, col4,
row_number() over (partition by col3 order by col1) as RowNumber
from #test
)
select col1, col2, col3, col4
from cteRowNumber
where RowNumber = 1
Returns
col1 col2 col3 col4
----------------------------
A B C D
E F G H
M N Z H
ROLL UP or CUBE could be helpful for your problem, since they can aggregate (i.e. subtotal) data based on the GROUP BY and still return the individual rows.
I want to do something like
insert into my table (select * from anothertable where id < 5)
What is the correct MSSQL syntax?
Thanks!
Is this what you're looking for?
INSERT INTO MyTable
SELECT * FROM AnotherTable
WHERE AnotherTable.ID < 5
That syntax looks correct, but you the fields have to match exactly otherwise it won't work.
You can specify the fields eg:
INSERT INTO myTable(COL1, COL2, COL3)
SELECT COL1, COL2, COL3 FROM anotherTable where anotherTable.id < 5
Insert Into MyTable
(
Col1,
Col2,
Col3
)
Select
Col1,
Col2,
Col3
From
AnotherTable
Where
ID < 5
You can also do
select *
into MyTable
from AnotherTable
where ID < 5
which will create MyTable with the required columns, as well as fill the data in.