How to get COUNT when using EXCEPT and UNION - sql-server-2008

Why can not I use the COUNT in a table?
SELECT COUNT(*) FROM
(
(SELECT * FROM task.tableA EXCEPT SELECT * FROM task.tableB)
UNION
(SELECT * FROM task.tableB EXCEPT SELECT * FROM task.tableA)
)

This type of query is called as inline view (Derived tables). An inline view is a SELECT statement in the FROM-clause of another SELECT statement.
Benefits:
We do not need to create the temporary table. This prevents the database from having too many objects, which is a good thing as each additional object in the database costs resources to manage.
We can use a single SQL query to accomplish what we want.
Execute your query with a table name "temp" as below
SELECT COUNT(*)
FROM
(
(SELECT * FROM task.tableA EXCEPT SELECT * FROM task.tableB)
UNION
(SELECT * FROM task.tableB EXCEPT SELECT * FROM task.tableA)
) temp;

Related

Selectively searching from a set of tables in MS Access

I wanted to know if it's possible to have one query which I could design so that it would take a certain record from only one table i.e. I have 3 tables and my query, based on a certain parameter would choose one table to take the records from.
You can do this if all tables have equal field names using a UNION query, and by adding a field that specifies from which table the row originated:
PARAMETERS ParamTableName VARCHAR(255);
SELECT * FROM (
SELECT *, "Table1" As TableName FROM Table1
UNION ALL
SELECT *, "Table2" As TableName FROM Table2
UNION ALL
SELECT *, "Table3" As TableName FROM Table3
) TheTables
WHERE TableName = ParamTableName

Selecting distinct values from joined results in Mysql

I am trying to access complete data from two tables in mysql and then find distinct values of a column from the resulting set. tried doing with nested query as follows :-
Select distinct s.BlockName
from (SELECT *
FROM constucted
LEFT JOIN required ON constucted.BlockName = required.BlockName
UNION
SELECT *
FROM constucted
RIGHT JOIN required ON constucted.BlockName = required.BlockName
) s
As mentioned in Stackoverflow Reference
and also tried using the with keyword as follows :-
WITH CTE
AS
(
SELECT *
FROM constucted
LEFT JOIN required ON constucted.BlockName = required.BlockName
UNION
SELECT *
FROM constucted
RIGHT JOIN required ON constucted.BlockName = required.BlockName
)
SELECT DISTINCT BlockName
FROM CTE
But unable to find the distinct value for the column BlockName unable to prepare a query for the same.
Your query would seem to be more simply written as:
select BlockName
from constructed
union -- intentional to remove duplicates
select BlockName
from required;

Mysql Union in different columns

I have the following query
SELECT *
FROM(
(SELECT
MAX(c.start_time) as start_1
FROM
c1 c)
UNION ALL
(SELECT
MAX(cc.created_at) as ccmax
FROM
cc1)
) as t
I'd like to have the result in a table with 2 columns start_1 and cmax instead of the single column I get with all the different results listed.
How should I do it? I ended up in a subselect believing this would have done the job.
For the data to be in two columns you would have to use a sub select.
SELECT
MAX(c1.start_time) as start_1, (SELECT MAX(cc1.created_at) FROM cc1) as ccmax
FROM c1

How do I multiple select in mysql using one query?

I have an sql statement like this:
Select Id,Name From table where id = var
Select Id,Name From table where id = var
Select Id,Name From table where id = var
I was able to a multiple select in one query but it's only allows one column per select.
SELECT (
SELECT COUNT(*)
FROM user_table
) AS tot_user,
(
SELECT COUNT(*)
FROM cat_table
) AS tot_cat,
(
SELECT COUNT(*)
FROM course_table
) AS tot_course
Use join if tables relates each other else
use union.
You can use any of subselects, unions or joins - it depends on what you're trying to do:
SQL joins are used to query data from two or more tables, based on a relationship between certain columns in these tables.
For instance:
SELECT user_table.name, user_table.email, cat_table.title
FROM user_table
FULL JOIN cat_table
ON user_table.cat_id=cat_table.id
ORDER BY user_table.id
More to read at http://www.w3schools.com/sql/sql_join.asp

How to easily get the unmatched condition in mysql

I have a "server" table which has a column named 'SN' in mysql, when do query to retrive servers with some sns from 'sn1' to 'sn10000', we can:
select * from server where sn in ('sn1','sn2','sn3',...'sn10000');
If there is only one sn in 'sn1'-'sn10000' which not exists in database, then the query above will retrive 9999 rows of result.
The question is how can I easily get which one in 'sn1'-'sn10000' is not exists in database except the additional work, such as handling the result with shell script etc.
I have an ugly sql like below can use:
select * from (select 'sn1' as sn
union select 'sn2'
union select 'sn3'
....
union select 'sn10000') as SN
where not exists (select id from server where server.sn=SN.sn);
Is Anyone has other better methods? Thanks.
Your query is perfectly fine for the intended use, but on MySQL the NOT IN and LEFT JOIN/IS NULL are more effecient that NOT EXISTS. Here are your alternatives:
NOT IN
SELECT *
FROM ( SELECT 'sn1' as sn
UNION ALL SELECT 'sn2'
UNION ALL SELECT 'sn3'
....
UNION ALL SELECT 'sn10000') as SN
WHERE sn.sn NOT IN (SELECT s.id FROM SERVER s)
LEFT JOIN/IS NULL
SELECT s.id
FROM SERVER s
LEFT JOIN ( SELECT 'sn1' as sn
UNION ALL SELECT 'sn2'
UNION ALL SELECT 'sn3'
....
UNION ALL SELECT 'sn10000') as SN ON SN.sn = s.id
WHERE sn.sn IS NULL
You might notice I used UNION ALL, rather than UNION - UNION removes duplicates (which won't happen in your example), making it slower so UNION ALL is a better choice.
Stick your sn1, sn2, sn3... sn10000 values in a temporary table, and then use Joins.
Select server.* from server inner join tempt on (tempt.value = server.sn)
will give you the ones that match, where as
Select sn.* from server right outer join tempt on (tempt.value = server.sn)
where server.somefield is Null
should take care of finding the missing ones.