English 中文(简体)
• 如何在服务器库内使用一台库房的情况下制作一张表
原标题:How to Populate a Table without Using a Cursor in SQL Server 2008 R2

我需要确定数字先后的失踪产品。 换言之,我们每个商店都有一套独一无二的产品出售。 终端用户希望得到一份报告,其中将查明在日期范围内缺失的植被。

Store   Product Number
Store 1  Widget   100
Store 1  Widget   101
Store 1  Widget   102
Store 1  Widget   104
Store 2  Widget   201
Store 2  Widget   202
Store 2  Widget   203

我认为,这样做的最佳途径是获得“市值”和MIN(Number)”,然后按顺序列出所有数字的表变量。 如果表变量中的植被数量在产品表中确实存在,我将把数字作为缺失的标识。

然而,我们的德国统计局对库勒人非常不利,我知道,WHILE休息室有很多间接费用。 除此以外,我不相信如何以储存方式做到这一点。

是否有任何人知道如何以既定方式做到这一点?

植被本身有数量,因此,当你出售植被第1号时,出售的下一个植被应该为2号植被。 然而,有时,这种情况发生并出售植被第3号——没有植被,也没有数据库。 我需要在一份报告中确定,第1和第3号植被被出售,第2号没有。

最佳回答

www.un.org/Depts/DGACM/index_spanish.htm 最新答复

下表和工作解决方案的样本如下。 基本内容是: 检查——看看是否有比目前数字高的数字,没有比目前的数字高出一个。

DECLARE @t Table (Store int, Product varchar(100), number int)
INSERT INTO @t
VALUES
(1,  Widget ,   100),
(1,  Widget ,   101),
(1,  Widget ,   102),
(1,  Widget ,   104),
(2,  Widget ,   201),
(2,  Widget ,   202),
(2,  Widget ,   203)

SELECT Store, Product, t.Number+1 as Missing
FROM @t t
WHERE EXISTS (SELECT 1 FROM @t t2
              WHERE t2.Store = t.Store
              AND t2.product = t.product
              AND t2.number > t.number)
AND Not Exists (SELECT 1 FROM @t t3
                WHERE t3.Store = t.store
                AND t3.product = t.product
                AND t3.number = t.number + 1)
问题回答

Create a numbers table and populate it with more numbers than you need. Then you can use a set-based query to get the results

Create table #temp (product varchar(15), id int)
insert into #temp
values ( test , 1), ( test , 3),( test , 4),( test2 , 6),( test2 , 2),( test2 , 10),( test3 , 10),( test3 , 9),( test3 ,7),( test4 , 1),( test4 , 2),( test4 , 3)

create table #product (product varchar (15))
insert into #product 
values ( test ),( test2 ),( test3 ),( test4 ), ( test5 )
create table #num (number int)

insert into #num
values (1),(2),(3),(4),(5),(6),(7),(8),(9),(10),(11),(12),(13),(14),(15)

select n.number,p.product from #num n
cross join #product p 
left join (
select product, max(id)as maxid from #temp group by product)a
on a.product = p.product 
left join #temp t on n.number = t.id and t.product = p.product
where  n.number <=a.maxid and t.id is null
order by p.product




相关问题
Performance impact of indexed view in MS SQL Server 2008

Does anyone have experience with using indexed view in MS SQL Server 2008? I am trying to find out how does indexed view affect performance of insert / update statements, that are adding / updating ...

Lock Escalation - What s happening here?

While altering a table (removing a column) in SQL Server 2008, I clicked the Generate Change Script button and I noticed that the change script it generated drops the column, says "go" and then runs ...

Round to nearest 5 in SQL Server

I have a Money column in my SQL Server 2008 table. In my below query how can I round it to nearest 5$ select FineAmount from tickets Thanks

热门标签