Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8781283
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T20:15:39+00:00 2026-06-13T20:15:39+00:00

I don’t know how to explain my problem in the title, so I’ll explain

  • 0

I don’t know how to explain my problem in the title, so I’ll explain it better here…

I have two tables

CREATE TABLE [dbo].[Ventas]
(
    [IdVenta] [int] IDENTITY(1,1) NOT NULL,
    [FechaVenta] [date] NULL,
    [HoraVenta] [varchar](10) NULL,
    [Subtotal] [money] NULL,
    [Iva] [money] NULL,
    [Total] [money] NULL,
    [Saldo] [money] NULL,
    [Abono] [money] NULL,
    [FormaDePago] [varchar](50) NULL,
    [Plazos] [int] NULL,
    [Estado] [varchar](50) NULL,
)

CREATE TABLE [dbo].[Plazos]
(
    [IdPlazo] [int] IDENTITY(1,1) NOT NULL,
    [IdVenta] [int] NULL,
    [NumeroPlazo] [int] NULL,
    [FechaVencimiento] [date] NULL,
    [FechaCorte] [date] NULL,
    [FechaPenalizacion] [date] NULL,
    [FechaLiquidacion] [date] NULL,
    [Total] [money] NULL,
    [Cargo] [money] NULL,
    [Abono] [money] NULL,
    [Estado] [varchar](50) NULL,
)

now to add some data

INSERT [dbo].[Ventas] ([IdVenta], [FechaVenta], [HoraVenta], [Subtotal], [Iva], [Total], [Saldo], [Abono], [FormaDePago], [Plazos], [Estado]) VALUES (182, CAST(0x54360B00 AS Date), N'11:20', 500.0000, 55.0000, 555.0000, 333.0000, 222.0000, N'A Credito', 5, N'Pendiente De Pago')
INSERT [dbo].[Ventas] ([IdVenta], [FechaVenta], [HoraVenta], [Subtotal], [Iva], [Total], [Saldo], [Abono], [FormaDePago], [Plazos], [Estado]) VALUES (183, CAST(0x54360B00 AS Date), N'12:29', 575.0000, 63.2500, 638.2500, 638.2500, 0.0000, N'Una Sola Exhibicion', 1, N'Pendiente De Pago')


INSERT [dbo].[Plazos] ([IdPlazo], [IdVenta], [NumeroPlazo], [FechaVencimiento], [FechaCorte], [FechaPenalizacion], [FechaLiquidacion], [Total], [Cargo], [Abono], [Estado]) VALUES (93, 182, 1, CAST(0x54360B00 AS Date), CAST(0x57360B00 AS Date), CAST(0x5C360B00 AS Date), CAST(0x54360B00 AS Date), 111.0000, 0.0000, 111.0000, N'Liquidado')
INSERT [dbo].[Plazos] ([IdPlazo], [IdVenta], [NumeroPlazo], [FechaVencimiento], [FechaCorte], [FechaPenalizacion], [FechaLiquidacion], [Total], [Cargo], [Abono], [Estado]) VALUES (94, 182, 2, CAST(0x73360B00 AS Date), CAST(0x75360B00 AS Date), CAST(0x7A360B00 AS Date), CAST(0x54360B00 AS Date), 111.0000, 0.0000, 111.0000, N'Liquidado')
INSERT [dbo].[Plazos] ([IdPlazo], [IdVenta], [NumeroPlazo], [FechaVencimiento], [FechaCorte], [FechaPenalizacion], [FechaLiquidacion], [Total], [Cargo], [Abono], [Estado]) VALUES (95, 182, 3, CAST(0x91360B00 AS Date), CAST(0x94360B00 AS Date), CAST(0x99360B00 AS Date), NULL, 111.0000, 111.0000, 0.0000, N'Pendiente')
INSERT [dbo].[Plazos] ([IdPlazo], [IdVenta], [NumeroPlazo], [FechaVencimiento], [FechaCorte], [FechaPenalizacion], [FechaLiquidacion], [Total], [Cargo], [Abono], [Estado]) VALUES (96, 183, 1, CAST(0x54360B00 AS Date), CAST(0x57360B00 AS Date), CAST(0x5C360B00 AS Date), NULL, 639.0000, 639.0000, 0.0000, N'Pendiente')

Foreign Key On Ventas.IdVenta = Plazos.IdVenta

Ok, Here’s the deal…
I need to use a query that brings data from all sales (Ventas), which only it supposed to be 2 rows…

However, I need data from Plazos, but I only need data from Plazos
What I need is to display data from plazos on the same row as Ventas, but only data from the most recent Plazo…

you may notice that for example, in Plazos there is a column called NumeroPlazo which increases on the same IdVenta… what I need in this example is to display:

Ventas IdVenta 182 with data from Plazos IdPlazo 95 (since from Plazos, IdPlazo 95 has the highest number on the column Numero Plazos…
and of course IdVenta 183, but since it only has one Plazo, it will display data from that plazo…

At the moment I had this query…

SELECT Ventas.*, Plazos.*, 
FROM Ventas INNER JOIN Plazos ON Plazos.IdVenta = Ventas.IdVenta 
WHERE Ventas.Estado = 'Pendiente De Pago' 
ORDER BY Ventas.FechaVenta DESC, Ventas.HoraVenta DESC

but it returns 4 rows (3 rows for Venta where IdVenta = 182, and one where IdVenta = 183)
What I want is only 2 rows…

Then I tried this query that worked… but only for one row

SELECT Ventas.*, Plazos.*, 
FROM Ventas INNER JOIN Plazos ON Plazos.IdVenta = Ventas.IdVenta 
WHERE Ventas.Estado = 'Pendiente De Pago' 
AND Plazos.NumeroPlazo = (SELECT MAX(Plazos.NumeroPlazo) FROM Plazos WHERE Plazos.IdVenta = 182)
ORDER BY Ventas.FechaVenta DESC, Ventas.HoraVenta DESC

Obviously it only works for one sale since I specify Plazos.IdVenta = 182…
My question here is… how can I use the latter query to get the data I want for each sale…

I hope yuo can help me…
If you need me to be more specific, please let me know.

Thanks in advance

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-13T20:15:41+00:00Added an answer on June 13, 2026 at 8:15 pm

    You can use CROSS APPLY which allows you to run a subquery per-each-row of the preceding tables in the FROM list.

    SELECT Ventas.*, Plazos.*
    FROM Ventas
    cross apply (
        select TOP(1) *
        from Plazos 
        WhERE Plazos.IdVenta = Ventas.IdVenta
        ORDER BY [NumeroPlazo] DESC) Plazos
    WHERE Ventas.Estado = 'Pendiente De Pago' 
    ORDER BY Ventas.FechaVenta DESC, Ventas.HoraVenta DESC
    

    Doing it via Row_Number() is probably faster, but here the subquery will require you to alias all the columns at the inner level, which is probably not a bad idea anyway.

    SELECT *
    FROM
    (
        SELECT
            v.[IdVenta], v.[FechaVenta], v.[HoraVenta], v.[Subtotal], v.[Iva], v.[Total], v.[Saldo], v.[Abono], v.[FormaDePago], v.[Plazos], v.[Estado],
            p.[IdPlazo], p.[NumeroPlazo], p.[FechaVencimiento], p.[FechaCorte], p.[FechaPenalizacion], p.[FechaLiquidacion], p.[Total] plazostotal, p.[Cargo], p.[Abono] plazasabono, p.[Estado] plazosestado,
            RowN = ROW_NUMBER() over (partition by v.[IdVenta] order by p.[NumeroPlazo] desc)
        FROM Ventas v
        JOIN Plazos p ON p.IdVenta = v.IdVenta
        WHERE v.Estado = 'Pendiente De Pago'
    ) X
    WHERE RowN = 1
    ORDER BY FechaVenta DESC, HoraVenta DESC
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

don't know better title for this, but here's my code. I have class user
Don't know a better title but here is what im trying to do. I
Don't know how to explain it better but i'm trying to get a response
don't know if this is possible.. I'm using sqlite3 schema: CREATE TABLE docs (id
don't know if the title describes anything about what I'm trying to say but
Don't really know how to formulate the title, but it should be pretty obvious
Don't know if I'm missing something here. I'd like to run multiple global variables
Don't know what's wrong here, when I run the application it says Specified method
(Don't know if this is strictly on-topic, but I don't see any better Stack
Don't know if this is an eclipse specific problem but whenever I declare a

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.