How can I resolve the distinct issue in SQL Server 2005? - sql

I am trying to get distinct values for my query. I tried like below, but I am not getting proper result, will any one suggest me how to do resolve the issue.
Here the I want to distinct part_id.
http://tinypic.com/view.php?pic=9scx21&s=8#.UupFqT2SzyQ
Thanks in advance.

Why do you think the result is not correct, the rows returned are distinct.
DISTINCT is applied to all the columns, there's nothing like give me a DISTINCT(p.part_id) and don't care about other columns.
What you probably want is a single row for each part.id
If you don't have any rules which row you want to be returned you can go with a ROW_NUMBER:
select *
from
(
select all your columns
, row_number() over (partition by p.partid order by p.part_id) as rn
from ....
where ...
) as dt
where rn = 1
If there are some rules to determine which row should be returned (oldest/newest/whatever) you simply ORDER BY this column DESC instead of ORDER BY p.part
order by part_id;

Change SELECT DISTINCT P.PART_ID FROM.. at begining and add GROUP BY p.part_id at end.
Distinct must be applied for all columns which values are the same so you can add columns but remenber to add thet to GROUP BY also

Related

Get a new column with updated values, where each row change in time depending on the actual column?

I have some data that includes as columns an ID, Date and Place denoted by a number. I need to simulate a real time update where I create a new column that says how many different places are at the moment, so each time a new place appear in the column, the new column change it's value and shows it.
This is just a little piece of the original table with hundreds of millions of rows.
Here is an example, the left table is the original one and the right table is what I need.
I tried to do it with this piece of code but I cannot use the function DISTINCT with the OVER clause.
SELECT ID, Dates, Place,
count (distinct(Place)) OVER (PARTITION BY Place ORDER BY Dates) AS
DiffPlaces
FROM #informacion_prendaria_muestra
order by ID;
I think it will be possible by using DENSE_RANK() in SQL server
you can try this
SELECT ID, Dates, Place,
DENSE_RANK() OVER(ORDER BY Place) AS
DiffPlaces
FROM #informacion_prendaria_muestra
I think you can use a self join query like this - without using windows functions -:
select
t.ID, t.[Date], t.Place,
count(distinct tt.Place) diffPlace
from
yourTable t left join
yourTable tt on t.ID = tt.ID and t.[Date] >= tt.[Date]
group by
t.ID, t.[Date], t.Place
order by
Id, [Date];
SQL Fiddle Demo

Remove a column after selection with SQL

I want my result set to include only one column, but I'm using a different column to group by and order by.
Can I somehow, after selecting and order by removing the column from the result set?
Using MSSQL2008
Just add another SELECT around your query, like so:
SELECT
sum_columnB
FROM
(SELECT
columnA
, SUM(columB) sum_columnB
FROM Table
GROUP BY columnA
ORDER BY columnA
, sum_columnB) resultset
But if you would post your query, my answer could be more specific and maybe clearer.
You do not have to select all the columns your order or group by, you can just select the column you want.
SELECT A
FROM dbo.Table
GROUP BY A,B
ORDER BY A,B

SQL 2012 locating duplicate column entries in a table

I am using SQL 2012 and trying to identify rows where the SourceDataID column has two unique entries in the PartyCode column, and I'm having difficulties.
SELECT PartyCode, SourceDataID, count (*) as CNT
FROM CustomerOrderLocation (nolock)
GROUP BY PartyCode, SourceDataID
HAVING (Count(PartyCode)>1)
ORDER BY PartyCode
Results are returning as such:
W3333 948_O 31
(party code/sourcedataid/CNT)
This is showing me the total entries where the Partycode and the SourceDataID are listed together in the table. However, I need it to show a count of any instances where W333 lists 948_O as the SourceDataID more than once.
I'm not having luck structuring the query to pull the results I am looking to get. How can I do this?
A CTE coupled with the PARTITION BY function is helpful in finding duplicates of this manner. Code below:
WITH CTE AS(
SELECT PartyCode, SourceDataID,
ROW_NUMBER()OVER(PARTITION BY SourceDataID ORDER BY SourceDataID) RN
FROM CustomerOrderLocation (NOLOCK))
SELECT * FROM CTE WHERE RN > 1
This should return every duplicate PartyCode attached to a SourceDataID.
If you want to see the entire result, change the last SELECT statement to:
SELECT * FROM CTE ORDER BY PartyCode, RN
Thanks for the help everyone. I did not do the best job of describing the issue but this is the query I ended up creating to get my result set.
;with cte1 (sourcedataid, partycode) as (select sourcedataid, partycode from customerorderparty (nolock) group by PartyCode, SourceDataID)
select count(sourcedataid), sourcedataid from cte1 group by sourcedataid having count(sourcedataid) >1

use Row_number after applying distinct

I am creating an SP which gives some result by applying distinct on it, now I want to implement sever side paging, so I tried using Row_number on distinct result like:
WITH CTE AS
(
SELECT ROW_NUMBER() OVER(ORDER BY tblA.TeamName DESC)
as Row,tblA.TeamId,tblA.TeamName,tblA.CompId,tblA.CompName,tblA.Title,tblA.Thumbnail,tblA.Rank,tblA.CountryId,tblA.CountryName
FROM
(
--The table query starts with SELECT
)tblA
)
SELECT CTE.* FROM CTE
WHERE CTE.Row BETWEEN #StartRowIndex AND #StartRowIndex+#NumRows-1
ORDER BY CTE.CountryName
but rows are first assigned RowNumber then distinct get applied that is why I am getting duplicate values, how to get distinct rows first then get row numbers for the same.
Any solution on this? Am I missing something?
need answer ASAP.
thanks in advance!
Don't you need to add "partition by" to your ROW_NUMBER statement?
ROW_NUMBER() OVER(Partition by ___, ___, ORDER BY tblA.TeamName DESC)
In the blank spaces, place the column names you would like to create a new row number for. Duplicates will receive a number that is NOT 1 so you might not need the distinct.
To gather the unique values you could write a subquery where the stored procedure only grabs the rows with a 1 in them.
select * from
(
your code
) where row = 1
Hope that helps.
I'm not sure why you're doing this:
WHERE CTE.Row BETWEEN #StartRowIndex AND #StartRowIndex+#NumRows-1

Why distinct produces syntax error when mixed with other none distinct rows?

All these attempts fail:
SELECT DISTINCT(row1,row2), * FROM foo
SELECT *, DISTINCT (row1,row2) FROM foo
SELECT *, DISTINCT row1,row2 FROM foo
I want to fetch all rows but not duplicate rows on row1 & row2
PS. I don't want to use GROUP By.
Thanks!
You can only use DISTINCT on the entire row, not part of it.
PS. I don't want to use GROUP By.
That's a shame, because GROUP BY would have been the correct way to do this.