SQL PICKUPS

in sql •  7 years ago  (edited)

I rarely post about development, and most recently i was posting about KOTLIN
I just wanted to post something, about SQL
Let us say we have a table TBL holding a key varchar column and a value varchar column:

CREATE TABLE TBL (ky varchar2(10), val varchar2(10));

we may have inserted our data:

ky: X val :11
ky: X val :22
ky: Y val: 35
ky: X val: 33

Let us do a SELECT * FROM TBL:

You can clearly notice we have a redundancy on the KY column and we can transpose them:

Doing it so:

SELECT
  *
FROM
  (
   SELECT
   ROW_NUMBER() OVER (PARTITION BY T1.KY ORDER BY T1.KY) AS TAGNUM
   , T1.KY
   , T1.VAL as VAL1
   , T2.VAL as VAL2
   , T3.VAL as VAL3
   FROM
   TBL T1
   LEFT JOIN TBL T2 ON T2.KY = T1.KY AND T2.ROWID NOT IN (T1.ROWID)
   LEFT JOIN TBL T3 ON T3.KY = T1.KY AND T3.ROWID NOT IN (T1.ROWID, T2.ROWID)
   )
WHERE
  TAGNUM = 1

Note that you may extend your left joins until you reach your expected "transpose" window using the same analogy.

Authors get paid when people like you upvote their post.
If you enjoyed what you read here, create your account today and start earning FREE STEEM!
Sort Order:  

You received a 31.98% upvote from @arabpromo courtesy of @aro.steem!

You can earn daily profit by delegating SP to our bot and support the community. To do so, click below:
50SP, 100SP, 250SP, 500SP, 1000SP, 5000SP, 10000SP,
Custom Amount
Make sure you have at least 15 SP left on your account.

If you believe this post is spam or abuse, please report it to our Discord #bot-bug channel.

Nice tip!

Congratulations, your post received 7.04% up vote form @spydo courtesy of @aro.steem! I hope, my gratitude will help you getting more visibility.
You can also earn by making delegation. Click here to delegate to @spydo and earn 95% daily reward payout! Follow this link to know more about delegation benefits.

You got a 40.65% upvote from @steemcreators courtesy of @aro.steem!

Steemcreators is a collaborative group of influential Steemians leveraging their expertise and resources to create a better platform for all. 75% of all revenue from this bot goes to those that have delegated their hard-earned Steempower to our cause. The rest of the revenue goes to fund new events, projects and development around the globe involving the Steem blockchain to continue revolutionize the way the world uses social media and cryptocurrency (minus a small management fee to run/update this service).

For more information or to get involved with Steemcreators contact Jacob Billett @entrepreneur916 or IJ Maha @steemcafe.


حصلت على تصويت من
@arabsteem curation trail !
يمكنك الحصول على تصويت اضافي عبر ارسال مبلغ اقله
0.05
ستيم او اسبيدي الى حساب التصويت الالي
@arabpromo
مع رابط المقال في حقل المذكرة (memo)
مما يتيح لك الحصول على تصويت مربح بحوالي 2.5 اضعاف :)