-1

我要创建一个查询,用于在我们的项目中打印标签,但发现它很困难,因为标签数量的计数是基于字符串的。我做了一个如下所示的查询:

SELECT 
  wipdatavalue
  , containername
  , l
  , q as qtybox
  , d
  , qtyperbox AS q
  , productname
  , dt
  , dsn
  , CASE
      WHEN instr(wipdatavalue, '-') = 0
        THEN
          to_number(wipdatavalue)
      ELSE
          to_number(substr(wipdatavalue, 1, instr(wipdatavalue, '-') - 1))
    END AS una
  , CASE
      WHEN instr(wipdatavalue, '-') = 0 
        THEN
          to_number(wipdatavalue)
      ELSE
        to_number(substr(wipdatavalue, instr(wipdatavalue, '-') + 1))
    END AS dulo  
  , ROW_NUMBER() OVER (ORDER BY containername) AS n
  , count(*) over() m
FROM trprinting_ls
WHERE containername = 'TRALTESTU0A'
GROUP BY wipdatavalue, containername, l, q, d, qtyperbox, productname, dt, dsn
ORDER BY wipdatavalue

上面的查询将导致以下结果:

在此处输入图像描述

但实际上,我不仅要显示第一个项目(Wipdatavalue 1-4),还要显示四次才能看起来像这样:

在此处输入图像描述

我尝试了另一个运行良好的查询,但是当我尝试在我们的项目中加载它时,它不会打印标签。我们发现这是因为 WITH 语句,我们不知道为什么。查询是:

WITH DATA (WIPDATAVALUE, CONTAINERNAME, L, Q, D, QTYBOX, PRODUCTNAME, DT, una, dulo, m1, n)
     AS (SELECT WIPDATAVALUE, CONTAINERNAME, L, Q, D, QTYBOX, PRODUCTNAME, DT, una, dulo,(dulo - una) + 1 AS m1,una n
           FROM (SELECT WIPDATAVALUE, CONTAINERNAME, L, Q, D, QTYPERBOX AS QTYBOX, PRODUCTNAME, DT,
                        CASE
                          WHEN instr(wipdatavalue, '-') = 0
                          THEN
                            to_number(wipdatavalue)
                          ELSE
                            to_number(substr(wipdatavalue, 1, instr(wipdatavalue, '-') - 1))
                          END AS una,
                          CASE
                            WHEN instr(wipdatavalue, '-') = 0 
                            THEN
                              to_number(wipdatavalue)
                            ELSE
                              to_number(substr(wipdatavalue, instr(wipdatavalue, '-') + 1))
                          END AS dulo
                   FROM trprinting_ls
                  WHERE containername = 'TRALTESTU0A'
                  )
         UNION ALL
         SELECT WIPDATAVALUE, CONTAINERNAME, L, Q, D, QTYBOX, PRODUCTNAME, DT, una, dulo, m1, n + 1
           FROM DATA
          WHERE n + 1 <= dulo)
SELECT WIPDATAVALUE, CONTAINERNAME, L, Q, D, QTYBOX, PRODUCTNAME, DT, una, dulo, n,
       count(*) OVER () m
FROM DATA
ORDER BY n, wipdatavalue

谢谢大家帮忙。

4

2 回答 2

0

试试这个查询

select column_name, count(column_name)
from table
group by column_name
having count (column_name) > 1;
于 2013-03-14T08:41:15.277 回答
0

试试这个

select * 
from your_data
start with instr(Wipdatavalue, '1') > 0
connect by level between regexp_substr(Wipdatavalue, '^\d+') 
and regexp_substr(Wipdatavalue, '\d+$')

这是一个简化的例子

如果您愿意,可以用 substr 和 instr 替换 regexp_substr(也可能更快)

这是一个 sqlfiddle 演示

于 2013-03-14T09:23:48.193 回答