简体   繁体   中英

Count string values From row using sql?

I have a SQL query in which I am using INNER JOIN but I have to calculate the string values ERROR , OK and their total from column d.sd_figure .

This is my SQL query

    SELECT 
        count(d.sd_figure = 'ERROR') AS error,
        count(d.sd_figure = 'OK') AS OK count,
        count(*) AS total,
        m.contr_num, bu.buyer_name, 
        m.S_remarks, cr.contr_name, br.brand_name,
        m.S_date, c.colour_name, s.size_name,
        mn.manu_name
    FROM 
        Scanning_M m
    INNER JOIN 
        colour c ON m.color_id = c.colour_id
    INNER JOIN  
        Buyer bu ON m.buyer_id = bu.Byer_ID
    INNER JOIN 
        Scanning_D d ON m.S_id = d.S_id
    INNER JOIN 
        Brand br ON m.Brand_id = br.Brand_id
    INNER JOIN 
        Contract cr ON m.Contr_num = cr.Contr_id
    INNER JOIN 
        Size s ON m.Size_id = s.Size_id
    INNER JOIN 
        Manufacturer mn ON m.Manu_id = mn.manu_id
    WHERE 
        m.S_date BETWEEN '2016-01-13' AND '2016-01-13'

I also want to add count of 'error' , 'OK' and 'Total' too error is in this line

SELECT  
    count(d.sd_figure = 'ERROR') AS error, 
    count(d.sd_figure = 'OK') as OK count,
    count(*) as total ,

I tried many solution butt returned group by not found etc

SAMPLE DATA DEMANDED FOR d.sd_figure

   S_id sd_res  sd_figure   sd_datetime           sd_id
   4    456456  ERROR      2016-01-09 03:11:07.000  1
   4    456456  ERROR       2016-01-09 03:11:07.000 2
   4    456456  ERROR       2016-01-09 03:11:07.000 3

   6    123     ERROR      2016-01-09 10:54:47.000  22
   6    123     ERROR      2016-01-09 10:54:47.000  23
   6    123     ERROR      2016-01-09 10:54:47.000  24
   6    123     ERROR      2016-01-09 10:54:48.000  25
   6    123     ERROR      2016-01-09 10:54:48.000  26

Untested, but should be something like this:

SELECT sum(CASE WHEN d.sd_figure = 'ERROR' THEN 1 ELSE 0 END) AS error
    ,sum(CASE WHEN d.sd_figure = 'OK' THEN 1 ELSE 0 END) AS [OK count]
    ,count(*) AS total
    ,m.contr_num
    ,bu.buyer_name
    ,m.S_remarks
    ,cr.contr_name
    ,br.brand_name
    ,m.S_date
    ,c.colour_name
    ,s.size_name
    ,mn.manu_name
FROM Scanning_M m
INNER JOIN colour c ON m.color_id = c.colour_id
INNER JOIN Buyer bu ON m.buyer_id = bu.Byer_ID
INNER JOIN Scanning_D d ON m.S_id = d.S_id
INNER JOIN Brand br ON m.Brand_id = br.Brand_id
INNER JOIN Contract cr ON m.Contr_num = cr.Contr_id
INNER JOIN Size s ON m.Size_id = s.Size_id
INNER JOIN Manufacturer mn ON m.Manu_id = mn.manu_id
WHERE m.S_date BETWEEN '2016-01-13' AND '2016-01-13'
GROUP BY m.contr_num
    ,bu.buyer_name
    ,m.S_remarks
    ,cr.contr_name
    ,br.brand_name
    ,m.S_date
    ,c.colour_name
    ,s.size_name
    ,mn.manu_name

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM