Query with 3 Tables - sql

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;

Related

Max() not filtering out MIN()

when I use run the query below, it returns duplicate StockNo's because some of them have duplicate WorkInProgress codes (FiWipStatus Code).
Is there a way to exclude the record based on the the MIN() on rowlastupdated?
as always, appreciate any help!
SELECT dbo.InventoryVehicle.StockNo, dbo.VehicleSales.FiWipStatusCode,
MAX(dbo.VehicleSales.RowLastUpdated) AS Expr1
FROM dbo.VehicleSales RIGHT OUTER JOIN
dbo.InventoryVehicle ON dbo.VehicleSales.StockNo = dbo.InventoryVehicle.StockNo
GROUP BY dbo.InventoryVehicle.StockNo, dbo.VehicleSales.FiWipStatusCode,
dbo.VehicleSales.RowLastUpdated
If I got it correctly, you need to get the records based on their last update date and time (which is RowLastUpdated). if so, you can do something like this :
SELECT
iv.StockNo
, vs.FiWipStatusCode
, vs.RowLastUpdated
FROM (
SELECT
iv.StockNo
, vs.FiWipStatusCode
, vs.RowLastUpdated
, ROW_NUMBER() OVER(PARTITION BY iv.StockNo ORDER BY vs.RowLastUpdated DESC) AS RN
FROM
VehicleSales vs
LEFT JOIN InventoryVehicle iv ON vs.StockNo = iv.StockNo
) D
WHERE
RN = 1
where ROW_NUMBER() will number the rows based on StockNo and order them based on RowLastUpdated in DESC. So, the first row of each distinct StockNo will be the MAX() datetime in your aggregation query. if you want to get the MIN() just change the order to ASC

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

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

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

Ms Sql repetitive rows

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

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