Click here to Skip to main content
15,891,633 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
iam use this table with this query

HTML
FUserName	FScoreID	FGrantedScoreID
u157	            2                6
u157	            2	              7
u4	            2	             10
u4	            2	             11
u4	            2	             12
u6	            2	            14
u6	            2	            15
u6	            2	            18

iam need this result
XML
<pre lang="HTML">
FUserName   FScoreID    FGrantedScoreID
u157            2                6
u4              2                10
u6              2                14

</pre>


this query
SQL
SELECT DISTINCT FUserName,FScoreID , [FGrantedScoreID]

  FROM [HrstPortal].[dbo].[VDB_Scores]
 where FScoreID=2
Posted

The above query would return only one row, because the FScoreID is same throughout the table.

You can create two queries. One would select the DISTINCT data, and the second one can be used inside a loop, to see for the data present for each of this DISTINCT data.

Try this,

SQL
SELECT DISTINCT FUserName, [FGrantedScoreID] -- Select
FROM [HrstPortal].[dbo].[VDB_Scores] -- Table 
WHERE FScoreID=2 -- Constraint


Now for this result, you can loop and run a query for the FUserName value, and get all of the FScoreID values for each of the DISTINCT result; as you're doing in the HTML table.
 
Share this answer
 
Comments
saeed1364 22-Nov-14 3:20am    
please write two query
Afzaal Ahmad Zeeshan 22-Nov-14 3:41am    
OriginalGriff has already provided you with an answer, have a look at this link please. http://stackoverflow.com/questions/3800551/select-first-row-in-each-group-by-group
The problem is that there is no easy way to get the "first" row of each "group" without having anything in the table to order it by - since SQL is at liberty to return rows in any order it sees fit unless an ORDER BY clause is added.
But...see here: http://stackoverflow.com/questions/3800551/select-first-row-in-each-group-by-group[^]
 
Share this answer
 
Try this:
SQL
select t1.* from tablename t1
inner join
(
  select FUserName,MIN(FGrantedScoreID) MinFGrantedScoreID from tablename group by FUserName
) t2
on t1.FUserName=t2.FUserName
where t1.FGrantedScoreID=t2.MinFGrantedScoreID
 
Share this answer
 
The simplest query is:
SQL
SELECT FUserName, FScoreID, MIN(FGrantedScoreID) AS FGrantedScoreID
FROM TableName
WHERE FScoreID = 2
GROUP BY FUserName, FScoreID


See:
Aggregate Functions (Transact-SQL)[^] - MIN[^]
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900