SQL PHP如果第二列没有值,则将一列设置为零

时间:2015-11-17 23:23:09

标签: php sql sql-server web match

我使用PHP,2列,Amt和Period

从Sql获取数据

通常数量是干净的,这意味着amt和period都可用。期间为1-12,amt可用于这些列

Amount  Period 
100 P1
125 P1
150 P2
160 P3
120 P4
130 P5
160 P6
170 P7
180 P8
145 P9
144 P10
155 P11
160 P12

我正在运行一个查询,根据日期等从两个不同的表中获取amt字段和句点字段,如下所示

SELECT Sum(CONVERT(FLOAT, amt))                  AS sum_amt, 
       CONVERT(FLOAT, Substring(f.period, 2, 4)) AS period 
FROM   bookings b, 
       fiscalcal f 
WHERE  b.[invoice date] >= f.start 
       AND b.[invoice date] <= f.[end] 
       AND f.[year] = '2015' 
GROUP  BY CONVERT(FLOAT, Substring(f.period, 2, 4)) 

获取正确的所需数据

我处于这样的情况,其中一些时段丢失,如P4,P5缺失,所以没有时段P4,P5的行,但在网页上,我应该在时段P4的情况下显示零金额,P5。我无法做到这一点。

所以看起来应该是

amount period
225 P1
150 P2
160 P3
 0  P4
 0  P5
160 P6
170 P7
180 P8
145 P9
144 P10
155 P11
165 P12

请帮助如何在PHP和SQL上执行此操作。我非常感谢

2 个答案:

答案 0 :(得分:0)

CREATE TABLE YourTable
    ([Amount] int, [Period] varchar(3));

INSERT INTO YourTable
    ([Amount], [Period])
VALUES
    (100, 'P1'),        (125, 'P1'),
    (150, 'P2'),
    (160, 'P3'),        (120, 'P3'),
    (130, 'P6'),        (160, 'P6'),
    (170, 'P7'),
    (180, 'P8'),
    (145, 'P9'),
    (144, 'P10'),
    (155, 'P11'),
    (160, 'P12');

<强> SqlFiddleDemo

WITH periods (period_id) AS (
    SELECT 'P1' UNION ALL
    SELECT 'P2' UNION ALL
    SELECT 'P3' UNION ALL
    SELECT 'P4' UNION ALL
    SELECT 'P5' UNION ALL
    SELECT 'P6' UNION ALL
    SELECT 'P7' UNION ALL
    SELECT 'P8' UNION ALL
    SELECT 'P9' UNION ALL
    SELECT 'P10' UNION ALL
    SELECT 'P11' UNION ALL
    SELECT 'P12'
)
SELECT SUM(COALESCE(Amount, 0)), p.period_id
FROM periods p
LEFT JOIN YourTable y
       ON p.period_id = y.Period 
GROUP BY p.period_id

<强>输出

|     | period_id |
|-----|-----------|
| 225 |        P1 |
| 150 |        P2 |
| 280 |        P3 |
|   0 |        P4 |
|   0 |        P5 |
| 290 |        P6 |
| 170 |        P7 |
| 180 |        P8 |
| 145 |        P9 |
| 144 |       P10 |
| 155 |       P11 |
| 160 |       P12 |

修改

编辑完成后,您可以将查询放入另一个cte。

WITH periods (period_id) AS (
        SELECT 'P1' UNION ALL
        SELECT 'P2' UNION ALL
        SELECT 'P3' UNION ALL
        SELECT 'P4' UNION ALL
        SELECT 'P5' UNION ALL
        SELECT 'P6' UNION ALL
        SELECT 'P7' UNION ALL
        SELECT 'P8' UNION ALL
        SELECT 'P9' UNION ALL
        SELECT 'P10' UNION ALL
        SELECT 'P11' UNION ALL
        SELECT 'P12'
    ),
YourTable (Amount, period) AS (
    SELECT Sum(CONVERT(FLOAT, amt))                  AS sum_amt, 
           CONVERT(FLOAT, Substring(f.period, 2, 4)) AS period 
    FROM   bookings b, 
           fiscalcal f 
    WHERE  b.[invoice date] >= f.start 
           AND b.[invoice date] <= f.[end] 
           AND f.[year] = '2015' 
    GROUP  BY CONVERT(FLOAT, Substring(f.period, 2, 4)) 

)
    SELECT SUM(COALESCE(Amount, 0)), p.period_id
    FROM periods p
    LEFT JOIN YourTable y
           ON p.period_id = y.Period 
    GROUP BY p.period_id

答案 1 :(得分:0)

带有字段调用期的有效期间表(包括P01 - P12)。

SELECT periods.period, sum(convert(float,AMT)) as sum_amt2
from Periods
left join Sales on periods.period = substring(sales.period,2,4)
group by periods.period