Solved

sql user Function to get list of values

Posted on 2016-10-27
3
51 Views
Last Modified: 2016-10-27
Here is some sample data from my table named campaignoffers
(the fields are True or False to indicate whether an offer is valid for a given campaign)

campaignid      FreeCash          Points          GiftCard      Food      

236                         F                            F                F               F      
244                        T                            F                 F               F
249                        F                           T                     T              F      

I would like to write a function that will look at the individual flags and return a comma separated list of valid offers
(taking in campaign id as input parameter)

There should be at least one flag set to T for each campaign but if for some reason there isn't I want the function to return the string
'No Valid Offers'

Given the sample data above, I would like it to return the following

  'No Valid Offers' ( for campaign id  236)

'Free cash' ( for campaign id  244)

'Points, Gift Card'  ( for campaign id  249)
0
Comment
Question by:johnnyg123
3 Comments
 
LVL 12

Assisted Solution

by:Nakul Vachhrajani
Nakul Vachhrajani earned 150 total points
ID: 41862575
Does this help? It's a quick approach that came to my mind - I have created a scalar valued function which implements UNPIVOT and subsequent string concatenation.

USE tempdb;
GO

IF OBJECT_ID('dbo.OffersByCampaign','U') IS NOT NULL
    DROP TABLE dbo.OffersByCampaign;
GO

IF OBJECT_ID('dbo.func_GetOffersByCampaign') IS NOT NULL
    DROP FUNCTION dbo.func_GetOffersByCampaign;
GO 

--Table & data for demo purposes only. 
--Please use actual tables/columns in your query
CREATE TABLE dbo.OffersByCampaign (CampaignId INT     NOT NULL,
                                   FreeCash   CHAR(1) NOT NULL,
                                   Points     CHAR(1) NOT NULL,
                                   GiftCard   CHAR(1) NOT NULL,
                                   Food       CHAR(1) NOT NULL
                                  );
GO

--Insert Test data
INSERT INTO dbo.OffersByCampaign (CampaignId, FreeCash, Points, GiftCard, Food)
VALUES (236, 'F', 'F', 'F', 'F'),
       (244, 'T', 'F', 'F', 'F'),
       (249, 'F', 'T', 'T', 'F');
GO


CREATE FUNCTION dbo.func_GetOffersByCampaign (@CampaignId INT)
    RETURNS VARCHAR(MAX)
AS
BEGIN
    DECLARE @offersByCampaign VARCHAR(MAX) = NULL;

    ;WITH OffersCTE (CampaignId, OfferEligibility, Offer)
    AS (SELECT Unpivoted.CampaignId, 
               Unpivoted.OfferEligibility, 
               Unpivoted.Offer
        FROM (SELECT oc.CampaignId, oc.FreeCash, oc.Points, oc.GiftCard, oc.Food
              FROM dbo.OffersByCampaign AS oc
              WHERE oc.CampaignId = @CampaignId
             ) AS UnPvt
        UNPIVOT (OfferEligibility FOR Offer IN (FreeCash, Points, GiftCard, Food)) AS Unpivoted
       )
    SELECT @offersByCampaign = COALESCE(@offersByCampaign + ', ','') + ofCTE.Offer
    FROM OffersCTE AS ofCTE
    WHERE ofCTE.OfferEligibility = 'T'

    RETURN ISNULL(@offersByCampaign,'No Valid Offers');
END
GO

SELECT oc.CampaignId, 
       oc.FreeCash, 
       oc.Points,
       oc.GiftCard,
       oc.Food,
       dbo.func_GetOffersByCampaign (oc.CampaignId) AS ConcatenatedOffers
FROM dbo.OffersByCampaign AS oc;
GO

/* RESULTS
CampaignId  FreeCash Points GiftCard Food ConcatenatedOffers
----------- -------- ------ -------- ---- -------------------
236         F        F      F        F    No Valid Offers
244         T        F      F        F    FreeCash
249         F        T      T        F    Points, GiftCard
*/

Open in new window

0
 
LVL 69

Accepted Solution

by:
ScottPletcher earned 350 total points
ID: 41862588
For best overall performance, a table-valued function is best.  First I'll show the code to invoke the function, then the function code itself:

SELECT co.*, gvo.*
FROM dbo.campaignoffers co
CROSS APPLY dbo.GetValidOffers ( co.campaignid ) gvo


/* these SETings ARE important, don't leave out*/
SET ANSI_NULLS ON;
SET QUOTED_IDENTIFIER ON;
GO
CREATE FUNCTION dbo.GetValidOffers
(
    @campaignId int
)
RETURNS TABLE
AS
RETURN (
SELECT
    ISNULL(NULLIF(STUFF(
    CASE WHEN FreeCash = 'T' THEN CAST(', FreeCash' AS varchar(100)) ELSE '' END +
    CASE WHEN Points = 'T' THEN CAST(', Points' AS varchar(100)) ELSE '' END +
    CASE WHEN GiftCard = 'T' THEN CAST(', GiftCard' AS varchar(100)) ELSE '' END +
    CASE WHEN Food = 'T' THEN CAST(', Food' AS varchar(100)) ELSE '' END
    , 1, 2, ''), ''), 'No Valid Offers') AS ValidOffers
FROM dbo.campaignoffers
WHERE campaignid = @campaignId
)
GO
0
 

Author Closing Comment

by:johnnyg123
ID: 41862679
Thanks so much for the responses!

I elected to go with scott's solution because of the performance and cleaner approach

I did want to give Nakul some points for taking the time to post a working solution as well
1

Featured Post

Microsoft Certification Exam 74-409

VeeamĀ® is happy to provide the Microsoft community with a study guide prepared by MVP and MCT, Orin Thomas. This guide will take you through each of the exam objectives, helping you to prepare for and pass the examination.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

I have a large data set and a SSIS package. How can I load this file in multi threading?
This article shows gives you an overview on SQL Server 2016 row level security. You will also get to know the usages of row-level-security and how it works
Viewers will learn how the fundamental information of how to create a table.
Viewers will learn how to use the SELECT statement in SQL and will be exposed to the many uses the SELECT statement has.

910 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

24 Experts available now in Live!

Get 1:1 Help Now