-- WHERE P.barcode = '8801063310486' 이부분을
-- WHERE P.comcode = '1005' 로 변경시 아래와 같이 에러가 뜹니다 Row 가 하나 이상이어서 그런듯한데 어떻게해야하나요?
-- 고수님들 부탁드립니다
-- Subquery returned more than 1 value. This is not permitted when the subquery follows =, !=, <, <= , >, >= or when the --subquery is used as an expression.
-- 테스트_DB 생성
CREATE DATABASE [Test_DB] ON PRIMARY
( NAME = N'Test_DB', FILENAME = N'C:\Test_DB.MDF' , SIZE = 3072KB , FILEGROWTH = 1024KB )
LOG ON
( NAME = N'Test_DB_log', FILENAME = N'C:\Test_DB_Log.LDF' , SIZE = 1024KB , FILEGROWTH = 10%)
Go
USE [Test_DB]
Go
CREATE TABLE [dbo].[parts](
[barcode] [varchar](17) NOT NULL,
[descr] [varchar](30) NULL,
[comcode] [varchar](5) NULL
)
Go
INSERT INTO parts ( barcode, descr, comcode ) VALUES ( '8801063310486', '테스트01', '1005' )
INSERT INTO parts ( barcode, descr, comcode ) VALUES ( '8809111694533', '테스트02', '1005' )
INSERT INTO parts ( barcode, descr, comcode ) VALUES ( '8801075014532', '테스트03', '1005' )
Go
CREATE TABLE [dbo].[outd_1710](
[icomcode] [varchar](5) NULL,
[barcode] [varchar](17) NOT NULL,
[mitemcount] [decimal](12, 2) NOT NULL,
[bitemcount] [decimal](12, 2) NOT NULL,
[day1] [varchar](10) NOT NULL
)
Go
CREATE TABLE [dbo].[outd_1711](
[icomcode] [varchar](5) NULL,
[barcode] [varchar](17) NOT NULL,
[mitemcount] [decimal](12, 2) NOT NULL,
[bitemcount] [decimal](12, 2) NOT NULL,
[day1] [varchar](10) NOT NULL
)
Go
CREATE TABLE [dbo].[outd_1712](
[icomcode] [varchar](5) NULL,
[barcode] [varchar](17) NOT NULL,
[mitemcount] [decimal](12, 2) NOT NULL,
[bitemcount] [decimal](12, 2) NOT NULL,
[day1] [varchar](10) NOT NULL
)
CREATE TABLE [dbo].[outd_yymm](
[icomcode] [varchar](5) NULL,
[barcode] [varchar](17) NOT NULL,
[mitemcount] [decimal](12, 2) NOT NULL,
[bitemcount] [decimal](12, 2) NOT NULL,
[day1] [varchar](10) NOT NULL
)
Go
INSERT INTO outd_1710 ( icomcode, barcode, mitemcount, bitemcount, day1 ) VALUES ( '1005', '8801063310486', '10', '1', '2017-10-01' )
INSERT INTO outd_1710 ( icomcode, barcode, mitemcount, bitemcount, day1 ) VALUES ( '1005', '8809111694533', '20', '2', '2017-10-02' )
INSERT INTO outd_1710 ( icomcode, barcode, mitemcount, bitemcount, day1 ) VALUES ( '1005', '8801075014532', '30', '3', '2017-10-03' )
INSERT INTO outd_1711 ( icomcode, barcode, mitemcount, bitemcount, day1 ) VALUES ( '1005', '8801063310486', '10', '1', '2017-11-01' )
INSERT INTO outd_1711 ( icomcode, barcode, mitemcount, bitemcount, day1 ) VALUES ( '1005', '8809111694533', '20', '2', '2017-11-02' )
INSERT INTO outd_1711 ( icomcode, barcode, mitemcount, bitemcount, day1 ) VALUES ( '1005', '8801075014532', '30', '3', '2017-11-03' )
INSERT INTO outd_1712 ( icomcode, barcode, mitemcount, bitemcount, day1 ) VALUES ( '1005', '8801063310486', '10', '1', '2017-12-01' )
INSERT INTO outd_1712 ( icomcode, barcode, mitemcount, bitemcount, day1 ) VALUES ( '1005', '8809111694533', '20', '2', '2017-12-02' )
INSERT INTO outd_1712 ( icomcode, barcode, mitemcount, bitemcount, day1 ) VALUES ( '1005', '8801075014532', '30', '3', '2017-12-03' )
Go
CREATE TABLE [dbo].[ipgod_1710](
[barcode] [varchar](17) NULL,
[comcode] [varchar](5) NULL,
[iitemcount] [decimal](15, 2) NULL,
[bitemcount] [decimal](15, 2) NULL,
[day1] [varchar](10) NOT NULL
)
Go
CREATE TABLE [dbo].[ipgod_1711](
[barcode] [varchar](17) NULL,
[comcode] [varchar](5) NULL,
[iitemcount] [decimal](15, 2) NULL,
[bitemcount] [decimal](15, 2) NULL,
[day1] [varchar](10) NOT NULL
)
Go
CREATE TABLE [dbo].[ipgod_1712](
[barcode] [varchar](17) NULL,
[comcode] [varchar](5) NULL,
[iitemcount] [decimal](15, 2) NULL,
[bitemcount] [decimal](15, 2) NULL,
[day1] [varchar](10) NOT NULL
)
Go
INSERT INTO ipgod_1710 ( barcode, comcode, iitemcount, bitemcount, day1 ) VALUES ( '8801063310486', '1005', '5', '1', '2017-10-01' )
INSERT INTO ipgod_1710 ( barcode, comcode, iitemcount, bitemcount, day1 ) VALUES ( '8809111694533', '1005', '30', '2', '2017-10-02' )
INSERT INTO ipgod_1710 ( barcode, comcode, iitemcount, bitemcount, day1 ) VALUES ( '8801075014532', '1005', '50', '3', '2017-10-03' )
INSERT INTO ipgod_1711 ( barcode, comcode, iitemcount, bitemcount, day1 ) VALUES ( '8801063310486', '1005', '10', '1', '2017-11-01' )
INSERT INTO ipgod_1711 ( barcode, comcode, iitemcount, bitemcount, day1 ) VALUES ( '8809111694533', '1005', '120', '2', '2017-11-02' )
INSERT INTO ipgod_1711 ( barcode, comcode, iitemcount, bitemcount, day1 ) VALUES ( '8801075014532', '1005', '330', '3', '2017-11-03' )
INSERT INTO ipgod_1712 ( barcode, comcode, iitemcount, bitemcount, day1 ) VALUES ( '8801063310486', '1005', '110', '1', '2017-12-01' )
INSERT INTO ipgod_1712 ( barcode, comcode, iitemcount, bitemcount, day1 ) VALUES ( '8809111694533', '1005', '220', '2', '2017-12-02' )
INSERT INTO ipgod_1712 ( barcode, comcode, iitemcount, bitemcount, day1 ) VALUES ( '8801075014532', '1005', '30', '3', '2017-12-03' )
Go
CREATE TABLE [dbo].[bojung_1710](
[comcode] [varchar](5) NOT NULL,
[barcode] [varchar](17) NULL,
[itemcount] [decimal](10, 2) NULL,
[day1] [varchar](10) NOT NULL
)
Go
CREATE TABLE [dbo].[bojung_1711](
[comcode] [varchar](5) NOT NULL,
[barcode] [varchar](17) NULL,
[itemcount] [decimal](10, 2) NULL,
[day1] [varchar](10) NOT NULL
)
Go
CREATE TABLE [dbo].[bojung_1712](
[comcode] [varchar](5) NOT NULL,
[barcode] [varchar](17) NULL,
[itemcount] [decimal](10, 2) NULL,
[day1] [varchar](10) NOT NULL
)
Go
INSERT INTO bojung_1710 ( comcode, barcode, itemcount, day1 ) VALUES ( '1005', '8801063310486', '1', '2017-10-01' )
INSERT INTO bojung_1710 ( comcode, barcode, itemcount, day1 ) VALUES ( '1005', '8809111694533', '2', '2017-10-02' )
INSERT INTO bojung_1710 ( comcode, barcode, itemcount, day1 ) VALUES ( '1005', '8801075014532', '3', '2017-10-03' )
INSERT INTO bojung_1711 ( comcode, barcode, itemcount, day1 ) VALUES ( '1005', '8801063310486', '1', '2017-11-01' )
INSERT INTO bojung_1711 ( comcode, barcode, itemcount, day1 ) VALUES ( '1005', '8809111694533', '2', '2017-11-02' )
INSERT INTO bojung_1711 ( comcode, barcode, itemcount, day1 ) VALUES ( '1005', '8801075014532', '3', '2017-11-03' )
INSERT INTO bojung_1712 ( comcode, barcode, itemcount, day1 ) VALUES ( '1005', '8801063310486', '1', '2017-12-01' )
INSERT INTO bojung_1712 ( comcode, barcode, itemcount, day1 ) VALUES ( '1005', '8809111694533', '2', '2017-12-02' )
INSERT INTO bojung_1712 ( comcode, barcode, itemcount, day1 ) VALUES ( '1005', '8801075014532', '3', '2017-12-03' )
Go
USE [Test_DB]
Go
-- 쿼리 실행
-- 입고-판매-보정 = 총합계
-----------------------------------------------------------------------------------------------------------------------------------------------
---------------------------------------- 입고 -------------------------------------------------------------------------------------------------
-----------------------------------------------------------------------------------------------------------------------------------------------
SELECT
p.barcode, p.descr, '입고-판매-보정' AS "구분", 'FULL' AS "구분2",
ISNULL(i1710.cnt,0) + ISNULL(i1711.cnt, 0) + ISNULL(i1712.cnt, 0) +
ISNULL(yy.cnt,0)
-
-----------------------------------------------------------------------------------------------------------------------------------------------
---------------------------------------- 판매 -------------------------------------------------------------------------------------------------
-----------------------------------------------------------------------------------------------------------------------------------------------
(
SELECT
ISNULL( o1710.cnt, 0) + ISNULL( o1711.cnt, 0) + ISNULL( o1712.cnt, 0) +
ISNULL( yy.cnt, 0)
FROM
(SELECT barcode, SUM(mitemcount - bitemcount) cnt FROM outd_1710 GROUP BY barcode) as o1710
FULL OUTER JOIN ( SELECT * FROM parts ) p ON o1710.barcode = p.barcode
FULL OUTER JOIN ( SELECT barcode, SUM(mitemcount - bitemcount) cnt FROM outd_1711 GROUP BY barcode ) o1711
ON p.barcode = o1711.barcode
FULL OUTER JOIN ( SELECT barcode, SUM(mitemcount - bitemcount) cnt FROM outd_1712
WHERE day1 BETWEEN '2017-10-01' AND '2017-12-31' GROUP BY barcode ) o1712
ON p.barcode = o1712.barcode
FULL OUTER JOIN ( SELECT barcode, SUM(ISNULL(mitemcount,0) - ISNULL(bitemcount,0)) cnt FROM outd_yymm GROUP BY barcode ) yy
ON p.barcode = yy.barcode
-- WHERE P.barcode = '8801063310486'
WHERE P.comcode = '1005'
)
+
-----------------------------------------------------------------------------------------------------------------------------------------------
---------------------------------------- 보정 ------------------------------------------------------------------------------------------------
-----------------------------------------------------------------------------------------------------------------------------------------------
(
SELECT
ISNULL(b1710.cnt,0) + ISNULL(b1711.cnt, 0) + ISNULL(b1712.cnt, 0) +
ISNULL(yy.cnt,0)
FROM
(SELECT barcode, itemcount AS cnt FROM bojung_1710 GROUP BY barcode, itemcount) as b1710
FULL OUTER JOIN ( SELECT * FROM parts ) p ON b1710.barcode = p.barcode
FULL OUTER JOIN ( SELECT barcode, itemcount AS cnt FROM bojung_1711 GROUP BY barcode, itemcount ) b1711
ON p.barcode = b1711.barcode
FULL OUTER JOIN ( SELECT barcode, itemcount AS cnt FROM bojung_1712
WHERE day1 BETWEEN '2017-10-01' AND '2017-12-31' GROUP BY barcode, itemcount ) b1712
ON p.barcode = b1712.barcode
LEFT OUTER JOIN ( SELECT '0' yymm, 0 cnt ) yy
ON p.barcode = yy.yymm
-- WHERE P.barcode = '8801063310486'
WHERE P.comcode = '1005'
) AS "총합계"
FROM
(SELECT barcode, SUM(iitemcount - bitemcount) cnt FROM ipgod_1710 GROUP BY barcode) i1710
FULL OUTER JOIN ( SELECT * FROM parts ) p ON i1710.barcode = p.barcode
FULL OUTER JOIN ( SELECT barcode, SUM(iitemcount - bitemcount) cnt FROM ipgod_1711 GROUP BY barcode ) i1711
ON p.barcode = i1711.barcode
FULL OUTER JOIN ( SELECT barcode, SUM(iitemcount - bitemcount) cnt FROM ipgod_1712
WHERE day1 BETWEEN '2017-10-01' AND '2017-12-31' GROUP BY barcode ) i1712
ON p.barcode = i1712.barcode
LEFT OUTER JOIN ( SELECT '0' yymm, 0 cnt ) yy
ON p.barcode = yy.yymm
-- WHERE P.barcode = '8801063310486'
WHERE P.comcode = '1005'
|