Many Thanks
i have created tables
CREATE TABLE [dbo].[Trn_GRN](
[Grn_ID] [int] NOT NULL,
[Grn_Number] [nvarchar](50) NULL,
[Grn_Date] [date] NULL,
[Grn_PoNo] [int] NULL,
CONSTRAINT [PK_Trn_GRN] PRIMARY KEY CLUSTERED
(
[Grn_ID] 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].[Trn_GrnDetails](
[Grn_ID_D] [int] NOT NULL,
[Grn_ID] [int] NULL,
[Grn_ItmNo] [int] NULL,
[Grn_Qty] [numeric](18, 4) NULL,
[Grn_LastStockQty] [numeric](18, 4) NULL,
[Grn_LastAvgRate] [numeric](18, 4) NULL,
CONSTRAINT [PK_Trn_GrnDetails] PRIMARY KEY CLUSTERED
(
[Grn_ID_D] 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].[Trn_MIS](
[Mis_ID] [int] NOT NULL,
[Mis_Number] [nvarchar](50) NULL,
[Mis_Date] [date] NULL,
CONSTRAINT [PK_Trn_MIS] PRIMARY KEY CLUSTERED
(
[Mis_ID] 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].[Trn_MisDetails](
[Mis_ID_D] [int] NOT NULL,
[Mis_ID] [int] NULL,
[Mis_ItmNo] [int] NULL,
[Mis_Qty] [numeric](18, 4) NULL,
[Mis_StockQty] [numeric](18, 4) NULL,
[Mis_AveragRate] [numeric](18, 4) NULL,
CONSTRAINT [PK_Trn_MisDetails] PRIMARY KEY CLUSTERED
(
[Mis_ID_D] 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].[Mst_Stock](
[Stock_ItmNo] [int] NULL,
[Stock_Qty] [numeric](18, 4) NULL,
[Stock_Location] [nvarchar](50) NULL,
[Stock_AvgRate] [numeric](18, 4) NULL
) ON [PRIMARY]
GO
When i write your i got error
SELECT
Stock_ItmNo = COALESCE(G.Grn_itmNo,T.Mis_itmNo)
,Stock_Qty = COALESCE(G.Grn_Qty,0)- COALESCE(T.Mis_Qty,0)
,Grn_Qty_AVG = G.Grn_Qty_AVG
,Mis_Qty_AVG = T.Mis_Qty_AVG
FROM
(SELECT
Grn_itmNo = Grn_itmNo
,Grn_Qty = COALESCE(SUM(Grn_Qty),0)
,Grn_Qty_AVG = AVG(Grn_Qty)
FROM
Trn_Grn AS G
INNER JOIN Trn_GrnDetails AS GD
ON G.Grn_ID = GD.Grn_ID
WHERE
G.Grn_Date<='20160130'
GROUP BY
Grn_itmNo
)G
FULL JOIN
(SELECT
Mis_itmNo = Mis_itmNo
,Mis_Qty = COALESCE(Mis_Qty,0)
,Mis_Qty_AVG = AVG(Mis_Qty)
FROM
Trn_MIS AS M
INNER JOIN Trn_MisDetails AS MD
ON M.Mis_ID = MD.Mis_ID
WHERE
M.Mis_Date<='20160130'
GROUP BY
Mis_itmNo
)T
ON G.Grn_itmNo = T.Mis_itmNo
error is
Msg 8120, Level 16, State 1, Line 23
Column 'Trn_MisDetails.Mis_Qty' is invalid in the select list because it is not contained in either an aggregate function or the GROUP BY clause.
Msg 8120, Level 16, State 1, Line 23
Column 'Trn_MisDetails.Mis_Qty' is invalid in the select list because it is not contained in either an aggregate function or the GROUP BY clause.
another query what you sent that also got error.
(SELECT
Grn_itmNo = Grn_itmNo
,Grn_Qty = COALESCE(SUM(Grn_Qty),0)
,Grn_Qty_AVG = AVG(Grn_Qty)
FROM
Trn_Grn AS G
INNER JOIN Trn_GrnDetails AS GD
ON G.Grn_ID = GD.Grn_ID
WHERE
G.Grn_Date<='20160130'
GROUP BY
Grn_itmNo
)G
error is
Msg 102, Level 15, State 1, Line 13
Incorrect syntax near 'G'.
Once again many thanks
Thanks
Basit.