Is there a work around with this type of sort in Microsoft SQL sever 2008?
Just want it to sort with my temp column.
SELECT x AS TempTest FROM table1 WHERE TempTest IS NOT NULL
I can never remember which versions support referencing aliased columns, but a surefire method is to use a derived table.
Select * From
(
SELECT x AS TempTest FROM table1
)MyTempTable
WHERE TempTest IS NOT NULL
Order By TempTest
All you're doing is aliasing an existing column, so just sort by the original column name. Alternatively, you could sort by the column ordinal position.
SELECT x AS TempTest FROM table1 WHERE x IS NOT NULL ORDER BY 1
Related
I have a query (similar to the one below) that supposed to create a temporary table from a join:
SELECT p.pid,*
INTO #temp_insert_inventory
FROM
(SELECT *
FROM main_inventory.dbo.Inv i
LEFT JOIN web_inventory.dbo.Variant v ON i.id = v.ext4
WHERE v.ext4 IS NULL
AND i.qty > 0) PL
INNER JOIN web_inventory.dbo.Product p ON PL.invcode = p.ext
The problem is that SQL Server is refusing to create the temporary table because there are multiple CreatedOn columns being returned from the select.
I really don't need any one of these columns so how can I just omit it in the select?
Thanks!
List out all the columns instead of SELECT *.
If you need both versions of the conflicting CreatedOn columns, you can use an alias for one of them. If you don't want the additional column at all then don't include it in the SELECT list.
Bad Habits to Kick: Using SELECT * / omitting the column list
We have a PostgreSQL database where I have two tables with column text[] datatype. When I use an inner join to get details I do not see it is matching with any row.
for e.g.
create table test{
names text[],
id
}
create table test_b{
names text[],
id
}
now when I run below query,
SELECT t.* from test t inner join test_b tt
where t.names=tt.names;
I don't see any results. I even tried normal query with
SELECT * FROM test where names='{/test/test}';
It also did not work.
Any suggestions?
I suspect that you want array overlap operator &&. Also, since you don't need to return anything from test_b, using exists with a correlated subquery is more relevant than a join:
select t.*
from test t
where exists (select 1 from test_b b where t.names && b.names)
I have three tables table A , table B and table C. I need to populate C with the combination of A and B.
How can i populate table C with the results of the query below?
UPDATE dbo.C
SELECT TOP (200000) dbo.A.Id, dbo.B.Id
FROM [testDB].[dbo].A
CROSS JOIN [testDB].[dbo].B
You can use INSERT INTO . .SELECT if table already exists :
INSERT INTO dbo.c(aID, bID) -- Qualify correct column names
SELECT TOP (200000) dbo.A.Id, dbo.B.Id
FROM [testDB].[dbo].A CROSS JOIN
[testDB].[dbo].B
ORDER BY ???; -- Use ordering column to specify the sequence
If table C doesn't exists then use INTO :
SELECT TOP (200000) dbo.A.Id, dbo.B.Id INTO [testDB].[dbo].C
FROM [testDB].[dbo].A CROSS JOIN
[testDB].[dbo].B
ORDER BY ???; -- Use ordering column to specify the sequence
I have a sql select statement which contains some columns that are computed from some other columns or tables. I gave a name for this column using As keyword.
Now, I want to sort this table by the computed column.
I cant use that name for sorting.
Someone please help to sort the sql table using computed column.
In older versions of SQL Server, you can define the alias in a subquery:
select *
from (
select col1 + col2 as col3
from YourTable
) SubQueryAlias
order by
col3
In SQL Server 2008+ you should be able to order by an alias without a subquery:
select col1 + col2 as col3
from YourTable
order by
col3
one more option you can use COLUMN INDEX NUMBER in order by as show in follwoing example
select ACol,AVal,CAST(ACol as varchar(3)) + aval as 'New' from ABC order by 3
this will use 'New' columnd to sort
I have an aplication that passes parameters to a procedure in SQL. One of the parameters is an table valued parameter containing items to include in a where clause.
Because the table valued parameter has no statistics attached to it when I join my TVP to a table that has 2 mil rows I get a very slow query.
What alternatives do I have ?
Again, the goal is to pass certain values to a procedure that will be included in a where clause:
select * from table1 where id in
(select id from #mytvp)
or
select * from table1 t1 join #mytpv
tvp on t1.id = tvp.id
although it looks like it would need to run the query once for each row in table1, EXISTS often optimizes to be more efficient than a JOIN or an IN. So, try this:
select * from table1 t where exists (select 1 from #mytvp p where t.id=p.id)
also, be sure that t.id is the same datatype as p.id and t.id has an index.
You can use a temp table with an index to boost performance....(assuming you have more than a couple of records in your #mytvp)
just before you join the table you could insert the data from the variable #mytvp to a temp table...
here's a sample code to create a temp table with index....The primary key and unique field determines which columns to index on..
CREATE TABLE #temp_employee_v3
(rowID int not null identity(1,1)
,lname varchar (30) not null
,fname varchar (30) not null
,city varchar (20) not null
,state char (2) not null
,PRIMARY KEY (lname, fname, rowID)
,UNIQUE (state, city, rowID) )
I had the same issue that table-valued parameters where very slow in my context. I came up with a solution that passed the list of values as a comma separated string to the stored procedure. the procedure then made a PATINDEX(...) > 0 comparision. This was about a factor of 1:6 faster.
As mentioned here and explained here you can have primary key and unique constraints on the table type. E.g.
CREATE TYPE IdList AS TABLE ( Id UNIQUEIDENTIFIER NOT NULL PRIMARY KEY )
However, check if it improves performance in your case as now, these indexes exist when the TVP is populated which might lead to a counter effect depending if your input is sorted and/or if you use more than one column.
In common with table variables, table-valued parameters have no statistics (see the section "restrictions"); the query optimiser works on the assumption that they contain only one row, which if your parameter contains a lot of rows is likely to result in an inappropriate query plan.
One way to improve your chances of a better plan is to add a statement level recompile; this should enable the optimiser to take the size of the TVP into account when selecting a plan.
select * from table1 t where exists (select 1 from #mytvp p where t.id=p.id) OPTION (RECOMPILE)
(incorporating KM's suggestion)