SQL Server - select from (select union select)
Asked By Niron Kag on 21-May-07 04:38 AM
hi,
I am trying to do something like this
select * from (select ... from ... where ... UNION select ... from ... where...)
but I get the message :
Incorrect syntax near ')'.
why cant I do that? thanks alot...
Response
F Cali replied to Niron Kag on 21-May-07 10:59 AM
You have to provide a table alias to your sub-query:
select * from (select ... from ... where ... UNION select ... from ... where...)
A
SQL Server Helper
http://www.sql-server-helper.com/tips/date-formats.aspx
Thanks F Cali good to know
Niron Kag replied to F Cali on 21-May-07 02:13 PM
still i am getting the same error!
srini vasan replied to Niron Kag on 02-Jun-08 07:22 AM
SELECT MIN(dbo.FS_ItemInventory.Bin) FROM
(SELECT MIN(dbo.FS_LotTrace.LotNumber) FROM
dbo.FS_LotTrace RIGHT OUTER JOIN dbo.FS_MOHeader AS h INNER JOIN
dbo.FS_MOLine AS l ON l.MOHeaderKey = h.MOHeaderKey INNER JOIN
dbo.FS_Item AS i ON i.ItemKey = l.ItemKey INNER JOIN
dbo.FS_MOLineData ON l.MOLineKey = dbo.FS_MOLineData.MOLineKey LEFT OUTER JOIN
dbo.FS_DemandSupply AS ds ON l.MOLineKey = ds.TopLevelDemandSupplyKey LEFT OUTER JOIN
dbo.FS_ItemInventory RIGHT OUTER JOIN
dbo.FS_Item AS di ON dbo.FS_ItemInventory.ItemKey = di.ItemKey and ds.DemandItemKey = di.ItemKey and
dbo.FS_LotTrace.LotTraceKey = dbo.FS_ItemInventory.LotTraceKey
where (dbo.FS_ItemInventory.InventoryCategory!='H' or dbo.FS_ItemInventory.InventoryCategory is null) group by di.ItemNumber)
where (dbo.FS_ItemInventory.InventoryCategory!='H' or dbo.FS_ItemInventory.InventoryCategory is null) group by di.ItemNumber,dbo.FS_LotTrace.LotNumber
still ia am getting the same error!
Incorrect syntax near 'where'
why cant I do that? thanks alot..
is it correct?
srini vasan replied to Niron Kag on 02-Jun-08 05:47 AM
hi!
i want to use IN query like
select ... from ...
where field1 in (...)
and field2 in (....)
when i write query like this, the result is display.
but its wrong.
is it correct?
Correct
Willy Viana replied to Niron Kag on 03-Jun-09 02:48 PM
Hy,
Try:
select * from (select ... from ... where ... UNION select ... from ... where...) TEST
Comment: Alias to table(select).
Bye.
Hi all, I am using Sql Server 2000.I have a SQL Stmt something like this: SELECT id, photo from table1 UNION SELECT id, photo from table2 note: the photo field type is image I want to query the distinct records so i dont want to use the UNION ALL but I'm receiving the error msg below The text, ntext, or image data
I have a simple union statement in a stored procedure SET @SQL = "select cad_type, cad_desc , " + " CASE loads WHEN '1' THEN 'True' " + "WHEN '0' THEN 'False' " + "ELSE '' " + "END AS from Audit_Copy_Cadtype " + " where id = " + CAST(@ID AS VARCHAR(10)) if ( @CADID is not null ) SET @SQL = @SQL + "union " + "select cad_type, cad_desc, " + " CASE loads WHEN '1' THEN 'True' " + "WHEN '0' THEN 'False' " + "ELSE '' " + "END
- -we can write generic solution for any query. - -sample table SELECT * INTO #comp FROM ( SELECT '001' ProductID, 'Mouse' Descr, 10 Qty UNION all SELECT '002' ProductID, 'Cable' Descr, 20 Qty ) x - -selected record to #tmp SELECT * INTO #tmp FROM #comp - - WHERE car_id = 1 - -generic solution DECLARE @sql VARCHAR(max) SET @sql = '' SELECT @sql = @sql + 'SELECT ProductID, '''+Name+''' as ColumnName, cast(['+Name+'] as varchar(500)) as Value
i am having 4 unions select Count(*) from table1 union select Count(*) from table2 union select Count(*) from table3 and returns the output as 3 4 5 and total count wll be 12 how to get this total count with average for the above union query Hello Try as follows select COUNT (*) from table1 union select COUNT (*) from table2 union select COUNT (*) from table3 union select
select * from products order by date union select * from orders order by date union select top 3 * from sales order by date does not work with order by, any ideas You can only have a single ORDER BY clause in an UNION query and you must make sure that it is a part of the last Select
i have this query [CODE] INSERT INTO LEVCompaniesDepartment (UserID, DepartmentID) SELECT 422, 170 UNION ALL SELECT 422, 320 UNION ALL SELECT 422, 520 where not EXISTS (select UserID from LEVCompaniesDepartment where UserID = 422 and DepartmentID = 170
can we join these . . . . . (select dateadd(WEEK, -26, getdate()) union SELECT dateadd(WEEK, -25, getdate()) select (Likeliness*Priority*FTE)as netfte, from WL_projectdetails Are you talking about a JOIN or a UNION? A UNION comprises two separate queries that both return identical sets of columns. A JOIN combines columns different tables (usually) based on a common field. If you are talking about joining the union query to the select query, yes you can join them but since there is no
Hi, Can anyone suggest me a procedure or tens CHAR ( 1 ) DECLARE @ones CHAR ( 1 ) IF @Number = 0 Return 'Zero' - - initialize the variables SELECT @inputNumber = CONVERT ( varchar ( 38 ), @Number ) , @outputString = '' , @counter = 1 SELECT @length = LEN ( @inputNumber ) , @position = LEN ( @inputNumber ) - 2 , @loops = LEN ( @inputNumber ) / 3 - - make sure there is 0 SET @loops = @loops + 1 - - insert data for the numbers and words INSERT INTO @NumbersTable SELECT '00' , '' UNION ALL SELECT '01' , 'one' UNION ALL SELECT '02' , 'two' UNION ALL SELECT '03' , 'three' UNION ALL SELECT '04' , 'four' UNION ALL SELECT
plz, If any body having code for convert tens CHAR (1) DECLARE @ones CHAR (1) IF @Number = 0 Return 'Zero' - - initialize the variables SELECT @inputNumber = CONVERT ( varchar (38), @Number) , @outputString = '' , @counter = 1 SELECT @length = LEN (@inputNumber) , @position = LEN (@inputNumber) - 2 , @loops = LEN (@inputNumber) / 3 - - make sure there is 0 SET @loops = @loops + 1 - - insert data for the numbers and words INSERT INTO @NumbersTable SELECT '00' , '' UNION ALL SELECT '01' , 'one' UNION ALL SELECT '02' , 'two' UNION ALL SELECT '03' , 'three' UNION ALL SELECT '04' , 'four' UNION ALL SELECT