This article is based on the thread: microsoft.public.sqlserver.programming Monday, October 22, 2007 "select rows that contain both arg1 = 'x' and arg1 = 'y'" http://tinyurl.com/386lbx >I need to select only rows where the name contains x and y. I do not want >to get rows that only contain x or contain x, z, or contain y, z. I only >want rows where the name has an x an a y (they can have a z but they must >have an x and a y). Forget QA or SSMS. Just draw a picture. You use the term row. So draw a row. row{aName as sName, acode as code} Now you want people who have code x and y. These are two rows. So draw them. Use 'burns' for the person. row{'burns' as sName, 'x' as code} row{'burns' as sName, 'y' as code} Now what do you call two rows together? How about calling it a table ☺ . Draw it. table { row{'burns' as sName, 'x' as code} row{'burns' as sName, 'y' as code} } Now if this table, made just for 'burns', is in your #tmp1 table then 'burns' is a guy you want. In other words, if both rows (one for x, one for y) are in #tmp1 then you have a hit. Super simple ☺ . Now in QA or SSMS do this. declare @x int, @y int set @x=2 set @y=3 if @x<=@y print 'Yes' else print 'No' No explanation necessary. Super simple. Now take the same idea of comparing two integers and extend it to comparing two tables. if table { row{'burns' as sName, 'x' as code} row{'burns' as sName, 'y' as code} } <= #tmp1 print 'Yes' else 'No' In other words, does each row for 'burns' occur in #tmp1? 'Burns' can have codes in #tmp1 in addition to x and y (ie. 2<=3) So 'burns' has to have at least a row for x and a row for y in #tmp1. If in #tmp1 'burns' has only an 'x' or only a 'y' no matter what other codes he has that's no good (2<=1). In the case above you will see 'Yes' printed since the comparison is true. This whole scenario is referred to as relational division in database terminology. But these simple ideas are obscured by sql because you can't draw a picture of a row, nor a table nor does sql understand comparing tables like integers. So instead you're left with grouping and counting, joins, intersects, existential queries and whatnot all trying to express a simple idea yet at the same time obscuring it. Now in a query you want to substitute all the unique names from #tmp1 into our little table so for each person we can test the comparison with #tmp1. What would such a query look like? select select distinct sName as aPerson from #tmp1 where -- draw a table with two rows for each aPerson -- the 1st column has a value aPerson and -- the column is named 'sName'. The 2nd column is -- called 'code'. The column names and datatypes -- are the same as in #tmp1. table { row{aPerson as sName, 'x' as code}, row{aPerson as sName, 'y' as code} } -- Compare the tables. <= -- Form a table from #tmp1 of rows belonging to the -- aPerson above. (Tmp1 where sName=aPerson); Now this won't quite work in sql no matter where you execute it ☺ . But what would a query really look like that will work with #tmp1. Here it is. And it really is almost self-explanatory. And it works in the D4 language! :) (Tmp1 is a table same as #tmp1 stored in an Sql Server 2005 database). select Tmp1 {sName aPerson} where table { row{aPerson sName, 'x' code}, row{aPerson sName, 'y' code} } <= (Tmp1 where sName=aPerson); aPerson ------- burns jones smith Now this is what MS should be doing. Sql is a language of choice for some things. But it certainly is not the choice language for others.
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 dataphor # 29 another example of relational division. Show all posts
Showing posts with label dataphor # 29 another example of relational division. Show all posts
Monday, October 22, 2007
Sql - Really simple division
Subscribe to:
Posts (Atom)
