This question already has answers here:
GROUP BY / aggregate function confusion in SQL
(5 answers)
Closed 5 years ago.
I have SQL code like this :
SELECT
TB_DataProperti.*,
TBL_Rating.ISNULL(AVG(Rating), 0) AverageRating
FROM
TB_DataProperti
INNER JOIN
TBL_Rating ON TB_DataProperti.Kode_Properti = TBL_Rating.Kode_Properti
GROUP BY
TB_DataProperti.JudulListing
ORDER BY
AverageRating DESC
I get this error:
Msg 8120, Level 16, State 1, Line 3
Column 'TB_DataProperti.Kode_Properti' is invalid in the select list because it is not contained in either an aggregate function or the GROUP BY clause.
I just want to select all data columns using *, because I have many columns
Problem is You are trying to use aggregate function of one table and group by on another table.The rule is if you are using aggregate function with another column then that column should use in group by.Still try this I hope this is useful.
SELECT
TB_DataProperti.*,
ISNULL(AVG(TBL_Rating.Rating), 0) over (partition by TBL_Rating.Kode_Properti) as AverageRating
FROM
TB_DataProperti
INNER JOIN
TBL_Rating ON TB_DataProperti.Kode_Properti = TBL_Rating.Kode_Properti
ORDER BY
AverageRating DESC
Related
This question already has answers here:
Optimal way to concatenate/aggregate strings
(8 answers)
Simulating group_concat MySQL function in Microsoft SQL Server 2005?
(12 answers)
Closed 11 months ago.
I have 2 related tables as given below where I need to concatenate the names of group members into a query result as given below:
I am not sure whether this is possible with SQL. I tried to use group by, but I do not know a useful expression to search for concatenation in the context of group by. Any hints where and with what key words to search for will be gratefully appreciated.
use String_agg as follows
SELECT groupid,
groupname,
String_agg(groupmembername, ',') AS GroupMemberName
FROM lefttable1 t1
JOIN righttable2 t2
ON t1.groupid = t2.groupid
GROUP BY groupid,
groupname
or as follows
SELECT groupid,
groupname,
String_agg(groupmembername, ',') AS GroupMemberName
FROM (SELECT groupid,
groupname,
groupmembername
FROM lefttable1 t1
JOIN righttable2 t2
ON t1.groupid = t2.groupid) T3
This question already has answers here:
Select a random row from each group SQL Server
(4 answers)
Closed 1 year ago.
I want to get one random library card from each group, group are column "state"
select library card, state
from [stategov]..library
where states in (
'Ohio',
'Arizon'
)
group by state, librarycard
Should I use top 1? or CTE?
Since each state can have more than 10,000 library cards. What is the best way to get one random library cards from each state using group by?
You can try like following using ROW_NUMBER(). To get random you need to order by newid()
;WITH cte
AS (
SELECT *,ROW_NUMBER() OVER (PARTITION BY STATE ORDER BY (SELECT newid())) rn
FROM [stategov]..library WITH (NOLOCK)
WHERE states IN (
'Ohio'
,'Arizon'
)
)
SELECT *
FROM cte
WHERE rn = 1
This question already has answers here:
In SQL, how to select the top 2 rows for each group
(8 answers)
Closed 3 years ago.
I am trying to find the top 3 sick leavers per Site in my employee table. Im attempting to write a query to find the top sick leaver per site first.
Here is what I have but I can't seem to get it right
Here's what I have
select Site.SiteName,
[FullName] where MAX([SickLeaveTaken])
from Employee
left join Site
on(Employee.SiteID=Site.SiteID)
GROUP BY Site.SiteName;
GO
You can get top 3 employees with max sick leave by using RANK function in SQL
select top 3 EmployeeID,SickLeaveTaken,s.SiteName,rn from
(select e.EmployeeID,SickLeaveTaken,e.SiteID ,RANK() over(order by [SickLeaveTaken]
desc ) as rn
from #Employee e )e
left join #Site s on e.SiteID=s.SiteID
order by rn desc
Assuming that the table Employee contains the column SickLeaveTaken (although this is not normal) you need to sort descending the results and get TOP 3:
select TOP(3) WITH TIES
s.SiteName,
e.FullName,
MAX(e.SickLeaveTaken) maxleave
from Site s inner join Employee e
on e.SiteID = s.SiteID
GROUP BY s.SiteName, e.FullName
ORDER BY MAX(e.SickLeaveTaken) DESC;
I'm trying to find out the most dosed patients in a database. The sum of the doses has to be calculated and then I have to dynamically list out the patients who have been dosed that much. The query has to be dynamic, and there can be more than 5 patients listed - For example, the 5 most doses are 7,6,5,4,3 doses, but 3 people have gotten 5 doses, so I'd have to list out 7 people in total (the patients getting 7,6,5,5,5,4,3 doses). I'm having issues because you cannot refer to a named column in a where clause and I have no idea how to fix this.
The query goes like this:
SELECT
info.NAME, SUM(therapy.DOSE) AS total
FROM
dbo.PATIENT_INFORMATION_TBL info
JOIN
dbo.PATIENT_THERAPY_TBL therapy ON info.HOSPITAL_NUMBER = therapy.HOSPITAL_NUMBER
LEFT JOIN
dbo.FORMULARY_CLINICAL clinical ON clinical.ITEMID = therapy.ITEMID
WHERE
total IN (SELECT DISTINCT TOP 5 SUM(t.DOSE) AS 'DOSES'
FROM dbo.PATIENT_INFORMATION_TBL i
JOIN dbo.PATIENT_THERAPY_TBL t ON i.HOSPITAL_NUMBER = t.HOSPITAL_NUMBER
LEFT JOIN dbo.FORMULARY_CLINICAL c ON c.ITEMID = t.ITEMID
GROUP BY NAME
ORDER BY 'DOSES' DESC)
GROUP BY
info.NAME
ORDER BY
total DESC
The database looks like this:
The main question is: how can I use a where/having clause where I need to compare a calculated column to a list of dynamically calculated values?
I'm using Microsoft's SQL Server 2012. The DISTINCT in the subquery is needed so that only the top 5 dosages appear (e.g. without DISTINCT I get 7,6,5,4,3 with DISTINCT I get 7,6,6,5,4 and my goal is the first one).
Most DBMSes support Standard SQL Analytical Functions like DENSE_RANK:
with cte as
(
SELECT info.NAME, SUM(therapy.DOSE) as total,
DENSE_RANK() OVER (ORDER BY SUM(therapy.DOSE) DESC) AS dr
FROM dbo.PATIENT_INFORMATION_TBL info
JOIN dbo.PATIENT_THERAPY_TBL therapy ON info.HOSPITAL_NUMBER=therapy.HOSPITAL_NUMBER
LEFT JOIN dbo.FORMULARY_CLINICAL clinical ON clinical.ITEMID=therapy.ITEMID
GROUP BY info.NAME
)
select *
from cte
where dr <= 5 -- only the five highest doses
ORDER BY total desc
Btw, you probably don't need the LEFT JOIN as you're not selecting any column from dbo.FORMULARY_CLINICAL
This question already has answers here:
Update a table using JOIN in SQL Server?
(13 answers)
Closed 7 years ago.
I am VERY rusty with my SQL, it's been a few years, but I need to write a query to fix something in a table I have.
There are erroneous duplicates where not every column is the same, but I know at least one is.
So I have this query that works:
SELECT
[IntMsgID], [SortDate], COUNT(*)
FROM
[databasename].[dbo].[tblDoc]
GROUP BY
[IntMsgID], [SortDate]
HAVING
COUNT(*) >= 2
AND [IntMsgID] IS NOT NULL
It identifies the documents in question. What I need to do, then, is take the results from that and update another field simply with the value of Y or 1.
I've done searching and it seems any query I've tried to plug in fails, such as
UPDATE [databasename].[dbo].[tblDoc] AS t
INNER JOIN
(SELECT [IntMsgID] msgid
FROM [databasename].[dbo].[tblDoc]
GROUP BY [IntMsgID]) t1 ON t.[IntMsgID] = t1.[IntMsgID]
SET [JG_SQLDupe] = 'Y'
I get syntax errors at "AS" and "INNER" and "t1"
I would use a CTE to achieve this:
;with updts as (SELECT [IntMsgID], [SortDate], count(*)
FROM [databasename].[dbo].[tblDoc]
Group By [IntMsgID], [SortDate]
HAVING count(*) >= 2 AND [IntMsgID] is not null)
update t
set t.Flag = 'Y'
from [databasename].[dbo].[tblDoc] t inner join
updts u on t.[IntMsgID] = u.[IntMsgID]
and t.[SortDate] = u.[SortDate]