Facebook Twitter Sign in | Join
Pushing database changes needn't be hard work with SQL Compare
Getting Started with ASP.NET MVC - Part 5: How to do programming with razor syntax
First Time? You can support us by signing up. It takes only 5 seconds. Click here to sign up. If you already have an account, click here to login.

TSQL Challenge 15 - matching data between rows and columns

This challenge is derived from a realistic reporting scenario that requires the transformation of rows into columns. This is commonly known as a matrix, pivot, or crosstab query. This challenge involves two primary tables @Row and @Cols. The objective will be to create a matrix result set. Before I explain the rest of the requirements, let me show you the sample tables. Column Table:

Col
-----------
1
2
3
4
5
6
7
8
9

Row Table:

Row
-----------
100
104
101
99
77
20
10

The final output should look like this:

Row         1    2    3    4    5    6    7    8    9
----------- ---- ---- ---- ---- ---- ---- ---- ---- ----
10          x    x              x                   
20          x    x         x    x                   
77          x                             x         
99          x         x                             x
100         x    x         x    x                   
101         x                                       
104         x    x         x                   x    

The challenge is to mark a coordinate, with a value of X, if and only if the row value is divisible by the col value, i.e. it has a modulo of zero. The additional requirements are: the final query must work with random row values and the pivot operator should be used.

Sample Data

Use the following script to generate the sample data needed for this challenge

DECLARE @Cols TABLE(Col INT);

INSERT INTO @Cols VALUES (1);
INSERT INTO @Cols VALUES (2);
INSERT INTO @Cols VALUES (3);
INSERT INTO @Cols VALUES (4);
INSERT INTO @Cols VALUES (5);
INSERT INTO @Cols VALUES (6);
INSERT INTO @Cols VALUES (7);
INSERT INTO @Cols VALUES (8);
INSERT INTO @Cols VALUES (9);

DECLARE @Rows TABLE(Row INT);

INSERT INTO @Rows VALUES (100);
INSERT INTO @Rows VALUES (104);
INSERT INTO @Rows VALUES (101);
INSERT INTO @Rows VALUES (99);
INSERT INTO @Rows VALUES (77);
INSERT INTO @Rows VALUES (20);
INSERT INTO @Rows VALUES (10);

Challenge Requirements Summary

  1. Only coordinates where the column value modulo the row value equals zero should be marked, with an "X"
  2. Number of rows in the table is not fixed. The query should work with variable number of rows in the table.

Notes

  1. Read the Submission Guidelines and make sure that your solution follows them.
  2. The solution should work on SQL Server 2005, 2008 or later versions
  3. Use this forum for any questions related to TSQL Challenge #15

About the Authors.


Share

Comments

# re: TSQL Challenge 15 - matching data between rows and columns

Tuesday, October 20, 2009 4:17 AM by Parth Patel

Can we use temporary table instead of table variable?

like

CREATE TABLE #Cols(Col INT);

INSERT INTO #Cols VALUES (1);

INSERT INTO #Cols VALUES (2);

INSERT INTO #Cols VALUES (3);

INSERT INTO #Cols VALUES (4);

INSERT INTO #Cols VALUES (5);

INSERT INTO #Cols VALUES (6);

INSERT INTO #Cols VALUES (7);

INSERT INTO #Cols VALUES (8);

INSERT INTO #Cols VALUES (9);

CREATE TABLE #Rows(Row INT);

INSERT INTO #Rows VALUES (100);

INSERT INTO #Rows VALUES (104);

INSERT INTO #Rows VALUES (101);

INSERT INTO #Rows VALUES (99);

INSERT INTO #Rows VALUES (77);

INSERT INTO #Rows VALUES (20);

INSERT INTO #Rows VALUES (10);


# re: TSQL Challenge 15 - matching data between rows and columns

Tuesday, October 20, 2009 11:30 PM by Niladri Biswas

What do you want to achieve by this ? (:


# re: TSQL Challenge 15 - matching data between rows and columns

Saturday, October 24, 2009 6:20 AM by Sergejack

A good thing : making dynamic queries able to call the two tables without recreating them.


# re: TSQL Challenge 15 - matching data between rows and columns

Saturday, October 24, 2009 7:56 AM by Divya Agrawal

The number of columns would also be variable?


# re: TSQL Challenge 15 - matching data between rows and columns

Sunday, October 25, 2009 10:30 AM by Jacob Sebastian

Number of columns are fixed.


# re: TSQL Challenge 15 - matching data between rows and columns

Monday, October 26, 2009 12:22 AM by Parth Patel

Well, assuming the fact that Number of columns are fixed (and thus one can use column name manually), I think solution is quite easy.


# re: TSQL Challenge 15 - matching data between rows and columns

Monday, October 26, 2009 8:22 AM by Vijaya Krishna Kadiyala

Although the columns are fixed, but we would like to see what best approach is used to convert them into column headings!!!!


# re: TSQL Challenge 15 - matching data between rows and columns

Tuesday, October 27, 2009 12:18 AM by Parth Patel

So, the number of columns are fixed(9) but values can be different. Am I right?


# re: TSQL Challenge 15 - matching data between rows and columns

Sunday, January 03, 2010 10:08 PM by Jacob Sebastian

yes, you are right.


# re: TSQL Challenge 15 - matching data between rows and columns

Thursday, January 21, 2010 1:50 PM by Madhangi

I have a question about the results, the query submitted by the first winner. The query doesnt consider the @Cols tables at all. Technically the columns are hardcoded. So he saves a lot on the join.

Is that acceptable?

Regards,

Maddy


# re: TSQL Challenge 15 - matching data between rows and columns

Thursday, January 21, 2010 9:52 PM by Jacob Sebastian

@Madhangi,

This is something that we had been debating for some time and realized that the challenge description was a little confusing. We will be posting a revised version and will be re-inviting solutions soon.

To avoid this type of situations in the future, we have started making the challenges available for an 'early review' by selected people from the community. By doing this we can make sure that every aspect of the challenge is clearly examined, discussed and clarified before it is published.


# re: TSQL Challenge 15 - matching data between rows and columns

Friday, January 22, 2010 12:05 PM by Madhangi

Thanks Jacob. Sincerly appreciate your efforts.


Copyright © Beyondrelational.com