One doubt in MSSQL. There are two tables in a databases.
Table 1 named Property contain fields PRPT_Id(int),PRPT_Name(varchar), PRPT_Status(bit)
Table 2 named PropertyImages contain fields PIMG_Id(int),PIMG_ImageName(varchar),PRPT_Id(int),PIMG_Status(bit)
These two tables follow a one-to-many relationship. That means the each Property can have zero, one or more PropertyImages corresponding to it.
What is required is a query to display
PRPT_Id, PRPT_Name, ImageCount(Count of all images corresponding to a PRPT_Id where PIMG_Status is true. o if there arent any images), FirstImageName(if there are n images, the name of the first image in the image table corresponding to the PRPT_Id with PIMG_Status true. if there aren’t any images we fill that with whitespace/blank) . another condition is that PRPT_Status should be true.
Edit Note – Both the tables are having autoincremented integers as primary key. So first Image name will be the name with MIN(PIMG_Id), isn’t that so?
I want the PIMG_ImageName corresponding to the MIN(PIMG_ID) in the resultset
Assuming that FirstImage means the one with the lowest Id, then this should be at least close enough to test to completion:
The double LEFT JOIN assures that you get the first image record in pi1. If the ‘First’ rule is different, then adjust this join accordingly.
This should be about as efficient as possible. It has no subqueries.