Sql server SQL-重复行(更改一列)
我很难在SQLServerManagementStudio 17中创建包含表中重复值的视图 我的桌子是这样的:Sql server SQL-重复行(更改一列),sql-server,view,duplicates,Sql Server,View,Duplicates,我很难在SQLServerManagementStudio 17中创建包含表中重复值的视图 我的桌子是这样的: DATE = datetime Product = varchar(10) Price = decimal(18,2) Date | Product | Price 07-12-17 | 1 | 32 06-12-17 | 24 | 35 每天有1-24种产品 我要创建以下视图选项卡: Date | Product | Price
DATE = datetime
Product = varchar(10)
Price = decimal(18,2)
Date | Product | Price
07-12-17 | 1 | 32
06-12-17 | 24 | 35
每天有1-24种产品
我要创建以下视图选项卡:
Date | Product | Price
07-12-17 | 1.1 | 32
07-12-17 | 1.2 | 32
07-12-17 | 1.3 | 32
07-12-17 | 1.4 | 32
06-12-17 | 24.1 | 35
06-12-17 | 24.2 | 35
06-12-17 | 24.3 | 35
06-12-17 | 24.4 | 35
每天24种产品都是如此 尝试使用
交叉连接和值
SELECT
t.[Date],
CONCAT(t.Product,'.',l.num) ProductLabel,
t.Price
FROM [Your table] t
CROSS JOIN (VALUES(1),(2),(3),(4)) l(num)
ORDER BY t.[Date],t.Product,l.num
带有UNION ALL的变量
SELECT
t.[Date],
t.Product+'.'+l.Label ProductLabel,
t.Price
FROM [Your table] t
CROSS JOIN
(
SELECT '1' Label UNION ALL SELECT '2' UNION ALL SELECT '3' UNION ALL SELECT '4'
) l
ORDER BY t.[Date],t.Product,l.Label
SELECT [Date],Product+'.1' ProductLabel,Price FROM [Your table]
UNION ALL SELECT [Date],Product+'.2',Price FROM [Your table]
UNION ALL SELECT [Date],Product+'.3',Price FROM [Your table]
UNION ALL SELECT [Date],Product+'.4',Price FROM [Your table]
ORDER BY [Date],ProductLabel
如果您有另一个表,其中包含数字为1-4的行,则可以对该表使用CROS JOIN
SELECT
t.[Date],
CONCAT(t.Product,'.',l.[ID from another table]) ProductLabel,
t.Price
FROM [Your table] t
CROSS JOIN [Your table with numbers] l
ORDER BY t.[Date],t.Product,l.[ID from another table]
如果您只需要4个副本,那么您还可以使用4个查询与UNION ALL
SELECT
t.[Date],
t.Product+'.'+l.Label ProductLabel,
t.Price
FROM [Your table] t
CROSS JOIN
(
SELECT '1' Label UNION ALL SELECT '2' UNION ALL SELECT '3' UNION ALL SELECT '4'
) l
ORDER BY t.[Date],t.Product,l.Label
SELECT [Date],Product+'.1' ProductLabel,Price FROM [Your table]
UNION ALL SELECT [Date],Product+'.2',Price FROM [Your table]
UNION ALL SELECT [Date],Product+'.3',Price FROM [Your table]
UNION ALL SELECT [Date],Product+'.4',Price FROM [Your table]
ORDER BY [Date],ProductLabel
尝试将交叉联接
与值一起使用
SELECT
t.[Date],
CONCAT(t.Product,'.',l.num) ProductLabel,
t.Price
FROM [Your table] t
CROSS JOIN (VALUES(1),(2),(3),(4)) l(num)
ORDER BY t.[Date],t.Product,l.num
带有UNION ALL的变量
SELECT
t.[Date],
t.Product+'.'+l.Label ProductLabel,
t.Price
FROM [Your table] t
CROSS JOIN
(
SELECT '1' Label UNION ALL SELECT '2' UNION ALL SELECT '3' UNION ALL SELECT '4'
) l
ORDER BY t.[Date],t.Product,l.Label
SELECT [Date],Product+'.1' ProductLabel,Price FROM [Your table]
UNION ALL SELECT [Date],Product+'.2',Price FROM [Your table]
UNION ALL SELECT [Date],Product+'.3',Price FROM [Your table]
UNION ALL SELECT [Date],Product+'.4',Price FROM [Your table]
ORDER BY [Date],ProductLabel
如果您有另一个表,其中包含数字为1-4的行,则可以对该表使用CROS JOIN
SELECT
t.[Date],
CONCAT(t.Product,'.',l.[ID from another table]) ProductLabel,
t.Price
FROM [Your table] t
CROSS JOIN [Your table with numbers] l
ORDER BY t.[Date],t.Product,l.[ID from another table]
如果您只需要4个副本,那么您还可以使用4个查询与UNION ALL
SELECT
t.[Date],
t.Product+'.'+l.Label ProductLabel,
t.Price
FROM [Your table] t
CROSS JOIN
(
SELECT '1' Label UNION ALL SELECT '2' UNION ALL SELECT '3' UNION ALL SELECT '4'
) l
ORDER BY t.[Date],t.Product,l.Label
SELECT [Date],Product+'.1' ProductLabel,Price FROM [Your table]
UNION ALL SELECT [Date],Product+'.2',Price FROM [Your table]
UNION ALL SELECT [Date],Product+'.3',Price FROM [Your table]
UNION ALL SELECT [Date],Product+'.4',Price FROM [Your table]
ORDER BY [Date],ProductLabel
获取以下错误消息“num附近的语法不正确”。“您使用的SQL Server版本是什么?SQL Server Management Studio 17TryVariant with UNION ALL
。不,它是SSMS
的版本。SQLServer的版本可能不同。请尝试运行选择@@Version
以显示它。Microsoft SQL Azure(RTM)收到以下错误消息“num附近的语法不正确”。“您使用的是什么版本的SQL Server?SQL Server Management Studio 17Try带有UNION ALL的变体
”。不,它是SSMS
的版本。SQLServer的版本可以不同。请尝试运行选择@Version
以显示它。它是Microsoft SQL Azure(RTM)