|

Splitting Values in one Column to Multiple Columns using CASE Expression Statement SQL Server

Sometimes, you would want to split values in a column into separate rows  in SQL server.  This is a simple guide.

Lets check the fields in the table we will be working with.

Let’s check the result:

sql-server-case-statement-check-result1

 

We can see that the itemType field has different values wheres as the price and quantity all have the same values.
So even when you do a group by you still get a long list of each ordernumber falling on different rows instead of a single row as we intended with the group by

Let’s check the result:

intended-group-by-sql-server

Now lets use a case statement to flag the item type as 1 when the specific item type we are catering for is available and 2 when it is not available.
Let’s put the result in a temporary table which will we call #itemTypes

 

Now lets see which item type was present for each of our order ids , by taking the MAX number of each flag for each item type (those we flagged in the steps above)
We will put the result in a temporary table which we will call #itemTypesFlags

 

Data quality check if the flags have been assigned appropriately

Let’s check the result:

data-quality-check-case-expression-sql-server

Now let’s assign the actual item-type names to the flags where the flag has one and 0 if that item-type was not bought by the customer.
We will put that in a temporary table which we will call #itemTypesFlagsNames

 

Now lets get our order ids and the item types all on one row

 

Let’s check the final result:

result-sql-server-case-expression-split-columns-into-rows

Get full code here:

Hope it helps you 🙂

Want more information like this?

Similar Posts

  • |

    Creating Oracle PL/SQL Tables for Intercollegiate Athletic Database – Data Warehousing

    Creating Oracle  tables for Intercollegiate Athletic Database. Part of Assignment one of Data Warehousing for Business Intelligence on Coursera

    After creating the tables lets insert values

      In case you want to change a constraint on a field, you can use a syntax examples as the following. ALTER TABLE FACILITY ADD CONSTRAINT UNQ_FAC UNIQUE (FACNAME)…

  • How to Manually Start Stop and Restart MySql Server on Windows PC

    When installing MySql server  package, you can instruct the installation to configure MySql server to start automatically on each windows start up. However, if you want to reserve resources whilst you will not be working with the MySql server and hence you do not want the server to automatically start up on every boot of…

  • | |

    Using Subqueries To Join 2 More Tables Which Do Not Have Unique Primary and Foreign Keys

    Using Subqueries to join 2 more tables which do not have distinct values in a column We want to find the number of Flight bookings by Country, number of Car bookings by Country and the number of Hotel bookings by Country all appearing in one table with same country on one row. We want to…

  • |

    Pyodbc Sqlalchemy Python 2 and 3 SQL Server Native Client 11.0

    This is a sample code to use Python to connect to SQL Server Native Client.

    Want more information like this?

  • |

    SQL Statement Query Clause Evaluation Order – Which Part of SQL Statements Execute First and in What Order?

    Which Part of SQL Statements Execute First and in What Order. It is very important to understand which parts of the SQL Statement executes first and which part comes next until full completion. The diagram below gives an overview of the execution order. The steps in which the SQL statement is executed are as below:…

  • | |

    Teradata Viewpoint – SQL Scratchpad – Writing Queries Dillard’s Department Store Database

    Analysing ‘Big Data’ from a database with real world data requires retrieving data from the database by writing relevant queries. As part of “Managing Big Data with MySQL” by Duke University on Cousera, I dealt with real world data that comprise hundreds to millions of entries/rows. This is the database of Dillard’s Department stores, specifically, the UA_DILLARDS that contains…

Leave a Reply

Your email address will not be published. Required fields are marked *