简体   繁体   中英

SQL-EX.ru query problemset #28

I'm trying to write an SQL query to solve a question at www.sql-ex.ru (Q. 28), I got the correct result, but I got this error "Your query failed on third checking database."

SQL Query Question:

To within two decimal digits, define the average quantity of paint per square.

SQL Notes:

Database schema consists of 3 tables:

utQ (Q_ID int, Q_NAME varchar(35)), utV (V_ID int, V_NAME varchar(35), V_COLOR char(1)),

utB (B_Q_ID int, B_V_ID int, B_VOL tinyint, B_DATETIME datetime). The table utQ includes square identifier, square name. Note that non-painted square is black.

utV includes balloon identifier, balloon name, and paint color. The table utB shows information on painting square with paint balloon and includes the square identifier, the balloon identifier, the paint quantity, and time of painting.

  • It should be noted that balloon may be of one from three colors: red (V_COLOR='R'), green (V_COLOR='G'), or blue (V_COLOR='B');
  • any balloon is originally full with volume of 255;
  • square color is defined in accordance with RGB rule, ie R=0, G=0, B=0 is black, whereas R=255, G=255, B=255 is white;
  • any record in the table utB decreases paint quantity in the balloon by B_VOL and increase paint quantity in the square by the same value;
  • B_VOL must be more than 0 and less or equal 255;
  • paint quantity of the same color in one square may not be over 255;
  • paint quantity in a balloon may not be less than 0;
  • time of painting (B_DATETIME) is given to within a second, ie it does not contain milliseconds.

Scheme

方案

My Code

WITH x AS (SELECT b_q_id, b_v_id, ball.v_color, b_vol as vol FROM utb
JOIN utv ball
ON utb.b_v_id = ball.v_id)
, y AS (
SELECT b_q_id, v_color, totalcolor = CASE WHEN SUM(vol) > 255 THEN 255
ELSE SUM(vol) END FROM x
GROUP BY b_q_id, v_color),
z AS(
SELECT b_v_id, totalbcolor = 
CASE WHEN SUM(b_vol) > 255 THEN 255
ELSE SUM(b_vol)
END
FROM utb
GROUP BY b_v_id)
, a AS
(SELECT b_q_id, SUM(totalcolor) totalacolor FROM y GROUP BY b_q_id)
, b AS 
(SELECT b_q_id,b_v_id, totalacolor FROM a, z)
, c AS
(SELECT DISTINCT b.b_q_id, totalacolor FROM b
INNER JOIN utb
ON (b.b_q_id = utb.b_q_id AND b.b_v_id = utb.b_v_id))

SELECT 
CAST(CAST(SUM(totalacolor) AS NUMERIC(8,2)) 
/ (SELECT COUNT(*) FROM utq)AS NUMERIC(8,2))  
 FROM c

Correct Answer

386.25

Comments My code is probably overly long. But, there is probably a shorter way of doing it. But, I still don't get where I'm wrong. Please help.

here your code)

SELECT  CONVERT(NUMERIC(15,2),SUM(COALESCE(UB.B_VOL,0)) / CONVERT(FLOAT,COUNT(DISTINCT UQ.Q_ID)))
FROM    utQ AS UQ
        LEFT JOIN utB AS UB ON UQ.Q_ID = UB.B_Q_ID
        LEFT JOIN utV AS UV ON UB.B_V_ID = UV.V_ID

test below

在此输入图像描述

if you want go alongside help topics you could use this query:

SELECT cast(SUM(case when UB.B_VOL IS NULL then 0 else UB.B_VOL end) /cast(COUNT(DISTINCT UQ.Q_ID) as float) as decimal(8,2))FROM utQ AS UQ
    LEFT JOIN utB AS UB ON UQ.Q_ID = UB.B_Q_ID

Here is another solution, using CASE WHEN ... THEN ... ELSE ... END :

SELECT CONVERT(NUMERIC(15,2),
(
SELECT SUM(total_paint) * 1.0 / COUNT(Q_ID) AS avg_paint 
FROM
(
SELECT DISTINCT utQ.Q_ID, 
CASE
WHEN SUM(B_VOL) IS NULL
THEN 0
ELSE SUM(B_VOL)
END
AS total_paint
FROM utQ
LEFT JOIN utB ON utB.B_Q_ID = utQ.Q_ID
GROUP BY utQ.Q_ID
) AS tmp
)
)

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