|

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

  • |

    Rank a Place Visited By a Customer- Microsoft SQL Server – T-Transact

    This is a short guide to getting ranking or rating from Microsoft Sql Server customers. Ranking is taken by checking how many times a customer has visited a particular country divided by the total number of travels the customer has made within the database  

        Want more information like this?

  • Visual Explanation illustration and presentation of SQL JOINS query statements

    SQL Joins can sometimes be a bit confusing, particularly if you a newbie. I have below 2 visual representations which should help enhance your visual understanding of SQL JOINS First is from From CodeProject, by C L Moffatt You can check full explanation here: Sql Joins visually explained   2. Second was from Duke University….

  • The sp_execute_external_script (Transact-SQL) Definition And Arguments

    The sp_execute_external_script  system stored procedure is the stored procedure which invokes the external environment to run an external script (either Python or R ) in a T-SQL script. This is quite highlights of the sp_execute_external_script stored and some short notes on it.   let’s quickly look at the structure of the stored procedure sp_execute_external_script @language = N’language,…

  • | |

    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…

  • | |

    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…

  • |

    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)…

Leave a Reply

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