有100家快餐连锁店store,每家店有总店发出的货品H2S,也有其他店转给他的货品S2S。
现在需要将100家店所有的H2S和S2S取唯一值,然后按照store列和item货品列来显示出来。通过什么语句呢?
比如
store item
0001 50000001
0001 50000002
0002 50000001
0003 50000001
这样显示。只用select语句,不涉及修改或添加表。
现在需要将100家店所有的H2S和S2S取唯一值,然后按照store列和item货品列来显示出来。通过什么语句呢?
比如
store item
0001 50000001
0001 50000002
0002 50000001
0003 50000001
这样显示。只用select语句,不涉及修改或添加表。
select distinct store, item from t where type='H2S' or type='S2S' order by 1,2;
由于H2S和S2S语句里也有store字段且重复,所以用2楼朋友的算法只能看到一家store
0001 xxx
0002 yyyy
S2S : 对应表B 列 store item
0001 dddd
0003 eeeeeselect * from a union all select * from b
不知道楼主表达的可是这意思
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的循环?还是别的方法呢?谢谢大神指点啊
(
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家分店,怎么统计呢
storecode count(plu)
001 20
002 30
003 40
(select h2scode code,plu from h2s union select s2sincode,plu from s2s)sh where
s.storecode = sh.code(+) group by s.storecode
因为会重复,所以使用minus和union来找出H2S和S2S两表的唯一值
from (store left join test1 on store.storecode=test1.h2scode) left join test2 on store.storecode=test2.h2scode;这3步是可以简化写成1步的,不过我公司里没有SQL,帮不了你做测试.原理基本如此.