Ms Sql repetitive rows - sql

http://postimage.org/delete/pwnan1ud4/
there are repetitive rows (column Name : aracId) in the picture as linked above. How can i modify query for the unique row.

Since your image is not viewable, try adding DISTINCT in your SELECT clause. Example,
SELECT DISTINCT *
FROM tableName

Try this :
With Cte as
(
Select row_number() over (partition by arac.aracID order by arac.aracID ) as row
-- other columns
left join ...
Where .... and
tip.tipadi in ('Kamyon','Kamyonet','Tir')
)
Select cte.aracID, cte.Model, ..........
from cte where cte.row=1

Related

best way to get count and distinct count of rows in single query

What is the best way to get count of rows and distinct rows in a single query?
To get distinct count we can use subquery like this:
select count(*) from
(
select distinct * from table
)
I have 15+ columns and have many duplicates rows as well and I want to calculate count of rows as well as distinct count of rows in one query.
More if I use this
select count(*) as Rowcount , count(distinct *) as DistinctCount from table
This will not give accurate results as count(distinct *) doesn't work.
Why don't you just put the subquery inside another query?
select count(*),
(select count(*) from (select distinct * from table))
from table;
create table tbl
(
col int
);
insert into tbl values(1),(2),(1),(3);
select count(*) as distinct_count, sum(sum) as all_count
from (
select count(col) sum from tbl group by col
)A
I think I have understood what you are looking for. You need to use some window function. So, you query should be look like =>
Select COUNT(*) OVER() YourRowcount ,
COUNT(*) OVER(Partition BY YourColumnofGroup) YourDistinctCount --Basic of the distinct count
FROM Yourtable
NEW Update
select top 1
COUNT(*) OVER() YourRowcount,
DENSE_RANK() OVER(ORDER BY YourColumn) YourDistinctCount
FROM Yourtable ORDER BY TT DESC
Note: This code is written sql server. Please check the code and let me know.

Query with 3 Tables

I need to get the result from query with 3 column.
I tried this query:
select distinct
f1.deskripsi_pekerjaan,f2.indikator_keberhasilan,f3.hasil_kerja,
ROW_NUMBER() over
(
partition by f1.deskripsi_pekerjaan
order by f1.id_penilaian
) as 'Row Number'
from form_3_1 f1 , form_3_2 f2 , form_3_3 f3;
Here is the image of the result of executed query.
Can I get the distinct right? It has same record. I just need two records every column.
Thank you for help..
You are are using Cartesian product /cross join of there tables and then using row_number() you would be getting distinct record as you are using row_number().
so distinct clause will not behave as per your requirement.
Please go throw below link on row_number() detail read
https://msdn.microsoft.com/en-IN/library/ms186734.aspx
Please go throw below link for cross join
http://www.w3resource.com/sql/joins/cross-join.php
From above query i understand, you already have distinct records and you need only two rows. For that you can use top records
select distinct Top 2
f1.deskripsi_pekerjaan,f2.indikator_keberhasilan,f3.hasil_kerja,
ROW_NUMBER() over
(
partition by f1.deskripsi_pekerjaan
order by f1.id_penilaian
) as 'Row Number'
from form_3_1 f1 , form_3_2 f2 , form_3_3 f3;

Distinct on three columns in sql server 2008

I have a 20 Columns Table which have duplicate value but not in all columns. that what normal distinct clause not working on it..
so i want to apply distinct on three columns (name,fname,dob) , but how? . Please give me any solution .
You could use ROW_NUMBER with a common-table-expression(CTE):
WITH CTE AS
(
SELECT t.*, RN = ROW_NUMBER() OVER (PARTITION BY name,fname,dob ORDER BY name,fname,dob)
FROM dbo.TableName t
)
SELECT * FROM CTE WHERE RN = 1
This takes one per group. Change ORDER BY name,fname,dob according to your logic.

Select Single and Duplicate Row and Return Multiple Columns

I'm currently working with my database in SQL Server. I have a table with 23 fields and it has single and duplicate rows. How can I select both of them without having any duplicate data.
I have try this query:
SELECT
Code, Stuff, and other fields....
FROM
(
SELECT
*,ROW_NUMBER() OVER (PARTITION BY Code ORDER BY Code) AS RN
FROM
my_table
)t
WHERE RN = 1
The above code just return the data from the duplicate rows. But, I want the "single rows" also returned.
This is the illustration.
Thank you for the help.
Could it be as simple as:
SELECT DISTINCT Code, Stuff FROM MyTable
Or, just add stuff to the partition by clause:
PARTITION BY Code,Stuff ORDER BY Code
Try This
You may need to add Stuff and more fields in Partition BY
SELECT
Code, Stuff
FROM
(
SELECT
*,ROW_NUMBER() OVER (PARTITION BY Code,Stuff ORDER BY Code) AS RN
FROM
my_table
)t
WHERE RN = 1

How to retrieve specific rows from SQL Server table?

I was wondering is there a way to retrieve, for example, 2nd and 5th row from SQL table that contains 100 rows?
I saw some solutions with WHERE clause but they all assume that the column on which WHERE clause is applied is linear, starting at 1.
Is there other way to query a SQL Server table for a specific rows in case table doesn't have a column whose values start at 1?
P.S. - I know for a solution with temporary tables, where you copy your select statement output and add a linear column to the table. I am using T-SQL
Try this,
SELECT * FROM (
SELECT
ROW_NUMBER() OVER (ORDER BY ColumnName ASC) AS rownumber
FROM TableName
) as temptablename
WHERE rownumber IN (2,5)
With SQL Server:
; WITH Base AS (
SELECT *, ROW_NUMBER() OVER (ORDER BY id) RN FROM YourTable
)
SELECT *
FROM Base WHERE RN IN (2, 5)
The id that you'll have to replace with your primary key or your ordering, YourTable that is your table.
It's a CTE (Common Table Expression) so it isn't a temporary table. It's something that will be expanded together with your query.
There is no 2nd or 5th row in the table.
There is only the 2nd or 5th result in a resultset that you return, as determined by the order you specify in that query.
If you are on SQL Server 2005 or above, you could use Row_Number() function. Ex:
;With CTE as (
select col1, ..., row_number() over (order by yourOrderingCol) rn
from yourTable
)
select col1,...
from cte
where rn in (2,5)
Please note that yourOrderingCol will decide the value of row number (i.e. rn).