Slicer to include 2 columns at once - sql-server

So I have a view like this in SQL Server which I'm using to build a dashboard in Power BI:
ID | Name | IsRegional | IsFederal | Department | ...
1 | John | Yes | No | Paris | ...
2 | Mike | No | Yes | Brussels | ...
3 | Bill | No | Yes | Berlin | ...
4 | Bart | Yes | Yes | Berlin | ...
5 | Suzy | Yes | No | New York | ...
Currently I have 2 slicers in PowerBi that say "Is Regional: Yes/no" and "Is Federal: Yes/no". I want to make one slicer of this saying "Type: Federal/Regional"
My idea was to add a column TYPE in the view that says
WHEN IsRegional = 'Yes' THEN 'Regional'
WHEN IsFederal = 'Yes' THEN 'Federal'
ELSE 'None'
and then use the new column for the slicer
ID | Name | IsRegional | IsFederal | Type | Department | ...
1 | John | Yes | No | Regional | Paris | ...
2 | Mike | No | Yes | Federal | Brussels | ...
3 | Bill | No | Yes | Federal | Berlin | ...
4 | Bart | Yes | Yes | Regional | Berlin | ...
5 | Suzy | Yes | No | Regional | New York | ...
However, this creates an issue with record 4 where the type can be both. I would like the slicer to include row 4 when I have Federal selected as type (since it's both federal as Regional). Is there a way to solve this issue so I can use the single slicer? I would rather not add a 4th option saying "Both" to the slicer because I'm sure people will look over that one.

Just change your CASE expression slightly:
CASE WHEN IsRegional = 'Yes' AND IsFederal = 'Yes' THEN 'Both'
WHEN IsRegional = 'Yes' THEN 'Region'
WHEN IsFederal = 'Yes' THEN 'Federal'
ELSE 'None'
END

Related

I can't apply multiple conditions in this SQL statement

I don't know why it doesn't give me the answer for students enrolled in Database Systems but not in Operating System Design.
select student.snum, student.sname, enrolled.cname
-> from enrolled
-> inner join student ON enrolled.snum = student.snum
-> where enrolled.cname="Database Systems" AND enrolled.cname<>"Operating System Design";`
+-----------+--------------------+------------------+
| snum | sname | cname |
+-----------+--------------------+------------------+
| 112348546 | Joseph Thompson | Database Systems |
| 115987938 | Christopher Garcia | Database Systems |
| 348121549 | Paul Hall | Database Systems |
| 322654189 | Lisa Walker | Database Systems |
| 552455318 | Ana Lopez | Database Systems |
+-----------+--------------------+------------------+
My student table.
+-----------+--------------------+------------------------+-------+------+
| snum | sname | major | level | age |
+-----------+--------------------+------------------------+-------+------+
| 51135593 | Maria White | English | SR | 21 |
| 60839453 | Charles Harris | Architecture | SR | 22 |
| 99354543 | Susan Martin | Law | JR | 20 |
| 112348546 | Joseph Thompson | Computer Science | SO | 19 |
| 115987938 | Christopher Garcia | Computer Science | JR | 20 |
| 132977562 | Angela Martinez | History | SR | 20 |
| 269734834 | Thomas Robinson | Psychology | SO | 18 |
| 280158572 | Margaret Clark | Animal Science | FR | 18 |
| 301221823 | Juan Rodriguez | Psychology | JR | 20 |
| 318548912 | Dorthy Lewis | Finance | FR | 18 |
| 320874981 | Daniel Lee | Electrical Engineering | FR | 17 |
| 322654189 | Lisa Walker | Computer Science | SO | 17 |
| 348121549 | Paul Hall | Computer Science | JR | 18 |
| 351565322 | Nancy Allen | Accounting | JR | 19 |
| 451519864 | Mark Young | Finance | FR | 18 |
| 455798411 | Luis Hernandez | Electrical Engineering | FR | 17 |
| 462156489 | Donald King | Mechanical Engineering | SO | 19 |
| 550156548 | George Wright | Education | SR | 21 |
| 552455318 | Ana Lopez | Computer Engineering | SR | 19 |
| 556784565 | Kenneth Hill | Civil Engineering | SR | 21 |
| 567354612 | Karen Scott | Computer Engineering | FR | 18 |
| 573284895 | Steven Green | Kinesiology | SO | 19 |
| 574489456 | Betty Adams | Economics | JR | 20 |
| 578875478 | Edward Baker | Veterinary Medicine | SR | 21 |
+-----------+--------------------+------------------------+-------+------+
My enrolled table
+-----------+----------------------------+
| snum | cname |
+-----------+----------------------------+
| 112348546 | Database Systems |
| 115987938 | Database Systems |
| 348121549 | Database Systems |
| 322654189 | Database Systems |
| 552455318 | Database Systems |
| 455798411 | Operating System Design |
| 552455318 | Operating System Design |
| 567354612 | Operating System Design |
| 112348546 | Operating System Design |
| 115987938 | Operating System Design |
| 322654189 | Operating System Design |
| 567354612 | Data Structures |
| 552455318 | Communication Networks |
| 455798411 | Optical Electronics |
| 301221823 | Perception |
| 301221823 | Social Cognition |
| 301221823 | American Political Parties |
| 556784565 | Air Quality Engineering |
| 99354543 | Patent Law |
| 574489456 | Urban Economics |
+-----------+----------------------------+
You need to use the NOT EXISTS as follows:
select s.snum, s.sname, e.cname
from enrolled e
inner join student s ON e.snum = s.snum
where e.cname='Database Systems'
AND not exists
(select 1 from enrolled ee
where ee.snum = e.snum and e.cname = 'Operating System Design');
Compare string with single quotation mark(' ') than double quotation(" "). Your code seems ok to me.
Remember:
Single quotes are for strings.
Double quotes are for tables names and column names.
select student.snum, student.sname, enrolled.cname
from enrolled
inner join student ON enrolled.snum = student.snum
where enrolled.cname='Database Systems'
AND enrolled.cname<>'Operating System Design';
or try this
select student.snum, student.sname, enrolled.cname
from enrolled
inner join student ON enrolled.snum = student.snum
where enrolled.cname='Database Systems'

Designing a database for categories and subcategories

Basically I'm trying to figure out how Amazon architected their book section. Check out Amazon's book page here (https://www.amazon.com/s/ref=lp_2_ex_n_1?rh=n%3A283155&bbn=283155&ie=UTF8&qid=1522817105).
We are given several main categories: Arts & Photography, Biographies & Memoirs, etc.
If I click on Biographies & Memoirs for example, I'm lead to a series of sub categories. I.E. Biographies & Memoirs > Historical > Asia > Japan
There are repeating sub-category names for example: History > Asia > Japan
How can I map this kind of information so that it is scalable?
Below is the wrong way to do it...?
Categories table
+----+-----------------------+-----------+
| id | name | parent_id |
+----+-----------------------+-----------+
| 1 | Biographies & Memoirs | null |
| 2 | Historical | 1 |
| 3 | Asia | 2 |
| 4 | History | null |
| 5 | Asia | 4 |
| 6 | Japan | 5 |
| 7 | Japan | 3 |
+----+-----------------------+-----------+
Books
+----+-------------------------------------+----------+
| id | name | category |
+----+-------------------------------------+----------+
| 1 | The Lone Samurai | 7 |
| 2 | The Human Tradition in Modern Japan | 7 |
| 3 | Okinawa: The Last Battle | 6 |
+----+-------------------------------------+----------+
Authors
+----+---------------+----------+
| id | firstname | lastname |
+----+---------------+----------+
| 1 | James M. | Burns |
| 2 | Roy E. | Appleman |
| 3 | Russell A. | Gugeler |
| 4 | John | Stevens |
| 5 | William Scott | Wilson |
| 6 | Anne | Walthall |
+----+---------------+----------+
Authors to books (Many to many)
+---------+-----------+
| book_id | author_id |
+---------+-----------+
| 3 | 1 |
| 3 | 2 |
| 3 | 3 |
| 3 | 4 |
| 1 | 5 |
| 2 | 6 |
+---------+-----------+

How to select multiple rows with same foreignKey, but one column has all values in a set

So I'm currently working on a stored procedure that returns a table comprising of data about the user (user ID) and performance metrics (field, metric_obtained). I was originally doing it so I would return all data back, but I was thinking it would be more efficient to return only people who meet the minimum to be recognized. I've already filtered them out based on minimum requirement, but the thing is they can be qualified based on a combination of things, so if I have 3 metrics A, B, and C, one recognition can be for A and B, or another is just C. I'm also not limited to a max of 3 or this wouldn't be a problem.
My tables look like this:
|Employee | Metric | Obtained|
|_________|________|_________|
| John | Email | .98 |
| Sue | Email | .99 |
| Sue | Phone | .82 |
| Larry | Email | .93 |
| Larry | Phone | .83 |
| Jess | Phone | .9 |
| Jess | Email | .94 |
| Bob | Phone | .99 |
So if I need to get back both Phone AND Email my results would look like this:
|Employee | Metric | Obtained|
|_________|________|_________|
| Sue | Email | .99 |
| Sue | Phone | .82 |
| Larry | Email | .93 |
| Larry | Phone | .83 |
| Jess | Phone | .9 |
| Jess | Email | .94 |
Like I said, this would be easy if I had a guaranteed number of metrics, but I don't. Any thoughts?

How to Write Conditional Statement in SQL Server

I am having a logic issue in relation to querying an SQL database. I need to exclude 3 different categories and any item that is included in those categories; however, if an item under one of those categories meets the criteria for another category I need to keep said item.
This is an example output I will get after querying the database at its current version:
ExampleDB | item_num | pro_type | area | description
1 | 45KX-76Y | FLCM | Finished | coil8x
2 | 68WO-93H | FLCL | Similar | y45Kx
3 | 05RH-27N | FLDR | Finished | KH72n
4 | 84OH-95W | FLEP | Final | tar5x
5 | 81RS-67F | FLEP | Final | tar7x
6 | 48YU-40Q | FLCM | Final | bile6
7 | 19VB-89S | FLDR | Warranty | exp380
8 | 76CS-01U | FLCL | Gator | low5
9 | 28OC-08Z | FLCM | Redo | coil34Y
item_num and description are in a table together, and pro_type and area are in 2 separate tables--a total of 3 tables to pull data from.
I need to construct a query that will not pull back any item_num where area is equal to: Finished, Final, and Redo; but I also need to pull in any item_num that meets the type criteria: FLCM and FLEP. In the end my query should look like this:
ExampleDB | item_num | pro_type | area | description
1 | 45KX-76Y | FLCM | Finished | coil8x
2 | 68WO-93H | FLCL | Similar | y45Kx
3 | 84OH-95W | FLEP | Final | tar5x
4 | 81RS-67F | FLEP | Final | tar7x
5 | 19VB-89S | FLDR | Warranty | exp380
6 | 76CS-01U | FLCL | Gator | low5
7 | 28OC-08Z | FLCM | Redo | coil34Y
Try this:
select * from table
join...
where area not in('finished', 'final', 'redo') or type in('flcm', 'flep')
Are you looking for something like
SELECT *
FROM Table_1
JOIN Table_ProType ON Table_1.whatnot = Table_ProType.whatnot
JOIN Table_Area ON Table_1.whatnot = Table_Area.whatnot
WHERE Table.area NOT IN ('Finished','Final','Redo') OR ProType.pro_type IN ('FLCM','FLEP')
Giving the names of the three tables and the joining criteria will help me improve the answer.

Database design for download presets

Newbie with databases, I would like some advise please..
I have agencies who can download photo's.
Standard each agency can download "medium" & "large" photos.
Now from their account page I would like them to make extra custom presets and manage those.
I looked in the database of some blog software how they handle categories and wrapped my head around this example. Is this the right approach?
Cheers
agency 1 has preset "medium" & "large"
agency 2 has preset "medium", "large" & "Bill custom"
-----------
| presets |
-----------------------------------------------
| preset_id | preset_name | preset_dimensions |
-----------------------------------------------
| 1 | medium | 800x600 |
| 2 | large | 3000x2000 |
| 3 | Bill custom | 640x420 |
-----------------------------------------------
----------------
| preset_assoc |
------------------------------------------------------------
| presassoc_id | presassoc_preset_id | presassoc_agency_id |
------------------------------------------------------------
| 1 | 1 | 1 |
| 2 | 2 | 1 |
| 3 | 1 | 2 |
| 4 | 2 | 2 |
| 5 | 3 | 2 |
------------------------------------------------------------
------------
| agencies |
---------------------------
| agency_id | agency_name |
---------------------------
| 1 | Joe ltd |
| 2 | Bill inc |
---------------------------
The approach is right. Because you have NxN relation (1 agency can have multiple presets, and the same preset could be used by multiple agencies) you need to have a joining table. The only questionable thing is that preset_assoc doesn't have to have presassoc_id because the other 2 columns could be used as a combined primary key.

Resources