This is similar to the code I use:
SELECT id, col1 FROM table1
UNION ALL
SELECT id, col2 FROM table2
UNION ALL
SELECT id, col3 FROM table3
...
For example col2 in table2 does not exist, how to make this query work and not to query for columns that do not exist?
Using NULL or any other value and alias it to match column list defintion:
SELECT id, col1 FROM table1
UNION ALL
SELECT id, NULL AS col2 FROM table2
UNION ALL
SELECT id, col3 FROM table3
Related
I have a scenario where I need to convert column to rows. I have two tables table1 and table2 with the following structure
Table1:
Col11 Col12 Col13
-------------------------
200 text 55
Table2:
Col1 Col2
--------------------
Col11
Col12
Col13
In need the following result from the above two tables
Col1 Col2
--------------------
Col11 200
Col12 text
Col13 55
Is it possible to do this by using temp tables ?
You can do it using UNION ALL:
SELECT 'Col1' AS Col1, Col1 AS Col2
FROM mytable
UNION ALL
SELECT 'Col2', Col2
FROM mytable
UNION ALL
SELECT 'Col3', Col3
FROM mytable
Table2 doesn't seem to play any role in producing the required result set. So it is left out in the above query.
you can use cross apply or UNPIVOT to get
select
coll,colvalue
from PivotColToRow
cross apply
(
select 'col1', col1 union all
select 'col2', col2 union all
select 'col3', col3
) c (Coll, colvalue);
Using UNPIVOT
select col,colvalue
from PivotColToRow
unpivot
(
colvalue
for col in (col1, col2, col3)
) unpiv;
try this
I understand how to display the 5 most occurring value of a column like so:
select top 5 col1, count(col1)
from table1
group by col1
order by count(col1) desc;
However, how do I create a query that displays all other values of the same column that are not in the result of the above query?
I tried the following sub query:
select col1
from table1
where col1 not in
(select top 5 col1, count(col1)
from table1
group by col1
order by count(col1) desc);
However the query failed and I got the following error message:
Only one expression can be specified in the select list when the
subquery is not introduced with EXISTS.
For Sql Server 2012+ you can use offset:
select col1, count(col1)
from table1
group by col1
order by count(col1) desc
offset 5 rows
You may want to add tiebreaker to your ordering here to make it deterministic:
select col1, count(col1)
from table1
group by col1
order by count(col1) desc, col1
offset 5 rows
Problem is you cannot select more than one column inside subquery.
(select top 5 col1, count(col1)..
You can remove the count(col1) from subquery but NOT IN clause can fail when col1 in subquery has NULL values
Try changing like this
with cte as
(
select top 5 col1
from table1
group by col1
order by count(col1) desc
)
select * from table1 A
where not exists (select 1 from cte B where a.Col=b.col)
Use OFFSET
select col1, count(col1)
from table1
group by col1
order by count(col1) desc
OFFSET 5 ROWS -- skip 5 rows, must use with order by
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 have two tables and want to return both table's rows. Both tables do not have any relation.
Table 1 has columns userid, name, and other columns... and Table 2 has only two columns id, name.
I want both table results in one query result set.
Table results:
userid name and other columns from Table 1.
id name and NULL, NULL should show as Table 2 do not have extra columns.
Use a union
select userid, name, col1, col2, col3 from table1
union all
select id, name, null, null, null from table2
select userid, name, col1, col2, col3 from table1
union all
select id, name, null, null, null from table2
This should work:
(SELECT userid, name, column3, column4, column5 FROM table1)
UNION ALL
(SELECT id, name, NULL, NULL, NULL FROM table2)
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)