Mysql Input/Output query - mysql

I have two querys:
SELECT LancamentoEntrada.*,
TipoEntrada.descricao AS nome,
Usuario.nome AS obreiro
FROM lancamento_entradas LancamentoEntrada,
tipo_entradas TipoEntrada,
obreiros Obreiro,
usuarios Usuario
WHERE LancamentoEntrada.tipo_entrada_id = TipoEntrada.id
AND TipoEntrada.somar_caixa = 1
AND LancamentoEntrada.obreiro_id = Obreiro.id
AND Usuario.id = Obreiro.usuario_id
AND LancamentoEntrada.data_entrada >= '{$begin}'
AND LancamentoEntrada.data_entrada <= '{$end}'
ORDER BY LancamentoEntrada.data_entrada
And
SELECT LancamentoSaida.*,
TipoSaida.descricao AS nome
FROM lancamento_saidas LancamentoSaida,
tipo_saidas TipoSaida
WHERE LancamentoSaida.tipo_saida_id = TipoSaida.id
AND TipoSaida.somar_caixa = 1
AND LancamentoSaida.data_saida >= '{$begin}'
AND LancamentoSaida.data_saida <= '{$end}'
ORDER BY LancamentoSaida.data_saida
Which generate the follow arrays:
// Query 1
Array(
[0] => Array (
[id] => 3
[tipo_entrada_id] => 1
[data_entrada] => 2012-05-08
[data_vencimento] => 2012-05-08
[obreiro_id] => 2
[valor_pago] => 20.00
[valor_pagar] => 0.01
[observacoes] => TESTE
)
[1] => Array (
[...]
)
)
// Query 2
Array (
[0] => Array (
[id] => 1
[tipo_saida_id] => 1
[data_saida] => 2012-05-08
[data_vencimento] => 2012-05-08
[valor_pago] => 200.00
[observacoes] => tESTE
)
[1] => Array (
[...]
)
)
But, I want to do one query, listing inputs and outputs, how I can acomplish this?
If need more explanation, please, ask-me.
EDIT 1
inputs are generated from first query, output from second.
EDIT 2
The querys need to generate report of financial input/output, so, the first query get all input stored and the second get all output generated, both betwenn from one period. I need to generate a list with all, input and output, ordered by date.
Edit 3
I have done this query, the problem is, how I know when is input and when is output?
Tried ISNULL and CASEs, but not work.
(SELECT LancamentoEntrada.data_entrada AS data,
LancamentoEntrada.data_vencimento AS vencimento,
LancamentoEntrada.valor_pago AS valor,
LancamentoEntrada.observacoes AS observacoes,
TipoEntrada.descricao AS nome
FROM lancamento_entradas LancamentoEntrada,
tipo_entradas TipoEntrada
WHERE LancamentoEntrada.tipo_entrada_id = TipoEntrada.id
AND TipoEntrada.somar_caixa = 1
)
UNION
(SELECT LancamentoSaida.data_saida AS data,
LancamentoSaida.data_vencimento AS vencimento,
LancamentoSaida.valor_pago AS valor,
LancamentoSaida.observacoes AS observacoes,
TipoSaida.descricao AS nome
FROM lancamento_saidas LancamentoSaida,
tipo_saidas TipoSaida
WHERE LancamentoSaida.tipo_saida_id = TipoSaida.id
AND TipoSaida.somar_caixa = 1
)

If the only thing you still need is to identify which records came from which query you just need to add a literal to each query.
( SELECT
'Input' as rec_type,
LancamentoEntrada.data_entrada AS data,
LancamentoEntrada.data_vencimento AS vencimento,
LancamentoEntrada.valor_pago AS valor,
LancamentoEntrada.observacoes AS observacoes,
TipoEntrada.descricao AS nome
FROM lancamento_entradas LancamentoEntrada,
tipo_entradas TipoEntrada
WHERE LancamentoEntrada.tipo_entrada_id = TipoEntrada.id
AND TipoEntrada.somar_caixa = 1
)
UNION ALL
(SELECT
'Output' as rec_type,
LancamentoSaida.data_saida AS data,
LancamentoSaida.data_vencimento AS vencimento,
LancamentoSaida.valor_pago AS valor,
LancamentoSaida.observacoes AS observacoes,
TipoSaida.descricao AS nome
FROM lancamento_saidas LancamentoSaida,
tipo_saidas TipoSaida
WHERE LancamentoSaida.tipo_saida_id = TipoSaida.id
AND TipoSaida.somar_caixa = 1
)
As an aside you'll get better performance if you UNION ALL Since UNION would remove duplicates from the two sets which you won't have in this case.

Related

Dynamically fetch columns as JSON in oracle

I have to get some columns as is and some columns from a query as JSON document. The as is column names are known to me but rest are dynamic columns so there are not known beforehand.
Below is the query like
select col1,col2,col3,
sum(col4) as col4,
sum(col5) as col5
from my_table
group by col1,col2,col3;
here col4,col5 names are unknown to me as they are been fetched dynamically.
Suppost my_table data looks like
The expected result is like below
I tried
select JSON_OBJECT(*) from
(
select col1,col2,col3,
sum(col4) as col4,
sum(col5) as col5
from my_table
group by col1,col2,col3
);
But obviously it does not yield expected output.
I'm on 19c DB version 19.17
Any help or suggestion would be great help!
It's kinda hacky, but you could:
Use json_object(*) to convert the whole row to json
Pass the result of this json_transform*, which you can use to remove unwanted attributes
So you could do something like:
with rws as (
select mod ( level, 2 ) col1, mod ( level, 3 ) col2,
level col3, level col4
from dual
connect by level <= 10
), grps as (
select col1,col2,
sum(col3) as col3,
sum(col4) as col4
from rws
group by col1,col2
)
select col1,col2,
json_transform (
json_object(*),
remove '$.COL1',
remove '$.COL2'
) json_data
from grps;
COL1 COL2 JSON_DATA
---------- ---------- ------------------------------
1 1 {"COL3":8,"COL4":8}
0 2 {"COL3":10,"COL4":10}
1 0 {"COL3":12,"COL4":12}
0 1 {"COL3":14,"COL4":14}
1 2 {"COL3":5,"COL4":5}
0 0 {"COL3":6,"COL4":6}
json_transform is a 21c feature that's been backported to 19c in 19.10.
You may achieve this by using Polymorphic Table Function available since 18c.
Define the function that will project only specific columns and serialize others into JSON. An implementation is below.
PTF package (function implementation).
create package pkg_jsonify as
/*Package to implement PTF.
Functions below implement the API
described in the DBMS_TF package*/
function describe(
tab in out dbms_tf.table_t,
keep_cols in dbms_tf.columns_t
) return dbms_tf.describe_t
;
procedure fetch_rows;
end pkg_jsonify;
/
create package body pkg_jsonify as
function describe(
tab in out dbms_tf.table_t,
keep_cols in dbms_tf.columns_t
) return dbms_tf.describe_t
as
add_cols dbms_tf.columns_new_t;
new_col_cnt pls_integer := 0;
begin
for i in 1..tab.column.count loop
/*Initially remove column from the output*/
tab.column(i).pass_through := FALSE;
/*and keep it in the row processing (to be available for serialization*/
tab.column(i).for_read := TRUE;
for j in 1..keep_cols.count loop
/*If column is in a projection list, then remove it
from processing and pass it as is*/
if tab.column(i).description.name = keep_cols(j)
then
tab.column(i).pass_through := TRUE;
/*Skip column in the row processing (JSON serialization)*/
tab.column(i).for_read := FALSE;
end if;
end loop;
end loop;
/*Define new output column*/
add_cols := dbms_tf.columns_new_t(
1 => dbms_tf.column_metadata_t(
name => 'JSON_DOC_DATA',
type => dbms_tf.type_clob
)
);
/*Return the list of new cols*/
return dbms_tf.describe_t(
new_columns => add_cols
);
end;
procedure fetch_rows
/*Process rowset and serialize cols*/
as
rowset dbms_tf.row_set_t;
num_rows pls_integer;
new_col dbms_tf.tab_clob_t;
begin
/*Get rows*/
dbms_tf.get_row_set(
rowset => rowset,
row_count => num_rows
);
for rn in 1..num_rows loop
/*Calculate new column value in the same row*/
new_col(rn) := dbms_tf.row_to_char(
rowset => rowset,
rid => rn,
format => dbms_tf.FORMAT_JSON
);
end loop;
/*Put column to output*/
dbms_tf.put_col(
columnid => 1,
collection => new_col
);
end;
end pkg_jsonify;
/
PTF function definition based on the package.
create function f_cols_to_json(tab in table, cols in columns)
/*Function to serialize into JSON using PTF*/
return table pipelined
row polymorphic using pkg_jsonify;
/
Demo.
create table sample_tab
as
select
trunc(level/10) as id
, mod(level, 3) as id2
, level as val1
, level * level as val2
from dual
connect by level < 40
with prep as (
select
id
, id2
, sum(val1) as val1_sum
, max(val2) as val2_max
from sample_tab
group by
id
, id2
)
select *
from table(f_cols_to_json(prep, columns(id, id2)))
ID
ID2
JSON_DOC_DATA
0
1
{"VAL1_SUM":12, "VAL2_MAX":49}
0
2
{"VAL1_SUM":15, "VAL2_MAX":64}
0
0
{"VAL1_SUM":18, "VAL2_MAX":81}
1
1
{"VAL1_SUM":58, "VAL2_MAX":361}
1
2
{"VAL1_SUM":42, "VAL2_MAX":289}
1
0
{"VAL1_SUM":45, "VAL2_MAX":324}
2
2
{"VAL1_SUM":98, "VAL2_MAX":841}
2
0
{"VAL1_SUM":72, "VAL2_MAX":729}
2
1
{"VAL1_SUM":75, "VAL2_MAX":784}
3
0
{"VAL1_SUM":138, "VAL2_MAX":1521}
3
1
{"VAL1_SUM":102, "VAL2_MAX":1369}
3
2
{"VAL1_SUM":105, "VAL2_MAX":1444}
with prep as (
select
id
, id2
, sum(val1) as val1_sum
, max(val2) as val2_max
from sample_tab
group by
id
, id2
)
select *
from table(f_cols_to_json(prep, columns(id)))
ID
JSON_DOC_DATA
0
{"ID2":1, "VAL1_SUM":12, "VAL2_MAX":49}
0
{"ID2":2, "VAL1_SUM":15, "VAL2_MAX":64}
0
{"ID2":0, "VAL1_SUM":18, "VAL2_MAX":81}
1
{"ID2":1, "VAL1_SUM":58, "VAL2_MAX":361}
1
{"ID2":2, "VAL1_SUM":42, "VAL2_MAX":289}
1
{"ID2":0, "VAL1_SUM":45, "VAL2_MAX":324}
2
{"ID2":2, "VAL1_SUM":98, "VAL2_MAX":841}
2
{"ID2":0, "VAL1_SUM":72, "VAL2_MAX":729}
2
{"ID2":1, "VAL1_SUM":75, "VAL2_MAX":784}
3
{"ID2":0, "VAL1_SUM":138, "VAL2_MAX":1521}
3
{"ID2":1, "VAL1_SUM":102, "VAL2_MAX":1369}
3
{"ID2":2, "VAL1_SUM":105, "VAL2_MAX":1444}
fiddle

How to query in EF core with OrderByDescending, Take, Select and FirstOrDefault

So I've got a table named Summaries, it looks like this
I need to get to sum the latest entries of TotalPieces based on CoveredDate and should be grouped by ServiceCode and queried by month
for example, ServiceCode 'A' has entries on 2020-01-01, 2020-01-02, 2020-01-03, 2020-01-31, 2020-02-01, 2020-02-28, 2020-02-29
and ServiceCode 'B' has entries on 2020-01-01, 2020-01-02, 2020-01-31, 2020-02-20, 2020-02-21,
i need to get the sum based on month, lastest entry on 'A' on January is on 2020-01-31, and 'B' has latest entry on 2020-01-31, I need to sum their 'TotalPieces', so I should get 25 + 25 = 50.
basically i need to do is
Get all the lastest entries based on CoveredDate and month/year
Sum the TotalPieces by ServiceCode
i got a working query, but this is just a workaround because i can't get it right on query.
int sum_totalpieces = 0;
foreach (var serviceCode in service_codes)
{
var totalpieces = _DbContext.ActiveSummaries.Where(acs =>
acs.CoveredDate.Date.Month == query_month
&& acs.CoveredDate.Date.Year == query_year
&& acs.service_codes == serviceCode
)
.OrderByDescending(obd => obd.CoveredDate)
.Take(1)
.Select(s => s.TotalPieces)
.ToList()
.FirstOrDefault();
sum_totalpieces += totalpieces;
}
the service_codes is just a List of string
If you guys could just get rid of the foreach block their and make it services_codes.Contains() on query, or another workaround to make the result faster that would be great. Thanks a lot.
This will do it, but I don't think it will translate to SQL and run at the server:
_DbContext.ActiveSummaries
.Where(b =>
b.CoveredDate >= new DateTime(2020,1,1) &&
b.CoveredDate < new DateTime(2020,2,1) &&
new [] { "A", "B" }.Contains(b.ServiceCode)
)
.GroupBy(g => g.ServiceCode)
.Sum(g => g.OrderByDescending(gb=> gb.CoveredDate).First().TotalPieces);
If you want to do it as a raw SQL for best performance it would look like:
SELECT SUM(totalpieces)
FROM
x
INNER JOIN
(
SELECT servicecode, MAX(covereddate) cd
FROM x
WHERE x.servicecode IN ('A','B') AND covereddate BETWEEN '2020-01-01' AND '2020-01-31'
)y ON x.servicecode=y.servicecode and x.covereddate = y.cd

MySQL match pattern and select number and letters

I have a list of IDs which are created in various third party applications systems and manually added to our system. I need to try and auto increment these IDs based on the largest number. The values are either entirely a number or any number of letters followed by any number of numbers.
For example:
Array ( [works_id] => MD001 [num] => 0 )
Array ( [works_id] => WX9834V [num] => 0 )
Array ( [works_id] => WK009 [num] => 0 )
Array ( [works_id] => W4KHA2 [num] => 0 )
Array ( [works_id] => MD001 [num] => 0 )
Array ( [works_id] => DE1234 [num] => 0 )
Array ( [works_id] => 99 [num] => 99 )
Array ( [works_id] => 100 [num] => 100 )
In the above example, I would need to return 'DE' and 1234 as 1234 is the largest number which matches the pattern (WX9834V does not match as it is LLNNNNL)
So far I have tried:
SELECT works_id, CAST(works_id as UNSIGNED) as num
FROM table
WHERE (works_id REGEXP '^[a-zA-Z]+[0-9]' or works_id REGEXP '^[0-9]+$')
But this returns all rows and returns 0 for the number part unless it is only made up of numbers - how can I return only 'DE' and 1234 from the above?
From the comments, I understant that your primary intent is to select the records that do match your format spec (possibly characters at the beginning of the string, then mandatory numbers until the end of string).
The problem with you current query is that the first regexp, '^[a-zA-Z]+[0-9]' is too permissive: it does allow non-numbers characters at the end of the field, and would be better written '^[a-zA-Z]+[0-9]+$'
Bottom line, the two regexes can be combined into one:
SELECT works_id
FROM mytable
WHERE works_id REGEXP '^[a-zA-Z]*[0-9]+$'
The regexp means:
^ beginning of the string
[a-zA-Z]* 0 to N letters
[0-9]+ at least one digit
$ end of string
In this db fiddle with your test data, this returns:
| works_id |
| -------- |
| MD001 |
| WK009 |
| MD001 |
| 99 |
| 100 |
NB : in MySQL pre-8.0, splitting the string in order to find the max numerical pain is hard to do, since functions such as REGEXP_REPLACE are not available. It is probably easier to do this in your application (unless you have a very large numbers of matching records...). You can have a look at this post or this other one for solutions that mostly rely on MySQL functions.

Merging calculations from different columns to get an average

I want to calculate the average fuel consumption for every car in my table.
I have a spendforfuel table with IdCar, Odometer, Quantity fields.
Odometer field is current mileage.
Quantity are the litres consumed between the current mileage and last detected mileage.
Here is my formula:
100 / ( (MAX(Odometer) - MIN(Odometer)) / (SUM(Quantity) - FIRST(Quantity) ) )
Here is what I did:
$q = $this->db->select()
->from(array('s1' => 'spendforfuel'), array('fuel_consumption' => '100 / ( s2.odometer_sum / ( s3.quantity_sum - s4.first_quantity ) )'))
->joinLeft(array('s2' => 'spendforfuel'), 's2.IdCar = s1.IdCar', array('odometer_sum' => 'MAX(s2.Odometer) - MIN(s2.Odometer)'))
->joinLeft(array('s3' => 'spendforfuel'), 's3.IdCar = s1.IdCar', array('quantity_sum' => 'SUM(s3.Quantity)'))
->joinLeft(array('s4' => 'spendforfuel'), 's4.IdCar = s1.IdCar', array('first_quantity' => 's4.Quantity'))
->where('s4.Odometer > ?', 0)
->limit('1 ASC')
->group('s1.CarId')
->where('s1.CarId = ?', 76);
I am not sure if I have to use joins at all. Any ideas?

MySQL: How to delete rows from the set?

I have a request of translation:
(table structure:
line_id
lang ("en" or "ru")
text
so, in the table it looks like:
5 | en | Test
6 | en | Hello!
7 | en | Another words
6 | ru | Привет!
and all translations are in the same table with key line_id, that is independent from the language)
SELECT txt.line_id, txt.text AS o, ru.text AS t
FROM text AS txt, text AS ru
WHERE txt.line_id = ru.line_id
AND txt.lang = 'en';
it will return such array
> [5] => Array ( [line_id] => 5 [o] => Test [t] => Test
> [6] => Array ( [line_id] => 6 [o] => Hello! [t] => Hello! )
> [7] => Array ( [line_id] => 6 [o] => Hello! [t] => Привет! )
o - is original text, t - translation.
How to delete from set #6 row, because we have translation in the next row. GROUP BY will kill #7 and save #6 row.
the best result would be:
> [5] => Array ( [line_id] => 5 [o] => Test [t] => )
> [6] => Array ( [line_id] => 6 [o] => Hello! [t] => Привет! )
without [o] => Hello! [t] => Hello!
I'm just guessing you probably need:
SELECT txt.line_id,
txt.text AS o,
COALESCE (ru.text,'UNTRANSLATED') AS t
FROM
text AS txt
LEFT JOIN
text AS ru
ON txt.line_id=ru.line_id AND ru.lang='ru'
WHERE txt.lang='en'
How about sort by lang desc (so ru is on top) and then group by line_id?
exclude the original language from the translation table
SELECT txt.line_id, txt.text AS o, ru.text AS t
FROM text AS txt, text AS ru
WHERE txt.line_id=ru.line_id
AND txt.lang='en'
AND ru.lang!='en'
new version for new description
SELECT txt.line_id, ru.lang, ru.text AS t
FROM text AS txt, text AS ru
WHERE txt.line_id=ru.line_id
AND txt.lang='en'
ORDER BY text.lang, txt.line_id
In order to list translated rows and original rows without translation, you must face the lack of support in MySql for sub queries that access the same table.
In my opinion you could divide the problem: first list original rows without translations with one SELECT; then list translations with another SELECT; in the end use a UNION.
-- original without translations
SELECT txt.line_id, txt.text AS o, ru.text AS t
FROM text AS txt LEFT JOIN text AS ru
ON(txt.line_id = ru.line_id AND txt.lang = 'en' AND ru.lang != 'en')
WHERE ru.line_id IS NULL -- ensures no translations have been found
UNION
-- only matching translations
SELECT txt.line_id, txt.text AS o, ru.text AS t
FROM text AS txt JOIN text AS ru
ON(txt.line_id = ru.line_id AND txt.lang = 'en' AND ru.lang != 'en');
Best regards.