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


Need help with this query


Need help with this query

Author
Message
prads.cs
prads.cs
SSC Journeyman
SSC Journeyman (98 reputation)SSC Journeyman (98 reputation)SSC Journeyman (98 reputation)SSC Journeyman (98 reputation)SSC Journeyman (98 reputation)SSC Journeyman (98 reputation)SSC Journeyman (98 reputation)SSC Journeyman (98 reputation)

Group: General Forum Members
Points: 98 Visits: 153
I got this table

LicenseId ClassificationCode GroupCode HasMultiple
10537 GA02 GA 0
10537 GA03 GA 0
10537 GB98 GB 1
10537 GF08 GF 0
10537 GF09 GF 0

Result set should return me
either (GA02 or GA03) and (GF08 or GF09) so i can go and update the corresponding "HasMultiple"

Note : ignore GB98 since its got some business logic "98"

my sample table should look something like this after the update

LicenseId ClassificationCode GroupCode HasMultiple
10537 GA02 GA 1
10537 GA03 GA 0
10537 GB98 GB 1
10537 GF08 GF 1
10537 GF09 GF 0

Can someone help me here with this query :-)
prads.cs
prads.cs
SSC Journeyman
SSC Journeyman (98 reputation)SSC Journeyman (98 reputation)SSC Journeyman (98 reputation)SSC Journeyman (98 reputation)SSC Journeyman (98 reputation)SSC Journeyman (98 reputation)SSC Journeyman (98 reputation)SSC Journeyman (98 reputation)

Group: General Forum Members
Points: 98 Visits: 153
Sorry guys for the trouble ... just figured out some crazy method Hehe

UPDATE tvLRF
SET HasMultiple = 1
FROM @tv_LicenseRenewalFee tvLRF
INNER JOIN ( SELECT GroupCode, MIN(SUBSTRING(ClassificationCode,3, LEN(ClassificationCode))) AS Number
FROM @tv_LicenseRenewalFee
WHERE GroupCode <> (
SELECT GroupCode
FROM @tv_LicenseRenewalFee
WHERE HasMultiple = 1
)
GROUP BY GroupCode
HAVING COUNT(GroupCode) > 1 ) A
ON (A.GroupCode+''+CAST(A.Number AS VARCHAR(2))) = tvLRF.ClassificationCode
ChrisM@Work
ChrisM@Work
SSC-Forever
SSC-Forever (40K reputation)SSC-Forever (40K reputation)SSC-Forever (40K reputation)SSC-Forever (40K reputation)SSC-Forever (40K reputation)SSC-Forever (40K reputation)SSC-Forever (40K reputation)SSC-Forever (40K reputation)

Group: General Forum Members
Points: 40232 Visits: 20000
prads.cs (8/17/2012)
Sorry guys for the trouble ... just figured out some crazy method Hehe

UPDATE tvLRF
SET HasMultiple = 1
FROM @tv_LicenseRenewalFee tvLRF
INNER JOIN ( SELECT GroupCode, MIN(SUBSTRING(ClassificationCode,3, LEN(ClassificationCode))) AS Number
FROM @tv_LicenseRenewalFee
WHERE GroupCode <> (
SELECT GroupCode
FROM @tv_LicenseRenewalFee
WHERE HasMultiple = 1
)
GROUP BY GroupCode
HAVING COUNT(GroupCode) > 1 ) A
ON (A.GroupCode+''+CAST(A.Number AS VARCHAR(2))) = tvLRF.ClassificationCode


Gosh, I'm sure it doesn't have to be quite so complicated. Try this - it generates exactly the same results:

;WITH Updater AS (
SELECT LicenseId, ClassificationCode, GroupCode, HasMultiple,
MaybeMultiple = ROW_NUMBER() OVER(PARTITION BY GroupCode ORDER BY ClassificationCode)
FROM @tv_LicenseRenewalFee
) UPDATE Updater SET HasMultiple = 1 WHERE MaybeMultiple = 1



“Write the query the simplest way. If through testing it becomes clear that the performance is inadequate, consider alternative query forms.” - Gail Shaw

For fast, accurate and documented assistance in answering your questions, please read this article.
Understanding and using APPLY, (I) and (II) Paul White
Hidden RBAR: Triangular Joins / The "Numbers" or "Tally" Table: What it is and how it replaces a loop Jeff Moden
Exploring Recursive CTEs by Example Dwain Camps
Chris Hurlbut
Chris Hurlbut
SSCrazy
SSCrazy (2K reputation)SSCrazy (2K reputation)SSCrazy (2K reputation)SSCrazy (2K reputation)SSCrazy (2K reputation)SSCrazy (2K reputation)SSCrazy (2K reputation)SSCrazy (2K reputation)

Group: General Forum Members
Points: 2046 Visits: 540
Use ChrisM@Work solution as it is clean and trim...
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