How to use count function in the below mentioned query? - sql-server

I just want to know how to add count function in the below mentioned query in which I only have to display those records where count of p_id should equal to 1
select Distinct p_id,i.img_path as ImagesName,
(
select p.project_details from [Project] p where p.p_id=i.p_id
) as ProjectName
from [p_Image] i

Please try:
SELECT
*
FROM(
select
p_id,
i.img_path as ImagesName,
(
select p.project_details from [Project] p where p.p_id=i.p_id
) as ProjectName,
COUNT(*) OVER (PARTITION BY p_id) CNT
from [p_Image] i
)x WHERE CNT=1

Try this:
select p_id, i.img_path as ImagesName, p.project_details as ProjectName, count(*) as sum
from [p_Image] i
join [Project] p on (p.p_id=i.p_id)
group by p_id
having sum = 1;

Your query is equivalent to:
select p_id, i.img_path as ImagesName, p.project_details as ProjectName
from p_Image i left outer join
project p
on p.p_id=i.p_id;
Here are two ways to get "unique" p_id:
select p_id, min(i.img_path) as ImagesName, min(p.project_details) as ProjectName
from p_Image i left outer join
project p
on p.p_id = i.p_id
group by p_id
having count(*) = 1;
The logic: when the count is 1, then the min() there is only one value for img_path and project_details. The min() returns that value.
The following uses a subquery with a window function instead:
select p_id, ImagesName, ProjectName
from (select p_id, i.img_path as ImagesName, p.project_details as ProjectName,
count(*) over (partition by p_id) as cnt
from p_Image i left outer join
project p
on p.p_id=i.p_id
) pi
where cnt = 1;

Related

Single-row subquery returns more than one row - Issue in table conversion in snowflake

I am having a issue related to Snowflake. When I run the below query, I do get 'Single-row subquery returns more than one row.'
Do you have any idea on how to fix this issue?
Below is the query
SELECT DISTINCT J.JOB_ID , J.ACCEPTED_DATE ,
(SELECT DOCUMENT_ID FROM (SELECT DD.DOCUMENT_ID ,row_number() over (partition by DD.DOCUMENT_ID order by DD.DOCUMENT_ID ) as row_number
FROM SFG.RCM_JOB_INDUSTRY DD
LEFT JOIN USERS_L.JOB_MASTER J ON DD.JOB_ID = J.JOB_ID) WHERE row_number=1) AS DOCUMENT_ID,
(SELECT U.USERNAME FROM SFL.RCS_USER U LEFT JOIN SMW.RCS_STATUS RS ON RS.USER_ID = U.USER_ID) AS REPAIRED_BY
FROM USERS_L.JOB_MASTER J
Thank you
There are 2 subqueries, and I suspect the second one causing the issue:
SELECT
DISTINCT J.JOB_ID,
J.ACCEPTED_DATE,
(
SELECT
DOCUMENT_ID
FROM
(
SELECT
DD.DOCUMENT_ID,
row_number() over (
partition by DD.DOCUMENT_ID
order by
DD.DOCUMENT_ID
) as row_number
FROM
SFG.RCM_JOB_INDUSTRY DD
LEFT JOIN USERS_L.JOB_MASTER J ON DD.JOB_ID = J.JOB_ID
)
WHERE
row_number = 1
) AS DOCUMENT_ID,
(
SELECT
TOP 1 U.USERNAME
FROM
SFL.RCS_USER U
LEFT JOIN SMW.RCS_STATUS RS ON RS.USER_ID = U.USER_ID
) AS REPAIRED_BY
FROM
USERS_L.JOB_MASTER J
https://docs.snowflake.com/en/sql-reference/constructs/top_n.html

query is not returning distinct record

Hi can you please take a look why my query is not returning distinct record. i want result with following condition OE1='SCHEDCHNG', need only recent records per orderid or ordernum means only one record per ordernum or orderid and also dropdate is null. My query is as below
select DISTINCT TOP 100 OE.ORDERID,OE.ID,OE.ORDERNUM,OE.OE4 from OrderExports OE
inner join (
select ORDERNUM, max(OE4) as MaxDate
from OrderExports
group by ORDERNUM
) tm
on OE.ORDERNUM = tm.ORDERNUM and OE.OE4 = tm.MaxDate
inner join orde_ O on OE.ORDERID = O.ORDERID
WHERE OE1='SCHEDCHNG' AND O.DROPDATE is null
Pretty sparse on details here but I think you are wanting something along these lines.
with SortedResults as
(
select OE.ORDERID
, OE.ID
, OE.ORDERNUM
, OE.OE4
, ROW_NUMBER() over(partition by OE.ORDERID, OE.ORDERNUM order by OE.OE4 desc) as RowNum
from OrderExports OE
inner join
(
select ORDERNUM
, max(OE4) as MaxDate
from OrderExports
group by ORDERNUM
) tm on OE.ORDERNUM = tm.ORDERNUM and OE.OE4 = tm.MaxDate
inner join orde_ O on OE.ORDERID = O.ORDERID
WHERE OE1='SCHEDCHNG'
AND O.DROPDATE is null
)
select ORDERID
, ID
, ORDERNUM
, OE4
from SortedResults
where RowNum = 1
You can try using max and group by as below :
SELECT a.ID, max(a.ORDERID) as OrderID, max(a.ORDERNUM) as OrderNum,MAX(OE.OE4) as OE4 FROM
(
--your query
) a
group by a.ID

Select Multiple Columns join SQL Server

I have an Employee table like this
And a second table for EmployeeComments with the EmployeeID as foreign key:
I would like to query the employees with their comments in the following format:
select Name, Comment
from Employee emp
left join EmployeeComments empC on empC.EmployeeID = emp.ID
I would like the results to be like:
I have already looked at Pivot, but it doesn't resolve my issue
Use window function:
select case when row_number() over(partition by emp.name order by empC.ID) = 1
then Name
else '' end as Name,
Comment
from Employee emp
left join EmployeeComments empC On empC.EmployeeID = emp.ID
This might give you some kind of order in your result window at least
WITH cte AS(
SELECT emp.Name ,
empC.Comment,
RANK() OVER (ORDER BY emp.Name) NameOrder,
ROW_NUMBER() OVER (PARTITION BY emp.Name ORDER BY empC.ID) RN
FROM Employee emp
LEFT JOIN EmployeeComments empC ON empC.EmployeeID = emp.ID
)
SELECT
Name = (CASE WHEN RN = 1 THEN Name ELSE '' END),
Comment
FROM
cte
ORDER BY
NameOrder,
RN
"use Cross Join:"
Query:
select case t.cnt
when 1 then
coalesce(t.Name,' ')
end as Name,t.comment
from
(
select t1.Name,t2.comment,row_number()
over(partition by t1.name order by t1.Name)
as cnt
from
EmployeeComments t1
cross join
Employee t2
where t1.ID=t2.Employeeid
)t

How to list duplicate records?

I have the following table structure:
id|date|studenttypeid|name|audituser
1|.....|4|Jason|....
2|.....|4|Robin|....
3|.....|4|Jason|....
4|.....|4|Dan|....
5|.....|4|Robin|....
I need to list all records which are duplicates on studenttypeid + name.
With the above data, the query should give me the following output:
1|.....|4|Jason|....
2|.....|4|Robin|....
3|.....|4|Jason|....
5|.....|4|Robin|....
How can I achieve this on SQL Server 2008?
You can use a group by and then join back to the original table, like this:
WITH Temp(StudentTypeId, Name) AS(
SELECT
StudentTypeId, Name
FROM YourTable
GROUP BY
StudentTypeId, Name
HAVING Count(1) > 1
)
SELECT YourTable.*
FROM YourTable
INNER JOIN Temp
ON YourTable.StudentTypeId = Temp.StudentTypeId
AND YourTable.Name = Temp.Name
you can use ROW_NUMBER()
SELECT ID, DATE, studenttypeid, name, audituser
FROM
(
SELECT ID, DATE, studenttypeid, name, audituser,
ROW_NUMBER() OVER (PARTITION BY studenttypeid, name
ORDER BY id) rn
FROM yourTableName
) a
WHERE rn = 1
SQLFiddle Demo
If you want to include every occurrence of the duplicates, some more methods using CROSS APPLY...
SELECT p.*
FROM people p
CROSS APPLY (
SELECT TOP(1) * FROM people p2 WHERE p2.ID <> p.ID AND p2.name = p.name AND p.studenttypeid = p2.studenttypeid
) as pWithDups
Or an EXISTS check
SELECT p.*
FROM people p
WHERE EXISTS (
SELECT *
FROM people p2
WHERE p2.ID <> p.ID AND p2.name = p.name AND p.studenttypeid = p2.studenttypeid
)
Try this
SELECT a.*
FROM yourtable a
JOIN (SELECT studenttypeid,
name
FROM yourtable
GROUP BY studenttypeid,
name
HAVING Count(studenttypeid) > 1) b ON b.name = a.name
ORDER BY a.id

How to get rows with Maximum id with condition of a table in SQL Server

i have a table similar this
id-value-RowInid
1-xy-1
1-xx-2
1-xz-3
2-xx-1
2-xr-2
3-xq-1
4-xa-1
4-xc-2
...
i need a function for this table with similar output to get maximum of RowInid in separated id group
1-xz-3
2-xr-2
3-xq-1
4-xc-2
...
You just need to use MAX(RowInid) with GROUP BY Id, value
SELECT ID, VALUE, MAX(RowInid) FROM myTable GROUP BY ID, VALUE
EDIT:
As you updated your question, you can get value field using sub-query like this:
SELECT ID, VALUE, RowInid
FROM myTable t1 WHERE RowInid =
(
SELECT MAX(RowInid) FROM myTable WHERE id = t1.id GROUP BY id
)
ORDER BY id ASC;
You can also achieve this using INNER JOIN like this:
SELECT t2.ID, VALUE, t2.RowInid FROM myTable t1
INNER JOIN
(
SELECT ID, MAX(RowInid) AS RowIniD FROM myTable GROUP BY ID
) AS t2
ON t1.ID = t2.ID AND t1.RowInid = t2.RowInid
ORDER BY t1.ID ASC;
See this SQLFiddle
See more about GROUP BY and MAX in SQL Server.
no need for group by or max at all
select id, value, rowinid from
( select *, row_number() over (partition by id, order by rowinid desc) rn from yourtable ) v
where rn = 1
Try with,
Use Sub Query to get Id, Value and Max RowInid,
SELECT m1.[Id],m1.Value, m1.RowInid
FROM [Practice].[dbo].[myTable] m1 WHERE RowInid = (SELECT MAX(m2.RowInid) FROM [Practice].[dbo].[myTable] m2 WHERE M1.Id = m2.Id GROUP BY Id)
above query return result like:
4-xx-2
3-xx-1
2-xx-2
1-xx-3
To Ascending this use,
SELECT m1.[id],m1.Value, m1.RowInid
FROM [Practice].[dbo].[myTable] m1 WHERE m1.RowInid = (SELECT MAX(m2.RowInid) FROM [Practice].[dbo].[myTable] m2 WHERE M2.id = m1.id GROUP BY id) ORDER BY m1.id ASC

Resources