Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 8 years ago.
Improve this question
I have a table with Photos, allowing multiple rows per AlbumID:
Photos (photoID, photo,caption,albumID).
Albums (albumID,albumTitle)
I want a list of albumTitle and one photo for that albumID.
There are multiple photos in Photos table for a single albumID.
Can you provide a sql statement to select only one Photo per album id?
Thanks in advance!
Avi
SELECT AlbumID, MIN(PhotoID)
FROM PHOTOS
GROUP BY AlbumID
this will return only 1 of each instance of each albumID and a photo
SELECT DISTINCT `albumID `, `photo` FROM `Photos`;
and then you can:
SELECT
DISTINCT `albumID `, `photo`
FROM
`Photos`
INNER JOIN
`Albums`
ON `Albums.albumID` = `Photos.albumID`
AND `Albums.albumTitle` = `albumTitle`
Here's one way to do it using the Max(photoId):
Select a.albumId, a.albumTitle, p.photoId, p.photo, p.caption
From Albums a
Inner Join Photos p On a.albumId = p.albumId
Inner Join (
Select Max(photoId) maxPhotoId, albumId
From Photos
Group By albumId
) t On p.photoId = t.maxPhotoId And a.albumId = t.albumId
Related
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
I have new data coming into the app all the time, so I want to limit the number of rows in a table to, say, 100 records. I would describe it as a FIFO queue. Whenever there new data (just a few rows a time) coming in, old data at the 'bottom' of the table are flushed out and deleted. Since it's FIFO, I don't want to manually perform a sort, then delete, then insert back in. I guess there must be cheap way to do this, right?
Thanks
A query like this will show all recors, newest first:
SELECT *
FROM MyTable
ORDER BY Date DESC -- or some autoincrementing ID column
With an OFFSET clause, you can skip the first records.
This means that you get all records except the first 100 ones, i.e., you get those records that should be deleted:
SELECT *
FROM MyTable
ORDER BY Date DESC
LIMIT -1 OFFSET 100
You can then use this in a subquery to actually delete the records:
DELETE FROM MyTable
WHERE ID IN (SELECT ID
FROM MyTable
ORDER BY Date DESC
LIMIT -1 OFFSET 100)
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 3 years ago.
Improve this question
This is the output of joining 2 tables by inner join.
I want linq query of joing two table as inner join and group by name from one table and sum of total from another table column as taken snapshot as above.pls help
Check this post.
var result = (from a in ctx.TableA
join b in ctx.TableB on a.Id equals b.Id
group a by a.Name into g
select new
{
Name = g.Key,
Sum = g.Sum(i => i.Total)
}).ToList();
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 8 years ago.
Improve this question
Create an index-by table and populate with rows from emp, then loop over and print them.
hi can anyone understand this question and give me the suggestions to solve this please..
A collection is an ordered group of elements having the same data type
which can be accessed though subscript/index. Oracle PL/SQL supports
three different types of collection
Index-by tables or Associative array
Nested table
Variable-size array or Varray
Below is an example of how you can define a Index-by table and populate it with rows from Emp table and print them accordingly.
DECLARE
CURSOR c_emp is
select name from emp;
TYPE c_list IS TABLE of emp.name%type INDEX BY binary_integer;
name_list c_list;
counter integer :=0;
BEGIN
FOR n IN c_emp LOOP
counter := counter +1;
name_list(counter) := n.name;
dbms_output.put_line('Employee('||counter|| '):'||name_list(counter));
END LOOP;
END;
/
See Oracle Documentation as well as PL/SQL - Collections for more information on the same.
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
I want to update details using linq to entities. But instead of takin a new aspx page i want to update details in another view. will it work.? and please give the linq to entity update query
Let us assume:
db is the context of your Database Entity.
Table_name is the name of Table you need to update.
row_id is the value you are using to search for the data in the Table.
To update using linq you need to fetch the record first using the below query:
var data = (from r in db.Table_name
where r.id == row_id
select r).FirstOrDefault();
Now to update the values just update them. For example:
data.Name = "Firstname lastname"
data.IsActive = true;
.
.
and so on
After you have updated the values in data you need to Save the changes made by you by this command:
db.SaveChanges();
That's it.
Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 7 years ago.
Improve this question
Get the last record from a Microsoft SQL database table using ASP.net (VB) onto a web form.
I'm assuming he's trying to retrieve the last inserted record. As Ariel pointed out, the question is rather ambiguous.
SELECT TOP 1 * FROM Table ORDER BY ID DESC
If you have an identity column called ID, this is easiest. If you don't have an identity PK column for example a GUID you wont be able to do this.
Here is a basic solution:
var order = (from i in db.orders
where i.costumer_id.ToString() == Session["costumer_id"]
orderby i.order_id descending
select i).Take(1).SingleOrDefault();
You need to be more specific with actually putting it onto a web form, but the SQL to get the last record is:
SELECT *
FROM TABLE_NAME
WHERE ID = (SELECT MAX(ID) FROM TABLE_NAME)
Where ID is your ID and TABLE_NAME is your table name.