Getting Started with ASP.NET MVC - Part 6: ASP.NET MVC and Entity Framework
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.
Sponsored [Advertise Here]
Syndicate your blog!
beyondrelational.com
Syndicate your blog with us to get wider reach into the technology community. Click here to get started.

PLSQL Challenge 3 - Fight Your Fear for Date Calculations
beyondrelational.com
This challenge invites you to play with date calculations. A lot of people are scared of date calculations and this challenge aims to help them to fight their fear!

SQL Server Interview Questions
beyondrelational.com
Interview questions and answers on various SQL Server areas such as Database Administration, TSQL Development, BI etc

TSQL Challenge 1 - Pair-wise and ordered assignment of objects from two different lists

This challenge will be interesting for TSQL enthusiasts as well as bird lovers! It involves assigning food items to birds from two different baskets. Here is a ‘relational’ representation of birds, food items and baskets in the form of tables and rows.

Table Birds lists the birds which are the recipients of food items. The baskets containing the food items are the tables Grains and Fruits. Whenever possible, you must assign the food items in pairs and they must be taken from each table in alphabetical order. When one of the tables no longer has a food item for a bird you must output a null in the corresponding column and continue assigning food items from the other table until that one runs out of food baskets as well.

Sample Data

Birds Table
Code Name
---- -------
1    Pigeon
2    Sparrow
3    Parrot

Grains Table

Code Grain
---- ------
1    Wheat
1    Rice
2    Corn
2    Millet

Fruits Table

Code Fruit
---- ------
1    Banana
1    Mango
1    Guava
2    Grapes

Expected Results

Code Bird    Grain  Fruit
---- ------- ------ ------
1    Pigeon  Rice   Banana
1    Pigeon  Wheat  Guava
1    Pigeon  NULL   Mango
2    Sparrow Corn   Grapes
2    Sparrow Millet NULL
3    Parrot  NULL   NULL

Rules

  1. For a bird with no food basket at all, a single line should be output with the Grain and Fruit columns containing null.
  2. The output should be ordered by Code followed by the order in which the Grain/Fruit pairs were extracted from the Grains/Fruits tables.

Sample Script

Use the TSQL Script given below to generate the source tables and fill them with sample data.

IF OBJECT_ID('TC1_BIRDS','U') IS NOT NULL BEGIN
    DROP TABLE TC1_BIRDS
END
GO

CREATE TABLE TC1_BIRDS(
    Code INT, 
    Name VARCHAR(10)
)
GO

INSERT INTO TC1_BIRDS(Code,Name)
SELECT 1,'Pigeon'  UNION ALL
SELECT 2,'Sparrow' UNION ALL
SELECT 3,'Parrot'
GO

IF OBJECT_ID('TC1_GRAINS','U') IS NOT NULL BEGIN
    DROP TABLE TC1_GRAINS
END
GO

CREATE TABLE TC1_GRAINS(
    Code INT, 
    Grain VARCHAR(10)
)
GO

INSERT INTO TC1_GRAINS(Code,Grain)
SELECT 1,'Wheat'  UNION ALL
SELECT 1,'Rice'  UNION ALL
SELECT 2,'Corn' UNION ALL
SELECT 2,'Millet'

IF OBJECT_ID('TC1_FRUITS','U') IS NOT NULL BEGIN
    DROP TABLE TC1_FRUITS
END
GO

CREATE TABLE TC1_FRUITS(
    Code INT, 
    Fruit VARCHAR(10)
)
GO

INSERT INTO TC1_FRUITS(Code,Fruit)
SELECT 1,'Banana'  UNION ALL
SELECT 1,'Mango'  UNION ALL
SELECT 1,'Guava' UNION ALL
SELECT 2,'Grapes'

Restrictions

  1. The solution should be a single query that starts with a "SELECT" or “;WITH”

Notes

  1. Read the Submission Guidelines and make sure that your solution follows them.
  2. If you would like to use a Tally Table, you can use the script given here. Your solution should not include the script to create and populate the tally table. You can assume that the tally table will be available in the database where the evaluation team will run your Code.

Jacob Sebastian
1 · 100.00% · 22504
Submit a Solution
View Solutions
View All Challenges Next Challenge Solve this in Oracle
4
Liked
16
Comments
83
Solutions



Submit

16   Comments   

Subscribe to Notifications
  • -- content removed by administrator --

    commented on Feb 24 2011 12:32PM  .  Report Abuse This post is not formatted correctly
    Asif Ghanchi
    1205 · 0% · 10
  • Asif,

    You cannot post solutions in the discussion thread. If you would like to submit a solution, please use the 'submit' link on the page.

    commented on Feb 25 2011 12:27AM  .  Report Abuse This post is not formatted correctly
    Jacob Sebastian
    1 · 100% · 22502
  • Hi,

    Which version(s) of SQL? 2005/2008/2008r2?

    Regards David

    commented on Feb 27 2011 4:13PM  .  Report Abuse This post is not formatted correctly
    dtodd
    509 · 0% · 42
  • The evaluation software runs on a SQL Server 2008 R2 instance. However, you can write a solution that runs on any version of SQL Server - SQL Server 2008 R2 or earlier versions.

    commented on Feb 27 2011 8:24PM  .  Report Abuse This post is not formatted correctly
    Jacob Sebastian
    1 · 100% · 22502
  • The dearth of comments here suggests that this particular forum thread might be a touch harder to find than usual.
    Normally, there is a link to the thread in the challenge writeup...

    commented on Feb 28 2011 7:40AM  .  Report Abuse This post is not formatted correctly
    jimbobmcgee
    170 · 1% · 218
  • I see your point. The new challenge page has a comment section in the footer. You can write a comment directly from the comment page and it will be posted to this thread. It also has a link "view comments" which will take you directly to this thread.

    commented on Feb 28 2011 7:51AM  .  Report Abuse This post is not formatted correctly
    Jacob Sebastian
    1 · 100% · 22502
  • So it does; I'm not sure how I missed that originally.
    That said, posting direct from the challenge might lead to a lot of duplicates questions, especially if there is not a list of recent/upvoted comments on the main page. We'll see how it goes...

    commented on Feb 28 2011 8:41AM  .  Report Abuse This post is not formatted correctly
    jimbobmcgee
    170 · 1% · 218
  • OK, well, provided I've understood the requirements, the following should be able to generate reasonably repeatable load test data for the challenge. Please feel free to correct me if I am wrong...

    TRUNCATE TABLE tc1_birds
    TRUNCATE TABLE tc1_fruits
    TRUNCATE TABLE tc1_grains
    
    DECLARE 
         @seed INT
        ,@birds INT
        ,@max_fruits_per_bird INT
        ,@max_grains_per_bird INT
        ,@empty_food_chance FLOAT
        ,@seed_random FLOAT
        ,@loop INT
    
    -- PARAMETERS -------------------------------------------------------------------------------------
    SELECT
         @seed = 1
        ,@birds = 1000
        ,@max_fruits_per_bird = 100
        ,@max_grains_per_bird = 100
        ,@empty_food_chance = 0.1
    ---------------------------------------------------------------------------------------------------
    
    PRINT('Populating TC1_BIRDS, TC1_FRUITS and TC1_GRAINS with load test data')
    
    -- INSERT BIRDS -----------------------------------------------------------------------------------
    PRINT('...Inserting ' + CONVERT(VARCHAR(MAX), @birds) + ' birds into TC1_BIRDS')
    ;WITH cte_birds AS (
        SELECT 
        	 code = n
        	,name = 'B#' + CONVERT(VARCHAR(MAX), n)
        	,rn = ROW_NUMBER() OVER (ORDER BY NEWID())
        FROM tsqlc_tally t
        WHERE n BETWEEN 1 AND @birds
    )
    INSERT INTO tc1_birds (code, name)
    SELECT code, name FROM cte_birds ORDER BY rn
    ---------------------------------------------------------------------------------------------------
    
    -- INSERT GRAINS/FRUITS ---------------------------------------------------------------------------
    CREATE TABLE #tmp_grains(code INT, grain VARCHAR(10))
    CREATE TABLE #tmp_fruits(code INT, fruit VARCHAR(10))
    SELECT @loop = 1, @seed_random = RAND(@seed)
    
    WHILE @loop BETWEEN 1 AND @birds BEGIN
        IF @empty_food_chance > RAND() BEGIN
        	PRINT('...Bird #' + CONVERT(VARCHAR(MAX), @loop) + ' receives no fruits or grain')
        	SET @loop = @loop + 1
        	CONTINUE
        END
    
        PRINT('...Inserting up to ' + CONVERT(VARCHAR(MAX), @max_grains_per_bird) + ' grains and ' +
        	  CONVERT(VARCHAR(MAX), @max_fruits_per_bird) + ' fruits into TC1_GRAINS and TC1_FRUITS for bird #' +
        	  CONVERT(VARCHAR(MAX), @loop))
    
        ;WITH cte_grains AS (
        	SELECT 
        		 code = @loop
        		,grain = 'G#' + CONVERT(VARCHAR(MAX), n) + '.' + CONVERT(VARCHAR(MAX), @loop)
        		,rn = ROW_NUMBER() OVER (ORDER BY NEWID())
        	FROM tsqlc_tally
        	WHERE n BETWEEN 1 AND (RAND() * @max_grains_per_bird)
        )
        INSERT INTO #tmp_grains (code, grain)
        SELECT code, grain FROM cte_grains ORDER BY rn
    
        ;WITH cte_fruits AS (
        	SELECT 
        		 code = @loop
        		,fruit = 'F#' + CONVERT(VARCHAR(MAX), n) + '.' + CONVERT(VARCHAR(MAX), @loop)
        		,rn = ROW_NUMBER() OVER (ORDER BY NEWID())
        	FROM tsqlc_tally
        	WHERE n BETWEEN 1 AND (RAND() * @max_fruits_per_bird)
        )
        INSERT INTO #tmp_fruits (code, fruit)
        SELECT code, fruit FROM cte_fruits ORDER BY rn
    
        SET @loop = @loop + 1
    END
    
    ;WITH cte_grains AS (
        SELECT code, grain
        	,rn = ROW_NUMBER() OVER (ORDER BY NEWID())
        FROM #tmp_grains
    )
    INSERT INTO tc1_grains (code, grain)
    SELECT code, grain FROM cte_grains ORDER BY rn
    
    ;WITH cte_fruits AS (
        SELECT code, fruit
        	,rn = ROW_NUMBER() OVER (ORDER BY NEWID())
        FROM #tmp_fruits
    )
    INSERT INTO tc1_fruits (code, fruit)
    SELECT code, fruit FROM cte_fruits ORDER BY rn
    
    DROP TABLE #tmp_grains
    DROP TABLE #tmp_fruits
    ---------------------------------------------------------------------------------------------------
    
    DECLARE @summary VARCHAR(MAX)
    SELECT @summary = ('Done; inserted ' + (SELECT CONVERT(VARCHAR(MAX), COUNT(1)) FROM tc1_birds) + ' birds, ' + 
        			  (SELECT CONVERT(VARCHAR(MAX), COUNT(1)) FROM tc1_grains) + ' grains and ' + 
        			  (SELECT CONVERT(VARCHAR(MAX), COUNT(1)) FROM tc1_fruits) + ' fruits')
    PRINT(@summary)
    
    SELECT 'tc1_birds', * FROM tc1_birds
    SELECT 'tc1_fruits', * FROM tc1_fruits
    SELECT 'tc1_grains', * FROM tc1_grains
    

    Edit #1: Forms that require JavaScript to ensure that your post is formatted correctly can go die in a fire...

    Edit #2: Made it more likely that a row with NULL fruit and NULL grain would appear...

    Edit #3: Really made it more likely that a row with NULL fruit and NULL grain would appear...

    commented on Feb 28 2011 9:43AM  .  Report Abuse This post is not formatted correctly
    jimbobmcgee
    170 · 1% · 218
  • I understand.

    That said, posting direct from the challenge might lead to a lot of duplicates questions, especially if there is not a list of recent/upvoted comments on the main page. We'll see how it goes...

    You are right. Let us see how it goes. One option is to show the comment box on the challenge page only if there are no other comments in the thread. So this will be convenient to post the first comment. After we get some feedback we can get back to it and rework a little :-)

    Thanks for the load testing data. I will pass it to the evaluation team who will be very happy to review this.

    commented on Feb 28 2011 10:32AM  .  Report Abuse This post is not formatted correctly
    Jacob Sebastian
    1 · 100% · 22502
  • Using the test harness I get the following stats

    (61160 row(s) affected) Table 'TC1BIRDS'. Scan count 5, logical reads 3, physical reads 0, read-ahead reads 0, lob logical reads 0, lob physical reads 0, lob read-ahead reads 0. Table 'TC1GRAINS'. Scan count 5, logical reads 137, physical reads 0, read-ahead reads 0, lob logical reads 0, lob physical reads 0, lob read-ahead reads 0. Table 'TC1_FRUITS'. Scan count 5, logical reads 141, physical reads 0, read-ahead reads 0, lob logical reads 0, lob physical reads 0, lob read-ahead reads 0. Table 'Worktable'. Scan count 0, logical reads 0, physical reads 0, read-ahead reads 0, lob logical reads 0, lob physical reads 0, lob read-ahead reads 0.

    commented on Mar 2 2011 5:58PM  .  Report Abuse This post is not formatted correctly
    David Betteridge
    82 · 3% · 593
Previous 1 | 2 Next

Your Comment


Sign Up or Login to post a comment.


Managed Windows Shared Hosting by OrcsWeb

Copyright © Beyondrelational.com