SQL Top 1 in XML Path ('') - sql

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.

Related

SQL server multiple like query

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

Merge specific row in whole table

I'm working on a web app, considering how to merge specific row in whole table.
Here is the example
Original data:
Name Date
-------------------
Jason Jul 2017
Tom Jun 2018
Andy Jun 2018
Mary Jun 2018
Alex Feb 2018
David Aug 2018
I'd like to make the same result into one big cell,like what we can do in Excel
Name Date
------------------
Jason Jul 2017
Tom
Andy Jun 2018
Mary
Alex Feb 2018
David Aug 2018
Is this possible in SQL Server? Or I need to do this change in the java or css layer
The exact output you want is something which is better handled in your presentation layer, e.g. using something like PHP, Java, etc. Perhaps the closest we might get to your output in SQL Server would be to do a group concatenation of the names by date, something like this:
SELECT
t1.Date,
STUFF((
SELECT ',' + t2.Name
FROM yourTable t2
WHERE t1.Date = t2.Date
FOR XML PATH(''), TYPE).value('.', 'NVARCHAR(MAX)'), 1, 1, '') AS Names
FROM (SELECT DISTINCT Date FROM yourTable) t1;
Demo

Getting calculated percentages within group using SQL

I have a dataset:
Date
June 2011
July 2011
Aug 2011
Sep 2011
Oct 2011
Jan 2012
Feb 2012
Mar 2012
Apr 2013
May 2013
that records down the date registered for each yearly project. (There are no project IDs however)
I would like to add in the additional variable Percentage, which represent the average progress made for that month. (For instance, if the project is registered for 4 months, then each month would progress incrementally by 25 %, (25,50,75,100)), specifically:
Percentage Date
20 June 2011
40 July 2011
60 Aug 2011
80 Sep 2011
100 Oct 2011
33 Jan 2012
66 Feb 2012
100 Mar 2012
50 Apr 2013
100 May 2013
However, my main problem would be that I am unable to know the starting month (period) and ending month (period) for each project for each year.
Are there any functions in SQL to create the calculated percentages in this case? I thought of creating a year variable and further using an indicator to indicate the start/end of the progress, but could not move on further.
Thank you again!
You can try this query.
Getting ROW_NUMBER() by year(dates) on subquery.
then get the percent.
SELECT (FLOOR(CAST(T2.RK AS decimal) * 100/(
SELECT COUNT(1) AS Totle
FROM T
WHERE year(dates) = T2.dates))) [Percentage],
T2.dates as [years],
T2.months as [months]
FROM
(
SELECT ROW_NUMBER() OVER(PARTITION BY year(dates) ORDER BY dates DESC) AS RK,
year(dates) as dates,
month(dates) as months
FROM T
) AS T2
GROUP BY T2.dates,T2.RK,T2.months
Here is a simple Pseudo sql to get what you want . ?
select Year(date), Month(date) ,
(select sum(Progress_percentage) from dataset b where b.date <=a.date ) as
subquery_percentage
from dataset a
group by Year(date), Month(date)

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);

oracle pivot query suggestion

I have a simple table that has data like the following
FiscalYear Month Value
2013 01 10
2013 02 15
....
2014 01 15
2014 02 20
using Oracle(11g) Pivot query is it possible to get something like this?
Month 2013 2014
01 10 15
02 15 20
SELECT month, value_2013, value_2014
FROM (SELECT fiscalyear, month, value FROM your_table)
PIVOT (SUM (value) AS value
FOR (fiscal_year)
IN ('2013', '2014'))