Postgresql combine multiple rows into multiple columns - sql

I have following sql:
with modules as (
select *
from translate_dev.tab_module as m
join translate_dev.tab_translation_module as tm on m.id = tm.module_id
)
select tm.name,
t.id,
t.language_id,
t.text
from translate_dev.tab_translation as t
join modules as tm on tm.translation_id = t.id
where t.id in (
1166615, 1166401, 1166578, 1166579, 1166580, 1166581, 1166582, 1166583, 1166584, 1166586, 1166587,
1166588, 1166589, 1166591, 1166595, 1166597, 1166598, 1166599, 1166600, 1166601, 1166602, 1166603,
1166604, 1166605, 1166606, 1166607, 1166608, 1166610, 1166612, 1166614, 1166616, 1166617, 1166618,
1166619, 1166621, 1166623, 1166624, 1166626, 1166627, 1166628, 1166629, 1166631, 1166632, 1166633,
1166634, 1166635, 1166636, 1166637, 1166638, 1166640, 1166641, 1166642, 1166643, 1166644, 1166645,
1166646, 1166650, 1166651, 1166652, 1166653, 1166654, 1166655, 1166656, 1166657, 1166658, 1166659,
1166662, 1166664, 1166665, 1166667, 1166668, 1166669, 1166671, 1166672, 1166673, 1166674, 1166675,
1166676, 1166677, 1166678, 1166679, 1166680, 1166681, 1166682, 1166683, 1166685, 1166688, 1166689,
1166693, 1166696, 1166697, 1166698, 1166699, 1166700, 1166701, 1166702, 1166704, 1166705, 1166706,
1166707, 1166709, 1166710, 1166712, 1166713, 1166714, 1166716, 1166717, 1166718, 1166719, 1166721,
1166722, 1166723, 1166725, 1166726, 1166727, 1166728, 1166730, 1166731, 1166733, 1166734, 1166735,
1166736, 1166741, 1166742, 1166743, 1166744, 1166745, 1166747, 1166748, 1166749, 1166751, 1166752,
1166753, 1166754, 1166755, 1166756, 1166757, 1166758, 1166759, 1166760, 1167155, 1167157, 1167158,
1167539, 1167540, 1167546, 1167966, 1167967, 1168007, 1168010, 1168011, 1168012, 1168014, 1168015,
1168016, 1168017, 1168018, 1168019, 1168020, 1168021, 1168022, 1168023, 1168024, 1168025, 1168026,
1168027, 1168028, 1168029, 1168030, 1168031, 1168032, 1168033, 1168034, 1168035
)
order by t.id, t.language_id
Which results in (only an example):
What i want though is one result row for the id in which the text of the specific language_id is appended as extra column.
What i mean is:
Name
id
bg
cs
de
...
MobileServices
1166401`
bg translated text
cs translated text
de translated text
...
Someone has an idea how to do this? I tried it the crosstab function but since the sql needs to be a string in the function i have problems figuring out the error stacktraces.
Any help is appreciated!
Thanks

You can use conditional aggregation. Postgres provides filter for this purpose:
select tm.name, t.id,
max(t.text) filter (where t.language_id = 'bg') as bg,
max(t.text) filter (where t.language_id = 'cs') as cs,
. . .
from translate_dev.tab_translation t join
modules tm
on tm.translation_id = t.id
where t.id in (. . . )
group by tm.name, t.id

Related

MariaDB server version for the right syntax to use near 'GROUP BY

I build my project based on Laravel 9 and try to get count data by date group. I write using DB::raw to get sql query like this:
$rawActive = "
SELECT
SBC.SITE,
OPR.OPERATOR,
COUNT(*) TMO_COUNT,
DATE_FORMAT( TMO.TMO_DATE, '%m%Y' ) BULANTAHUN
FROM
TOP_TMO TMO
INNER JOIN SUBSCRIBER SBC ON TMO.SUBSCRIBER_ID = SBC.ID
INNER JOIN OPERATOR OPR ON SBC.SITE_ID = OPR.ID
WHERE
SBC.SITE_ID = ".$siteId."
GROUP BY
DATE_FORMAT(
TMO.TMO_DATE,
'%m%Y')
";
$queryAct = DB::select(DB::raw($rawActive));
the siteId is from form request.
I search for some solutions include edit 'strict' => false, in database.php , but still not find any solution.
I try to return $rawActive, and this is the result.
SELECT
SBC.SITE,
OPR.OPERATOR,
COUNT(*) TMO_COUNT,
DATE_FORMAT( TMO.TMO_DATE, '%m%Y' ) BULANTAHUN
FROM
TOP_TMO TMO
INNER JOIN SUBSCRIBER SBC ON TMO.SUBSCRIBER_ID = SBC.ID
INNER JOIN OPERATOR OPR ON SBC.SITE_ID = OPR.ID
WHERE
SBC.SITE_ID = 134
GROUP BY
DATE_FORMAT(
TMO.TMO_DATE,
'%m%Y')
As you can see, the siteId are seen well.
I also try this query on mysql, it's work fine.
Thanks for your help.
You need to adjust config\database.php as below:
'mysql' => [
...
....
'strict' => true,
'modes' => [
//'ONLY_FULL_GROUP_BY', // Disable this to allow grouping by one column
'STRICT_TRANS_TABLES',
'NO_ZERO_IN_DATE',
'NO_ZERO_DATE',
'ERROR_FOR_DIVISION_BY_ZERO',
'NO_AUTO_CREATE_USER',
'NO_ENGINE_SUBSTITUTION'
],
]
You can try this. You can enclose $siteId with single quote. '123' will work same like 123 and it will helps breaking query when there is no value assigned in $siteId. Instead try to use parameterized query that will prevent this issue and also is recommended solution for securing raw query.
$rawActive = "
SELECT
SBC.SITE,
OPR.OPERATOR,
COUNT(*) TMO_COUNT,
DATE_FORMAT( TMO.TMO_DATE, '%m%Y' ) BULANTAHUN
FROM
TOP_TMO TMO
INNER JOIN SUBSCRIBER SBC ON TMO.SUBSCRIBER_ID = SBC.ID
INNER JOIN OPERATOR OPR ON SBC.SITE_ID = OPR.ID
WHERE
SBC.SITE_ID = '".$siteId."'
GROUP BY
DATE_FORMAT(
TMO.TMO_DATE,
'%m%Y')
";
$queryAct = DB::select(DB::raw($rawActive));

SQL replace function error while using it to replace one string

I have event table in which there are two fields named as sport, event_name .
This was values such as:
{sport:"Athletic"; event_name:"Athletic 100 meter"}
What I want is to use replace function to replace the string in event_name that matches string in sport with nothing.
so the final output will be such :
{sport:"Athletic"; event_name:"100 meter"}
And I was also joined it with other table so only ID that are to be replace are also present in other table
so I used in this way in following code. But it should an error : "Expected item: < result-column > " . Thank you
SELECT
ae.id ,
ae.city AS event_city,
ae.sport,
REPLACE(ae.event,ae.sport,' ') AS event_name ,
FROM
athlete_events ae
inner join
players_personalinfo pp on
pp.id=ae.id
You need to define the table aliases:
SELECT ae.id AS event_id, ae.city AS event_city, ae.sport,
REPLACE(ae.event, ae.sport, ' ') AS event_name ,
ae.event
FROM athlete_events ae JOIN
players_personalinfo pp
ON pp.id = ae.id;
I would also advise you to trim the result:
TRIM(REPLACE(ae.event, ae.sport, ' ')) AS event_name,
This will remove leading and trailing spaces.
The REPLACE function is case sensitive. Try to check the data to make sure that the capitalization of each is the same.
The prior answers work, but you need to modify one of the field names in your query. In your description, you mentioned the field name is "event_name", but in your query, you reference just "event" (ae.event).
Also, I'm a little surprised that an event_id would join to a player's profile id. Seems a bit odd.
At any rate, I confirmed this SQL works in both postgres and oracle databases...
SELECT
ae.id AS event_id,
ae.city AS event_city,
ae.sport,
ae.event_name as event_name_original
REPLACE(ae.event_name,ae.sport,' ') AS event_name_kinda_ugly,
TRIM(REPLACE(ae.event_name,ae.sport,' ')) AS event_name_clean
FROM
athlete_events ae
inner join
players_personalinfo pp on pp.id=ae.id

ORA-00936: missing expression Java SQL Exception

I´ve been trying to find the error in this statement for a few hours and can´t seem to find it.
It must have something to do with the AND connecting the two WHERE´s since when deleting the first WHERE it works:
SELECT E_AUFMASS_KOMMENTARE.FIBU_FIRMA,
E_AUFMASS_KOMMENTARE.AUFTR_NR,
E_AUFMASS_KOMMENTARE.KOMMENTAR,
AUFTR_EXT.ART_GRUPPE
FROM HHNG_AU.E_AUFMASS_KOMMENTARE
INNER JOIN HHNG_AU.AUFTR_EXT ON E_AUFMASS_KOMMENTARE.AUFTR_NR = AUFTR_EXT.AUFTR_NR
WHERE (E_AUFMASS_KOMMENTARE.AUFTR_NR = '1248823' )
AND WHERE NOT EXISTS( SELECT * FROM HHNG_AU.EX_KOMMENTARE WHERE EX_KOMMENTARE.AUFTR_NR = '1248823' )
Too many WHERE. You only need where once, then use ANDs and ORs to combine conditions:
SELECT E_AUFMASS_KOMMENTARE.FIBU_FIRMA, E_AUFMASS_KOMMENTARE.AUFTR_NR, E_AUFMASS_KOMMENTARE.KOMMENTAR, AUFTR_EXT.ART_GRUPPE FROM HHNG_AU.E_AUFMASS_KOMMENTARE INNER JOIN HHNG_AU.AUFTR_EXT ON E_AUFMASS_KOMMENTARE.AUFTR_NR = AUFTR_EXT.AUFTR_NR WHERE (E_AUFMASS_KOMMENTARE.AUFTR_NR = '1248823' ) AND NOT EXISTS( SELECT * FROM HHNG_AU.EX_KOMMENTARE WHERE EX_KOMMENTARE.AUFTR_NR = '1248823' )

Inserting a variable in a raw sql query Laravel

I am inside a function in a controller.
So from the Form, I get a value for a variable, say:
$x = "whatever";
Then I need to embed that variable (so, its value), in the WHERE statement. If I hardcode the value, it brings a correct result, but I have tried in all ways to insert that variable without success. Well, supposing that I manage to use that variable, then I will have to look into binding to avoid sql injection, but so far, I would say, see if that variable can get used in the query.
I have tried, double quotes, concatenation . $vx . , curly braces {$x}, the variable plain like this $variable, but either gives syntax errors in some cases, (concatenation), or if I just embed the variable like this where author = $x, it tells me that it can't find the column named $x
$x = "whatever";
$results = DB::select(DB::raw('SELECT
t.id, t.AvgStyle, r.RateDesc
FROM (
SELECT
p.id, ROUND(AVG(s.Value)) AS AvgStyle
FROM posts p
INNER JOIN styles s
ON s.post_id = p.id
WHERE author = $x
GROUP BY p.id
) t
INNER JOIN rates r
ON r.digit = t.AvgStyle'
));
This appears to be a simple PHP variable interpolation issue.
DB::raw() wants literally raw SQL. So there are a couple of issues that need to be fixed in the SQL string you are passing.
PHP Variable interpolation (injecting variables into a string) only happens if you use double quotes around the string. With single quotes it becomes a string constant.
If Author is a char/varchar, then SQL syntax requires quotes around the string in your raw SQL statement. Query builders typically take care of these issues for you, but you are going around them.
So the "fixed" version of this would be:
$x = "whatever";
$results = DB::select(DB::raw("SELECT
t.id, t.AvgStyle, r.RateDesc
FROM (
SELECT
p.id, ROUND(AVG(s.Value)) AS AvgStyle
FROM posts p
INNER JOIN styles s
ON s.post_id = p.id
WHERE author = '$x'
GROUP BY p.id
) t
INNER JOIN rates r
ON r.digit = t.AvgStyle"
));
Like all interpolation, this opens you up to the possibility of SQL injection if the variable being interpolated comes from user input. From the original question it is unclear whether this is a problem.
DB::select() has an option that allows you to pass an array of parameters that is inherently safe from SQL injection. In that case the solution would be:
$x = "whatever";
$results = DB::select(DB::raw("SELECT
t.id, t.AvgStyle, r.RateDesc
FROM (
SELECT
p.id, ROUND(AVG(s.Value)) AS AvgStyle
FROM posts p
INNER JOIN styles s
ON s.post_id = p.id
WHERE author = :author
GROUP BY p.id
) t
INNER JOIN rates r
ON r.digit = t.AvgStyle"
),
array('author' => $x)
);
Regarding this tutorial
$results = DB::select( DB::raw("SELECT * FROM some_table WHERE some_col = :somevariable"), array(
'somevariable' => $someVariable,
));
This is one example for you to insert variable in a raw sql laravel
$query_result = Event::select(
DB::raw('(CASE WHEN status = "draft" THEN "draft"
WHEN events.end_time <= \''.$now.'\' THEN "closed"
ELSE "available"
END) AS status'))
->orderBy('status')
->get();

SQL Server Compact won't allow subselect but inner join with groupby not allowed on text datatype

I have the following sql syntax that I used in my database query (SQL Server)
SELECT Nieuwsbrief.ID
, Nieuwsbrief.Titel
, Nieuwsbrief.Brief
, Nieuwsbrief.NieuwsbriefTypeCode
, (SELECT COUNT(*) AS Expr1
FROM NieuwsbriefCommentaar
WHERE (Nieuwsbrief.ID = NieuwsbriefCommentaar.NieuwsbriefID
AND NieuwsbriefCommentaar.Goedgekeurd = 1)) AS AantalCommentaren
FROM Nieuwsbrief
I'm changing now to sql-server-ce (compact edition) which won't allow me to have subqueries like this. Proposed solution : inner join. But as I only need a count of the subtable 'NieuwsbriefCommentaar', I have to use a 'group by' clause on my base table attributes to avoid doubles in the result set.
However the 'Nieuwbrief.Brief' attribute is of datatype 'text'. Group by clauses are not allowed on 'text' datatype in sql-server-ce. 'Text' datatype is deprecated, but sql-server-ce doesn't support 'nvarchar(max)' yet...
Any idea how to solve this? Thx for your help.
I think that the solution could be easier. I don't know exactly how is your metadata but I think that this code could fit your requirements by simply using LEFT JOIN.
SELECT Nieuwsbrief.ID
, Nieuwsbrief.Titel
, Nieuwsbrief.Brief
, Nieuwsbrief.NieuwsbriefTypeCode
, COUNT(NieuwsbriefCommentaar.NieuwsbriefID) AS AantalCommentaren
FROM Nieuwsbrief
LEFT JOIN NieuwsbriefCommentaar ON (Nieuwsbrief.ID = NieuwsbriefCommentaar.NieuwsbriefID)
WHERE NieuwsbriefCommentaar.Goedgekeurd = 1
Edited: 2ndOption
SELECT N.ID, N.Titel, N.Brief, N.NieuwsbriefTypeCode, G.AantalCommentaren FROM Nieuwsbrief as N LEFT JOIN (SELECT NieuwsbriefID, COUNT(*) AS AantalCommentaren FROM NieuwsbriefCommentaar GROUP BY NieuwsbriefID) AS G ON (N.ID = G.NieuwsbriefID)
Please, let me know if this code works in order to find out another workaround..
regards,