select distinct from multipale tableshttp://forums.asp.net/t/1798723.aspx/1?select+distinct+from+multipale+tablesMon, 07 May 2012 07:58:43 -040017987234959356http://forums.asp.net/p/1798723/4959356.aspx/1?select+distinct+from+multipale+tablesselect distinct from multipale tables <p>Hi, i have a sql server data table which contains media information of songs like mediaID, mediaName, albumID, singerID and genre. can someone help me write a sql query getting the last song of each of the last 4 albums (the last song of each album) and secondly, the last song of each of the 4 last entered genre. the album names and data are in different table and connected to the media table by the id. so, what i need to do is some thing like- get the 4 last albumID, and give me one song data for each of them.&nbsp;</p> <p>i tried to use &quot;distinct&quot;, but i can't get all the table information by using it.</p> <p>thanks...</p> 2012-05-01T06:49:47-04:004959393http://forums.asp.net/p/1798723/4959393.aspx/1?Re+select+distinct+from+multipale+tablesRe: select distinct from multipale tables <p></p> <blockquote><span class="icon-blockquote"></span> <h4>yanivhanya</h4> tried to use &quot;distinct&quot;, but i can't get all the table information by using it.</blockquote> <p></p> <p>DISTINCT is working on select column list.</p> <p>You must have to select only those columns which are giving you distinct records.</p> <p>You can also use the rank/row number function to get the unique values.</p> <p>Post your other tables structure with data &amp; expected output</p> 2012-05-01T07:24:13-04:004959416http://forums.asp.net/p/1798723/4959416.aspx/1?Re+select+distinct+from+multipale+tablesRe: select distinct from multipale tables <p>i know you can't other columns, this is my problem.</p> <p>&nbsp;</p> <p>let's say-</p> <p>albums_table-</p> <p>ID, albumname, albumprice, singer</p> <p>madia_table</p> <p>ID, albumID, singName, singPrice, createDate</p> <p>&nbsp;</p> <p>now, what i need &quot;get all the data of each song from the last 4 albums... so if i entered new albums &quot;summer&quot;, &quot;winter&quot;,</p> <p>i need to get the last sing in &quot;summer&quot; and all the data (album data and sing data)</p> <p>and the last sing in &quot;winter&quot; and all the data</p> 2012-05-01T07:45:28-04:004959450http://forums.asp.net/p/1798723/4959450.aspx/1?Re+select+distinct+from+multipale+tablesRe: select distinct from multipale tables <pre class="prettyprint">--this will give you last 4 entered album details with singname SELECT DISTINCT TOP 4 albums_table.ID,albumname, albumprice, singer,singName, FROM albums_table INNER JOIN madia_table ON albums_table.ID=madia_table.albumID WHERE singName='summer' -- you can pass where condition here ORDER BY createDate DESC</pre> <p></p> 2012-05-01T08:21:06-04:004959631http://forums.asp.net/p/1798723/4959631.aspx/1?Re+select+distinct+from+multipale+tablesRe: select distinct from multipale tables <p>check if you are looking for this</p> <pre class="prettyprint">select t1.*, t2.* from albums_table t1 inner join madia_table t2 on t1.ID = t2.albumID inner join ( select top 4 albumID from madia_table order by createDate desc ) t3 on t1.ID = t3.albumID</pre> <p><br> <br> </p> 2012-05-01T10:21:02-04:004959899http://forums.asp.net/p/1798723/4959899.aspx/1?Re+select+distinct+from+multipale+tablesRe: select distinct from multipale tables <p>You can use a ranking function like row_number to get your result. Post some sample data along with your table structures and ecpexcted result.</p> <p>Thanks.</p> 2012-05-01T12:57:48-04:004959937http://forums.asp.net/p/1798723/4959937.aspx/1?Re+select+distinct+from+multipale+tablesRe: select distinct from multipale tables <p>I think this is what you want:&nbsp;&nbsp; The last 4 albums, plus the last song on each.&nbsp;&nbsp; Since you don't mention a date field in the album table, I am using ID:</p> <pre class="prettyprint">SELECT TOP 4 * FROM albums_table a INNER JOIN madia_table m ON m.ID=( SELECT TOP 1 m2.ID FROM madia_table m2 WHERE m2.albumID=a.ID ORDER BY createDate DESC ) ORDER BY a.ID DESC</pre> <p></p> 2012-05-01T13:24:29-04:004968548http://forums.asp.net/p/1798723/4968548.aspx/1?Re+select+distinct+from+multipale+tablesRe: select distinct from multipale tables <p>Hi yanivhanya,</p> <p>Did you get the right query? From your table structures, did you have any order when you insert data into madia_ table? Order by ID or order by createDate?&nbsp; If you didn't get the right query, please provide us some sample data, it will give us more inforation about your problem.</p> <p>Thanks.</p> 2012-05-07T07:58:43-04:00