I want in select statement using a column as somename and using in where. (SQL Server)
I've used in SyBase:
SELECT
'teste' as col1
from sometable
where col1 = 'teste'
Works!
In MySql:
SELECT
#col1:= 'teste'
from sometable
where #col1 = 'teste'
Works!
I need equivalent in SqlServer, but show erro that col1 doesn't exists.
You can achieve this using a subquery or a CTE (which is just a Syntactic sugar of subquery).
I am not sure of the use of '#variable:=' in Sybase so I am just ignoring it based on the conversation in comments.
Subquery:
SELECT
col1
FROM (
SELECT
'teste' AS col1
--,st.*
FROM sometable AS st
) s
WHERE col1 = 'teste'
CTE:
WITH CTE
AS (
SELECT
'teste' AS col1
--,st.*
FROM sometable AS st
)
SELECT
col1
FROM CTE
WHERE col1 = 'teste'
Related
If i just created a select query and want to use the new data in a new query do I reference the previously created query as the FROM in a new select query? The query I just created in my database is listed as SQLQuery1.sql so is that what I put as the FROM for the new query?
Here is an example of a query that contains multiple select statements:
SELECT T1.col_a, T1.col_b, T2.col_c
FROM (SELECT col_a, col_b, ...etc...) AS T1
JOIN (SELECT col_a, col_c, ...etc...) AS T2
ON T1.col_a = T2.col_a
I would use CTE (Common Table Expression).
Have a look at the following code:
WITH MainQuery
AS
(
select col1, col2, col3
from dbo.TableName
)
select a.col1, a.col2, a.col3, b.col1
from MainQuery as a
join someotherdata as b
on a.col1 = b.col1;
I need to remove some duplicate entries from an intersection table.
The table is incredibly badly set up, without primary keys, so I'm having some trouble removing entries which are duplicates.
Here's just a rough overview of the table:
col1 col2
------------
1 70
1 70
1 71
Both columns carry id's, and duplicates breaks stuff.
You can use RANKING Functions
with cte as
(
select row_number() over(partition by col1,col2 order by col1,col2 )as rowNum
from tableName
)
delete from cte where rowNum>1
SQL FIDDLE DEMO
with t1dups (col1, coldups)
AS (
select col2, ROW_NUMBER() Over (Partition by col1, col2 order by col2) as dups from t1 )
delete from t1dups where coldups > 1
drop table #t
create table #t(col1 int,col2 int)
insert into #t values(1,70),(1,70),(2,71)
;with cte as
(
select [col1],[col2],rn=row_number() over(partition by col1 order by col2) from #t
)
delete from cte where rn>1
select * from #t
DEMO
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 have the following table:
CREATE TABLE TEST(ID TINYINT NULL, COL1 CHAR(1))
INSERT INTO TEST(ID,COL1) VALUES (1,'A')
INSERT INTO TEST(ID,COL1) VALUES (2,'B')
INSERT INTO TEST(ID,COL1) VALUES (1,'A')
INSERT INTO TEST(ID,COL1) VALUES (1,'B')
INSERT INTO TEST(ID,COL1) VALUES (1,'B')
INSERT INTO TEST(ID,COL1) VALUES (2,'B')
I would like to select duplicate rows from that table. How can I select them?
I tried the following:
SELECT TEST.ID,TEST.COL1
FROM TEST WHERE TEST.ID IN
(SELECT ID
FROM TEST WHERE TEST.COL1 IN
(SELECT COL1
FROM TEST WHERE TEST.ID IN
(SELECT ID
FROM TEST
GROUP BY ID
HAVING COUNT(*) > 1)
GROUP BY COL1
HAVING COUNT(*) > 1)
GROUP BY ID
HAVING COUNT(*) > 1)
Where's the error? What do I need to modify?
And I would like it to show as:
ID COL1
---- ----
1 A
1 A
1 B
1 B
(4 row(s) affected)
SELECT id, col1
FROM Test
GROUP BY id, col1
HAVING COUNT(*) > 1
when you use
SELECT id, col1, COUNT(*) AS cnt
FROM Test
GROUP BY id, col1
HAVING COUNT(*) > 1
you practically have all duplicate rows and how often they appear. You can't identify them individually either way.
A slower way would be:
SELECT id, col1
FROM Test T
WHERE (SELECT COUNT(*)
FROM Test I
WHERE I.id = T.id AND I.col1 = T.col1) > 1
Using Sql Server 2005+ and CTE you could try
;WITH Dups AS (
SELECT *,
ROW_NUMBER() OVER(PARTITION BY ID, Col1 ORDER BY ID, Col1) Rnum
FROM #TEST t
)
SELECT *
FROM Dups
WHERE Rnum > 1
OR just a standard
SELECT ID,
Col1,
COUNT(1) Cnt
FROM #TEST
GROUP BY ID,
Col1
HAVING COUNT(1) > 1
EDIT:
Display duplicate rows
SELECT t.*
FROM #Test t INNER JOIN
(
SELECT ID,
Col1,
COUNT(1) Cnt
FROM #TEST
GROUP BY ID,
Col1
HAVING COUNT(1) > 1
) dups ON t.ID = dups.ID
AND t.Col1 = dups.Col1
Every row in that set of data is a duplicate
select id, col1, count(*)
from test
group by id, col1
shows this
if you want to exclude the 2,B rows you need to do it explicitly
eg
SELECT id, col1
FROM Test
WHERE NOT (id = 2 and col1 = 'B')
SELECT t.*
FROM TEST t
INNER JOIN (
SELECT ID,COL1
from test
GROUP BY ID,COL1
HAVING COUNT(*) > 1
)
AS t2
ON t2.ID = t.ID AND t2.COL1 =t.COL1
order by t.ID,t.COL1