dbTalk Databases Forums  

SELECT * FROM "multiple_tables" WHERE "field_(in_those_tables)_have_the_same_paricular_v alue" ?

comp.databases.ms-sqlserver comp.databases.ms-sqlserver


Discuss SELECT * FROM "multiple_tables" WHERE "field_(in_those_tables)_have_the_same_paricular_v alue" ? in the comp.databases.ms-sqlserver forum.



Reply
 
Thread Tools Display Modes
  #11  
Old   
AT
 
Posts: n/a

Default Re: SELECT * FROM "multiple_tables" WHERE "field_(in_those_tables)_have_the_same_paricular_v alue" ? - 04-25-2006 , 06:24 AM






Sorry - I forgot

What would be the syntax to include additional column data representing
the "system" in a combined table ?


Reply With Quote
  #12  
Old   
Erland Sommarskog
 
Posts: n/a

Default Re: SELECT * FROM "multiple_tables" WHERE "field_(in_those_tables)_have_the_same_paricular_v alue" ? - 04-25-2006 , 04:17 PM






(av1 (AT) email (DOT) t-com.hr) writes:
Quote:
Question still remains: how to SELECT similar data of interest across
many tables?

What would be the syntax to include additional column data representing
the "system" in a combined table ?
SELECT system = 'system of tbl1', col1 ,col2, col2,
FROM tbl1
WHERE ...
UNION ALL
SELECT 'system2, col1, col2, col3,
FROM tbl2
WHERE ...
UNION ALL

If the WHERE clause is generally applicable, you can do:

SELECT system, co1l, col2, col3, ...
FROM (SELECT system = 'system of tbl1', col1 ,col2, col2,
FROM tbl1
UNION ALL
SELECT 'system2, col1, col2, col3,
FROM tbl2
UNION ALL
...) AS x
WHERE ...

Here I'm using a derived table, which logically a temp table within
the query, but not necessarily comnputed as such. That is the optimizer
may recast the computation order for better effeciency.


--
Erland Sommarskog, SQL Server MVP, esquel (AT) sommarskog (DOT) se

Books Online for SQL Server 2005 at
http://www.microsoft.com/technet/pro...ads/books.mspx
Books Online for SQL Server 2000 at
http://www.microsoft.com/sql/prodinf...ons/books.mspx


Reply With Quote
  #13  
Old   
Hugo Kornelis
 
Posts: n/a

Default Re: SELECT * FROM "multiple_tables" WHERE "field_(in_those_tables)_have_the_same_paricular_v alue" ? - 04-26-2006 , 04:45 PM



On 25 Apr 2006 03:41:43 -0700, av1 (AT) email (DOT) t-com.hr wrote:

Quote:
I accept that this is bad database design.
(Any help toward improving it is appreciated, TIA)
But this is a "real world" example.

Designers are working on project consisting of several piping systems
(say 30) preparing (separate) bills of materials (BOMs) for each system
(water, oil, fuel, steam etc). BOMs looks similar to "TBL1_water" shown
at the beginning.
Later on - the project manager wants to know how many similar items we
have accross the project for purchasing purposes (discounts). He wants
to group similar items say ball valves or flanges for bidding purposes.
Regarding the table look - they use primarily Excel ( ** don't blame me
here ! ** )
I know that we need more advanced tool. That's why I raised this
question here.
Anyway, we can use this *.xls files more or less efficiently. (not to
mention problems with inconsistency with naming conventions, formats
etc. which exists ).
Question still remains: how to SELECT similar data of interest across
many tables?
Hi Andro,

And the answer still remains that this is VERY easy if you use just one
table instead of 30 tables for the 30 piping systems.

In my previous message, I asked you to post the DDL (CREATE TABLE
statements, including all constraints and indexes) for the tables that
you are currently using. If you had done that, I could now have given
you the CREATE TABLE statement for the single table to replace your 30
current tables.

Quote:
Regarding the "identification_no": this data will be populated later on
after retrieving the data of interest (in question above), selecting
the supplier and after purchasing the items so it is not important in
the initial stage.
It is unique mark for the same item and same type across all the tables
(Say
ball valve with size 1", rated pressure 150#, manually operated and
made of bronze has assigned it's own identification_no (at
will) across the all tables. Similar valve type (ball) with all the
items the same except one (say different material ) has different
indent_no).
Okay. So what exactly does this mean for the sample population for two
of the current tables that I posted? Is that particular combination of
vallues logically permitted? If not, what column(s) need to be changed
and why?

Quote:
Generally, I assume that all the tables should be kept in one table
with one additional distinct column data representing the "system" at
every corresponding row.
Yes.

--
Hugo Kornelis, SQL Server MVP


Reply With Quote
  #14  
Old   
Amit
 
Posts: n/a

Default RE: SELECT * FROM "multiple_tables" WHERE "field_(in_those_tables)_have_the_same_paricular_v alue" ? - 12-04-2007 , 08:11 AM



Hi,
I am able to retrive the uncommon columns from two similar tables, but the problem is i require a query, which can help me to find out which columns are added separately.
Another query which can say which columns are deleted.
And the last one can say which column has changed .

Plzz.. help me soon.

From http://www.developmentnow.com/g/95_2...lar-value-.htm

Posted via DevelopmentNow.com Groups
http://www.developmentnow.com

Reply With Quote
  #15  
Old   
Hugo Kornelis
 
Posts: n/a

Default Re: SELECT * FROM "multiple_tables" WHERE "field_(in_those_tables)_have_the_same_paricular_v alue" ? - 12-04-2007 , 02:47 PM



On Tue, 04 Dec 2007 14:11:47 GMT, Amit wrote:

Quote:
Hi,
I am able to retrive the uncommon columns from two similar tables, but the problem is i require a query, which can help me to find out which columns are added separately.
Another query which can say which columns are deleted.
And the last one can say which column has changed .

Plzz.. help me soon.
Hi Amit,


In order to properly help you, you must give me a better understanding
of your problem. The best way to do that is by posting
* CREATE TABLE statements for the table(s) involved in the problem,
including all constraints, properties, and indexes (though you may omit
irrelevant columns);
* INSERT statements with some well-chosen rows of sample data to
illustrate the problem; and
* expected results.

See www.aspfaq.com/5006 for more information.

--
Hugo Kornelis, SQL Server MVP
My SQL Server blog: http://sqlblog.com/blogs/hugo_kornelis


Reply With Quote
  #16  
Old   
Erland Sommarskog
 
Posts: n/a

Default RE: SELECT * FROM "multiple_tables" WHERE "field_(in_those_tables)_have_the_same_paricular_v alue" ? - 12-04-2007 , 04:57 PM



Amit (complete.amit (AT) gmail (DOT) com) writes:
Quote:
I am able to retrive the uncommon columns from two similar tables, but
the problem is i require a query, which can help me to find out which
columns are added separately. Another query which can say which columns
are deleted. And the last one can say which column has changed .
It is not clear if you are looking for metadata information or data. But
I assumed that you want to compare the metadata for two tables.

This may get you started:

select a.tblname, a.colname, a.typename, a.max_length,
b.tblname, b.colname, b.typename, b.max_length
from (SELECT tblname = o.name, colname = c.name,
typename = t.name, c.max_length
FROM sys.objects o
JOIN sys.columns c ON o.object_id = c.object_id
JOIN sys.types t ON t.system_type_id = c.system_type_id
AND t.user_type_id <= 255
WHERE o.name = 'tbl1') AS a
full join
(SELECT tblname = o.name, colname = c.name,
typename = t.name, c.max_length
FROM sys.objects o
JOIN sys.columns c ON o.object_id = c.object_id
JOIN sys.types t ON t.system_type_id = c.system_type_id
AND t.user_type_id <= 255
WHERE o.name = 'tbl2') AS b
ON a.colname = b.colname
AND a.typename = b.typename
AND coalesce(a.max_length, 0) = coalesce(b.max_length, 0)
WHERE a.colname IS NULL OR b.colname IS NULL

This query is for SQL 2005. If you are on SQL 2000, the query is similar,
but you need to use sysobjects, syscolumns and systypes, and somewhat
different colunm names. You could look that up in Books Online.


--
Erland Sommarskog, SQL Server MVP, esquel (AT) sommarskog (DOT) se

Books Online for SQL Server 2005 at
http://www.microsoft.com/technet/pro...ads/books.mspx
Books Online for SQL Server 2000 at
http://www.microsoft.com/sql/prodinf...ons/books.mspx


Reply With Quote
Reply




Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off



Powered by vBulletin Version 3.5.3
Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.