The following example is based on the post: microsoft.public.sqlserver.programming Friday, November 21, 2008 11:56 AM "T-SQL Comparing a String of Items to a Table of Items; Listing Missing Table Items" http://tinyurl.com/5dvc6o Here's what the future of application development will hopefully be like using a relational system. To program relationally you not only have to think in "terms of sets" (the declarative thing:) but you have to think "in terms of type" (the relation thing). Sql essentially doesn't concern itself with variables and types. They are essentially viewed as foreign thingies found in net. But in a relational system they are every bit as significant as understanding how to write an outer join. The following example illustrates the relational approach using the Dataphor system (which can play nicely with Sql Server ☺) create session table Products { product_key:Integer, part_no: String, options: list(String), //options is defined as a list type, not a string. price: Money, key{product_key} }; All tables, like numbers, are variables with an explicit 'table' type, the column names and their datatypes. And like integers they can be 'assigned' values. Tables are composed of 'rows'. For each row options is input as a 'list' of type string ({'<string>','<string>','<string>'..}). Products:= table { row{11 product_key, 'ASP-20J' part_no, {'Wiz Bang', 'Hopla Enhancer'} options, $10.00 price}, row{12, 'ASP-20R', {'Widget Holder','Wiz Bang', 'Hopla Enhancer'}, $12.00} }; create session table Options { option_key: Integer, product_key: Integer, option: String, key{option_key}, reference OptiontoProduct {product_key} references Products {product_key} }; Options:= table { row{5 option_key, 11 product_key, 'Wiz Bang' option}, row{6, 11, 'Hopla Enhancer'}, row{7, 12, 'Wiz Bang'}, row{8, 12, 'Hopla Enhancer'} }; Here are easy ways to find the missing product from the Options table. Using row in table. First a table of numbers (Numbers) with a single column N (from 0-10K) is used to create a row for each element in the options list bringing along the other Products columns. The row is constructed from this table to see if it's not in options. select ( Products times //Like an sql cross join. Numbers where N<options.Count() //create a row for each element in the list. {product_key,options[N] option} ) where not ( row{product_key product_key,option option} in (Options {product_key,option}) ) ; Using relational division. Because tables (table expressions) are variables one table can be tested to see if it's contained in another. select ( Products times Numbers where N<options.Count() {product_key,options[N] option} ) where not ( table{row{product_key product_key,option option}} <= (Options {product_key,option}) ) ; Using a left join. Test whether the table on the right has a matching row. select ( Products times Numbers where N<options.Count() {product_key,options[N] option} ) left join Options include rowexists //A special Boolean column for whether there is a match. where not rowexists {product_key,option} ; Instead of inputting a list directly, a delimited string can be converted to a list when input. Products:= table{ row{11 product_key, 'ASP-20J' part_no, ('Wiz Bang, Hopla Enhancer '.Split()) options, $10.00 price}, row{12, 'ASP-20R', (' Widget Holder ,Wiz Bang, Hopla Enhancer'.Split()), $12.00} }; Options:= table{ row{5 option_key, 11 product_key, 'Wiz Bang' option}, row{6, 11, 'Hopla Enhancer'}, row{7, 12, 'Wiz Bang'}, row{8, 12, 'Hopla Enhancer'} }; The queries are the same as above except for trimming each element of the list. select ( Products times Numbers where N<options.Count() {product_key,options[N].Trim() option} ) where not ( row{product_key product_key,option option} in (Options {product_key,option}) ) ; select ( Products times Numbers where N<options.Count() {product_key,options[N].Trim() option} ) where not ( table{row{product_key product_key,option option}} <= (Options {product_key,option}) ) ; select ( Products times Numbers where N<options.Count() {product_key,options[N].Trim() option} ) left join Options include rowexists where not rowexists {product_key,option}; All queries produce a result of: product_key option ----------- ------------- 12 Widget Holder Types eliminate violations of normal forms much like education eliminates ignorance ☺ Dataphor is a RAD tool, R(elational) A(ccelerator) D(evelopment). Visit dataphor at: www.dataphor.org
Dataphor SQL RAC (Relational Application Companion)
A site of hope for those looking for a true relational database system
- a one-one requirement constraint with dataphor (1)
- anatomy of sql server part I - what is a stored procedure (1)
- anatomy of sql server part II - the unit test as part of the database (1)
- anatomy of sql server part III - what does deferred name resolution really mean (1)
- censoring sql posts (1)
- creating an opposite constraint in dataphor (1)
- dataphor (2)
- Dataphor (7)
- dataphor # 13 a table as a parameter (1)
- dataphor - download and start working with it (1)
- dataphor - fixed sized word segments (1)
- dataphor # 10 sql mythology (1)
- dataphor # 11 string differences (1)
- dataphor # 12 trimming a string (1)
- dataphor # 14 sql the meaning of Update..From (1)
- dataphor # 15 views with substance (1)
- dataphor # 16 inclusive vs exclusive solutions (1)
- dataphor # 17 a visual look at ranking queries (1)
- dataphor # 18 data scrubbing using lists (1)
- dataphor # 19 create intervals over strings (1)
- dataphor # 20 browsing an sql window (1)
- dataphor # 21 an example of relational division (1)
- dataphor # 22 reusable procedures (1)
- dataphor # 23 repley to Michel (1)
- dataphor # 24 basics of the table type (1)
- dataphor # 25 extending the dense rank function (1)
- dataphor # 26 query a hierarchy with explode (1)
- dataphor # 27 combine strings with Split and Concat (1)
- dataphor # 28 constants and variables or sql and D4 (1)
- dataphor # 29 another example of relational division (1)
- dataphor #1 introduction (1)
- dataphor #2 splitting strings (1)
- dataphor #3 string concatenation (1)
- dataphor #4 comment (1)
- dataphor #5 comment (1)
- dataphor #6 formal definition (1)
- dataphor #7 sql: table this (1)
- dataphor #8 list to table (1)
- dataphor #9 table constraints (1)
- dataphor creating lists in a query (1)
- extracting numbers from a string with dataphor (1)
- jeff modens dynamic crosstabs for sql server (1)
- linq to sql the what and why (1)
- linq to sql as a window of opportunity to sql users (1)
- linq to sql should be important to sql users (1)
- linq to sql vs. older 4GL attempts (1)
- listing missing table item (1)
- Multiple cascade paths to the same table (1)
- RAC (4)
- RAC #1 comment (1)
- RAC #2 example (1)
- RAC #3 finding the Nth number in a string (1)
- RAC #4 Sql Server 2005 ranking functions vs. Rac ranking (1)
- sorting a delimited string by its numerical string parts (1)
- sql an example of extreme implicit conversions (1)
- sql can't handle complicated cascading updates (1)
- sql CTE should be a variable not a value (1)
- sql dense rank for identifying consecutive runs (1)
- sql is there really a table variable (1)
- sql ranking functions explained by relational types (1)
- sql server triggers are best set based (1)
- sql the idea of using substring to simulate lists (1)
- sql the undefined trigger in Sql Server (1)
- sql vs relational on tables (1)
- sql what the sql CTE covers up (1)
- types and procedures (1)
Showing posts with label listing missing table item. Show all posts
Showing posts with label listing missing table item. Show all posts
Sunday, November 23, 2008
Listing Missing Table Item
Subscribe to:
Posts (Atom)
