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
Related
I have query which will group column 1 and 2 and calculate count for column3
(select col1, col2, count(col3)
from table1
group by col1,col2
Having count(col3) < 50)
But I want to display all fields values for col3 where count(col3) < 50 . Can you help me with this? Thank you
Try this:
select col1, col2, col3, t.cnt
from (
select col1, col2, col3,
count(col3) over (partition by col1, col2) AS cnt
from table1 ) as t
where t.cnt < 50
The idea is to use windowed version of COUNT instead of GROUP BY. COUNT(col3) OVER (PARTITION BY col1, col2) returns the count of col3 occurrences per col1, col2 for every row of table1. You can filter out col1, col2 slices having a count of 50 or more in an outer query.
You can also use something like this:
select t.col1, t.col2, t.col3
from table1 t
where
EXITS(select 1 from table1 t1 where t.col1 = t1.col1 and t.col2 = t.col2 having count(t1.col3) > 50)
or something like this:
select t.col1, t.col2, t.col3
from table1 t
inner join (select t1.col1, t1.col2, count(t1.col3) from table1 t1
group by t1.col1, t1.col2 having count(t1.col3) > 50) x
on t.col1 = x.col1 and t.col2 = x.col2
I need to query for a dataset in SQL Server and have one of the return columns based on either one of two subqueries. The control of which subquery is from a value of another column in the query. Some basic pseudo query language of what I'm trying to do:
select col1, col2, col3,
if col3 = 1
(select count(*) from table2 where table2.col1 = table1.col1) as count1
else
(select count(*) from table3 where table3.col1 = table1.col1) as count1
from table1
What's the best way to accomplish this?
SELECT col1, col2, col3,
CASE WHEN col3 = 1 THEN
(SELECT count(*)
FROM table2
WHERE table2.col1 = table1.col1)
ELSE
(SELECT count(*)
FROM table3
WHERE table3.col1 = table1.col1)
END AS count1
FROM table1
You should declare a variable and SELECT column col3 into it.
Or you could do a CASE...WHEN block.
I am a complete beginner with SQL, and I have to turn a query like this one:
SELECT col1, col2, col3 FROM db1
LEFT OUTER JOIN (SELECT col1, col2 FROM db2 WHERE some_conditions) AS query1
ON (query1.col1 = col2)
Into a query like this one:
SELECT col1, col2, col3 FROM db1
if col1 = 1
LEFT OUTER JOIN (SELECT col1, col2 FROM db2 WHERE some_conditions) AS query1
ON (query1.col1 = col2)
else
LEFT OUTER JOIN (SELECT col1, col2 FROM db2 WHERE some_other_conditions) AS query1
ON (query1.col1 = col2)
But the latter obviously doesn't work in SQL Server. What would be the proper format for a query like this?
You could use a complex on clause:
select *
from db1
left join
db2
on db1.col1 = db2.col2
and (
(db1.col1 = 1 and some_conditions)
or (db1.col1 <> 1 and some_other_conditions)
)
Using or, you can use one codition set in one case, and another condition set in another case.
I have a SQL Select and I am not sure how I can achieve this. I am checking two fields to see if any of those fields are in a list. So like,
Select * from MyTable where col1 or col2 in (select col3 from OtherTable where ID=1)
I tried
Select * from MyTable where
col1 in (select col3 from OtherTable where ID=1)
or col2 in (select col3 from OtherTable where ID=1)
But, this returns the records that match first condition (only returns col1, but not col2) for some reasons.
Try this -
Select * from MyTable where
(col1 in (select col3 from OtherTable where ID=1))
or
(col2 in (select col3 from OtherTable where ID=1) )
if you're subquery is the same for both columns, i'd throw it into a cte, then do a left outer join on the cte on col1 and col2, then do your where statement.
;with c3 as
(
select col3
from OtherTable
where ID=1
)
select m.*
from MyTable m
left outer join c3 as c1
on m.col1=c1.col3
left outer join c3 as c2
on m.col2=c2.col3
where
(c1.col3>'')
or (c2.col3>'')
if a blank varchar that isn't null is a viable option, change your where clauses to >=.
SELECT t.*
FROM MyTable t
INNER JOIN (
select col3
from OtherTable
where ID=1
) sel ON sel.col3 IN (t.col1, t.col2)
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.