How to use SQL for inference _mssql2008

Source: Internet
Author: User
Tags how to use sql

Database environment: SQL SERVER 2008r2

There are the following requirements:
Baker, Cooper, Fletcher, Miller and Smith live on different floors of a house.
Baker doesn't live on the top floor. Cooper doesn't live on the bottom.
Fletcher neither the top nor the bottom. Miller lives taller than Cooper.
Smith's floor and Fletcher are not adjacent.
Fletcher's floors are not adjacent to Cooper's.
Write it in SQL.

Ideas for solving problems:
The first is to achieve the arrangement of all the people living floors, and then put the conditions into the set. How to implement permutations and combinations,

1. Basic Data Preparation
--Prepare basic data, using a, B, C, D, and E to represent Baker, Cooper, Fletcher, Miller and Smith

CREATE TABLE TTB
 (
  subname VARCHAR (1),
  realname VARCHAR ()
 )
INSERT into TTB
VALUES (' A ', ' Baker '),
  (' B ', ' Cooper '),
  (' C ', ' Fletcher '), ('
  D ', ' Miller '),
  (' E ', ' Smith ')

2. Generate all possible permutations and combinations
--Generate all permutations and combinations of a, B, C, D, E

With x0 as
   (select CONVERT (VARCHAR, ' A ") as HID
    UNION all
    SELECT convert (VARCHAR, ' B ') as HID
    UNI On all
    Select CONVERT (VARCHAR (a), ' C ') as HID
    UNION ALL
    Select CONVERT (VARCHAR, ' D ') as HID
    Union AL L
    Select CONVERT (VARCHAR (), ' E ')
    as HID),
  x1 as
   (select hid from  x0
    WHERE LEN ( HID) <= 5
    UNION all
    SELECT CONVERT (VARCHAR (Ten), A.hid + B.hid) as hid
    from  x0 a
      INNER JOIN x1 B on CHARINDEX (A.hid, B.hid, 1) = 0
    )
 SELECT hid as name to
 #tt from
 x1
 WHERE LEN (HID) = 5
   
    order by HID
   

3. Add conditions to find the floor arrangements to meet the requirements

With the X2
   as (SELECT
    name
    from #tt WHERE SUBSTRING (name, 5, 1) <> ' A '--baker not be in the top and
      SUBSTRING ( Name, 1, 1) <> ' B '--cooper do not live with the underlying and
      (SUBSTRING (name, 1, 1) <> ' C ' and
        SUBSTRING (name, 5, 1) <> ' C '--fletcher Neither the top nor the bottom
       ) and
      name like '%b%d% '--miller live higher than the Cooper and
      name not like '%ce% ' and ' '%ec% '--smith floors and Fletcher are not adjacent to and name not like '
      %bc% ' and ', ' and ' '%cb% '--fletcher live floors and Cooper
    , 
   x3--generates floor numbers as
   (SELECT number as ID,
      SUBSTRING (x2.name, number, 1) as name
    from  master.dbo.spt_ Values
      INNER JOIN x2 on 1 = 1
    WHERE type = ' P ' and number
      <= 5 and number
      >= 1
 Selec T a.id as Floor,
   B.realname as name from
 X3 a
   INNER JOIN ttb b in b.subname = A.name ORDER by
 ID

The floor arrangement is as follows:

Through the introduction of the above code, I hope to help you learn.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.