SQL Clone
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


Pulling back a distinct column


Pulling back a distinct column

Author
Message
thomasrichardson2000
thomasrichardson2000
SSC-Enthusiastic
SSC-Enthusiastic (168 reputation)SSC-Enthusiastic (168 reputation)SSC-Enthusiastic (168 reputation)SSC-Enthusiastic (168 reputation)SSC-Enthusiastic (168 reputation)SSC-Enthusiastic (168 reputation)SSC-Enthusiastic (168 reputation)SSC-Enthusiastic (168 reputation)

Group: General Forum Members
Points: 168 Visits: 154
--Basically this query pulls back all the outstanding invoices for sold cars, the main issue I am having is that there can be more than one car on an invoice. In that case what I need to achieve is just pull back just one of these rows because the value outstanding on the invoice stays the same.Here is an example of an invoice with more than one vehicle. So I would just be looking to pull back the top row for this invoice. The query itself pulls back 380 rows with all invoices.

SELECT vehicle.platenr, 'invoicefm' as Company, Invoice_FM.ClientId, client.Name,invoice_fm.Invoice_id as Document_Nr,Invoice_FM.Invoice_Date,Invoice_FM.Paid, Total_Value as AllValues

FROM Invoice_FM inner join Client on Invoice_FM.ClientId = Client.ClientID left join vehicle on Invoice_FM.Invoice_id = vehicle.Invoice_Nr
where Invoice_FM.deleted = 0

and client.type_client = 'Fleet' order by Document_Nr



Platenr Company Client Name Document_NR Date Value
10D1234 invoicefm 213 BRENNAN 18 2012-03-03 1 49000.00
11D4321 invoicefm 213 BRENNAN 18 2012-03-03 1 49000.00
11D2123 invoicefm 213 BRENNAN 18 2012-03-03 1 49000.00
Andy Hyslop
Andy Hyslop
SSCrazy
SSCrazy (2.1K reputation)SSCrazy (2.1K reputation)SSCrazy (2.1K reputation)SSCrazy (2.1K reputation)SSCrazy (2.1K reputation)SSCrazy (2.1K reputation)SSCrazy (2.1K reputation)SSCrazy (2.1K reputation)

Group: General Forum Members
Points: 2123 Visits: 3045
Without knowing the structure of all your tables and data its difficult to help, this may point you in the right direction (untested as no consumable data):


SELECT
Qry.platenr
, 'invoicefm' AS Company
, Qry.ClientId
, Qry.Name
, Qry.Invoice_id AS Document_Nr
, Qry.Invoice_Date
, Qry.Paid, Qry AS AllValues

FROM
(
SELECT
*
,ROW_NUMBER() OVER (PARTITION BY Invoice_FM .DOCUMENT_NR ORDER BY vehicle .Platenr) AS RowNum

FROM

Invoice_FM
INNER JOIN Client
ON Invoice_FM.ClientId = Client.ClientID

LEFT JOIN vehicle
ON Invoice_FM.Invoice_id = vehicle.Invoice_Nr

WHERE Invoice_FM.deleted = 0
AND client.type_client = 'Fleet'

) AS Qry

WHERE
Qry.RowNum = 1

ORDER BY Document_Nr



Andy

==========================================================================================================================
A computer lets you make more mistakes faster than any invention in human history - with the possible exceptions of handguns and tequila. Mitch Ratcliffe
John Mitchell-245523
John Mitchell-245523
SSC-Dedicated
SSC-Dedicated (33K reputation)SSC-Dedicated (33K reputation)SSC-Dedicated (33K reputation)SSC-Dedicated (33K reputation)SSC-Dedicated (33K reputation)SSC-Dedicated (33K reputation)SSC-Dedicated (33K reputation)SSC-Dedicated (33K reputation)

Group: General Forum Members
Points: 33875 Visits: 16649
The top row by what? Age of vehicle? Price of vehicle? Date vehicle added to invoice? Please also provide some table DDL and sample data.

John
thomasrichardson2000
thomasrichardson2000
SSC-Enthusiastic
SSC-Enthusiastic (168 reputation)SSC-Enthusiastic (168 reputation)SSC-Enthusiastic (168 reputation)SSC-Enthusiastic (168 reputation)SSC-Enthusiastic (168 reputation)SSC-Enthusiastic (168 reputation)SSC-Enthusiastic (168 reputation)SSC-Enthusiastic (168 reputation)

Group: General Forum Members
Points: 168 Visits: 154
Cheers andy, that worked a treat. learning all the time. Thanks.
Andy Hyslop
Andy Hyslop
SSCrazy
SSCrazy (2.1K reputation)SSCrazy (2.1K reputation)SSCrazy (2.1K reputation)SSCrazy (2.1K reputation)SSCrazy (2.1K reputation)SSCrazy (2.1K reputation)SSCrazy (2.1K reputation)SSCrazy (2.1K reputation)

Group: General Forum Members
Points: 2123 Visits: 3045
Your Welcome

==========================================================================================================================
A computer lets you make more mistakes faster than any invention in human history - with the possible exceptions of handguns and tequila. Mitch Ratcliffe
Go


Permissions

You can't post new topics.
You can't post topic replies.
You can't post new polls.
You can't post replies to polls.
You can't edit your own topics.
You can't delete your own topics.
You can't edit other topics.
You can't delete other topics.
You can't edit your own posts.
You can't edit other posts.
You can't delete your own posts.
You can't delete other posts.
You can't post events.
You can't edit your own events.
You can't edit other events.
You can't delete your own events.
You can't delete other events.
You can't send private messages.
You can't send emails.
You can read topics.
You can't vote in polls.
You can't upload attachments.
You can download attachments.
You can't post HTML code.
You can't edit HTML code.
You can't post IFCode.
You can't post JavaScript.
You can post emoticons.
You can't post or upload images.

Select a forum

































































































































































SQLServerCentral


Search