I have the records below. I want to show subjects when Monday in column all subjects appear on Monday with start time and end time. How do I do this?
SELECT t.teacher_name, tci.class_name, tsb.Subject_Name, tdn.DaysName,
tss.subject_start, tss.subject_end
FROM tblsubjectSchedule tss
INNER JOIN tblsubjects tsb ON tss.subject_id = tsb.Idx
INNER JOIN tblclassinfo tci ON tss.class_id = tci.Idx
INNER JOIN tbldaysnames tdn ON tss.days_id = tdn.Idx
INNER JOIN tblteacher t ON tss.techer_id = t.Idx
WHERE tss.class_id = 2 AND t.school_id = 1
Your attachment is not completely clear. Are you going to ignore teacher_name on result?
In your screenshot you haven't included 'samad teacheer' on Monday results. You want the query only for Monday date?
If you only focus on class and days and date, I got below. please refer the attachment.Let me know if it works for you and accept my answer.
SELECT t1.class_name,t1.daysname,
subject_name =REPLACE( (SELECT (subject_name+'-start:'+CONVERT(VARCHAR, subject_start, 120)+'-End:'+CONVERT(VARCHAR, subject_end, 120)) AS [data()]
FROM [practice].[dbo].[test] t2
WHERE t2.daysname = t1.daysname
ORDER BY subject_name
FOR XML PATH('')
), ' ', ';')
FROM [practice].[dbo].[test] t1
GROUP BY daysname,class_name ;
You could also add teacher name to the schedule column: see the attachment
SELECT t1.class_name,t1.daysname,
schedule =REPLACE( (SELECT (teacher_name+'-'+subject_name+'-start:'+CONVERT(VARCHAR, subject_start, 120)+'-End:'+CONVERT(VARCHAR, subject_end, 120)) AS [data()]
FROM [practice].[dbo].[test] t2
WHERE t2.daysname = t1.daysname
ORDER BY subject_name
FOR XML PATH('')
), ' ', ';')
FROM [practice].[dbo].[test] t1
GROUP BY daysname,class_name ;
Here is the query only for the 'Monday'
SELECT t1.class_name,t1.daysname,
schedule =REPLACE( (SELECT (teacher_name+'-'+subject_name+'-start:'+CONVERT(VARCHAR, subject_start, 120)+'-End:'+CONVERT(VARCHAR, subject_end, 120)) AS [data()]
FROM [practice].[dbo].[test] t2
WHERE t2.daysname = t1.daysname
ORDER BY subject_name
FOR XML PATH('')
), ' ', ';')
FROM [practice].[dbo].[test] t1 where daysname='Monday'
GROUP BY daysname,class_name ;
Related
I got introduced to SSRS recently and I am facing a problem in extracting data from the database.
I searched online and I found out that this is called concatenate text from multiable rows. Now, I tried a code.. but in the results, it give me the data duplicated next to each other separated by a comma. Now I am happy that it is separating the result with a coma... but why giving me the data duplicated?
The Code:
select distinct wm1.Date, p1.[Medical Record Number], Wound_Type =CONCAT
((
select distinct CAST(wt2.Name as VARCHAR(MAX))+ ' /// ' from dbo.[Wound Type] as wt2
inner join dbo.[Wound Management] as wm2 on wm2.[Wound Type_ID] = wt2.ID
inner join dbo.Patient as p2 on wm2.[Owner (Patient)_Patient_ID]=p2.ID
where wm1.Date=wm2.Date
FOR XML PATH('')
),wt1.Name)
from dbo.[Wound Type] as wt1
inner join dbo.[Wound Management] as wm1 on wm1.[Wound Type_ID] = wt1.ID
inner join dbo.Patient as p1 on wm1.[Owner (Patient)_Patient_ID]=p1.ID
group by wm1.Date, wt1.Name, p1.[Medical Record Number]
the result:
Please Help.
Your data is duplicated because you are CONCATing the list of wt2.Names with the wt1.Name - ,wt1.Name). The one with NULL date isn't finding a matching record and is only showing the wt1.Name
Actually I think you may want to use the STUFF function instead of CONCAT to remove any extra slashes at the end.
SELECT DISTINCT wm1.Date, p1.[Medical Record Number], Wound_Type = STUFF(
(
SELECT DISTINCT ' /// ' + CAST(wt2.Name as VARCHAR(MAX))
from dbo.[Wound Type] as wt2
inner join dbo.[Wound Management] as wm2 on wm2.[Wound Type_ID] = wt2.ID
inner join dbo.Patient as p2 on wm2.[Owner (Patient)_Patient_ID]=p2.ID
where wm1.Date=wm2.Date
FOR XML PATH('')
), 1, 5, '' )
from dbo.[Wound Type] as wt1
inner join dbo.[Wound Management] as wm1 on wm1.[Wound Type_ID] = wt1.ID
inner join dbo.Patient as p1 on wm1.[Owner (Patient)_Patient_ID]=p1.ID
group by wm1.Date, wt1.Name, p1.[Medical Record Number]
This doesn't fix that NULL date issue. You could wrap the STUFF in an ISNULL.
SELECT DISTINCT wm1.Date, p1.[Medical Record Number], Wound_Type = ISNULL(STUFF(
(
SELECT DISTINCT ' /// ' + CAST(wt2.Name as VARCHAR(MAX))
from dbo.[Wound Type] as wt2
inner join dbo.[Wound Management] as wm2 on wm2.[Wound Type_ID] = wt2.ID
inner join dbo.Patient as p2 on wm2.[Owner (Patient)_Patient_ID]=p2.ID
where wm1.Date=wm2.Date
FOR XML PATH('')
), 1, 5, '' )
, wt1.Name)
from dbo.[Wound Type] as wt1
inner join dbo.[Wound Management] as wm1 on wm1.[Wound Type_ID] = wt1.ID
inner join dbo.Patient as p1 on wm1.[Owner (Patient)_Patient_ID]=p1.ID
group by wm1.Date, wt1.Name, p1.[Medical Record Number]
Here's an explanation of stuff:
How Stuff and 'For Xml Path' work in SQL Server?
Please help, I have a problematic query to display the column OrderNo2.
the following is the query:
SELECT
V.id, V.TypeApv, V.CreateDate,
P.Requestor, VE.VendorName, V.InvoiceNo, V.Hawb,
PA.PaymentFor, V.Amount, V.Curr, V.DueDate, V.Remarks, V.OrderNo,
(SELECT
STUFF((SELECT DISTINCT ', ' + CASE WHEN [e-SAM Case]='Subsequent' OR [e-SAM Case]='DDT' AND [Local SAP PO] LIKE '5%' OR [e-SAM Case]='FBS 4'
THEN PoNo ELSE [Local SAP PO] END
FROM v_copo VC
WHERE VC.AWB1 = V.Hawb
FOR XML PATH('')), 1, 1, '')) AS OrderNo2,
(SELECT ISNULL(OrderNo2, V.OrderNo)) AS OrderNoFinal
FROM
APV_AF V
LEFT JOIN
APV_Person P ON P.id = V.Requestor
LEFT JOIN
APV_Vendor VE ON VE.IDVendor = V.VendorName
LEFT JOIN
APV_Payment PA ON PA.IDPayment = V.PaymentFor
ORDER BY
V.CreateDate DESC
You cannot reference an alias in the same scope where it was defined (apart from the order by clause). Typical work arounds include a subquery or CTE.
In SQL Server though, a simple option is a lateral join:
SELECT
V.id, V.TypeApv, V.CreateDate,
P.Requestor, VE.VendorName, V.InvoiceNo, V.Hawb,
PA.PaymentFor, V.Amount, V.Curr, V.DueDate, V.Remarks, V.OrderNo,
X.OrderNo2,
ISNULL(X.OrderNo2, V.OrderNo) AS OrderNoFinal
FROM APV_AF V
LEFT JOIN APV_Person P ON P.id = V.Requestor
LEFT JOIN APV_Vendor VE ON VE.IDVendor = V.VendorName
LEFT JOIN APV_Payment PA ON PA.IDPayment = V.PaymentFor
OUTER APPLY (
SELECT
STUFF((SELECT DISTINCT ', ' + CASE WHEN [e-SAM Case]='Subsequent' OR [e-SAM Case]='DDT' AND [Local SAP PO] LIKE '5%' OR [e-SAM Case]='FBS 4'
THEN PoNo ELSE [Local SAP PO] END
FROM v_copo VC
WHERE VC.AWB1 = V.Hawb
FOR XML PATH('')), 1, 1, '') AS OrderNo2
) X
ORDER BY V.CreateDate DESC
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 3 years ago.
Improve this question
I have two tables as follow:
Let's say tbl1(Which keeps the main applicant info) and tbl2(the main applicant with family members info, note that we must keep the main applicant here too)
I'm trying to check whether the main applicant added oneself to tbl2 or not (that means if main applicant info is in tbl2)
CID FullName
-----------------------
1001 AYNALEM A NIGUSSIE
while in tbl2 (Same CID as tbl1):
HID CID FullName
-------------------------------------
1 1001 AYNALEM A NIGUSSIE
2 1001 CARLSTON HEITH
3 1001 Q LEE
I declared a variable to hold FullName list from tbl2 and compare the full name from tbl1 (to check if it exists in tbl2, function [dbo].[ufn_GetFullName] is used to get fullName from tbl1 ).
DECLARE #HH_FullName VARCHAR(250);
SELECT
#HH_FullName = (SELECT
STUFF((SELECT DISTINCT ', '+ DR.FullName
FROM
(SELECT DR.HID, DR.FullName
FROM dbo.tbl1 DR WITH(NOLOCK)
WHERE DR.CID = PC.CID) DR
FOR XML PATH(''), TYPE).value('.', 'varchar(max)'), 1, 2, ''))
FROM [dbo].[tbl2] PC WITH (NOLOCK)
LEFT JOIN [dbo].[tbl1] HC WITH (NOLOCK) ON HC.CID = PC.CID
WHERE PC.CID = 100037
/* --SELECT #HH_FullName -- 'AYNALEM A NIGUSSIE', 'CARLSTON
HEITH', 'Q LEE'
SELECT
(STUFF((SELECT ',''' + Val+''''
FROM
(SELECT CONVERT(VARCHAR(250), value) AS Val
FROM string_split(#HH_FullName, ',')) DF
FOR XML PATH('')), 1, 1, '')) -- 'AYNALEM A NIGUSSIE',' CARLSTON HEITH',' Q LEE'
SELECT CONVERT(VARCHAR(250), value) AS Val
FROM string_split(#HH_FullName, ',')
*/
SELECT *
FROM [dbo].[tbl1] PC WITH(NOLOCK)
LEFT JOIN [dbo].[tbl2] HC WITH(NOLOCK) ON HC.ClaimantID = PC.ClaimantID
WHERE HC.CID IS NOT NULL
AND ([dbo].[ufn_GetFullName](Pc.FirstName, Pc.MiddleName, Pc.LastName)
NOT IN ( SELECT ( STUFF((
SELECT ',''' + Val+''''
FROM (SELECT convert(VARCHAR(250), value) AS Val FROM string_split(#HH_FullName, ','))DF
FOR XML PATH('')
), 1, 1, '')) ))
-- OR as follow
SELECT * FROM [dbo].[tbl1] PC WITH(NOLOCK)
LEFT JOIN [dbo].[tbl2] HC WITH(NOLOCK) ON
HC.CID = PC.CID
WHERE HC.CID IS NOT NULL AND
( [dbo].[ufn_GetFullName]
(Pc.FirstName,Pc.MiddleName,Pc.LastName) NOT IN (SELECT convert(VARCHAR(250), value)
as VAL FROM string_split(#HH_FullName, ',') ) )
-- Also tried this way, it won't work either
SELECT * FROM [dbo].[tbl1] PC WITH(NOLOCK)
LEFT JOIN [dbo].[tbl2] HC WITH(NOLOCK) ON
HC.CID = PC.CID
WHERE HC.CID IS NOT NULL AND
( [dbo].[ufn_GetFullName]
(Pc.FirstName,Pc.MiddleName,Pc.LastName) NOT IN ( #HH_FullName )
WHERE Cond NOT IN (#HH_FullName) not returning the right value.
Like #UnhandledExcepSean commented you are complicating this beyond your stated objective...
I want to check if the FullName in tbl1 also exists in tbl2.
This statement will do it...
SELECT t1.*
FROM tbl1 AS t1
INNER JOIN tbl2 AS t2
ON CONCAT_WS(' ', t1.FirstName, t1.MiddleNAme, t1.LastName) = t2.FullName
This would work too...
SELECT t1.*
FROM tbl1 as t1
WHERE EXISTS (
SELECT * FROM tbl2
WHERE FullName = CONCAT_WS(' ', t1.FirstName, t1.MiddleNAme, t1.LastName))
Another way...
SELECT t1.*
FROM tbl1 as t1
WHERE CONCAT_WS(' ', t1.FirstName, t1.MiddleNAme, t1.LastName)
IN (SELECT FullName FROM tbl2)
You will have to account for the case where there is no middle name. Is it NULL or just an empty string? Or perhaps even spaces?
For small volumes of data you are not going to be able to detect a difference in performance. However, for larger amounts of data I believe you will be better off with the EXISTS approach.
This question already has an answer here:
SQL Server recursive self join
(1 answer)
Closed 3 years ago.
I have a SQL Server table that holds groups and subgroups that I am using to define user permissions in application. What I am trying to accomplish is to select all users ids from all subgroups based on used id assigned to group.
I have tried with this query but it only shows one level of subgroup users.
SET #UserIds =
ISNULL(
STUFF((SELECT ',' + CAST(tbl.UserID AS VARCHAR(MAX))
FROM
(SELECT grpUser.UserId AS UserID
FROM dbo.GroupUser grpUser
INNER JOIN (SELECT subGrp.Id, subGrp.GroupName
FROM dbo.Groups grp
INNER JOIN dbo.GroupUser guser ON grp.Id = guser.GroupId
AND guser.UserId = #UserId
INNER JOIN dbo.Groups subGrp ON grp.Id = subGrp.ParentGroupId) tbl ON grpUser.GroupId = tbl.Id) AS tbl
FOR XML PATH('')), 1, 1, ''), '')
I am struggling to get users ids in all subgroups. Number of subgroups is not defined. Is there a way to do in sql or should I shift this part to application side? Thank you for any advice.
Finally I came up with this code. Thank you for hint.
declare #UserGroupId int;
Set #UserGroupId = (Select GroupId from GroupUser gu
left join dbo.Groups gr on gu.GroupId = gr.id
where UserId = #UserId and gr.IsDeleted = 0)
declare #UsersGroups Table (
Id int not null,
GroupName nvarchar(50) not null
)
;WITH grp AS
(
SELECT Id, GroupName
FROM Groups
WHERE ParentGroupId = 1
UNION ALL
SELECT g.Id, g.GroupName
FROM Groups g
INNER JOIN dbo.GroupUser guser ON g.Id = guser.GroupId
INNER JOIN grp ON g.ParentGroupId = grp.Id
)
Insert into #UsersGroups
SELECT Id, GroupName
FROM grp
SET #UserIds = ISNULL(STUFF(( SELECT ',' + CAST(tbl.UserID AS VARCHAR(max)) FROM (
SELECT grpUser.UserId AS UserID FROM dbo.GroupUser grpUser
INNER JOIN ( SELECT * FROM #UsersGroups
) tbl ON grpUser.GroupId = tbl.Id
) AS tbl FOR XML PATH('') ), 1, 1, ''), '')
I work with the AventureWorks2014 database in Microsoft SQL Server. I need to create a view that will show the CustomerID, the full name and the TOTAL amount sold to client through the web.
My problem is that I can't seem to get the values corresponding to a single customer add up so that a single customer answers to a single line in my result. This is the code I have, any help would be appreciated. I basically need to show the total amount sold to clients on the web.
if object_id('vTotalWebSalesPerCustomer', 'v') is not null
drop view vTotalWebSalesPerCustomer;
go
create view vTotalWebSalesPerCustomer
as
select distinct
c.CustomerID,
ltrim(rtrim(concat(concat(concat(concat(concat(concat(concat(p.Title, ' '), p.LastName), ', '), ' '), p.FirstName), ' '), p.Suffix))) as NomClient,
soh.TotalDue
from
[Sales].[Customer] as c
left join
[Person].[Person] as p on c.CustomerID = p.BusinessEntityID
left join
[Sales].[SalesOrderHeader] as soh on soh.CustomerID = c.CustomerID
where
year(soh.OrderDate) = 2014
and datepart(quarter, soh.OrderDate) = 1
and [OnlineOrderFlag] = 1
go
select *
from vTotalWebSalesPerCustomer
Thanks
Sounds like you need to use GROUP BY and SUM(), example:
SELECT column-names
FROM table-name
WHERE condition
GROUP BY column-names
ORDER BY column-names
Such as:
SELECT SUM(O.TotalPrice), C.FirstName, C.LastName
FROM [Order] O JOIN Customer C
ON O.CustomerId = C.Id
GROUP BY C.FirstName, C.LastName
ORDER BY SUM(O.TotalPrice) DESC
Would return:
Sum FirstName LastName
117483.39 Horst Kloss
115673.39 Jose Pavarotti
113236.68 Roland Mendel
57317.39 Patricia McKenna
52245.90 Paula Wilson
34101.15 Mario Pontes
32555.55 Maria Larsson
DISTINCT filters result rows to remove duplicates. What I think you want is to aggregate rows. Perhaps this will do what you want:
create view vTotalWebSalesPerCustomer as
select c.CustomerID,
ltrim(rtrim(concat(concat(concat(concat(concat(concat(concat(p.Title, ' '), p.LastName), ', '), ' '), p.FirstName), ' '), p.Suffix))) as NomClient,
sum(soh.TotalDue) as TotalDue
from [Sales].[Customer] as c
left join [Person].[Person] as p on c.CustomerID = p.BusinessEntityID
left join [Sales].[SalesOrderHeader] as soh on soh.CustomerID = c.CustomerID
where year(soh.OrderDate) = 2014 and datepart(quarter, soh.OrderDate)=1 and [OnlineOrderFlag] = 1
group by c.CustomerID,NomClient
Note that I removed distinct, added sum operator on the amount you want to total, and group by the customer id and name fields (SQL Server requires that you list in the GROUP BY all result columns which are not being aggregated).
You can use the following VIEW using a GROUP BY on the SELECT:
IF OBJECT_ID('vTotalWebSalesPerCustomer', 'v') IS NOT NULL
DROP VIEW vTotalWebSalesPerCustomer;
GO
CREATE VIEW vTotalWebSalesPerCustomer AS
SELECT
x.CustomerID,
LTRIM(RTRIM(CONCAT(p.Title, ' ', p.LastName, ', ', p.FirstName, ' ', p.Suffix))) AS NomClient,
x.TotalDue
FROM (
SELECT
c.CustomerID AS CustomerID,
SUM(soh.TotalDue) AS TotalDue
FROM [Sales].[Customer] AS c
LEFT JOIN [Sales].[SalesOrderHeader] AS soh ON soh.CustomerID = c.CustomerID
WHERE YEAR(soh.OrderDate) = 2014 AND DATEPART(quarter, soh.OrderDate) = 1 AND [OnlineOrderFlag] = 1
GROUP BY c.CustomerID
)x LEFT JOIN [Person].[Person] AS p ON x.CustomerID = p.BusinessEntityID
GO
Note: You only need one CONCAT function to concat all string values.