如果SQL查询中的Statement?

时间:2016-02-04 07:29:39

标签: c# sql if-statement

我有一个查询给我多个结果,如下所示:

|DATE|------|DESCRIPTION|---|PDV|----------|ID|-------|NAME|--------|Value|-|Quantity|
2016-01-25    DESAYUNOS     REST            1   DEL PATRON COMBO    162.00    1
2016-01-25    COMIDAS       REST            1   DEL PATRON COMBO    162.00    1
2016-01-25    CENAS         REST            1   DEL PATRON COMBO    162.00    1
2016-01-25    DESAYUNOS     REST            1   DEL PATRON COMBO    162.00    1

我的查询:

SELECT
    [dbo].[CHEQUES].Fecha,  
    [dbo].[TURNOS].Descripcion, 
    [dbo].[CAPMO].Clave_PDV, 
    [dbo].[CAPMO].Pla AS Platillo_Id, 
    [dbo].[CAPMO].Descripcion, 
    [dbo].[CAPMO].Pre AS PrecioPlatillo, 
    [dbo].[CAPMO].Can AS CantidadPlatillo
FROM         
    [dbo].[CAPMO] 
INNER JOIN 
    [dbo].[CHEQUES] ON [dbo].[CAPMO].Clave_PDV = [dbo].[CHEQUES].Cla_PDV 
                    AND [dbo].[CAPMO].Che = [dbo].[CHEQUES].Che 
INNER JOIN 
    [dbo].[PLATILLOS] ON [dbo].[CAPMO].Pla = [dbo].PLATILLOS].Pla 
INNER JOIN 
    [dbo].[TURNOS] ON [dbo].[CAPMO].Clave_PDV = [dbo].[TURNOS].Clave_PDV
WHERE     
    ([dbo].[CHEQUES].St = 'P') AND ([dbo].[CAPMO].Stpl = 'A') 

第1行和第4行完全相同,是否有可以检测相同行的查询增加| Quantity |每列等于1? 喜欢和如果statemnt有一段时间我不知道这是否在SQL中是可行的

我正在寻找这样的输出:

|DATE|------|DESCRIPTION|---|PDV|----------|ID|-------|NAME|--------|Value|-|Quantity|

2016-01-25    COMIDAS       REST            1   DEL PATRON COMBO    162.00    1
2016-01-25    CENAS         REST            1   DEL PATRON COMBO    162.00    1
2016-01-25    DESAYUNOS     REST            1   DEL PATRON COMBO    162.00    2

2 个答案:

答案 0 :(得分:3)

SQL-Server 中,您可以在以下内容中使用ROW_NUMBER

<强> QUERY

select [date], [description], pdv, id, name, value, 
       case when rn > 1 then quantity+rn-1 else quantity end as quantity
from( 
    select *, 
           row_number() over(partition by [date], [description], pdv, id, name, value, quantity order by [date]) rn 
    from #t
)x

示例数据

create table #t
(
    [date] date,
    [description] nvarchar(60),
    pdv nvarchar(60),
    id int,
    name nvarchar(60),
    value nvarchar(60),
    quantity int
)
insert into #t values
 ('2016-01-25','DESAYUNOS','REST',1,'DEL PATRON COMBO','162.00',1)
,('2016-01-25','COMIDAS'  ,'REST',1,'DEL PATRON COMBO','162.00',1)
,('2016-01-25','CENAS'    ,'REST',1,'DEL PATRON COMBO','162.00',1)
,('2016-01-25','DESAYUNOS','REST',1,'DEL PATRON COMBO','162.00',1)
,('2016-01-25','DESAYUNOS','REST',1,'DEL PATRON COMBO','162.00',1)
,('2016-01-25','DESAYUNOS','REST',1,'DEL PATRON COMBO','162.00',1)

<强>输出

date        description pdv     id  name                value   quantity
2016-01-25  CENAS       REST    1   DEL PATRON COMBO    162.00  1
2016-01-25  COMIDAS     REST    1   DEL PATRON COMBO    162.00  1
2016-01-25  DESAYUNOS   REST    1   DEL PATRON COMBO    162.00  1
2016-01-25  DESAYUNOS   REST    1   DEL PATRON COMBO    162.00  2
2016-01-25  DESAYUNOS   REST    1   DEL PATRON COMBO    162.00  3
2016-01-25  DESAYUNOS   REST    1   DEL PATRON COMBO    162.00  4

看一下这部分quantity+rn-1即使你的副本多于2,它也会将每个重复的行增加1。

如果您只想增加1,而不是取决于使用quantity-1代替quantity+rn-1的重复项数量。

<强>更新

如果您希望只获得最高数量的重复结果,可以使用以下MAXGROUP BY子句:

<强> QUERY

select [date], [description], pdv, id, name, value, 
       max(case when rn > 1 then quantity +rn-1 else quantity end) as quantity
from( 
    select *, 
           row_number() over(partition by [date], [description], pdv, id, name, value, quantity order by [date]) rn 
    from #t
)x
group by [date], [description], pdv, id, name, value

<强> OUPUT

date        description pdv     id  name                value   quantity
2016-01-25  CENAS       REST    1   DEL PATRON COMBO    162.00  1
2016-01-25  COMIDAS     REST    1   DEL PATRON COMBO    162.00  1
2016-01-25  DESAYUNOS   REST    1   DEL PATRON COMBO    162.00  4

答案 1 :(得分:0)

对我来说,我将使用 GROUP BY ,请看下面这个例子

 SELECT  |DATE|
        ,|DESCRIPTION|
        ,|PDV|
        ,|ID|
        ,|NAME|
        ,|Value|
        ,SUM(|Quantity|)
FROM @YourTable
GROUP BY  |DATE|
         ,|DESCRIPTION|
         ,|PDV|
         ,|ID|
         ,|NAME|
         ,|Value|