表t1字段:
date             name          count         id
2007年3月6日     胡豆            20          0001
2007年3月6日     豌豆            5           0002
2007年3月6日     黄豆            8           0003
表t2字段:
name     id     stock_count   condition
胡豆    0001     10              20071
胡豆    0001     2               20072
胡豆    0001     5               20073
黄豆    0003     10              20025
如上面两表。
现在要得到以下结果:date             name          count         id      condition
2007年3月6日     胡豆            10          0001     20071
2007年3月6日     胡豆            2           0001     20072
2007年3月6日     胡豆            5           0001     20073
2007年3月6日     黄豆            10          0003     20025如何实现?求这个的sql语句~

解决方案 »

  1.   

    Select 
    A.[date],
    A.name,
    B.stock_count As [count],
    A.id,
    B.[condition]
    From
    t1 A
    Inner Join
    t2 B
    On A.name = B.name And A.id = B.id
      

  2.   

    不好意思,我把结果弄错了。
    应该得到的正确结果是如下:
    date             name          count         id      condition
    2007年3月6日     胡豆            10          0001     20071
    2007年3月6日     胡豆            2           0001     20072
    2007年3月6日     胡豆            5           0001     20073
    2007年3月6日     黄豆            8           0003     20025
    求,该sql~~~~
      

  3.   

    為什麼最後一個count是8?上面三條取的是t2中的stock_count,最後一條卻取的是t1的count?
      

  4.   

    select t1.name,
    [date]=t1.[date],
    id ,
    [count]=stock_count,
    condition,
    from t1 join t2 on t1.id=t2.id
      

  5.   


    select a.name,
    [date]=a.[date],
    id ,
    [count]=cast when (select count(1) from tb where id=b.id)>1 
            then b.stock_count else a.[count] end,
    b.condition,
    from t1 as a join t2 as b on t1.id=t2.id
      

  6.   

    select a.name,
    [date]=a.[date],
    id ,
    [count]=cast when (select count(1) from t2 where id=b.id)>1 --改一下
            then b.stock_count else a.[count] end,
    b.condition,
    from t1 as a join t2 as b on t1.id=t2.id
      

  7.   

    先有了t1表,然后跟t2表进行比较。
    若名字对,数量够就直接列出来;
    若名字对,数量在条件(condition)1的情况下,不够,就再取条件(conditiong)2的数量,列出来;
    若名字对,数量还不够,就以此类推,直到没有这个名字和数量的时候。上面,“為什麼最後一個count是8?”
    是因为,“黄豆”在t2表中够了,直接取8个就行了。而不需要10个。
      

  8.   

    我把题完整的再写一下,请大家帮我看看啊,该如何写sql
    表t1字段:
    date             name          count         id
    2007年3月6日     胡豆            20          0001
    2007年3月6日     豌豆            5           0002
    2007年3月6日     黄豆            8           0003
    表t2字段:
    name     id     stock_count   condition
    胡豆    0001     10              20071
    胡豆    0001     2               20072
    胡豆    0001     5               20073
    黄豆    0003     10              20025
    如上面两表。
    现在要得到以下结果:date             name          count         id      condition
    2007年3月6日     胡豆            10          0001     20071
    2007年3月6日     胡豆            2           0001     20072
    2007年3月6日     胡豆            5           0001     20073
    2007年3月6日     黄豆            8           0003     20025
    求,该sql~~~~
      

  9.   

    select a.*,b.* from b left join a on a.id=b.id
      

  10.   

    如果t2中數據是這樣的,結果是怎樣的?name     id     stock_count   condition
    胡豆    0001     10              20071
    胡豆    0001     2               20072
    胡豆    0001     5               20073
    胡豆    0001     5               20074
    胡豆    0001     5               20074
    黄豆    0003     10              20025
      

  11.   

    mugua604(熟不了的木瓜) ( ) 信誉:100    Blog  2007-03-23 16:22:22  得分: 0  
     
     
       简单的关联,分清主从表就可以了!
      
     
    --------------你沒看清題意,再看看下面的回復,這個題目不是那麼簡單的。
      

  12.   

    如果t2中數據是這樣的,結果是怎樣的?name     id     stock_count   condition
    胡豆    0001     10              20071
    胡豆    0001     2               20072
    胡豆    0001     5               20073
    胡豆    0001     5               20074
    胡豆    0001     5               20074
    黄豆    0003     10              20025
    结果是:
    date             name          count         id      condition
    2007年3月6日     胡豆            10          0001     20071
    2007年3月6日     胡豆            2           0001     20072
    2007年3月6日     胡豆            5           0001     20073
    2007年3月6日     胡豆            3           0001     20074
    2007年3月6日     黄豆            8           0003     20025
      

  13.   

    请大家帮我啊~~
    我把题完整的再写一下,请大家帮我看看啊,该如何写sql
    表t1字段:
    date             name          count         id
    2007年3月6日     胡豆            20          0001
    2007年3月6日     豌豆            5           0002
    2007年3月6日     黄豆            8           0003
    表t2字段:
    name     id     stock_count   condition
    胡豆    0001     10              20071
    胡豆    0001     2               20072
    胡豆    0001     5               20073
    黄豆    0003     10              20025
    如上面两表。
    现在要得到以下结果:date             name          count         id      condition
    2007年3月6日     胡豆            10          0001     20071
    2007年3月6日     胡豆            2           0001     20072
    2007年3月6日     胡豆            5           0001     20073
    2007年3月6日     黄豆            8           0003     20025
    求,该sql~~~~
      

  14.   

    Create Table t1
    ([date] Nvarchar(20),
      name Nvarchar(10),
      [count] Int,
      id Varchar(10))
    Insert t1 Select N'2007年3月6日',     N'胡豆',            20,          '0001'
    Union All Select N'2007年3月6日',     N'豌豆',            5,           '0002'
    Union All Select N'2007年3月6日',     N'黄豆',            8,           '0003'
    Create Table t2
    (name Nvarchar(10),
     id Varchar(10),
     [stock_count] Int,
     condition Varchar(10))
    Insert t2 Select N'胡豆',    '0001',     10,              '20071'
    Union All Select N'胡豆',    '0001',     2,               '20072'
    Union All Select N'胡豆',    '0001',     5,               '20073'
    Union All Select N'胡豆',    '0001',     5,               '20074'
    Union All Select N'胡豆',    '0001',     5,               '20075'
    Union All Select N'黄豆',    '0003',     10,              '20025'
    GO
    Select 
    A.[date],
    A.name,
    (Case When C.stock_count + C.SUMstock_count > A.[count] Then A.[count] - C.SUMstock_count Else C.stock_count End) As [count],
    A.id,
    C.[condition]
    From
    t1 A
    Inner Join
    (Select B.*, IsNull((Select SUM(stock_count) From t2 Where name = B.name And id= B.id And condition < B.condition), 0) As SUMstock_count From t2 B) C
    On A.name = C.name And A.id = C.id And A.[count] > C.SUMstock_count
    Go
    Drop Table t1, t2
    --Result
    /*
    date name count id condition
    2007年3月6日 胡豆 10 0001 20071
    2007年3月6日 胡豆 2 0001 20072
    2007年3月6日 胡豆 5 0001 20073
    2007年3月6日 胡豆 3 0001 20074
    2007年3月6日 黄豆 8 0003 20025
    */
      

  15.   

    create table tt(date varchar(20), name varchar(10),[count] int,id varchar(10))
    insert into tt select '2007年3月6日',     '胡豆',            '20',          '0001'
    union all select '2007年3月6日',     '豌豆',            5,           '0002'
    union all select '2007年3月6日',     '黄豆',            8,           '0003'
    create table tb(name  varchar(10),id  varchar(10),stock_count int,condition varchar(10))
    insert into tb select '胡豆',    '0001', 10,              '20071'
    union all select '胡豆',    '0001',     2,               '20072'
    union all select '胡豆',    '0001',     5,               '20073'
    union all select '胡豆',    '0001',     5,               '20074'
    union all select '胡豆',    '0001',     5,               '20075'
    union all select '黄豆',    '0003',     10,              '20025'
    select date,d.name,(case when [sum]<=[count] then [count]-[sum] else [count] end)[count],
    d.id,condition
    from (select *,
    (select sum(stock_count)as'stock_count' 
    from tb b 
    where b.id=a.id and b.condition<=a.condition)[sum]
    from tb a)d left join tt c
    on c.id=d.id
    where [sum]<[count] or [sum]=stock_count
      

  16.   

    感谢大家,特别感谢paoluo(一天到晚游泳的鱼) ( ) ,揭贴给分