有没有更优雅的方法可以从1个表中插入多个选择并将它们插入表2中?

时间:2019-02-07 10:36:08

标签: sql sql-server tsql

我现在的操作方式是:

INSERT INTO .TempVolumePrediction
SELECT YearNb, WeekNb, ProdID, Sum(Volume) AS SumOfVolume, Country, 'Stock' AS VolumeTypeCode
FROM StockVolumes
WHERE Country Not Like 'EE'
GROUP BY YearNb, WeekNb, ProdID, Country

UNION ALL

SELECT YearNb, WeekNb, ProdID, Sum(Volume) AS SumOfVolume, 'BUEU' AS Country, 'Stock' AS VolumeTypeCode
FROM StockVolumes
WHERE StockVolumes.Country Not Like 'EE'
GROUP BY YearNb, WeekNb, ProdID

UNION ALL 

SELECT YearNb, WeekNb, ProdID, Sum(Volume) AS SumOfVolume, 'DTEU' AS Country, 'Stock' AS VolumeTypeCode
FROM StockVolumes
WHERE StockVolumes.Country Not Like 'EE' AND GroupCode = 'DT'
GROUP BY YearNb, WeekNb, ProdID

UNION ALL

SELECT YearNb, WeekNb, ProdID, Sum(Volume) AS SumOfVolume, 'OREU' AS Country, 'Stock' AS VolumeTypeCode
FROM StockVolumes
WHERE StockVolumes.Country Not Like 'EE' AND GroupCode = 'OR'
GROUP BY YearNb, WeekNb, ProdID

对于每个选择查询,选择略有不同的分组数据。 因此,决赛桌将按产品列出所有销量。然后按州分组销售量,最后按国家分组销售量。

3 个答案:

答案 0 :(得分:2)

全部联盟:

Insert into Table 1

Select From Table 2
UNION ALL
Select From Table 2
UNION ALL
Select From Table 2

更新,如果您的主要目标是性能,请考虑使用SELECT INTO而不是INSERT INTO:

DROP TABLE IF EXISTS dbo.TempVolumePrediction

SELECT YearNb, WeekNb, ProdID,  SumOfVolume, Country,  VolumeTypeCode 
INTO dbo.TempVolumePrediction
FROM (


SELECT YearNb, WeekNb, ProdID, Sum(Volume) AS SumOfVolume, Country, 'Stock' AS VolumeTypeCode
WHERE Country Not Like 'EE'
GROUP BY YearNb, WeekNb, ProdID, Country

UNION ALL

SELECT YearNb, WeekNb, ProdID, Sum(Volume) AS SumOfVolume, 'BUEU' AS Country, 'Stock' AS VolumeTypeCode
WHERE StockVolumes.Country Not Like 'EE'
GROUP BY YearNb, WeekNb, ProdID

UNION ALL 

SELECT YearNb, WeekNb, ProdID, Sum(Volume) AS SumOfVolume, 'DTEU' AS Country, 'Stock' AS VolumeTypeCode
FROM .StockVolumes
WHERE StockVolumes.Country Not Like 'EE' AND GroupCode = 'DT'
GROUP BY YearNb, WeekNb, ProdID

UNION ALL

SELECT YearNb, WeekNb, ProdID, Sum(Volume) AS SumOfVolume, 'OREU' AS Country, 'Stock' AS VolumeTypeCode
FROM .StockVolumes
WHERE StockVolumes.Country Not Like 'EE' AND GroupCode = 'OR'
GROUP BY YearNb, WeekNb, ProdID
) d

答案 1 :(得分:1)

使用 OR

.menu{
	padding: 20px;
	background: #d80000;
}
.mainmenu{
	display: flex;
	list-style: none;
}
.heading{
	margin-right: 1px;
}
.mainmenu .heading a{
	display: inline-block;
	padding: 10px;
	text-decoration: none;
	background: #fff;
	color: #d80000;
	width: 80px;
	text-align: center;
	position: relative;
}

.submenu{
	list-style: none;
	margin-left: -40px;
	display: none;
	position: absolute;
}

.submenu a{
	border-top: 3px solid #d80000;
	width: 80px;
}

.heading a:hover{
	background: #d80000;
	color: #fff;
}

/* Use general sibling selector to select .submenu siblings of .heading a:hover*/
.heading a:hover ~ .submenu{
	display: block;
}
/* however, now when you move your pointer down to the submenu itself you're no longer hovering on the a so the submenu will go back to display none; To resolve this we can insist that the submenu themselves are display block when hovered: */
.heading .submenu:hover {
	display: block;
}

答案 2 :(得分:1)

如果您准备添加一个映射GroupCode和Country的表,则可能有一些简化

CREATE TABLE GroupCodeCountry( GroupCode VARCHAR(2), Country VARCHAR(4) );
INSERT INTO GroupCodeCountry('BU', 'BUEU'); --assumed from pattern - did your query set have a typo?
INSERT INTO GroupCodeCountry('DT', 'DTEU');
INSERT INTO GroupCodeCountry('OR', 'OREU');


--this query now handles the cases where the groupcode causes an override to the country    INSERT INTO TempVolumePrediction
SELECT s.YearNb, s.WeekNb, s.ProdID, Sum(s.Volume), COALESCE(g.Country, s.Country), 'Stock'
FROM StockCountry s LEFT OUTER JOIN GroupCodeCountry g on s.GroupCode = g.GroupCode
WHERE s.Country Not Like 'EE'
GROUP BY s.YearNb, s.WeekNb, s.ProdID, COALESCE(g.Country, s.Country)

性能差异必须进行测试/调整,但这代表了编写查询的更简单方法