SELECT
CONCAT(First_Name, ' ' , Last_Name) AS [Player Name], GWG
FROM
nhl
ORDER BY
GWG DESC;
Here there are two players that have the highest values of 12; I need to print both names without using TOP or limit. I'm using SQL Server.
I suspect you're not aware of with ties.
Using
Select top(1) with ties
CONCAT(First_Name, ' ' , Last_Name) as [Player Name],
GWG
from nhl
order by GWG desc;
Will give you only the row(s) with the maximum value of GWG
Why the focus on NOT using TOP? Typically questions that seek to avoid some feature often are "solution" to different issues that are better addressed in a different manner. But here is one method.
with cte as (select max(GWG) as mGWG from dbo.nhl)
select ...
from dbo.nhl as nhl
inner join cte on nhl.GWG = cte.mGWG
order by ...
;
You can use this method:
SELECT
CONCAT(First_Name, ' ' , Last_Name) AS [Player Name], GWG
FROM
nhl
JOIN (select max(GWG) as GWG from nhl) maxGWG on nhl.GWG = maxGWG.GWG
;
or
SELECT
CONCAT(First_Name, ' ' , Last_Name) AS [Player Name], GWG
FROM
nhl
where GWG in (select max(GWG) from nhl)
;
Related
What i want to achieve is the concatenation of all DISTINCT surname values for each DISTINCT name values.
What i have manage is the concatenation of DISTINCT name values but unfortunately all surname values.
Below is my code:
SELECT DISTINCT ST2.[Name],
SUBSTRING(
(
SELECT ','+ST1.Surname AS [text()]
FROM [Ext_Names] ST1
WHERE ST1.[Name] = ST2.[Name]
ORDER BY ST1.[Name]
FOR XML PATH ('')
), 2, 1000) [Surname]
FROM [Ext_Names] ST2
Sample Data
Result
Desired output
You need to select the distinct values first, then aggregate. If you are running SQL Server 2017 or higher, you can use string_agg():
select name, string_agg(surname, ',') within group (order by surname) surnames
from (select distinct name, surname from ext_names) t
group by name
You can done with this.
SELECT DISTINCT ST2.[Name], SUBSTRING((SELECT ','+ T.Surename AS [text()] FROM (SELECT DISTINCT ST1.Surename FROM Ext_Names ST1 WHERE ST1.Name = ST2.Name ) T ORDER BY T.Surename FOR XML PATH('')),2,1000) [SureEname]
FROM Ext_Names ST2
This is my code for a query to produce a specific report. This works, however when I added the last select statement
(Select Name
from RPT_CUSTOM_LIST_VALUES
where CUSTOM_PROPERTY_VALUE_ID = RI1.CUST_10) AS [Application]
The RI1.Cust_10 column holds multiple values delimited by commas. How can I get it so that the look up table pulls each value and provides the correct name for that value? I cannot create or modify the tables within this database.
select
RI1.incident_id as [Project Incident #],
(Select Name from RPT_CUSTOM_LIST_VALUES
where CUSTOM_PROPERTY_VALUE_ID = RI1.CUST_02) as [Business Name],
RI1.NAME as [Project Name],
RI1.INCIDENT_STATUS_NAME as [Phase],
(Select Name from RPT_CUSTOM_LIST_VALUES
where CUSTOM_PROPERTY_VALUE_ID = RI1.CUST_09) as [Key Milestone Name],
convert(nvarchar(10), RI1.CUST_26,103) as [Key Milestone Date], -- leave as date
convert(nvarchar(10), RI1.CUST_29,103) as [Target Completion Date], -- leave as date
RI1.SEVERITY_NAME as [Status Color],
RI1.CUST_01 as [Status Summary],
RI1.OWNER_NAME as [IT Owner],
(Select Name from RPT_CUSTOM_LIST_VALUES
where CUSTOM_PROPERTY_VALUE_ID = RI1.CUST_10) AS [Application]
from
RPT_INCIDENTS RI1
where
RI1.PROJECT_ID = 445
and RI1.IS_DELETED = 0
and (RI1.INCIDENT_STATUS_NAME <> '5.1-Cancelled' and RI1.INCIDENT_STATUS_NAME <> '5.2-Completed')
My output should be, however, the last column should have names not values. The values are from the Lookup table and I need a way to pull that data so that the values are now names.
Report Output
I can't test without your data, but hopefully it will work for you:
select RI1.incident_id as [Project Incident #]
, [Business Name] = s1.Name
,RI1.NAME as [Project Name]
,RI1.INCIDENT_STATUS_NAME as [Phase]
, [Key Milestone Name] = s2.Name
,convert(nvarchar(10), RI1.CUST_26,103) as [Key Milestone Date] -- leave as date
,convert(nvarchar(10), RI1.CUST_29,103) as [Target Completion Date] -- leave as date
,RI1.SEVERITY_NAME as [Status Color]
,RI1.CUST_01 as [Status Summary]
,RI1.OWNER_NAME as [IT Owner]
, [Application] = LEFT(s3.App,LEN(APP) - SIGN(LEN(s3.APP)))
From RPT_INCIDENTS AS RI1
OUTER APPLY (Select TOP 1 i.Name from RPT_CUSTOM_LIST_VALUES as i where i.CUSTOM_PROPERTY_VALUE_ID = RI1.CUST_02) as s1
OUTER APPLY (Select TOP 1 i.Name from RPT_CUSTOM_LIST_VALUES as i where i.CUSTOM_PROPERTY_VALUE_ID = RI1.CUST_09) as s2
OUTER APPLY (SELECT App = (
Select i.Name + ','
from RPT_CUSTOM_LIST_VALUES as i
where i.CUSTOM_PROPERTY_VALUE_ID = RI1.CUST_10
FOR XML PATH(''))
) as s3
where RI1.PROJECT_ID = 445
and RI1.IS_DELETED = 0
and (RI1.INCIDENT_STATUS_NAME <> '5.1-Cancelled' and RI1.INCIDENT_STATUS_NAME <> '5.2-Completed')
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.
I am trying to use a left join and concat 2 column names so that they display together in a dropdown list. This is my query so far.
SELECT DISTINCT CONCAT(CAST(Index.MR_ID AS INT),' - ', Master.MR_Name) AS MR_ID
FROM Index
LEFT JOIN Master
ON Master.MR_ID=Index.MR_ID
I want to order it by the MR_ID in the Index table but whenever I try to add an ORDER BY to the query, it will not work for me. Can someone help me out here?
WITH cte AS (
SELECT DISTINCT CONCAT(CAST(Index.MR_ID AS INT),' - ', Master.MR_Name) AS MR_ID
FROM [Index]
LEFT JOIN Master
ON Master.MR_ID=Index.MR_ID
)
SELECT *
FROM
cte
ORDER BY
MR_ID;
The above is if you want to order by the final column you got.
If you want to order by the MR_ID in the Index or Master table, it is not possible because you are using the DISTINCT operator, which means it is undetermined by the final query. In that case, you will need the below
WITH cte AS (
SELECT DISTINCT CONCAT(CAST(Index.MR_ID AS INT),' - ', Master.MR_Name) AS MR_ID
, Index.MR_ID AS sort_column
FROM [Index]
LEFT JOIN Master
ON Master.MR_ID=Index.MR_ID
)
SELECT MR_ID
FROM
cte
ORDER BY
sort_column;
Mister Positive beat me to it. I'd also prepare for nulls since you are using a left join.
select distinct concat(cast(index.mr_id as int),' - ', isnull(master.mr_name,'') as mr_id
from sindex
left join master on master.mr_id=index.mr_id
order by concat(cast(index.mr_id as int),' - ', isnull(master.mr_name,'')
I think your after this
SELECT DISTINCT CONCAT(CAST(Index.MR_ID AS INT),' - ', IsNull(Master.MR_Name, '')) AS MR_ID
FROM SIndex
LEFT JOIN Master ON Master.MR_ID=SIndex.MR_ID
Order by CONCAT(CAST(Index.MR_ID AS INT),' - ', IsNull(Master.MR_Name, ''))
This is my query having the Current Results as displayed below.
SELECT
Distinct CONVERT(int, Employees_1.Emp_Badge_No) AS Emp_Badge_No,
Employees_1.Emp_LastName, Employees_1.Emp_FirstName, Employees_1.Email,
Employees_1.NT_Name, Employees_1.Dept_key,
Employees_1.Emp_LastName + ',' + Employees_1.Emp_FirstName AS FullName,
dbo.department_vw.DepartmentShortName AS deptname,
Employees_1.active_flag
FROM data_common.dbo.employees_union_vw AS Employees_1
INNER JOIN dbo.department_vw
ON Employees_1.Dept_key = dbo.department_vw.DepartmentKey
Sample data:
I need help to achieve the Expected Results. What will I modify with my existing sql query?
I want to keep all the records even though it is inactive as long as the Emp_Badge_No is not repeated. I only want those duplicate Emp_Badge_No to be remove.
Thanks in advance.
You may want to use ROW_NUMBER for this. Modify the ORDER BY clause depending on which row from the duplicate entry you want to retrieve:
WITH Cte AS(
SELECT
e.Emp_Badge_No,
e.Emp_LastName,
e.Emp_FirstName,
e.Email,
e.NT_Name,
e.Dept_key,
e.Emp_LastName + ',' + e.Emp_FirstName AS FullName,
d.DepartmentShortName AS deptname,
e.active_flag,
rn = ROW_NUMBER() OVER(PARTITION BY e.Emp_Badge_No ORDER BY e.Active)
FROM data_common.dbo.employees_union_vw AS e
INNER JOIN dbo.department_vw d
ON e.Dept_key = d.DepartmentKey
)
SELECT
Emp_Badge_No,
Emp_LastName,
Emp_FirstName,
Email,
NT_Name,
Dept_key,
FullName,
deptname,
active_flag
FROM Cte
WHERE rn = 1
The above will get the Inactive record if there are duplicates. If you want to get the Active records instead, replace rn with:
ROW_NUMBER() OVER(PARTITION BY Emp_Badge_No ORDER BY e.Active DESC)
If you don't care whether it's Active or Inactive, replace rn with:
ROW_NUMBER() OVER(PARTITION BY Emp_Badge_No ORDER BY (SELECT NULL))