SELECT md_readings dt as dt min md_readings number_of_inputs as number

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
[
"
SELECT
md_readings.dt as dt,
min(md_readings.number_of_inputs) as number_of_input,
max(md_readings.number_of_inputs) as max_number_of_input,
SUM(COALESCE( md_readings.consumption, md_readings.indication, 0 )) as sum
,
SUM(
CASE
WHEN voc_md_volumes.type = 'M'
AND md_readings.target ILIKE '%подающий%'
THEN COALESCE( md_readings.consumption, md_readings.indication, 0 )
END
) AS m1
, SUM(
CASE
WHEN voc_md_volumes.type = 'M'
AND md_readings.target ILIKE '%обратный%'
THEN COALESCE( md_readings.consumption, md_readings.indication, 0 )
END
) AS m2
,
SUM(
CASE
WHEN voc_md_volumes.type = 'M'
AND md_readings.target ILIKE '%подающий%'
THEN COALESCE( md_readings.consumption, md_readings.indication, 0 )
END
) - SUM(
CASE
WHEN voc_md_volumes.type = 'M'
AND md_readings.target ILIKE '%обратный%'
THEN COALESCE( md_readings.consumption, md_readings.indication, 0 )
END
) AS m1_m2
,
SUM(
CASE
WHEN voc_md_volumes.type = 'V'
AND md_readings.target ILIKE '%подающий%'
THEN COALESCE( md_readings.consumption, md_readings.indication, 0 )
END
) AS v1
, SUM(
CASE
WHEN voc_md_volumes.type = 'V'
AND md_readings.target ILIKE '%обратный%'
THEN COALESCE( md_readings.consumption, md_readings.indication, 0 )
END
) AS v2
,
SUM(
CASE
WHEN voc_md_volumes.type = 'V'
AND md_readings.target ILIKE '%подающий%'
THEN COALESCE( md_readings.consumption, md_readings.indication, 0 )
END
) - SUM(
CASE
WHEN voc_md_volumes.type = 'V'
AND md_readings.target ILIKE '%обратный%'
THEN COALESCE( md_readings.consumption, md_readings.indication, 0 )
END
) AS v1_v2
,
AVG(
CASE
WHEN voc_md_volumes.type = 'PA'
AND md_readings.target ILIKE '%подающий%'
THEN COALESCE( md_readings.consumption, md_readings.indication, 0 )
END
) AS pa1
, AVG(
CASE
WHEN voc_md_volumes.type = 'PA'
AND md_readings.target ILIKE '%обратный%'
THEN COALESCE( md_readings.consumption, md_readings.indication, 0 )
END
) AS pa2
, AVG(
CASE
WHEN voc_md_volumes.type = 'PA'
AND md_readings.target ILIKE '%подающий%'
THEN COALESCE( md_readings.consumption, md_readings.indication, 0 )
END
) - AVG(
CASE
WHEN voc_md_volumes.type = 'PA'
AND md_readings.target ILIKE '%обратный%'
THEN COALESCE( md_readings.consumption, md_readings.indication, 0 )
END
) AS pa1_pa2
,
AVG(
CASE
WHEN voc_md_volumes.type = 'T'
AND md_readings.target ILIKE '%подающий%'
THEN COALESCE( md_readings.consumption, md_readings.indication, 0 )
END
) AS t1
, AVG(
CASE
WHEN voc_md_volumes.type = 'T'
AND md_readings.target ILIKE '%обратный%'
THEN COALESCE( md_readings.consumption, md_readings.indication, 0 )
END
) AS t2
, AVG(
CASE
WHEN voc_md_volumes.type = 'THV'
THEN COALESCE( md_readings.consumption, md_readings.indication, 0 )
END
) AS thv
, AVG(
CASE
WHEN voc_md_volumes.type = 'T'
AND md_readings.target ILIKE '%подающий%'
THEN COALESCE( md_readings.consumption, md_readings.indication, 0 )
END
) - AVG(
CASE
WHEN voc_md_volumes.type = 'T'
AND md_readings.target ILIKE '%обратный%'
THEN COALESCE( md_readings.consumption, md_readings.indication, 0 )
END
) AS t1_t2
,
SUM(md_readings.consumption) as sum
FROM
md_readings
INNER JOIN md_com_resources ON (
md_com_resources.id = md_readings.id_md_com_resource)
LEFT JOIN voc_md_resources ON (
md_com_resources.id_voc_com_resource = voc_md_resources.id_voc_com_resource
AND
voc_md_resources.fake = 0)
LEFT JOIN voc_md_volumes ON (
voc_md_volumes.id = md_readings.id_voc_md_volume
AND
voc_md_volumes.fake = 0)
WHERE
md_readings.fake = 0
AND
md_readings.id_metering_device = ?
AND
md_com_resources.id_voc_com_resource = ?
AND
md_readings.dt IS NOT NULL
AND (
voc_md_volumes.type = 'W'
OR
voc_md_volumes.type = 'M'
OR
voc_md_volumes.type = 'M'
OR
voc_md_volumes.type = 'V'
OR
voc_md_volumes.type = 'V'
OR
voc_md_volumes.type = 'PA'
OR
(voc_md_volumes.type = 'T' OR voc_md_volumes.type = 'THV')
)
GROUP BY
md_readings.dt
LIMIT
1
",
8767,
10
]