如何根据sql中的数量显示重复项? [英] how to display duplicate items based on the quantity in sql?

查看:207
本文介绍了如何根据sql中的数量显示重复项?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个包含字段内容的表格:



item qty

----- -----

茶2



我想显示如下



item qty

---- ------

茶1

茶1



如何创建如上所述的SQL查询?



提前感谢

I have a table with the field content :

item qty
----- -----
tea 2

I want to display as below

item qty
---- ------
tea 1
tea 1

how to create sql query like above ?

thanks in advance

推荐答案

这个问题没有感。项目记录:茶和数量:2 不重复!这只是一个记录,'2'是它的属性,并不表示有两个单独的记录。您无法获得查询输出的内容,而这些内容不在您查询的表中。



-SA
The question makes no sense. The record with item:"tea" and quantity:2 is not duplicate! This is only one record, and '2' is its attribute, not indication of having two separate records. You cannot get on output of a query something which is not in the table you query.

—SA

添加到以前的解决方案。您需要一个逻辑来重复所需的数据量。这意味着您需要使用Transact-SQL来完成这项工作。



也许最简单的方法可能是创建一个循环数据的存储过程(以防万一)你在表中有多行,然后在另一个循环中你将行添加到结果集所需的次数。



这可以在一个标准中完成程序或表值函数(例如使用表格值函数SQL Server [ ^ ])
To add to previous solution. You would need a logic to repeat the data desired amount of times. This means that you need yo use Transact-SQL to do the job.

Perhaps the easiest way could be to create a stored procedure which would loop your data (in case you have multiple rows in the table) and then in another loop you would add the rows into the result set desired amount of times.

This could be done in a standard procedure or in a table valued function (for example Using Table-Valued Functions in SQL Server[^])


对不起,如果我的问题有点混乱..感谢您的回复..

我找到了解决方案< a href =http://sqlfiddle.com/#!3/0049e/2>点击这里
sorry if my question is a bit confusing .. thanks for the response ..
I have found the solution click here


这篇关于如何根据sql中的数量显示重复项?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆