如何使用 t-sql 在销售报告中显示价格变化

发布于 2024-09-10 03:38:56 字数 1715 浏览 2 评论 0原文

我有两个表 Products 和 SalesLog,

SalesLog 表

CREATE TABLE [dbo].[SalesLog](
[SalesID] [int] IDENTITY(1,1) NOT NULL,
[MemoNo] [int] NULL,
[ProductCode] [int] NULL,
[Quantity] [int] NULL,
[Price] [decimal](10, 2) NULL,
[pGroup] [int] NULL,
[pName] [nvarchar](30) COLLATE SQL_Latin1_General_CP1_CI_AS NULL,
[pSize] [int] NULL,
[BillDate] [nchar](10) COLLATE SQL_Latin1_General_CP1_CI_AS NULL,
CONSTRAINT [PK_SalesLog] PRIMARY KEY CLUSTERED 
(
[SalesID] ASC
)WITH (PAD_INDEX  = OFF, IGNORE_DUP_KEY = OFF) ON [PRIMARY]
) ON [PRIMARY]

产品表

CREATE TABLE [dbo].[Products](
[ProductId] [int] IDENTITY(1,1) NOT NULL,
[pName] [nvarchar](30) COLLATE SQL_Latin1_General_CP1_CI_AS NULL,
[pSize] [int] NULL,
[pPrice] [decimal](10, 2) NULL,
[pPackQty] [int] NULL,
[pGroup] [int] NULL,
[pCode] [int] NULL,
[OpenStock] [int] NULL,
[CloseStock] [int] NULL,
[YrlyOpenStock] [int] NULL,
CONSTRAINT [PK_Products] PRIMARY KEY CLUSTERED 
(
[ProductId] ASC
)WITH (PAD_INDEX  = OFF, IGNORE_DUP_KEY = OFF) ON [PRIMARY]
) ON [PRIMARY]

在我的 RDLC 报告中,我必须显示不同项目的销售报告。现在的问题是我已经根据日期范围(例如 01/01/2010 到 15/03/2010 dd/mm/yyyy)显示报告,并且在此期间项目价格可能会发生多次变化。所以我也必须显示这些价格变化。例如,

Sales Report for the date 01/01/2010 to 15/03/2010

ItemName    Price   Quantity   Total
  A          $1       2          $2
  A          $1.20    5          $6
  A          $1.10    5          $5.50
  B          $5       1          $5

正如您在上面的示例中看到的,商品“A”价格已更改两次,因此我必须显示该期间以特定价格出售的数量。我的产品表包含商品的最新价格(pPrice 列),而 SalesLog 表包含开票时的商品价格(价格列)。

我尝试了很多,但没有接近它,任何人都可以帮助我使用任何其他技术的 T-SQL 查询来实现我的目标。

非常感谢............

I have two tables Products and SalesLog,

SalesLog Table

CREATE TABLE [dbo].[SalesLog](
[SalesID] [int] IDENTITY(1,1) NOT NULL,
[MemoNo] [int] NULL,
[ProductCode] [int] NULL,
[Quantity] [int] NULL,
[Price] [decimal](10, 2) NULL,
[pGroup] [int] NULL,
[pName] [nvarchar](30) COLLATE SQL_Latin1_General_CP1_CI_AS NULL,
[pSize] [int] NULL,
[BillDate] [nchar](10) COLLATE SQL_Latin1_General_CP1_CI_AS NULL,
CONSTRAINT [PK_SalesLog] PRIMARY KEY CLUSTERED 
(
[SalesID] ASC
)WITH (PAD_INDEX  = OFF, IGNORE_DUP_KEY = OFF) ON [PRIMARY]
) ON [PRIMARY]

Products Table

CREATE TABLE [dbo].[Products](
[ProductId] [int] IDENTITY(1,1) NOT NULL,
[pName] [nvarchar](30) COLLATE SQL_Latin1_General_CP1_CI_AS NULL,
[pSize] [int] NULL,
[pPrice] [decimal](10, 2) NULL,
[pPackQty] [int] NULL,
[pGroup] [int] NULL,
[pCode] [int] NULL,
[OpenStock] [int] NULL,
[CloseStock] [int] NULL,
[YrlyOpenStock] [int] NULL,
CONSTRAINT [PK_Products] PRIMARY KEY CLUSTERED 
(
[ProductId] ASC
)WITH (PAD_INDEX  = OFF, IGNORE_DUP_KEY = OFF) ON [PRIMARY]
) ON [PRIMARY]

In my RDLC report I have to show Sales report for different items. Now the problem is that I have show report according Date Range (for example 01/01/2010 to 15/03/2010 dd/mm/yyyy) and in this period Item Price may change for several times. So i have to show those price changes also. For example

Sales Report for the date 01/01/2010 to 15/03/2010

ItemName    Price   Quantity   Total
  A          $1       2          $2
  A          $1.20    5          $6
  A          $1.10    5          $5.50
  B          $5       1          $5

As you can see in above example Item "A" price has changed twice, so i have to show how many quantities sold at particular price in that period. My Products table contains latest price of an Item (pPrice column) and SalesLog table contains Item price at the time of billing (Price column).

I tried a lot but no where near it, Can anyone help me with T-SQL query of any other technique to achieve my goal.

thank a lot..............

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

紫罗兰の梦幻 2024-09-17 03:38:56

您想要获取示例 A 中的数据吗?如果是这样,这应该可行;

DECLARE @StartDate DATETIME ,
    @EndDate DATETIME

SELECT  @StartDate = '01 Jan 2010' ,
        @EndDate = '15 Mar 2010'

SELECT  [Products].pName AS ItemName,
        SalesLog.[Price] AS Price ,
        COUNT(*)AS Quantity ,
        SUM(SalesLog.[Price]) AS Total
FROM    SalesLog
        JOIN [Products] ON [Products].pCode = SalesLog.ProductCode /*Check this join - I'm not sure what your relationship is*/
WHERE   BillDate >= @StartDate
        AND BillDate < @EndDate + 1
GROUP BY [Products].pName ,
        SalesLog.[Price]

Are you looking to get the data in Example A? If so, this should work;

DECLARE @StartDate DATETIME ,
    @EndDate DATETIME

SELECT  @StartDate = '01 Jan 2010' ,
        @EndDate = '15 Mar 2010'

SELECT  [Products].pName AS ItemName,
        SalesLog.[Price] AS Price ,
        COUNT(*)AS Quantity ,
        SUM(SalesLog.[Price]) AS Total
FROM    SalesLog
        JOIN [Products] ON [Products].pCode = SalesLog.ProductCode /*Check this join - I'm not sure what your relationship is*/
WHERE   BillDate >= @StartDate
        AND BillDate < @EndDate + 1
GROUP BY [Products].pName ,
        SalesLog.[Price]
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文