I can't figure out how to Order by with string_agg - string-aggregation

I have this query (I am using SQL Server 2019) and is working fine (combining Dates and Notes into one column). However, the result I am looking for is to have the latest date show up first.
How can I achieve that from this query?
SELECT ID,
​(SELECT string_agg(​concat(Date, ': ', Notes), CHAR(13) + CHAR(10) + CHAR(13) + CHAR (10)) as Expr1​
FROM(SELECT DISTINCT nd.Notes, nd.Date
FROM dbo.ReleaseTrackerNotes AS nd
INNER JOIN dbo.ReleaseTracker AS ac4 ON ac4.ID = nd.ReleaseTrackerID
WHERE (ac4.ID = ac.ID)) AS z_1) AS vNotes
FROM dbo.ReleaseTracker AS ac
GROUP BY ID
I have tried the ORDER BY but is not working
Here is my table:
CREATE TABLE [dbo].[ReleaseTrackerNotes](
[ID] [int] IDENTITY(1,1) NOT NULL,
[ReleaseTrackerID] [int] NULL,
[AOC_ModelID] [int] NULL,
[Date] [date] NULL,
[Notes] [nvarchar](800) NULL,
CONSTRAINT [PK_ReleaseTrackerNotes] PRIMARY KEY CLUSTERED
CREATE TABLE [dbo].[ReleaseTracker](
[ID] [int] IDENTITY(1,1) NOT NULL,
[AOC_ModelID] [int] NOT NULL,
[MotherboardID] [int] NOT NULL,
[StatusID] [int] NOT NULL,
[TestCateoryID] [int] NULL,
[TestTypeID] [int] NULL,
[DateStarted] [date] NULL,
[DateCompleted] [date] NULL,
[LCS#/ORS#] [nvarchar](20) NULL,
[ETCDate] [date] NULL,
[CardsNeeded] [nvarchar](2) NULL,
CONSTRAINT [PK_Compatibility] PRIMARY KEY CLUSTERED

Use WITHIN GROUP (ORDER BY ...):
SELECT
ID,
STRING_AGG(​TRY_CONVERT(varchar, Date, 101) + ': ' + Notes +
CHAR(13) + CHAR(10) + CHAR(13), CHAR(10))
WITHIN GROUP (ORDER BY Date DESC) AS Expr1​
FROM
(
SELECT DISTINCT ac4.ID, nd.Notes, nd.Date
FROM dbo.ReleaseTrackerNotes AS nd
INNER JOIN dbo.ReleaseTracker AS ac4
ON ac4.ID = nd.ReleaseTrackerID
) AS vNotes
GROUP BY ID;

Related

Add SQL SELECT COUNT result to an existing column as text

I have two tables:
1. TABLE [dbo].[ItemCategories](
[Id] [int] IDENTITY(1,1) NOT NULL,
[CategoryId] [int] NULL,
[StockId] [int] NULL,
2. TABLE [dbo].[Categories](
[Id] [int] IDENTITY(1,1) NOT NULL,
[ParentCategoryId] [int] NULL,
[CategoryName] [nvarchar](100) NULL,
[Slug] [nvarchar](150) NULL
And this query in SQL Server 2012
SELECT [CategoryName], [Slug], [ParentCategoryId], [Id]
FROM [Categories]
ORDER BY [ParentCategoryId] DESC
Which returns these rows
[CategoryName] [Slug] [ParentCategoryId] [Id]
Exercise exercise 42 46
Fashion fashion 42 47
And I have a second query:
SELECT COUNT(*)
FROM [ItemCategories]
WHERE CategoryId = '46' <--- This Id is the same as [Id] from the first query
How can I a modify the first query to add total count from the second query to the returned CategoryName column (as a single string) ?
Like this:
[CategoryName] [Slug] [ParentCategoryId] [Id]
Exercise (31) exercise 42 46
Fashion (56) fashion 42 47
I have created this join, but I don't know how to add the COUNT(*) as text
SELECT [CategoryName], [Slug], [ParentCategoryId], [Categories].[Id]
FROM [Categories]
INNER JOIN [ItemCategories] ON [Categories].[Id]=[ItemCategories].[CategoryId]
ORDER BY [ParentCategoryId] DESC
You can use the count(*) window function. I would put it in a separate column, but you can do:
SELECT [CategoryName] + ' (' + cast(count(*) over (partition by Id) as varchar(255)) + ')',
[Slug], [ParentCategoryId], [Id]
FROM [Categories]
ORDER BY [ParentCategoryId] DESC;
EDIT:
For two tables, use a JOIN and GROUP BY:
SELECT c.CategoryName + ' (' + cast(count(ic.Id) as varchar(255)) + ')',
c.Slug, c.ParentCategoryId, c.Id
FROM Categories c LEFT JOIN
ItemCategories ic
on ic.CategoryId = c.Id
GROUP BY c.CategoryName, c.slug, c.ParentCategoryId, c.id
ORDER BY ParentCategoryId DESC;

Error converting data type varchar to bigint in stored procedure

I'm trying to call this procedure with the usp_TimesheetsAuditsLoadAllbyId 42747, NULL command.
But I always get an error
Msg 8114, Level 16, State 5, Procedure usp_TimesheetsAuditsLoadAllById, Line 9
Error converting data type varchar to bigint.
The ID of TimesheetsAudits table is a bigint type. I tried several types of conversions and casts, but I'm really stuck right now.
Hope somebody can help. Thanks
ALTER PROCEDURE [dbo].[usp_TimesheetsAuditsLoadAllById]
(
#Id INT,
#StartDate DATETIME
)
AS
BEGIN
SET NOCOUNT ON
SELECT TOP 51 *
FROM
(SELECT TOP 51
ID,
Type,
ReferrerId,
CAST(Description AS VARCHAR(MAX)) AS Description,
OnBehalfOf,
Creator,
DateCreated
FROM
TimesheetsAudits
WHERE
(ReferrerID = #Id) AND
(#StartDate IS NULL OR DateCreated < #StartDate)
ORDER BY
DateCreated DESC
UNION
SELECT TOP 51
tia.ID,
tia.Type,
tia.ReferrerId,
'[Day: ' + CAST(DayNr AS VARCHAR(5)) + '] ' + CAST(tia.Description AS VARCHAR(MAX)) AS Description,
tia.OnBehalfOf,
tia.Creator,
tia.DateCreated
FROM
TimesheetItemsAudits tia
INNER JOIN
TimesheetItems ti ON tia.ReferrerId = ti.ID
WHERE
(ti.TimesheetID = #Id) AND
(#StartDate IS NULL OR tia.DateCreated < #StartDate)
ORDER BY
tia.DateCreated DESC) t
ORDER BY
t.DateCreated DESC
END
Table definition for tables from comments:
CREATE TABLE [dbo].[TimesheetsAudits](
[ID] [bigint] IDENTITY(1,1) NOT NULL,
[Type] [tinyint] NOT NULL,
[ReferrerId] [varchar](15) NOT NULL,
[Description] [text] NULL,
[OnBehalfOf] [varchar](10) NULL,
[Creator] [varchar](10) NOT NULL,
[DateCreated] [datetime] NOT NULL
)
CREATE TABLE [dbo].[TimesheetItemsAudits](
[ID] [bigint] IDENTITY(1,1) NOT NULL,
[Type] [tinyint] NOT NULL,
[ReferrerId] [varchar](15) NOT NULL,
[Description] [text] NULL,
[OnBehalfOf] [varchar](10) NULL,
[Creator] [varchar](10) NOT NULL,
[DateCreated] [datetime] NOT NULL
)
You perform an INNER JOIN of [dbo].[TimesheetsAudits] and TimesheetItems ti ON tia.ReferrerId = ti.ID
tia.[ReferrerId] is varchar and ti.[ID] is [bigint].
I'd expect a value in tia.[ReferrerId] that cannot be converted to bigint.
Try the following:
SELECT [ReferrerId] FROM TimesheetItemsAudits WHERE ISNUMERIC(ReferrerId) = 0
This may help you to find the "offending rows".

How can I PIVOT this data?

First, sorry for the middling title. Didn't have a ton of success on that front.
So - I have a table of data that has one row for each volunteer shift a person is scheduled for. Many people, being generous with their time, have signed up for multiple shifts. What I would like is to PIVOT this data s.t. I end up with one row per person, with sets of columns for each shift. I've spent about 90 minutes on this so far, but can't figure out how to PIVOT without an aggregation function applied.
What I mean by this is that each person is scheduled for at least one shift, so each person's row would have their unique ID, their name, their phone number, and their region. Then there would be a set of columns for Shift1 - role/status/start time/end time/etc. - which everyone would have filled out since everyone has signed up for at least one shift. Then, the Shift2 set of columns would be populated for people who have signed up for 2+ shifts, Shift3 for people who signed up for 3+ shifts, etc.
My current table schema:
CREATE TABLE [dbo].[confirmexport](
[PersonID] [int] NULL,
[EventType] [varchar](14) NULL,
[Shift] [varchar](10) NULL,
[StartDate] [datetime] NULL,
[StartTime] [datetime] NULL,
[EndDate] [datetime] NULL,
[EndTime] [datetime] NULL,
[Location] [varchar](39) NULL,
[Role] [varchar](16) NULL,
[Status] [varchar](9) NULL,
[FirstName] [varchar](20) NULL,
[LastName] [varchar](22) NULL,
[Phone] [bigint] NULL,
[Region] [varchar](9) NULL
) ON [PRIMARY]
All columns but PersonID, FirstName, LastName, Phone, and Region are shift-specific.
In an ideal world, I'd end up with a table that looked something like so:
CREATE TABLE [dbo].[confirmexportpivoted](
[PersonID] [int] NULL,
[Phone] [bigint] NULL,
[FirstName] [varchar](20) NULL,
[LastName] [varchar](22) NULL,
[Region] [varchar](9) NULL,
[EventType1] [varchar](14) NULL,
[Shift1] [varchar](10) NULL,
[StartDate1] [datetime] NULL,
[StartTime1] [datetime] NULL,
[EndDate1] [datetime] NULL,
[EndTime1] [datetime] NULL,
[Location1] [varchar](39) NULL,
[Role1] [varchar](16) NULL,
[Status1] [varchar](9) NULL,
[EventType2] [varchar](14) NULL,
[Shift2] [varchar](10) NULL,
[StartDate2] [datetime] NULL,
[StartTime2] [datetime] NULL,
[EndDate2] [datetime] NULL,
[EndTime2] [datetime] NULL,
[Location2] [varchar](39) NULL,
[Role2] [varchar](16) NULL,
[Status2] [varchar](9) NULL
) ON [PRIMARY]
Except with as many sets of columns as necessary for my data. OR - if that's a deal-breaker, I can definitely make do with 3. Any suggestions?
Thanks in advance - I am super-confused and would appreciate any and all help.
Well It was hard to me to reproduce the whole table, so I've worked only with Start and End Dates and Times, but solution should work for any number of columns. You can test it in SQL FIDDLE
SQL FIDDLE EXAMPLE
declare #columns nvarchar(max), #stmt nvarchar(max)
declare #Temp_Columns table (RowNum int, COLUMN_NAME nvarchar(128))
insert into #Temp_Columns
select R.RowNum, c.COLUMN_NAME
from
(
select row_number() over (partition by c.PersonID, c.FirstName, c.LastName, c.Phone, c.Region order by c.StartDate asc, c.EndDate asc) as RowNum
from confirmexport as c
) as R
inner join INFORMATION_SCHEMA.[COLUMNS] as c on c.TABLE_NAME = 'confirmexport' and c.COLUMN_NAME not in ('PersonID', 'FirstName', 'LastName')
order by 1, 2
select #columns = isnull(#columns + ', ', '') +
'min(case when A.RowNum = ' + cast(T.RowNum as nvarchar(128)) +
' then A.[' + T.COLUMN_NAME + '] else null end) as [' +
T.COLUMN_NAME + cast(T.RowNum as nvarchar(128)) + ']'
from #Temp_Columns as T
select #stmt = '
select
A.PersonId, A.FirstName, A.LastName, A.Phone, A.Region,' + #columns + '
from
(
select
c.*,
row_number() over (partition by c.PersonID, c.FirstName, c.LastName, c.Phone, c.Region order by c.StartDate asc, c.EndDate asc) as RowNum
from confirmexport as c
) as A
group by
A.PersonId, A.FirstName, A.LastName, A.Phone, A.Region'
exec sp_executesql
#stmt = #stmt

SQL query that can display data with a null ID and with not null id on the same Datatable?

Hello I have a question here...
I'll show you my tables first...
[Vendedores](
[IdVendedor] [int] IDENTITY(1,1) NOT NULL,
[IdGrupo] [int] NULL,
[IdInfoContacto] [int] NULL,
[ApellidoPaterno] [varchar](30) NULL,
[ApellidoMaterno] [varchar](30) NULL,
[Nombre] [varchar](35) NULL,
[Estado] [varchar](10) NULL,
CONSTRAINT [PK_Vendedores] PRIMARY KEY CLUSTERED
[Clientes](
[IdCliente] [int] IDENTITY(1,1) NOT NULL,
[IdGrupo] [int] NULL,
[IdVendedor] [int] NULL,
[IdDireccion] [int] NULL,
[IdInfoContacto] [int] NULL,
[FechaAlta] [date] NULL,
[ApellidoPaterno] [varchar](30) NULL,
[ApellidoMaterno] [varchar](30) NULL,
[Nombre] [varchar](40) NULL,
[Empresa] [text] NULL,
[Estado] [varchar](20) NULL,
[Estatus] [varchar](20) NULL,
CONSTRAINT [PK_Clientes] PRIMARY KEY CLUSTERED
[dbo].[Mensajes](
[IdMensaje] [int] IDENTITY(1,1) NOT NULL,
[IdCliente] [int] NULL,
[IdVendedor] [int] NULL,
[CorreoRemitente] [varchar](100) NULL,
[CorreoCliente] [varchar](100) NULL,
[CorreosAdicionales] [varchar](max) NULL,
[Tema] [varchar](100) NULL,
[Mensaje] [varchar](max) NULL,
[Fecha] [date] NULL,
[Hora] [time](5) NULL,
CONSTRAINT [PK_Mensajes] PRIMARY KEY CLUSTERED
[Archivos](
[IdArchivo] [int] IDENTITY(1,1) NOT NULL,
[IdMensaje] [int] NULL,
[Nombre] [varchar](max) NULL,
[Ubicacion] [varchar](50) NULL,
CONSTRAINT [PK_Archivos] PRIMARY KEY CLUSTERED
There are 2 ways to send messages in the system I'm developing...:
the first one is, you select a client, and when the message is sent, the client's ID (IdCliente) is taken from Clientes Table and inserted in a new row in Mensajes table.
that way, when you want to check all of the sent messages, you can clearly see to which client was sent.
the second one would be a free one, you send to whoever you want, you don't have to select a client. Therefore, when you register the new message, the IdCliente column would be null.
Some notes out of the way now: Vendedor is the seller or user who sent the message, Archivos means files, and in case you want to attach files, for each one of them, a new row in Archivos would be inserted and each with the same IdMensaje.
FOR THE moment, I have a query that allows me to see all my messages (with clients), and to also show how many files were attached to that message. HERE IT IS:
SELECT (Clientes.ApellidoPaterno + ' ' + Clientes.ApellidoMaterno + ' ' + Clientes.Nombre) AS Cliente, Mensajes.* FROM (SELECT Mensajes.IdMensaje, Mensajes.IdCliente, Mensajes.IdVendedor, Mensajes.CorreoRemitente, Mensajes.CorreoCliente, Mensajes.CorreosAdicionales, Mensajes.Tema, Mensajes.Mensaje, Mensajes.Fecha, Mensajes.Hora, COUNT(Archivos.IdArchivo) AS Archivos FROM Mensajes LEFT OUTER JOIN Archivos ON Mensajes.IdMensaje = Archivos.IdMensaje GROUP BY Mensajes.IdMensaje, Mensajes.IdCliente, Mensajes.IdVendedor, Mensajes.CorreoRemitente, Mensajes.CorreoCliente, Mensajes.CorreosAdicionales, Mensajes.Tema, Mensajes.Mensaje, Mensajes.Fecha, Mensajes.Hora) AS Mensajes JOIN Clientes ON Clientes.IdCliente = Mensajes.IdCliente ORDER BY Mensajes.Fecha DESC, Mensajes.Hora DESC
"(Clientes.ApellidoPaterno + ' ' + Clientes.ApellidoMaterno + ' ' + Clientes.Nombre) AS Cliente" Displays the full name of the client
however, now I want to have a query in which displays the same information as above... plus the following...
Messages with IdCliente = null
if IdCliente = null then the Cliente Column that I mentioned above would be "" (blank space)
If I need to be more specific, please let me know in what part must I give more information.
I hope you guys can help me
Thanks in advance
Firstly, change your JOIN to a LEFT JOIN:
LEFT JOIN Clientes ON Clientes.IdCliente = Mensajes.IdCliente
This will allow Messages with no associated IdCliente to remain. To get the desired blank space, use COALESCE:
COALESCE(Clientes.ApellidoPaterno + ' ' + Clientes.ApellidoMaterno + ' ' + Clientes.Nombre, '') AS Cliente

Select rows not in another table, SQL Server query

Subject table
CREATE TABLE [dbo].[BS_Subject](
[SubjectID] [bigint] IDENTITY(1,1) NOT NULL,
[DepartmentID] [bigint] NOT NULL,
[SubjectName] [varchar](50) NOT NULL,
[SubjectDescription] [varchar](100) NULL,
[SubjectShortCode] [varchar](10) NOT NULL,
CONSTRAINT [PK_Subject] PRIMARY KEY CLUSTERED
(
[SubjectID] ASC
)
SubjectToClass table
CREATE TABLE [dbo].[BS_SubjectToClass](
[SubjectToClassID] [bigint] IDENTITY(1,1) NOT NULL,
[SubjectID] [bigint] NOT NULL,
[ClassID] [bigint] NOT NULL,
CONSTRAINT [PK_BS_SubjectToClass] PRIMARY KEY CLUSTERED
(
[SubjectToClassID] ASC
)
I need list all the rows in the Subject table where subjectid is not in SubjectToClass table of a specified class.
I have this but unable to go any further
select Distinct(BS_Subject.SubjectID) DepartmentID,
SubjectName, SubjectDescription, SubjectShortCode
from dbo.BS_Subject
where BS_Subject.SubjectID <> (
SELECT Distinct(BS_Subject.SubjectID)
FROM dbo.BS_Subject, dbo.BS_SubjectToClass
Where BS_Subject.SubjectID = BS_SubjectToClass.SubjectID
And BS_SubjectToClass.ClassID = 2)
SELECT SubjectID, DepartmentID, SubjectName, SubjectDescription, SubjectShortCode
FROM BS_Subject
WHERE NOT EXISTS
(SELECT SubjectToClassID FROM BS_SubjectToClass WHERE
BS_Subject.SubjectID = BS_SubjectToClass.SubjectID
AND BS_SubjectToClass.ClassID =2)
You need to use the NOT IN operator - not the <> (that's VB or something....)
SELECT
DISTINCT(BS_Subject.SubjectID) DepartmentID,
SubjectName, SubjectDescription, SubjectShortCode
FROM dbo.BS_Subject
WHERE
BS_Subject.SubjectID NOT IN
(SELECT DISTINCT(BS_Subject.SubjectID)
FROM dbo.BS_Subject, dbo.BS_SubjectToClass
WHERE BS_Subject.SubjectID = BS_SubjectToClass.SubjectID
AND BS_SubjectToClass.ClassID = 2)