有100家快餐连锁店store,每家店有总店发出的货品H2S,也有其他店转给他的货品S2S。
现在需要将100家店所有的H2S和S2S取唯一值,然后按照store列和item货品列来显示出来。通过什么语句呢?
比如
store         item
0001          50000001
0001          50000002
0002          50000001
0003          50000001
 
这样显示。只用select语句,不涉及修改或添加表。

解决方案 »

  1.   

    select distinct store,item from youtablename
      

  2.   

    不知道你的字段名具体是什么,我先随便用个。
    select distinct store, item from t where type='H2S' or type='S2S' order by 1,2;
      

  3.   

    忘记说了。之前的H2S和S2S都是查询语句,用减集和并集连起来的查询语句。那么如何嵌套呢?直接放进去?
    由于H2S和S2S语句里也有store字段且重复,所以用2楼朋友的算法只能看到一家store
      

  4.   

    H2S : 对应表A    列 store item 
                        0001   xxx
                        0002   yyyy
    S2S : 对应表B    列 store item 
                         0001 dddd
                         0003 eeeeeselect * from a union all select * from b 
    不知道楼主表达的可是这意思
      

  5.   

    select distinct store from !@#$!@#$
      

  6.   

    (
    select column_item from table_h2s where column_store='1001'
    union 
    (select column_item from table_h2s where column_store='1001'
    MINUS
    select column_item from table_S2S where column_store='1001')
    )--求出1001店中的唯一货号现在有100家店,每家店的货号都不同,所以不能在上面column_store里加between and,但是我又想知道每家店有多少货号count(coulmn_item),请问如何实现啊。用嵌套?还是PLSQL的循环?还是别的方法呢?谢谢大神指点啊
      

  7.   

    where column_store='1001' 不能用BEWTEEM and ,因为每家店的item不一样。
      

  8.   

    CREATE TABLE H2S VALUES 
    (
    h2scode varchar2(10),--收货方
    h2sno varchar2(10),--收货单据号
    plu varchar2(10)--收货物号
    )CREATE TABLE S2S VALUES 
    (
    s2sincode varchar2(10),--转入方
    s2soutcode varchar2(10),--转出方
    s2sinno varchar2(10),--转入单据号
    plu varchar2(10)--转入货号
    )CREATE TABLE store VALUES 
    (
    storename varchar2(10),--分店名
    storecode varchar2(10),--分店号
    )select storecode,count(plu) from store,
    (select DISTINCT plu
    from H2S where h2scode ='&分店号码'
    union( select DISTINCT plu
    from s2s where s2sincode ='&分店号码' minus 
    select DISTINCT plu
    from H2S where h2scode ='&分店号码'))--H2S表和S2S表取plu的唯一值,并进行统计数目,以上只能取一家分店。现有100家分店,怎么统计呢
      

  9.   

    是不是和loop有关,分店从1开始递增,到100结束,然后每次循环都运行一次select storecode,count(plu),统计该分店的数目,最后的结果应该是
    storecode   count(plu)
    001         20
    002         30
    003         40
      

  10.   

    总部的H2S和转货的S2S的PLU会重复不?优先看总部H2S里的PLU值?
      

  11.   

    select s.storecode,count(sh.plu) from store s,
    (select h2scode code,plu from h2s union select s2sincode,plu from s2s)sh where
    s.storecode = sh.code(+) group by s.storecode
      

  12.   


    因为会重复,所以使用minus和union来找出H2S和S2S两表的唯一值
      

  13.   

    select DISTINCT plu,h2scode,concat(plu,h2scode) as sign1 from H2S as test1;select DISTINCT plu,s2scode,concat(plu,s2scode) as sign2 from S2S where sign2 not in (select sign1 from test1);select storecode,case when test1.plu is not null then test1.plu when test2.plu is not null then test2.plu else null end as plu_ttl 
    from (store left join test1 on store.storecode=test1.h2scode) left join test2 on store.storecode=test2.h2scode;这3步是可以简化写成1步的,不过我公司里没有SQL,帮不了你做测试.原理基本如此.