How to count up a single character in multiple fields in SQL - sql

HI i have a table 'TableCustomers' and within this table are many fields titled 'Name1' 'Name2''Name3'.... 'Name40' some of these fields just have the letter 'x' i want to know how many 'x' are there in all 40 fields

One possible solution is something like;
SELECT
CASE WHEN [Name] = 'x' THEN 1 ELSE 0 END +
CASE WHEN [Name1] = 'x' THEN 1 ELSE 0 END +
CASE WHEN [Name2] = 'x' THEN 1 ELSE 0 END +
CASE WHEN [Name3] = 'x' THEN 1 ELSE 0 END +
CASE WHEN [Name4] = 'x' THEN 1 ELSE 0 END +
CASE WHEN [Name5] = 'x' THEN 1 ELSE 0 END +
CASE WHEN [Name6] = 'x' THEN 1 ELSE 0 END +
CASE WHEN [Name7] = 'x' THEN 1 ELSE 0 END +
CASE WHEN [Name8] = 'x' THEN 1 ELSE 0 END +
CASE WHEN [Name9] = 'x' THEN 1 ELSE 0 END +
CASE WHEN [Name10] = 'x' THEN 1 ELSE 0 END +
CASE WHEN [Name11] = 'x' THEN 1 ELSE 0 END +
CASE WHEN [Name12] = 'x' THEN 1 ELSE 0 END +
CASE WHEN [Name13] = 'x' THEN 1 ELSE 0 END +
CASE WHEN [Name14] = 'x' THEN 1 ELSE 0 END +
CASE WHEN [Name15] = 'x' THEN 1 ELSE 0 END +
CASE WHEN [Name16] = 'x' THEN 1 ELSE 0 END +
CASE WHEN [Name17] = 'x' THEN 1 ELSE 0 END +
CASE WHEN [Name18] = 'x' THEN 1 ELSE 0 END +
CASE WHEN [Name19] = 'x' THEN 1 ELSE 0 END +
CASE WHEN [Name20] = 'x' THEN 1 ELSE 0 END +
CASE WHEN [Name21] = 'x' THEN 1 ELSE 0 END +
CASE WHEN [Name22] = 'x' THEN 1 ELSE 0 END +
CASE WHEN [Name23] = 'x' THEN 1 ELSE 0 END +
CASE WHEN [Name24] = 'x' THEN 1 ELSE 0 END +
CASE WHEN [Name25] = 'x' THEN 1 ELSE 0 END +
CASE WHEN [Name26] = 'x' THEN 1 ELSE 0 END +
CASE WHEN [Name27] = 'x' THEN 1 ELSE 0 END +
CASE WHEN [Name28] = 'x' THEN 1 ELSE 0 END +
CASE WHEN [Name29] = 'x' THEN 1 ELSE 0 END +
CASE WHEN [Name30] = 'x' THEN 1 ELSE 0 END +
CASE WHEN [Name31] = 'x' THEN 1 ELSE 0 END +
CASE WHEN [Name32] = 'x' THEN 1 ELSE 0 END +
CASE WHEN [Name33] = 'x' THEN 1 ELSE 0 END +
CASE WHEN [Name34] = 'x' THEN 1 ELSE 0 END +
CASE WHEN [Name35] = 'x' THEN 1 ELSE 0 END +
CASE WHEN [Name36] = 'x' THEN 1 ELSE 0 END +
CASE WHEN [Name37] = 'x' THEN 1 ELSE 0 END +
CASE WHEN [Name38] = 'x' THEN 1 ELSE 0 END +
CASE WHEN [Name39] = 'x' THEN 1 ELSE 0 END +
CASE WHEN [Name40] = 'x' THEN 1 ELSE 0 END +
CASE WHEN [Name41] = 'x' THEN 1 ELSE 0 END +
CASE WHEN [Name42] = 'x' THEN 1 ELSE 0 END +
CASE WHEN [Name43] = 'x' THEN 1 ELSE 0 END +
CASE WHEN [Name44] = 'x' THEN 1 ELSE 0 END +
CASE WHEN [Name45] = 'x' THEN 1 ELSE 0 END +
CASE WHEN [Name46] = 'x' THEN 1 ELSE 0 END +
CASE WHEN [Name47] = 'x' THEN 1 ELSE 0 END +
CASE WHEN [Name48] = 'x' THEN 1 ELSE 0 END +
CASE WHEN [Name49] = 'x' THEN 1 ELSE 0 END +
CASE WHEN [Name50] = 'x' THEN 1 ELSE 0 END
FROM [TableCustomers]
I generated this using an application called Nimble Text (http://nimbletext.com/) though you could use dynamic SQL along with the sys.columns view to generate this statement within SQL server, if the columns of the table change. Let me know if you would like an example of this...

You may try something like this:
select len(name00 + name01 + .. + name40) - len(replace(name00 + name01 + .. + name40, 'x', ''))
from yourTable;

Related

Concatenation by using CASE statement

I want to CONCAT between this on my SELECT statement in SQL Server.
All the columns are booleans.
The output will be like this if all of columns that are true
GGPNES
I tried to use this and it doesn't work
DECLARE #concat VARCHAR(40) ='';
SELECT
Smoke, Invoice,
Party, Summary,
MySelf, Export,
CASE
WHEN MyTable.Smoke = 1 OR MyTable.Invoice = 1
THEN #concat + 'GG'
WHEN MyTable.Party = 1
THEN #concat + 'P'
WHEN MyTable.Summary = 1
THEN #concat + 'N'
WHEN MyTable.MySelf = 1
THEN #concat + 'E'
WHEN MyTable.Export = 1
THEN #concat + 'S'
END
FROM
MyTable
I think that you want a concatenation of CASE expressions:
CASE WHEN Smoke = 1 OR Invoice = 1 THEN 'GG' ELSE '' END +
CASE WHEN Party = 1 THEN 'P' ELSE '' END +
CASE WHEN Summary = 1 THEN 'N' ELSE '' END +
CASE WHEN MySelf = 1 THEN 'E' ELSE '' END +
CASE WHEN Export = 1 THEN 'S' ELSE '' END

Average of CASE WHEN field

How can I get the average of the column TotalTeamMembership?
SELECT Player,
CASE WHEN Basketball = '' THEN 0 ELSE 1 END +
CASE WHEN Baseball = '' THEN 0 ELSE 1 END +
CASE WHEN Football = '' THEN 0 ELSE 1 END AS TotalTeamMembership
FROM PlayerMembership;
This is what I currently get:
This is what I need:
Thank you for any guidance!
You can use AVG():
SELECT AVG(CASE WHEN Basketball = '' THEN 0.0 ELSE 1 END +
CASE WHEN Baseball = '' THEN 0 ELSE 1 END +
CASE WHEN Football = '' THEN 0 ELSE 1 END
) AS avg_TotalTeamMembership
FROM PlayerMembership;
You could use AVGaggregate function:
SELECT AVG(TotalTeamMembership)
FROM (SELECT Player,
CASE WHEN Basketball = '' THEN 0 ELSE 1 END +
CASE WHEN Baseball = '' THEN 0 ELSE 1 END +
CASE WHEN Football = '' THEN 0 ELSE 1 END AS TotalTeamMembership
FROM PlayerMembership) s;

Combining SQL string alias into comma delimited string

I am working on this stored proc which check to see which BIT fields are true and depending on it assigns a text to it using alias name. I want to be able to combine these alias values (string) and have it comma delimited to I can display it on my SSRS report.
Below is part of my stored proc.
,CASE
WHEN sr.[ReservationAlreadySentAttached] = 1 THEN 'Reservation Already Sent'
END AS ReservationAttached
,CASE
WHEN sr.[HPOfficeAttached] = 1 THEN 'H&P Office'
END AS HPOfficeAttached
WHEN sr.[NotesAttached] = 1 THEN 'Notes'
END AS NotesAttached
,CASE
WHEN sr.[OpPermitAttached] = 1 THEN 'Op Permit'
END AS OpPermitAttached
,CASE
WHEN sr.[TestResultsAttached] = 1 THEN 'Test Results'
END AS TestResultsAttached
,CASE
WHEN sr.[ConsultationReportsAttached] = 1 THEN 'Consultation Reports'
END AS ConsultationReportsAttached
,CASE
WHEN sr.[OtherAttached] = 1 THEN 'Other'
END AS OtherAttached
so lets say in case where only NotesAttached and ReservationAlreadySentAttached were only true ones then I want the end result to come out as Notes, Reservation Already Sent.
How do i concatenate these aliases ?
Another option is to build your string and then just remove the leading comma with STUFF()
Example (abbreviated)
Declare #YourTable table (ReservationAlreadySentAttached bit,HPOfficeAttached bit,NotesAttached bit)
Insert Into #YourTable values
(1,0,1)
Select NewValue = stuff(
case when ReservationAlreadySentAttached = 1 then ', Reservation Already Sent' else '' end
+case when HPOfficeAttached = 1 then ', H&P Office' else '' end
+case when NotesAttached = 1 then ', Notes' else '' end
,1,2,'')
From #YourTable
Returns
NewValue
Reservation Already Sent, Notes
EDIT - Just the Expression
NewValue = stuff(
case when sr.[ReservationAlreadySentAttached] = 1 then ', Reservation Already Sent' else '' end
+case when sr.[HPOfficeAttached] = 1 then ', H&P Office' else '' end
+case when sr.[NotesAttached] = 1 then ', Notes' else '' end
,1,2,'')
Not the prettiest solution...
RIGHT((CASE
WHEN sr.[ReservationAlreadySentAttached] = 1
THEN ',Reservation Already Sent'
END + CASE
WHEN sr.[HPOfficeAttached] = 1
THEN ',H&P Office'
END + CASE
WHEN sr.[NotesAttached] = 1
THEN ',Notes'
END + CASE
WHEN sr.[OpPermitAttached] = 1
THEN ',Op Permit'
END + CASE
WHEN sr.[TestResultsAttached] = 1
THEN ',Test Results'
END + CASE
WHEN sr.[ConsultationReportsAttached] = 1
THEN ',Consultation Reports'
END + CASE
WHEN sr.[OtherAttached] = 1
THEN ',Other'
END)
,LEN(CASE
WHEN sr.[ReservationAlreadySentAttached] = 1
THEN ',Reservation Already Sent'
END + CASE
WHEN sr.[HPOfficeAttached] = 1
THEN ',H&P Office'
END + CASE
WHEN sr.[NotesAttached] = 1
THEN ',Notes'
END + CASE
WHEN sr.[OpPermitAttached] = 1
THEN ',Op Permit'
END + CASE
WHEN sr.[TestResultsAttached] = 1
THEN ',Test Results'
END + CASE
WHEN sr.[ConsultationReportsAttached] = 1
THEN ',Consultation Reports'
END + CASE
WHEN sr.[OtherAttached] = 1
THEN ',Other'
END) - 1 )
Edit: You may have to take into consideration when NONE of them are 1 so you don't get Invalid length parameter passed to the right function.
CASE
WHEN sr.[ReservationAlreadySentAttached] = 1 THEN ', Reservation Already Sent' ELSE ''
END
+ CASE
WHEN sr.[HPOfficeAttached] = 1 THEN ', H&P Office' ELSE ''
END
+ CASE etc...
And wrap this in some function to remove the first comma + space.

SQL add conversion of date to existing query

I'm would like to convert the date using this function. I'm using SQL server 2014
CONVERT(varchar, sc.StartDate,103) + '" + " " + "' + '" + " - " + "' + CONVERT(varchar, sc.EndDate,103) SIPDate
so it will display in dd/mm/yyyy - dd/mm/yyyy. How do i add it into the query? Thanks.
The query is:
SELECT CONCAT(sc.StartDate, sc.ENDDate) SIPDate,
COUNT (sj.LOComment) WeekReviewed,
COUNT(sjd.WeekNo) TotalWeek,
SUM(sjj.TotalDaysRecord) TotalDaysRecord,
COUNT(CASE sjd.JournalStatusCode WHEN 'D' THEN 1 ELSE NULL END) PENDingComplete
FROM StudentJournalDate sjd
LEFT JOIN StudentJournal sj
ON sjd.WeekNo = sj.WeekNo
LEFT OUTER JOIN
(
SELECT sj.WeekNo,
CASE WHEN RTRIM(sj.Day1Journal) = '' OR sj.Day1Journal IS NULL THEN 0 ELSE 1 END +
CASE WHEN RTRIM(sj.Day2Journal) = '' OR sj.Day2Journal IS NULL THEN 0 ELSE 1 END +
CASE WHEN RTRIM(sj.Day3Journal) = '' OR sj.Day3Journal IS NULL THEN 0 ELSE 1 END +
CASE WHEN RTRIM(sj.Day4Journal) = '' OR sj.Day4Journal IS NULL THEN 0 ELSE 1 END +
CASE WHEN RTRIM(sj.Day5Journal) = '' OR sj.Day5Journal IS NULL THEN 0 ELSE 1 END +
CASE WHEN RTRIM(sj.Day6Journal) = '' OR sj.Day6Journal IS NULL THEN 0 ELSE 1 END +
CASE WHEN RTRIM(sj.Day7Journal) = '' OR sj.Day7Journal IS NULL THEN 0 ELSE 1 END AS TotalDaysRecord
FROM StudentJournal sj
) AS sjj
ON sjj.WeekNo = sj.WeekNo
LEFT OUTER JOIN StudentSIP sc
ON sc.AdminNo = sjd.AdminNo
GROUP BY CONCAT(sc.StartDate, sc.ENDDate)
You can just SELECT and GROUP BY this formatted date expression, i.e.:
SELECT CONVERT(varchar, sc.StartDate, 103) + " - " + CONVERT(varchar, sc.EndDate, 103) AS SIPDate,
COUNT (sj.LOComment) WeekReviewed,
COUNT(sjd.WeekNo) TotalWeek,
SUM(sjj.TotalDaysRecord) TotalDaysRecord,
COUNT(CASE sjd.JournalStatusCode WHEN 'D' THEN 1 ELSE NULL END) PENDingComplete
FROM StudentJournalDate sjd
LEFT JOIN StudentJournal sj
ON sjd.WeekNo = sj.WeekNo
LEFT OUTER JOIN
(
SELECT sj.WeekNo,
CASE WHEN RTRIM(sj.Day1Journal) = '' OR sj.Day1Journal IS NULL THEN 0 ELSE 1 END +
CASE WHEN RTRIM(sj.Day2Journal) = '' OR sj.Day2Journal IS NULL THEN 0 ELSE 1 END +
CASE WHEN RTRIM(sj.Day3Journal) = '' OR sj.Day3Journal IS NULL THEN 0 ELSE 1 END +
CASE WHEN RTRIM(sj.Day4Journal) = '' OR sj.Day4Journal IS NULL THEN 0 ELSE 1 END +
CASE WHEN RTRIM(sj.Day5Journal) = '' OR sj.Day5Journal IS NULL THEN 0 ELSE 1 END +
CASE WHEN RTRIM(sj.Day6Journal) = '' OR sj.Day6Journal IS NULL THEN 0 ELSE 1 END +
CASE WHEN RTRIM(sj.Day7Journal) = '' OR sj.Day7Journal IS NULL THEN 0 ELSE 1 END AS TotalDaysRecord
FROM StudentJournal sj
) AS sjj
ON sjj.WeekNo = sj.WeekNo
LEFT OUTER JOIN StudentSIP sc
ON sc.AdminNo = sjd.AdminNo
GROUP BY CONVERT(varchar, sc.StartDate, 103) + " - " + CONVERT(varchar, sc.EndDate, 103)
Try this. since your SQL SERVER is 2014 you can use format to format the date
SELECT CONCAT(FORMAT(sc.StartDate,'dd/MM/yyy'),+ ' - ' +FORMAT(sc.ENDDate,'dd/MM/yyy')) SIPDate,
COUNT (sj.LOComment) WeekReviewed,
COUNT(sjd.WeekNo) TotalWeek,
SUM(sjj.TotalDaysRecord) TotalDaysRecord,
COUNT(CASE sjd.JournalStatusCode WHEN 'D' THEN 1 ELSE NULL END) PENDingComplete
FROM StudentJournalDate sjd
LEFT JOIN StudentJournal sj
ON sjd.WeekNo = sj.WeekNo
LEFT OUTER JOIN
(
SELECT sj.WeekNo,
CASE WHEN RTRIM(sj.Day1Journal) = '' OR sj.Day1Journal IS NULL THEN 0 ELSE 1 END +
CASE WHEN RTRIM(sj.Day2Journal) = '' OR sj.Day2Journal IS NULL THEN 0 ELSE 1 END +
CASE WHEN RTRIM(sj.Day3Journal) = '' OR sj.Day3Journal IS NULL THEN 0 ELSE 1 END +
CASE WHEN RTRIM(sj.Day4Journal) = '' OR sj.Day4Journal IS NULL THEN 0 ELSE 1 END +
CASE WHEN RTRIM(sj.Day5Journal) = '' OR sj.Day5Journal IS NULL THEN 0 ELSE 1 END +
CASE WHEN RTRIM(sj.Day6Journal) = '' OR sj.Day6Journal IS NULL THEN 0 ELSE 1 END +
CASE WHEN RTRIM(sj.Day7Journal) = '' OR sj.Day7Journal IS NULL THEN 0 ELSE 1 END AS TotalDaysRecord
FROM StudentJournal sj
) AS sjj
ON sjj.WeekNo = sj.WeekNo
LEFT OUTER JOIN StudentSIP sc
ON sc.AdminNo = sjd.AdminNo
GROUP BY CONCAT(FORMAT(sc.StartDate,'dd/MM/yyy'),+ ' - ' +FORMAT(sc.ENDDate,'dd/MM/yyy'))

Nested case limit

I'm trying to use nested case but SQL is giving me an error on 10 limit. Below is my sql. Do you have any suggestions for me?
ALTER VIEW [dbo].[vwOpti_ChannelPreference] AS
SELECT SUB.*,
CASE WHEN PCYCResponse + JBIResponse > 1 THEN 'SF Multi'
ELSE CASE WHEN MDLResponse + SermoResponse + BioPharmResponse + MedscapeResponse + PharmaConnectResponse + THResponse
+ OtherResponse > 1 THEN 'MCM Multi'
ELSE CASE WHEN MDLResponse + SermoResponse + BioPharmResponse + MedscapeResponse + PharmaConnectResponse + THResponse
+ PCYCResponse + JBIResponse + OtherResponse > 1 THEN 'SF & MCM Multi'
ELSE CASE WHEN MDLResponse + SermoResponse + BioPharmResponse + MedscapeResponse + PharmaConnectResponse + THResponse
+ PCYCResponse + JBIResponse + OtherResponse = 0 THEN 'None'
ELSE CASE WHEN MDLResponse = 1 THEN 'MDL'
ELSE CASE WHEN SermoResponse = 1 THEN 'Sermo'
ELSE CASE WHEN BioPharmResponse = 1 THEN 'BioPharmCommunications'
ELSE CASE WHEN MedscapeResponse = 1 THEN 'Medscape'
ELSE CASE WHEN PharmaConnectResponse = 1 THEN 'PharmaConnect'
ELSE CASE WHEN THResponse = 1 THEN 'Targeted Healthcare'
ELSE CASE WHEN PCYCResponse = 1 THEN 'PCYC'
ELSE CASE WHEN JBIResponse = 1 THEN 'JBI'
ELSE CASE WHEN OtherResponse = 1 THEN 'Other'
END END END END END END END END END END END END AS ChannelPref
FROM ( SELECT CC.*,
MAX(CASE WHEN MTE.DesignVendor = 'MDL' THEN 1 ELSE 0 END) AS MDLResponse,
MAX(CASE WHEN MTE.DesignVendor = 'Sermo' THEN 1 ELSE 0 END) AS SermoResponse,
MAX(CASE WHEN MTE.DesignVendor = 'BioPharmCommunications' THEN 1 ELSE 0 END) AS BioPharmResponse,
MAX(CASE WHEN MTE.DesignVendor = 'Medscape' THEN 1 ELSE 0 END) AS MedscapeResponse,
MAX(CASE WHEN MTE.DesignVendor = 'PharmaConnect' THEN 1 ELSE 0 END) AS PharmaConnectResponse,
MAX(CASE WHEN MTE.DesignVendor = 'Targeted Healthcare' THEN 1 ELSE 0 END) AS THResponse,
MAX(CASE WHEN MTE.DesignVendor = 'PCYC' THEN 1 ELSE 0 END) AS PCYCResponse,
MAX(CASE WHEN MTE.DesignVendor = 'JBI' THEN 1 ELSE 0 END) AS JBIResponse,
MAX(CASE WHEN MTE.DesignVendor NOT IN ('MDL','Sermo', 'BioPharmCommunications', 'Medscape', 'PharmaConnect',
'Targeted Healthcare', 'PCYC', 'JBI') THEN 1 ELSE 0 END) AS OtherResponse
--SELECT DISTINCT MTE.CHANNELNAME
FROM slice_CampaignContact CC
--LEFT JOIN slice_MktgTactic MTI ON I.MktgTacticID = MTI.MktgTacticID AND I.SliceDate = MTI.SliceDate
LEFT JOIN slice_Engagements E ON CC.ContactID = E.ContactID AND CC.SliceDate = E.SliceDate
LEFT JOIN slice_MktgTactic MTE ON E.MktgTacticID = MTE.MktgTacticID AND E.SliceDate = MTE.SliceDate
GROUP BY CC.SliceDate, CC.BrandCd, CC.ContactID, CC.SyncID, CC.FirstName, CC.LastName, CC.CampaignName,
ClientSegment, ControlSegmentName) SUB
You don't need the ELSE CASE every time -- that is just the way CASE works -- each WHEN is only checked on when prior WHENs fail. Like this:
SELECT SUB.*,
CASE
WHEN PCYCResponse + JBIResponse > 1 THEN 'SF Multi'
WHEN MDLResponse + SermoResponse + BioPharmResponse + MedscapeResponse + PharmaConnectResponse + THResponse + OtherResponse > 1 THEN 'MCM Multi'
WHEN MDLResponse + SermoResponse + BioPharmResponse + MedscapeResponse + PharmaConnectResponse + THResponse + PCYCResponse + JBIResponse + OtherResponse > 1 THEN 'SF & MCM Multi'
WHEN MDLResponse + SermoResponse + BioPharmResponse + MedscapeResponse + PharmaConnectResponse + THResponse + PCYCResponse + JBIResponse + OtherResponse = 0 THEN 'None'
WHEN MDLResponse = 1 THEN 'MDL'
WHEN SermoResponse = 1 THEN 'Sermo'
WHEN BioPharmResponse = 1 THEN 'BioPharmCommunications'
WHEN MedscapeResponse = 1 THEN 'Medscape'
WHEN PharmaConnectResponse = 1 THEN 'PharmaConnect'
WHEN THResponse = 1 THEN 'Targeted Healthcare'
WHEN PCYCResponse = 1 THEN 'PCYC'
WHEN JBIResponse = 1 THEN 'JBI'
WHEN OtherResponse = 1 THEN 'Other'
ELSE 'Woah, nothing matched'
END AS ChannelPref
FROM ( SELECT CC.*, -- etc