# Another SQL Puzzle

**URL:** <https://ifnotnil.com/t/another-sql-puzzle/1953>\
**Category:** Other\
**Created:** [31 July 2021 21:37 UTC](https://ifnotnil.com/t/another-sql-puzzle/1953 "2021-07-31T21:37:48Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![DaveS](https://ifnotnil.com/user_avatar/ifnotnil.com/daves/32/79_2.png) [@DaveS](https://ifnotnil.com/u/DaveS)\
**Post date:** [31 July 2021 21:37 UTC](https://ifnotnil.com/t/another-sql-puzzle/1953/1 "2021-07-31T21:37:48Z")

</div>

Table Schema

```auto
CREATE TABLE GUI ( 
win_id Integer NOT NULL DEFAULT 0,
ctrl_id Integer NOT NULL DEFAULT 0,
field Text COLLATE NOCASE DEFAULT '',
data Text COLLATE NOCASE DEFAULT '')

```

Sample Data

```auto
"win_id","ctrl_id","field","data"

1,1,"NAME","Button1"
1,1,"SUPER","PushButton"
1,2,"NAME","Button2"
1,2,"SUPER","PushButton"

```

```auto
SELECT name,super FROM GUI where win_ID=1

```

For some reason I can’t get my brain to grasp this… I know it is Unions and Joins

The desired result would be  
Button1 , Pushbutton  
Button2 , Pushbutton

---

<div class="post-metadata">

**Author:** ![DaveS](https://ifnotnil.com/user_avatar/ifnotnil.com/daves/32/79_2.png) [@DaveS](https://ifnotnil.com/u/DaveS)\
**Post date:** [31 July 2021 21:52 UTC](https://ifnotnil.com/t/another-sql-puzzle/1953/2 "2021-07-31T21:52:36Z")

</div>

Solved 🙂

```auto
SELECT DISTINCT a.data AS nm, b.data AS super, c.data AS idx
  FROM (SELECT * FROM GUI WHERE field = "NAME") a,
       (SELECT * FROM GUI WHERE field = "SUPER") b,
       (SELECT * FROM GUI WHERE field = "INDEX") c
 WHERE a.ctrl_id = b.ctrl_id
   AND a.win_id = b.win_id
   AND ( a.ctrl_id = c.ctrl_ID OR b.ctrl_id = c.ctrl_id)
   AND a.win_id = 1

```

---

<div class="post-metadata">

**Author:** ![JeffT](https://ifnotnil.com/letter_avatar_proxy/v4/letter/j/e95f7d/32.png) [@JeffT](https://ifnotnil.com/u/JeffT)\
**Post date:** [1 August 2021 13:04 UTC](https://ifnotnil.com/t/another-sql-puzzle/1953/3 "2021-08-01T13:04:41Z")

</div>

```auto
select a.data, b.data 
from gui a, gui b
where a.win_id = b.win_id
and a.ctrl_id = b.ctrl_id
and a.field = 'NAME' and b.field = 'SUPER';

```

But since the super is a property of a control, and all controls have one, why not just have a ‘SUPER’ property in the GUI table?  
“win\_id”,“ctrl\_id”,“field”,“data”,“super”

---

<div class="post-metadata">

**Author:** ![DaveS](https://ifnotnil.com/user_avatar/ifnotnil.com/daves/32/79_2.png) [@DaveS](https://ifnotnil.com/u/DaveS)\
**Post date:** [1 August 2021 13:54 UTC](https://ifnotnil.com/t/another-sql-puzzle/1953/4 "2021-08-01T13:54:54Z")

</div>

It has to do with the source of the original data… would if I could but I can’t 🙂
