SQL-唯一列出重复的ID

时间:2020-03-10 09:19:11

标签: sql sql-server

这是我使用的代码:

if object_id('Items4sale') is not null  /* <--- creating the new table */
drop table Items4sale;
create table Items4sale (
ID int,
_Type NVARCHAR(100),
Subtype NVARCHAR(100),
Condition INT,
Descr NVARCHAR(100),
Quantity INT,
FullPrice float, /* <--- I actually want to limit the display for prices, for example to "3299.33" instead of "3299.332120" */
Price float, 
PotentialPrice float
)

insert into items4sale
    (ID, _Type, Subtype, FullPrice, Condition, Quantity, Descr, Price, PotentialPrice)
select
    p.ItemNum, 
    p.ItemType, 
    p.Subtype, 
    p.FullPrice,
    i.site_loc,
    i.limit,
    i.descriptions,
    p.FullPrice * 0.05,
    p.FullPrice * 0.05 * i.limit
from items i
inner join prices p on p.ItemNum = i.Material

这是我得到的结果:

ID    |  _Type        |  Subtype | Condition |  Descr | Quantity | FullPrice | Price | PotentialPrice 189 |  Plastic | Toy    
3998  | A plastic toy | 4        | 8         | 0.4    | 1.6    .........

但是,这是我运行以测试错误的查询,

SELECT * 
FROM Items4sale 
WHERE ID = 189; 

SELECT * FROM items_table
WHERE [Material] = 189;

上面是查看我的第一个查询中是否缺少任何重复的ID。事实证明,在“ items_table”中有多个“ 189”作为“材料”(物料的ID),因此从上面代码的第二个“ SELECT”中,我得到了几行以“ 189”作为材料(ID),因为实际上有不止一个-我没有考虑此表中名为“位置”(NVARCHAR(100))的列。我认为这是更高级的:如果在“ items_table”中有多个行具有ID,则不要将它们合并到“ Items4sale”(合并后的新表)中的一行中,而是将它们创建为ID =“ 189”,“(1)189”,“(2)189”(就像Windows尝试两次创建相同的文件/文件夹时一样),但它们的“条件”(items_table。[site_loc])在此大小写不以“ 8”结尾。我仍然想列出它们并从“ price_table”中应用它们的价格等。

这很重要,因为项目可能相同(具有相同的条形码/ ID),但是具有不同的“位置”,因此也可能具有不同的“数量”。

0 个答案:

没有答案