简体   繁体   English

SQL查询从一个表中获取数据,而从另一表中仅获取一行数据

[英]SQL Query get data from one table, and get data from only one row from another table

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 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... 好的,这是交易。我需要使用一个查询,该查询从所有销售(Ventas)中获取数据,该数据仅应为2行...

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... 但是,我需要来自Plazos的数据,但是我只需要来自Plazos的数据,我需要的是在与Ventas相同的行上显示来自plazos的数据,但仅显示来自最新Plazos的数据...

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: 您可能会注意到,例如,在Plazos中有一个名为NumeroPlazo的列,该列在同一IdVenta上增加...在此示例中,我需要显示:

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... 带有来自Plazos IdPlazo 95的数据的Ventas IdVenta 182(由于来自Plazos,因此IdPlazo 95在Numero Plazos列上的编号最高...当然还有IdVenta 183,但是由于它只有一个Plazo,它将显示来自该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... 但它返回4行(对于Venta,其中IdVenta = 182的行为3行,对于IdVenta = 183的行),我想要的只有2行...

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... 显然,因为我指定了Plazos.IdVenta = 182,所以它仅适用于一次销售...我的问题是...我如何使用后一个查询来获取每次销售所需的数据...

I hope yuo can help me... If you need me to be more specific, please let me know. 希望您能帮助我...如果您需要我提供更多具体信息,请告诉我。

Thanks in advance 提前致谢

You can use CROSS APPLY which allows you to run a subquery per-each-row of the preceding tables in the FROM list. 您可以使用CROSS APPLY,它允许您在FROM列表中的前面表格的每行中运行一个子查询。

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. 通过Row_Number()执行此操作可能会更快,但是在此子查询将要求您在内部级别为所有列添加别名,这反过来也不是一个坏主意。

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

It is simple. 很简单。 In your second question replace 182 on Ventas.IdVenta 在第二个问题中,在Ventas.IdVenta上替换182。

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 = Ventas.IdVenta)
ORDER BY Ventas.FechaVenta DESC, Ventas.HoraVenta DESC

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM