简化复杂的陈述

时间:2018-07-16 19:28:09

标签: sql-server tsql subquery case sql-server-2014

因此下面的查询将返回数字1和39的重复值。是否可以重写此查询,以便在存在30或39的情况下忽略值1。

SELECT       A.UserId,
             U.transformerID , 
             U.productID as ProductID,
             A.Name,
             A.Number,

            (CASE A.Number WHEN 39 THEN 'MEP' WHEN 30 THEN ' Disconnect' 
            ELSE (SELECT (CASE WHEN x.ProductMSP < 0.35  AND datediff(d,x.InsertTime, GETDATE()) < 10 THEN 'MSP' ELSE 'End of Life' END ) as cycle
            FROM Product x where x.ProductSerialNo = u.ProductID and x.UserId = a.UserId) END),
            (select datediff(d,InsertionDateTime, GETDATE()) from Product where productserialno = u.ProductID and userid = A.UserId and datediff(d,InsertTime, GETDATE()) > 60 ),
            (select x.InsertionDateTime from product x where x.productserialno = u.ProductID and x.userid = A.UserId) as InsertDate,
            max(A.DateTime) Date
FROM         [User] U ,
             MMA A,
             product p,
             HealthLinksData h

WHERE A.UserId = U.UserID
AND p.UserId = u.UserID
AND A.Number in (1,30,39)
AND LEN(u.TransformerID ) > 0
AND LEN(u.ProductID) > 0
AND datediff(d,s.InsertTime, GETDATE()) > 20
AND u.ProductID != 7679
AND CONVERT(INT, u.productID) = CONVERT(INT, h.Serial_Batchnumber)
and  SerialNo in (1650,
1918, 1925, 1160, 1919, 1941, 1927, 1195, 1131, 1175, 1985, 1949, 1919, 
1910, 1939, 1212, 1239, 1917, 1919, 1986, 1993, 1926, 1926, 1285, 1268, 
1971,)  
GROUP BY A.UserId, U.TransformerID, u.SensorID, A.Number, A.Name 
ORDER BY max(A.DateTime) desc

1 个答案:

答案 0 :(得分:0)

我刚刚重复了您的查询,第一个查询为30,39,第二个查询为1,它具有明确的检查是否存在的功能,如第一个注释所述(免责声明:未测试)

SELECT       A.UserId,
         U.transformerID , 
         U.productID as ProductID,
         A.Name,
         A.Number,

        (CASE A.Number WHEN 39 THEN 'MEP' WHEN 30 THEN ' Disconnect' 
        ELSE (SELECT (CASE WHEN x.ProductMSP < 0.35  AND datediff(d,x.InsertTime, GETDATE()) < 10 THEN 'MSP' ELSE 'End of Life' END ) as cycle
        FROM Product x where x.ProductSerialNo = u.ProductID and x.UserId = a.UserId) END),
        (select datediff(d,InsertionDateTime, GETDATE()) from Product where productserialno = u.ProductID and userid = A.UserId and datediff(d,InsertTime, GETDATE()) > 60 ),
        (select x.InsertionDateTime from product x where x.productserialno = u.ProductID and x.userid = A.UserId) as InsertDate,
        max(A.DateTime) Date
FROM         [User] U ,
         MMA A,
         product p,
         HealthLinksData h

WHERE A.UserId = U.UserID
AND p.UserId = u.UserID
AND A.Number in (30,39)
AND LEN(u.TransformerID ) > 0
AND LEN(u.ProductID) > 0
AND datediff(d,s.InsertTime, GETDATE()) > 20
AND u.ProductID != 7679
AND CONVERT(INT, u.productID) = CONVERT(INT, h.Serial_Batchnumber)
and  SerialNo in (1650,
1918, 1925, 1160, 1919, 1941, 1927, 1195, 1131, 1175, 1985, 1949, 
1919, 
1910, 1939, 1212, 1239, 1917, 1919, 1986, 1993, 1926, 1926, 1285, 1268, 
1971,)  
GROUP BY A.UserId, U.TransformerID, u.SensorID, A.Number, A.Name 
ORDER BY max(A.DateTime) desc

union

SELECT       A.UserId,
         U.transformerID , 
         U.productID as ProductID,
         A.Name,
         A.Number,

        (CASE A.Number WHEN 39 THEN 'MEP' WHEN 30 THEN ' Disconnect' 
        ELSE (SELECT (CASE WHEN x.ProductMSP < 0.35  AND datediff(d,x.InsertTime, GETDATE()) < 10 THEN 'MSP' ELSE 'End of Life' END ) as cycle
        FROM Product x where x.ProductSerialNo = u.ProductID and x.UserId = a.UserId) END),
        (select datediff(d,InsertionDateTime, GETDATE()) from Product where productserialno = u.ProductID and userid = A.UserId and datediff(d,InsertTime, GETDATE()) > 60 ),
        (select x.InsertionDateTime from product x where x.productserialno = u.ProductID and x.userid = A.UserId) as InsertDate,
        max(A.DateTime) Date
FROM         [User] U ,
         MMA A,
         product p,
         HealthLinksData h

WHERE A.UserId = U.UserID
AND p.UserId = u.UserID
AND A.Number in (1) and not exist(select * from MMA A2 where A2.UserId = A.UserId and A2.Number in (30,39)
AND LEN(u.TransformerID ) > 0
AND LEN(u.ProductID) > 0
AND datediff(d,s.InsertTime, GETDATE()) > 20
AND u.ProductID != 7679
AND CONVERT(INT, u.productID) = CONVERT(INT, h.Serial_Batchnumber)
and  SerialNo in (1650,
1918, 1925, 1160, 1919, 1941, 1927, 1195, 1131, 1175, 1985, 1949, 1919, 
1910, 1939, 1212, 1239, 1917, 1919, 1986, 1993, 1926, 1926, 1285, 1268, 
1971,)  
GROUP BY A.UserId, U.TransformerID, u.SensorID, A.Number, A.Name 
ORDER BY max(A.DateTime) desc
相关问题