How do I get this statement (which returns duplicates):
Select Name from Table
To return this:
Select Distinct Name, Count(Number of non-distinct Name rows)
?
Thanks in advance.
SELECT Name, COUNT(Name) AS DistinctCount
FROM tableName
GROUP BY Name
Try:
select
Name,
count(Name)
from tableName
GROUP BY name
Related
I have a select query which returns a couple of rows grouped by ParentId. How can I add a new row with sum of a column after each parentId group?
For now I have kept the data in a temp table and the result is as below.
And I want to add a new row at the end of each ParentId group as below with the sum of column LoanAmount.
Any help will be appreciated. Many thanks.
You can use a common table expression to achieve this. Here I've created a cte with rank column for getting it sorted in order.
;WITH cte AS
(SELECT ParentId,
sum(LoanAmount) LoanAmount,
max(rank) + 1 AS rank
FROM test
GROUP BY ParentId)
SELECT *
FROM test
UNION ALL
SELECT *
FROM cte
ORDER BY ParentId, rank
rextester
See this link here enter link description here
I think you want:
SELECT ParentID, SUM(VALUE1), SUM(VALUE2)
FROM tableName
GROUP BY ID
You cant do it after each group or at the bottom like in excel, but you create a 'new table' in your query effectively.
Yeah having seen your updated comment, you main issue is youre thinking of it like excel, SQL is not a spreadsheet tool - its a relational database. Id suggest going through a SQL intro - youll pick up the concepts quite fast.
The query I gave you could be created as a stored procedure.
If you feel I've answered your question, id appreciate an upvote :)
You can make sum of group by subquery then combine them in union
; with cte as
( select 9999 as Slno, Level, ParentId, Ent_id, relation, sum(colname) as colname from table group by Level, ParentId, Ent_id, relation)
, ct as ( select row_number() over (partition by ParentId order by level) as Slno, Level, ParentId, Ent_id, Name, --- rest of your column names
colname from table
union all
select Slno, Level, ParentId, Ent_id, '' as Name, ---rest of '' for each column with column name as alias
colname from cte )
select Slno, Level, ParentId, Ent_Id, Name, ---- your columns of table
colname from ct order by Slno
This is just rough idea. Feel free to ask for any confusion.
Post your exact schema for accurate details.
I have a table in SQL Server, in this table I have a column chq_no_start with some duplicate cheque no, but the account id is different according those cheque no.
Now I want to show only one cheque no from those duplicate cheque no, no matter if one account id is missing.
I show my output:
Try this...
Select
distinct chq_no_start,
(select top 1 alt_acc_id from Table_name where chq_no_start= tn.chq_no_start order by alt_acc_id) Account_ID
From
Table_name tn
Order by
chq_no_start
Try using the DISTINCT keyword before the column name.
You could try the following:
with cte as (
select alt_acc_id, issue_date, no_of_chq, chq_no_start,
row_number() over (partition by chq_no_start order by alt_acc_id) RowNum
from your_table
where your_column = 'your_value')
select * from cte
where RowNum = 1
i'm using sql server 2008 rs1 and I have a problem with a query .
my table is named : employees
table columns are :
first name , last name , job title , gender , date of birth and salary .
The required : to display job title, employee name, and the difference between salary of the employee and minimum salary for the job
I try to solve it as this :
SELECT First_Name, Title ,Salary-min(Salary) AS (Differance) FROM Employees;
but this cause an error
help me please
thanks in advance
Assuming that minimum salary is the minimum for all the employees you need to select the minimum from all employees and use;
SELECT
First_Name,
Title,
Salary - (SELECT MIN(SALARY) FROM Employees) As Difference
FROM Employees
try this.
;WITH cte
AS (SELECT Min(Salary) Differance
FROM Employees)
SELECT First_Name,
Title,
Salary - (SELECT * FROM cte) AS Differance
FROM Employees;
You can use a sub-query to calculate the min-salary:
SELECT First_Name, Title ,Salary-(SELECT MIN(e2.Salary)
FROM Employees e2) AS Difference
FROM Employees
You don't need to be afraid that this is calculated for every row.
try this
;WITH cte(minimumSalray)
AS
(
SELECT MIN(Salary)
FROM Employees
)
SELECT First_Name, Title ,Salary-cte.minimumSalray AS Differance
FROM Employees,minimumSalary
Don't have a SQL Server 2008 R2, just tried on SQL Server 2014.
SELECT First_Name, Title ,
Salary-min(Salary) OVER() AS [Differance]
FROM Employees;
If you want to get minimum salary of the same job, just query
SELECT First_Name, Title ,
Salary-min(Salary) OVER(PARTITION BY Title) AS [Differance]
FROM Employees;
Select myColumn, count(*) totalcount
from myTable
group by myColumn
having count(*) >1
order by count(*) desc;
The following works for counting integer fields, but not for text fields. How do I need to modify it so that it will work for text fields?
Thanks to all for their help!
I'm guessing you have trailing (or leading) spaces which are different values to trimmed values...
And you mean text values not datatype...
For SQL Server 2005+, you could:
SELECT CAST(myColumn AS NVARCHAR(MAX)), COUNT(*) totalcount
FROM myTable
GROUP BY CAST(myColumn AS NVARCHAR(MAX))
HAVING COUNT(*) >1
ORDER BY COUNT(*) DESC;
Does anyone know how can I display records that are not duplicated record inside results in SQL Server?
Use distinct
SELECT DISTINCT * FROM table
Or use group by
select field1,field2,field3 FROM table GROUP BY field1, field2, field3
If you really meant "records that with no duplicate record ", i.e., every row that exists once, and only once, Try this:
Select * From Table
Group By [Here list all columns in Table]
Having Count(*) = 1
Another interpretation of the question.
SELECT *
FROM yourtable t1
WHERE NOT EXISTS
(SELECT *
FROM yourtable t2
WHERE t1.col_to_match_for_duplicates=t2.col_to_match_for_duplicates
AND t1.primarykey <> t2.primarykey
)