SQL Clone
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


T-SQL Challenge #1


T-SQL Challenge #1

Author
Message
jacob sebastian
jacob sebastian
SSCrazy
SSCrazy (2.7K reputation)SSCrazy (2.7K reputation)SSCrazy (2.7K reputation)SSCrazy (2.7K reputation)SSCrazy (2.7K reputation)SSCrazy (2.7K reputation)SSCrazy (2.7K reputation)SSCrazy (2.7K reputation)

Group: General Forum Members
Points: 2712 Visits: 2523
Comments posted to this topic are about the item T-SQL Challenge #1

.
RBarryYoung
RBarryYoung
SSC-Dedicated
SSC-Dedicated (35K reputation)SSC-Dedicated (35K reputation)SSC-Dedicated (35K reputation)SSC-Dedicated (35K reputation)SSC-Dedicated (35K reputation)SSC-Dedicated (35K reputation)SSC-Dedicated (35K reputation)SSC-Dedicated (35K reputation)

Group: General Forum Members
Points: 35366 Visits: 9518
Jacob or Steve:

There seems to be some kind of formatting problem with the code blocks over-running the text that follows them. At least for me (Chrome 2.0).

-- RBarryYoung, (302)375-0451 blog: MovingSQL.com, Twitter: @RBarryYoung
Proactive Performance Solutions, Inc.
"Performance is our middle name."
jacob sebastian
jacob sebastian
SSCrazy
SSCrazy (2.7K reputation)SSCrazy (2.7K reputation)SSCrazy (2.7K reputation)SSCrazy (2.7K reputation)SSCrazy (2.7K reputation)SSCrazy (2.7K reputation)SSCrazy (2.7K reputation)SSCrazy (2.7K reputation)

Group: General Forum Members
Points: 2712 Visits: 2523
I see that too, and even in IE and firefox. I have sent an email to Steve requesting to take a look at it.

.
jacob sebastian
jacob sebastian
SSCrazy
SSCrazy (2.7K reputation)SSCrazy (2.7K reputation)SSCrazy (2.7K reputation)SSCrazy (2.7K reputation)SSCrazy (2.7K reputation)SSCrazy (2.7K reputation)SSCrazy (2.7K reputation)SSCrazy (2.7K reputation)

Group: General Forum Members
Points: 2712 Visits: 2523
The formatting problem is corrected.

.
sample1
sample1
SSC Eights!
SSC Eights! (869 reputation)SSC Eights! (869 reputation)SSC Eights! (869 reputation)SSC Eights! (869 reputation)SSC Eights! (869 reputation)SSC Eights! (869 reputation)SSC Eights! (869 reputation)SSC Eights! (869 reputation)

Group: General Forum Members
Points: 869 Visits: 266
The challenge can be easily handled in an another way like

WITH CTE1
AS
(
SELECT CODE, 1 AS ROWNUMBER, MAXROWNUMBER
FROM (
SELECT CODE, MAX(ROWNUMBER) MAXROWNUMBER
FROM ( SELECT CODE, ROW_NUMBER() OVER(PARTITION BY CODE ORDER BY CODE ASC) ROWNUMBER FROM TABLEA

UNION ALL SELECT CODE, ROW_NUMBER() OVER(PARTITION BY CODE ORDER BY CODE ASC) ROWNUMBER FROM TABLEB

UNION ALL SELECT CODE, ROW_NUMBER() OVER(PARTITION BY CODE ORDER BY CODE ASC) ROWNUMBER FROM TABLEC ) A
GROUP BY CODE )A

UNION ALL

SELECT CODE, ROWNUMBER + 1, MAXROWNUMBER
FROM CTE1
WHERE MAXROWNUMBER >= ROWNUMBER + 1
)
SELECT A.CODE, A.NAME, B.CODE, B.NAME,C.CODE, C.NAME
FROM CTE1
INNER JOIN ( SELECT *, ROW_NUMBER() OVER(PARTITION BY CODE ORDER BY CODE ASC) ROWNUMBER FROM TABLEA ) A ON A.CODE = CTE1.CODE
LEFT OUTER JOIN ( SELECT *, ROW_NUMBER() OVER(PARTITION BY CODE ORDER BY CODE ASC) ROWNUMBER FROM TABLEB ) B ON b.CODE = CTE1.CODE AND b.ROWNUMBER = CTE1.ROWNUMBER
LEFT OUTER JOIN ( SELECT *, ROW_NUMBER() OVER(PARTITION BY CODE ORDER BY CODE ASC) ROWNUMBER FROM TABLEC ) c ON c.CODE = CTE1.CODE AND c.ROWNUMBER = CTE1.ROWNUMBER
ORDER BY CTE1.CODE;
alok.dixit
alok.dixit
Grasshopper
Grasshopper (20 reputation)Grasshopper (20 reputation)Grasshopper (20 reputation)Grasshopper (20 reputation)Grasshopper (20 reputation)Grasshopper (20 reputation)Grasshopper (20 reputation)Grasshopper (20 reputation)

Group: General Forum Members
Points: 20 Visits: 5
I'm wondering where could in real life we can face challenge like this!
jacob sebastian
jacob sebastian
SSCrazy
SSCrazy (2.7K reputation)SSCrazy (2.7K reputation)SSCrazy (2.7K reputation)SSCrazy (2.7K reputation)SSCrazy (2.7K reputation)SSCrazy (2.7K reputation)SSCrazy (2.7K reputation)SSCrazy (2.7K reputation)

Group: General Forum Members
Points: 2712 Visits: 2523
This was a problem originally posted in one of the SQL server forums. That gives me an indication that there may be places when this type of queries are used.

Some of the challenges that we present through the 'TSQL challenges' series are taken from real-life scenarios and some are not. The goal is not only to solve the problem, but also to exercise and enhance the query writing skills and SET based thinking.

.
Sharon Matyk
Sharon Matyk
SSC Veteran
SSC Veteran (240 reputation)SSC Veteran (240 reputation)SSC Veteran (240 reputation)SSC Veteran (240 reputation)SSC Veteran (240 reputation)SSC Veteran (240 reputation)SSC Veteran (240 reputation)SSC Veteran (240 reputation)

Group: General Forum Members
Points: 240 Visits: 47
Whilst I enjoyed the mental exercise and learning something I didn't know about 2005, I just can't picture when I would actually do such a join in real life. Is there a logical, meaningful example that was the basis of this problem ... and if not, does anyone have any ideas of when you'd prefer results which only join multiple values in this manner?
Jim-872017
Jim-872017
SSC Rookie
SSC Rookie (32 reputation)SSC Rookie (32 reputation)SSC Rookie (32 reputation)SSC Rookie (32 reputation)SSC Rookie (32 reputation)SSC Rookie (32 reputation)SSC Rookie (32 reputation)SSC Rookie (32 reputation)

Group: General Forum Members
Points: 32 Visits: 202
Hi,

I find this interesting and like the idea. I\m thinking that when you present the other solutions a brief description of why they are shown would be helpful, like this one has the fastest performance and this one solves the challenge in the fewest lines. learning the differences of simplest vs highest performing would be of great benefit to me as I continue to learn.

Thanks and looking forward to the next challenge.

Regards,
Jim
frere01
frere01
Grasshopper
Grasshopper (21 reputation)Grasshopper (21 reputation)Grasshopper (21 reputation)Grasshopper (21 reputation)Grasshopper (21 reputation)Grasshopper (21 reputation)Grasshopper (21 reputation)Grasshopper (21 reputation)

Group: General Forum Members
Points: 21 Visits: 11
-- ?????


DECLARE @m TABLE (ID INT IDENTITY(1,1), code INT, aname VARCHAR(10), typeT INT)

INSERT INTO @m (code , aname , typeT )
SELECT *
FROM
(
SELECT code, bname, 1 Typet
FROM @b
UNION ALL
SELECT code, cname, 2 Typet
FROM @c
) x


SELECT A.CODE, A.ANAME, M.ANAME, N.ANAME
FROM @m M
FULL JOIN @m N
ON M.CODE = N.CODE
AND M.ID = (N.ID%(SELECT MIN(o.id) FROM @m O WHERE O.typet = 2 and O.code = M.code))+
((SELECT MIN(Q.id) FROM @m Q WHERE Q.typet = 1 and Q.code = M.code))
RIGHT JOIN @A A
ON A.code = M.CODE
OR A.code = N.CODE
WHERE
(M.TYPET = 1 OR M.TYPET IS NULL)
AND (N.TYPET = 2 OR N.TYPET IS NULL)
Go


Permissions

You can't post new topics.
You can't post topic replies.
You can't post new polls.
You can't post replies to polls.
You can't edit your own topics.
You can't delete your own topics.
You can't edit other topics.
You can't delete other topics.
You can't edit your own posts.
You can't edit other posts.
You can't delete your own posts.
You can't delete other posts.
You can't post events.
You can't edit your own events.
You can't edit other events.
You can't delete your own events.
You can't delete other events.
You can't send private messages.
You can't send emails.
You can read topics.
You can't vote in polls.
You can't upload attachments.
You can download attachments.
You can't post HTML code.
You can't edit HTML code.
You can't post IFCode.
You can't post JavaScript.
You can post emoticons.
You can't post or upload images.

Select a forum

































































































































































SQLServerCentral


Search