SQL server multiple like query - sql

I am trying to use multiple like query for column 1 and want show the data in seperate column, can I do
SELECT CONVERT(VARCHAR(100), DECRYPTBYKEY(ssr.optiontextresponse)) AS DueDate
FROM [dbo].[tblsubscribeReport] ssr
INNER JOIN [dbo].[tblsurveyque] sq on sq.surveyquestionid = CONVERT(VARCHAR(100), DECRYPTBYKEY(ssr.questionid)) --and [name] LIKE '%due%'
LEFT JOIN [dbo].[tblsurveyquestiono] so on so.surveyquestionoptionid = CONVERT(VARCHAR(100), DECRYPTBYKEY(ssr.optionid))
JOIN tblSubscribers s on s.SubscriberID = ssr.SubscriberId
WHERE
s.ClientID = '12'
and (sq.[name] LIKE '%due%' OR sq.[name] LIKE '%doc%' )
Due date
Feb 16, 2023
Shailesh Parihar
Feb 17 2023
Meet
Ank
feb 18 2023
Maria
Mar 2 2023
Mar 12 2023
Joun
Mar 11 2023
Smith
14/02/2023
Sarah
15-02-2023
Peggy
I Want show the my data in separate column

Related

SQL: Find number of active "events" each month

Background
I have an SQL table that contains all events, with each event containing a unique identifier.
As you can see for some IDs the "event" stretches across multiple months. What I'm trying to find is the number of "active events" per month.
For example event ID:342, is active in both the month of Jan and Feb. So it should count towards both Jan and Feb's final count.
Example dataset
ID
Start Date
End Date
342
01 Jan 2022
12 Feb 2022
231
12 Feb 2022
26 Feb 2022
123
20 Jan 2022
10 Apr 2022
Desired output:
Month
Start Date
Jan
2
Feb
3
Mar
1
Apr
1
btw: I'm using Alibaba's ODPS SQL and not MySQL or Postgres. So i appreciate if the solution provided could be SQL system agnostic. Thanks!
Here is an example is MySQL 8, using a recursive CTE to construct the list of months. It would be more efficient to use a Calendar Table.
If you are not using MySQL you will need to modify the syntax of the query.
create table dataset(
ID int, Start_date Date,End_date Date);
insert into dataset values
(342,'2022-01-01','2022-02-12'),
(231,'2022-01-12','2022-02-26'),
(123,'2022-01-20','2022-04-10');
/*
Desired output:
Month Start Date
Jan 2
Feb 3
Mar 1
Apr 1
*/
✓
✓
✓
select
min(month(Start_date)),
max(month(End_date))
from dataset;
min(month(Start_date)) | max(month(End_date))
---------------------: | -------------------:
1 | 4
with recursive m as
(select min(month(Start_date)) mon from dataset
union all
select mon + 1 from m
where mon < (select max(month(End_date)) from dataset)
)
select
mon "month",
count(id) "Count"
from m
left join dataset
on month(Start_date)<= mon
and month(End_date) >= mon
group by mon
order by mon;
month | Count
----: | ----:
1 | 3
2 | 3
3 | 1
4 | 1
db<>fiddle here

Distribute table data to another table using SQL query

Help on how to distribute Default Table into Transaction Table using SQL Server Query if month is not existing on the other side? Please refer on the sample tables below
Default Table
Month
Value
Jan
0
Feb
0
Mar
0
Apr
0
May
0
Transaction Table
Month
Sales
Jan
10
Feb
0
Apr
20
I want to achieve this below results.
Month
Sales
Jan
10
Feb
0
Mar
0
Apr
20
May
0
You can use INSERT...WHERE NOT EXISTS
INSERT INTO Transaction (Month, Sales)
SELECT c.Month, d.Sales
FROM [Default] d
WHERE NOT EXISTS (SELECT 1
FROM Transaction t
WHERE t.Month = d.Month)

SUM and Count in one SQL Query

I have this kind of data
time Members
-------------------------------------------------- -----------
Jun 23 2016 1
Jun 23 2016 1
Jun 23 2016 2
Jun 29 2016 6
Jul 11 2016 3
Jul 11 2016 1
Jul 13 2016 1
I obtained this data using this sql query
SELECT CONVERT (VARCHAR(12), a.registered_time), COUNT(b.member_id) AS Members
FROM b
Inner JOIN a ON b.mirror_id = a.mirror_id
GROUP BY
(a.registered_time) order by a.registered_time
I want to get the sum of total numbers if they are of the same date for exampple the date of June 23 2016 will have total members of 4 and so on. Is it possible to have SUM() FUnction on Count()? How can I do this?
Convert the value to a date and include that in both the select and group by:
SELECT CONVERT(date, a.registered_time) as dte, COUNT(b.member_id) AS Members
FROM b JOIN
a
ON b.mirror_id = a.mirror_id
GROUP BY CONVERT(date, a.registered_time)
ORDER BY CONVERT(date, a.registered_time);

SQL Top 1 in XML Path ('')

The following code:
select(
select m.Code + ' ' + cast(m.Completed as varchar(max)) + ' '+ cast(ol.Billed as varchar(max)) + ' ' + cast(m.Delete as varchar (max))
from matterdetails as m
full join officeledger as ol on ol.id=m.id
order by ol.Billed desc
for xml path(''))
returns the results
Code Completed Billed Deleted
-------- ----------- ----------- -----------
HK168/03 Mar 30 2012 Aug 17 2011 Mar 30 2012
HK168/03 Mar 30 2012 Feb 24 2011 Mar 30 2012
HK168/03 Mar 30 2012 Dec 23 2010 Mar 30 2012
FT080/03 Apr 4 2012 Mar 29 2012 Apr 4 2012
FT080/03 Apr 4 2012 Feb 9 2012 Apr 4 2012
FT080/03 Apr 4 2012 Oct 20 2011 Apr 4 2012
etc.
whereas i require the results
Code Completed Billed Deleted
-------- ----------- ----------- -----------
HK168/03 Mar 30 2012 Aug 17 2011 Mar 30 2012
FT080/03 Apr 4 2012 Mar 29 2012 Apr 4 2012
etc.
I know that i have to insert a select top 1 somewhere to limit the ol.Billed results, but i am unsure where. Any advice would be great.
try this: It will join with the max Billed Date for each Id
select(
select m.Code + ' ' + cast(m.Completed as varchar(max)) + ' '+ cast(ol.Billed as varchar(max)) + ' ' + cast(m.Delete as varchar (max))
matterdetails as m
full join (select id, Max(billed) as 'Billed' from officeledger) as ol on ol.id=m.id
order by ol.Billed desc
for xml path(''))
You could replace:
full join officeledger as ol on ol.id=m.id
with:
full join (select id,MIN(Billed) as Billed from officeledger group by id) as ol = ol.id = m.id
There should also be a way to do this with ROW_NUMBER(), but I won't write it unless you need it.

SQL Get count of users who participated in a survey

I want to know how I can write a SQL query to get the count of users who participated in each month survey
The table structures are like below
SurveyTable
SurveyId, SurveyName, Datetime
SurveyInfo
Id, SurveyId, First name, Last name DateTime
Sample Data
SurveyTable
1 Survey-ABC 1 Jan 2011
2 Survey-AXC 1 Feb 2011
3 Survey-AEW 1 Mar 2011
4 Survey-AVD 1 Apr 2011
....
SurveyInfo
1 1 Peter James 1 Jan 2011
2 1 John Tom 1 Jan 2011
3 1 Harry Kin 1 Jan 2011
4 2 Amber Wills 1 Feb 2011
5 2 Tom Kin 1 Feb 2011
I want a result like
3 users participated in Survey-ABC,
2 users participated in Survey-AXC
Thanks
SELECT
Count(*) AS "Number of participants",
SurveyTable.surveyName
FROM
surveyinfo
INNER JOIN surveytable
ON surveytable.surveyid = surveyinfo.surveyid
GROUP BY
Year(DateTime),
Month(Date_Time)
SELECT stab.Name, COUNT(si.ID)
FROM SurveyTable as stab INNER JOIN SurveyInfo si ON si.SurveyId = stab.SurveyId
GROUP BY stab.Name
Because you want to see the number or participant for each month, so you need to extract the month and group by this.
SELECT SurveyTable.SurveyId, SurveyTable.SurveyName, Month(SurveyInfo.DateTime) As M, COUNT(SurveyInfo.*) As Cnt
FROM SurveyTable
LEFT JOIN SurveyInfo ON SurveyInfo.SurveyId = SurveyTable.SurveyId
GROUP By SurveyTable.SurveyId, SurveyTable.SurveyName, Month(SurveyInfo.DateTime)
The following select statement
SELECT st.Name, COUNT(si.ID)
FROM SurveyTable.st INNER JOIN SurveyInfo si ON si.SurveyId = st.SurveyId
GROUP BY st.Name
should provide you with the users who participated in the surveys.