Сгруппировать в запросе SQL не работает в моем запросе - PullRequest
0 голосов
/ 24 октября 2019

result set У меня есть 3 таблицы. Заказы, OrderLines, BillLines. Первичный ключ Orders - это FK в двух других таблицах.

Я пытаюсь запросить сумму дебета и кредита клиента. Таблица действий просто описывает тип транзакции. В таблице Subindex хранятся имена и идентификаторы клиентов

  CREATE TABLE [dbo].[Orders](
    [OrderID] [int] IDENTITY(1,1) NOT NULL,
    [Ordertype] [nvarchar](max) NULL,
    [Orderdate] [datetime2](7) NOT NULL,
    [transactionNo] [int] NOT NULL,
    [DocumentNo] [int] NOT NULL,
    [ordernote] [nvarchar](max) NULL,
    [Profittax] [decimal](18, 2) NOT NULL,
    [DescDR] [decimal](18, 2) NULL,
    [DescCR] [decimal](18, 2) NULL,
    [ActionsID] [smallint] NOT NULL,
    [SubIndexesID] [smallint] NULL,
 CONSTRAINT [PK_dbo.Orders] PRIMARY KEY CLUSTERED 
(
    [OrderID] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
) ON [PRIMARY] TEXTIMAGE_ON [PRIMARY]
GO

ALTER TABLE [dbo].[Orders]  WITH CHECK ADD  CONSTRAINT [FK_dbo.Orders_dbo.Actions_ActionsID] FOREIGN KEY([ActionsID])
REFERENCES [dbo].[Actions] ([ActionsID])
ON DELETE CASCADE
GO

ALTER TABLE [dbo].[Orders] CHECK CONSTRAINT [FK_dbo.Orders_dbo.Actions_ActionsID]
GO

ALTER TABLE [dbo].[Orders]  WITH CHECK ADD  CONSTRAINT [FK_dbo.Orders_dbo.SubIndexes_SubIndexesID] FOREIGN KEY([SubIndexesID])
REFERENCES [dbo].[SubIndexes] ([SubIndexesID])
GO

ALTER TABLE [dbo].[Orders] CHECK CONSTRAINT [FK_dbo.Orders_dbo.SubIndexes_SubIndexesID]
GO

    CREATE TABLE [dbo].[Orderlines](
    [OrderlinesID] [int] IDENTITY(1,1) NOT NULL,
    [OrderID] [int] NOT NULL,
    [Qin] [smallint] NOT NULL,
    [Qout] [smallint] NOT NULL,
    [Price] [decimal](18, 2) NOT NULL,
    [currency] [nvarchar](max) NULL,
    [Discount] [decimal](18, 2) NOT NULL,
    [VAT] [decimal](18, 2) NOT NULL,
    [ItemsID] [smallint] NOT NULL,
    [Total] [decimal](18, 2) NOT NULL,
    [Vatvalue] [decimal](18, 2) NOT NULL,
    [Net] [decimal](18, 2) NOT NULL,
 CONSTRAINT [PK_dbo.Orderlines] PRIMARY KEY CLUSTERED 
(
    [OrderlinesID] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
) ON [PRIMARY] TEXTIMAGE_ON [PRIMARY]
GO

ALTER TABLE [dbo].[Orderlines] ADD  DEFAULT ((0)) FOR [Total]
GO

ALTER TABLE [dbo].[Orderlines] ADD  DEFAULT ((0)) FOR [Vatvalue]
GO

ALTER TABLE [dbo].[Orderlines] ADD  DEFAULT ((0)) FOR [Net]
GO

ALTER TABLE [dbo].[Orderlines]  WITH CHECK ADD  CONSTRAINT [FK_dbo.Orderlines_dbo.Items_ItemsID] FOREIGN KEY([ItemsID])
REFERENCES [dbo].[Items] ([ItemsID])
ON DELETE CASCADE
GO

ALTER TABLE [dbo].[Orderlines] CHECK CONSTRAINT [FK_dbo.Orderlines_dbo.Items_ItemsID]
GO

ALTER TABLE [dbo].[Orderlines]  WITH CHECK ADD  CONSTRAINT [FK_dbo.Orderlines_dbo.Orders_OrderID] FOREIGN KEY([OrderID])
REFERENCES [dbo].[Orders] ([OrderID])
ON DELETE CASCADE
GO

ALTER TABLE [dbo].[Orderlines] CHECK CONSTRAINT [FK_dbo.Orderlines_dbo.Orders_OrderID]
GO

    CREATE TABLE [dbo].[BillLines](
    [BillLinesID] [int] IDENTITY(1,1) NOT NULL,
    [VALIN] [decimal](18, 2) NULL,
    [VALOut] [decimal](18, 2) NULL,
    [OrderID] [int] NOT NULL,
    [SubIndexesID] [smallint] NULL,
 CONSTRAINT [PK_dbo.BillLines] PRIMARY KEY CLUSTERED 
(
    [BillLinesID] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
) ON [PRIMARY]
GO

ALTER TABLE [dbo].[BillLines]  WITH CHECK ADD  CONSTRAINT [FK_dbo.BillLines_dbo.Orders_OrderID] FOREIGN KEY([OrderID])
REFERENCES [dbo].[Orders] ([OrderID])
ON DELETE CASCADE
GO

ALTER TABLE [dbo].[BillLines] CHECK CONSTRAINT [FK_dbo.BillLines_dbo.Orders_OrderID]
GO

ALTER TABLE [dbo].[BillLines]  WITH CHECK ADD  CONSTRAINT [FK_dbo.BillLines_dbo.SubIndexes_SubIndexesID] FOREIGN KEY([SubIndexesID])
REFERENCES [dbo].[SubIndexes] ([SubIndexesID])
GO

ALTER TABLE [dbo].[BillLines] CHECK CONSTRAINT [FK_dbo.BillLines_dbo.SubIndexes_SubIndexesID]
GO

    CREATE TABLE [dbo].[Actions](
    [ActionsID] [smallint] IDENTITY(1,1) NOT NULL,
    [Actionname] [nvarchar](4000) NOT NULL,
 CONSTRAINT [PK_dbo.Actions] PRIMARY KEY CLUSTERED 
(
    [ActionsID] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
) ON [PRIMARY]
GO

    CREATE TABLE [dbo].[SubIndexes](
    [SubIndexesID] [smallint] IDENTITY(1,1) NOT NULL,
    [Subname] [nvarchar](max) NULL,
    [AccountIndexID] [smallint] NOT NULL,
    [Contacttype] [nvarchar](max) NULL,
    [phones] [nvarchar](max) NULL,
    [fax] [nvarchar](max) NULL,
    [email] [nvarchar](max) NULL,
    [address] [nvarchar](max) NULL,
    [zip] [nvarchar](max) NULL,
    [Website] [nvarchar](max) NULL,
 CONSTRAINT [PK_dbo.SubIndexes] PRIMARY KEY CLUSTERED 
(
    [SubIndexesID] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
) ON [PRIMARY] TEXTIMAGE_ON [PRIMARY]
GO

ALTER TABLE [dbo].[SubIndexes]  WITH CHECK ADD  CONSTRAINT [FK_dbo.SubIndexes_dbo.AccountIndexes_AccountIndexID] FOREIGN KEY([AccountIndexID])
REFERENCES [dbo].[AccountIndexes] ([AccountIndexID])
ON DELETE CASCADE
GO

ALTER TABLE [dbo].[SubIndexes] CHECK CONSTRAINT [FK_dbo.SubIndexes_dbo.AccountIndexes_AccountIndexID]
GO

Мой запрос

select distinct
               o.OrderID , 
               s.Subname as subindexname,  
               o.SubIndexesID as Subindex ,
               sum(ol.Qout * (ol.Price - ol.Discount)* (1+(ol.VAT/100)-(o.Profittax/100))+ bl.VALOut) -iif(o.ActionsID between 3 and 4, o.DescDR+o.DescCR,0) as Debit ,
               sum(ol.Qin * (ol.Price - ol.Discount )* (1+(ol.VAT/100)-(o.Profittax/100))+ bl.VALIN)-iif(o.ActionsID not between 3 and 4,o.DescCR + o.DescDR,0) as Credit

from Orders o 
     inner join 
     Orderlines ol  on o.OrderID = ol.OrderID 
     inner join 
     BillLines bl on o.OrderID = bl.OrderID 
     inner join 
     SubIndexes s on o.SubIndexesID= s.SubIndexesID
group by o.OrderID, 
         o.ActionsID, 
         o.SubIndexesID,
         o.DescCR,
         o.DescDR,
         ol.Qout,
         ol.Qin,
         s.Subname,
         s.SubIndexesID, 
         ol.VAT, 
         ol.Price, 
         ol.Discount
having o.ActionsID=2 
       or o.ActionsID=3 
       or o.ActionsID=4 
       or o.ActionsID=5

Я хочу показать дебет и кредит вв одной строке даже у меня несколько строк заказа на один заказ.

1 Ответ

0 голосов
/ 24 октября 2019

Необходимо также включить все столбцы в Group-by в Select:

select distinct
               o.OrderID , 
               s.Subname as subindexname,  
               o.SubIndexesID as Subindex ,              
               o.ActionsID,          
               o.DescCR,
               o.DescDR,
               ol.Qout,
               ol.Qin,
               s.Subname,
               s.SubIndexesID, 
               ol.VAT, 
               ol.Price, 
               ol.Discount
               sum(ol.Qout * (ol.Price - ol.Discount)* (1+(ol.VAT/100)-(o.Profittax/100))+ bl.VALOut) -iif(o.ActionsID between 3 and 4, o.DescDR+o.DescCR,0) as Debit ,
               sum(ol.Qin * (ol.Price - ol.Discount )* (1+(ol.VAT/100)-(o.Profittax/100))+ bl.VALIN)-iif(o.ActionsID not between 3 and 4,o.DescCR + o.DescDR,0) as Credit

from Orders o 
     inner join 
     Orderlines ol  on o.OrderID = ol.OrderID 
     inner join 
     BillLines bl on o.OrderID = bl.OrderID 
     inner join 
     SubIndexes s on o.SubIndexesID= s.SubIndexesID
group by o.OrderID, 
         o.ActionsID, 
         o.SubIndexesID,
         o.DescCR,
         o.DescDR,
         ol.Qout,
         ol.Qin,
         s.Subname,
         s.SubIndexesID, 
         ol.VAT, 
         ol.Price, 
         ol.Discount
having o.ActionsID=2 
       or o.ActionsID=3 
       or o.ActionsID=4 
       or o.ActionsID=5
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...