Select from union in SQL Server
You should give alias to your table. So try this: SELECT A FROM ( SELECT A, B FROM TableA UNION SELECT A, B FROM TableB ) AS tbl WHERE B > ‘some value’
You should give alias to your table. So try this: SELECT A FROM ( SELECT A, B FROM TableA UNION SELECT A, B FROM TableB ) AS tbl WHERE B > ‘some value’
When you do set() you are creating an empty set. When you do set().intersection(…) you are intersecting this empty set with other stuff. The intersection of an empty set with any other collection of sets is empty. If you actually have a list of sets, you can get their intersection similar to how you did …
There is no default order. Without an Order By clause the order returned is undefined. That means SQL Server can bring them back in any order it likes. EDIT: Based on what I have seen, without an Order By, the order that the results come back in depends on the query plan. So if there …
Just checked that this will work: (SELECT * FROM catalog_productimage ORDER BY date_modified LIMIT 10) UNION SELECT * FROM catalog_productimage WHERE id=5;
select id, sum(amount) from ( select id,amount from table_1 union all select id,amount from table_2 union all select id,amount from table_3 ) x group by id
If you want a total count for all records, then you would do this: SELECT COUNT(*) FROM ( select distinct profile_id from userprofile_… union all select distinct profile_id from productions_… ) x
When you use a UNION, the DBMS removes any duplicate rows, and in order to do so it needs to identify whether two rows are equal / identical. This in turn means looking at each column of the two rows it’s comparing, and deciding if they’re equal. The error message you’re seeing is where one …
Aggregate functions may help you out here. Aggregate functions ignore NULLs (at least that’s true on SQL Server, Oracle, and Jet/Access), so you could use a query like this (tested on SQL Server Express 2008 R2): SELECT FK, MAX(Field1) AS Field1, MAX(Field2) AS Field2 FROM table1 GROUP BY FK; I used MAX, but any aggregate …
You’d have to use a temp table like this. UNION is for SELECTs, not stored procs CREATE TABLE #foo (bar int …) INSERT #foo exec MyStoredProcedure 1 INSERT #foo exec MyStoredProcedure 2 INSERT #foo exec MyStoredProcedure 3 … And hope the stored procs don’t have INSERT..EXEC.. already which can not be nested. Or multiple resultsets. …
You can use set.update to update your master set in place. This saves allocating a new set all the time so it should be a little faster than set.union… >>> s = set(range(3)) >>> s.update(range(4)) >>> s set([0, 1, 2, 3]) Of course, if you’re doing this in a loop: masterSet = set() for setA …