How to be distinct on a name column and only display the highest ts_rank for that column PostgreSQL - postgresql

I posted a question on this here but then I realized I am wanting more than just what I was asking.
I actually need to be DISTINCT on the name column by its highest ts_rank, So my code is,
SELECT name
,ts_rank(to_tsvector(name), query) + ts_rank(to_tsvector(content), query2) AS rank
FROM users
INNER JOIN microposts ON users.id = microposts.user_id
,plainto_tsquery('re') query
,plainto_tsquery('comics') query2
WHERE users.name ## query
OR microposts.content ## query2
ORDER BY rank DESC;
Gives
╔════════════════╤═════════════════════════════════════════╤═══════════╗
║ name │ content │ rank ║
╠════════════════╪═════════════════════════════════════════╪═══════════╣
║ Dawson Kreiger │ dc comics dc comics dc comics dc comics │ 0.0919062 ║
╟────────────────┼─────────────────────────────────────────┼───────────╢
║ Kaylin Green │ dc comics dc comics dc comics │ 0.0889769 ║
╟────────────────┼─────────────────────────────────────────┼───────────╢
║ Dawson Kreiger │ dc comics dc comics │ 0.0827456 ║
╟────────────────┼─────────────────────────────────────────┼───────────╢
║ Kaylin Green │ dc comics │ 0.0759909 ║
╟────────────────┼─────────────────────────────────────────┼───────────╢
║ Dawson Kreiger │ I went to the beach dc comics │ 0.0607927 ║
╟────────────────┼─────────────────────────────────────────┼───────────╢
║ Dawson Kreiger │ I went to the beach dc comics │ 0.0607927 ║
╟────────────────┼─────────────────────────────────────────┼───────────╢
║ Kaylin Green │ I went to the beach dc comics │ 0.0607927 ║
╟────────────────┼─────────────────────────────────────────┼───────────╢
║ Kaylin Green │ I went to the beach dc comics │ 0.0607927 ║
╚════════════════╧═════════════════════════════════════════╧═══════════╝
So I need the output to be this,
╔════════════════╤═════════════════════════════════════════╤═══════════╗
║ name │ content │ rank ║
╠════════════════╪═════════════════════════════════════════╪═══════════╣
║ Dawson Kreiger │ dc comics dc comics dc comics dc comics │ 0.0919062 ║
╟────────────────┼─────────────────────────────────────────┼───────────╢
║ Kaylin Green │ dc comics dc comics dc comics │ 0.0889769 ║
╚════════════════╧═════════════════════════════════════════╧═══════════╝
So I need to select a record that is distinct by name and has the highest rank. But how will the code know how to select the distinct user with the highest ts_rank?
EDIT
For instance if I do this
SELECT name
, ts_rank(to_tsvector(name), query) + ts_rank(to_tsvector(content), query2) AS rank
FROM
(
SELECT DISTINCT name FROM users WHERE rank = MAX(rank)
)
INNER JOIN microposts ON users.id=microposts.user_id
, plainto_tsquery('re') query
,plainto_tsquery('comics') query2
WHERE users.name ## query
OR microposts.content ## query2
ORDER BY rank DESC;
I get error: column "rank" does not exist

You could do a GROUP BY with a MAX.
SELECT name
,MAX(ts_rank(to_tsvector(name), query) + ts_rank(to_tsvector(content), query2)) AS rank
FROM users
INNER JOIN microposts ON users.id = microposts.user_id
,plainto_tsquery('re') query
,plainto_tsquery('comics') query2
WHERE users.name ## query
OR microposts.content ## query2
GROUP BY name
ORDER BY rank DESC;

Related

Transposition using multiple merges (feedback wanted)

i have a solution in danfojs (codepen below) but would like some feedback on how to improve it, since im new to danfo, maybe the same result can be obtained more elegantly.
Essentially i want to do a transposition by merging around twenty Dataframes, but realized that the merge feature only allows to merge two single Dataframes, so i implemented a loop, but end up removing extra columns (played with joins but was the best i could get).
The input files (file1...file20) have the following structure:
╔═══╤══════════╤═══════════════════╤═══════════╗
║ │ tag_name │ ts_utc │ tag_value ║
╟───┼──────────┼───────────────────┼───────────╢
║ 0 │ C1 │ Thu Sep 15 2022… │ 1 ║
╟───┼──────────┼───────────────────┼───────────╢
║ 1 │ C1 │ Sat Sep 17 2022… │ 10 ║
╟───┼──────────┼───────────────────┼───────────╢
║ 2 │ C1 │ Sat Sep 17 2022… │ 11 ║
╚═══╧══════════╧═══════════════════╧═══════════╝
...
╔═══╤══════════╤═══════════════════╤═══════════╗
║ │ tag_name │ ts_utc │ tag_value ║
╟───┼──────────┼───────────────────┼───────────╢
║ 0 │ C3 │ Thu Sep 15 2022… │ 3 ║
╟───┼──────────┼───────────────────┼───────────╢
║ 1 │ C3 │ Sat Sep 17 2022… │ 30 ║
╟───┼──────────┼───────────────────┼───────────╢
║ 2 │ C3 │ Sat Sep 17 2022… │ 31 ║
╚═══╧══════════╧═══════════════════╧═══════════╝
And the desired result is:
tag_value header becomes the tag_name (C1,C2...)
Components are merged by date (ts_utc)
tag_name is replaced by constant "sourcetag"
╔═══╤═══════════════════╤═══════════╤═════╤════╤════╗
║ │ ts_utc │ tag_name │ C1 │ C2 │ C3 ║
╟───┼───────────────────┼───────────┼─────┼────┼────╢
║ 0 │ Thu Sep 15 2022… │ sourceTag │ 1 │ 2 │ 3 ║
╟───┼───────────────────┼───────────┼─────┼────┼────╢
║ 1 │ Sat Sep 17 2022… │ sourceTag │ 10 │ 20 │ 30 ║
╟───┼───────────────────┼───────────┼─────┼────┼────╢
║ 2 │ Sat Sep 17 2022… │ sourceTag │ 11 │ 21 │ 31 ║
╚═══╧═══════════════════╧═══════════╧═════╧════╧════╝
Sample code:
https://codepen.io/dotmindlabs/pen/xxJwMqZ?editors=1111
Thanks, any feedback is very appreciatted.

Select multiple data from other table for each id separated by comma

Table one - workorder
╔══════════╦══════════════╦══════════════╗
║ id ║ wpeople ║ start_date ║
╠══════════╬══════════════╬══════════════╣
║ 1 ║ 1,2,4 ║ 02.08.2016 ║
║ 2 ║ 4,5 ║ 28.09.2016 ║
╚══════════╩══════════════╩══════════════╝
Table two - employees
╔══════════╦═════════════════╗
║ id ║ name ║
╠══════════╬═════════════════╣
║ 1 ║ John ║
║ 2 ║ Ben ║
║ 3 ║ Ian ║
║ 4 ║ Hank ║
║ 5 ║ George ║
╚══════════╩═════════════════╝
Output selection for who need to work at the project
╔══════════╦════════════════╦════════════╗
║ 1 ║ John,Ben,Hank ║ 02.08.2016 ║
║ 2 ║ Hank,George ║ 28.09.2016 ║
╚══════════╩════════════════╩════════════╝
I have tried with GROUP_CONCAT and FIND_IN_SET
SELECT w.id,
GROUP_CONCAT(e.name ORDER BY e.id) workorder
FROM workorder w
INNER JOIN employees e
ON FIND_IN_SET(e.id, a.wpeople) > 0
GROUP BY w.id
But the output it's
╔══════════╦════════════════╦════════════╗
║ 1 ║ John ║ 02.08.2016 ║
║ 1 ║ Ben ║ 02.08.2016 ║
║ 1 ║ Hank ║ 02.08.2016 ║
║ 2 ║ Hank ║ 28.09.2016 ║
║ 2 ║ George ║ 28.09.2016 ║
╚══════════╩════════════════╩════════════╝
I search on google for this and the solution it's GROUP_CONCAT - FIND_IN_SET. Can be that I didn't understand very well this function.
Thanks for you time!
Stefan
For anyone who will need this:
I added a new table werkbon_employee
╔══════════╦═══════════════════╦═══════════════╗
║ id ║ workorder_id ║ employee_id ║
╠══════════╬═══════════════════╬═══════════════╣
║ 1 ║ 1 ║ 1 ║
║ 2 ║ 1 ║ 2 ║
║ 3 ║ 1 ║ 4 ║
║ 4 ║ 2 ║ 4 ║
║ 5 ║ 2 ║ 5 ║
╚══════════╩═══════════════════╩═══════════════╝
I used to select
SELECT *,
GROUP_CONCAT(e.name ORDER BY e.id) ename
FROM werkbon
LEFT JOIN werkbon_employee we ON werkbon.id = we.werkbon_id
INNER JOIN employees e ON FIND_IN_SET(e.id, we.employee_id) > 0
GROUP BY werkbon.id DESC LIMIT 1
Now the result it's
Werk mensen
John,Ben,Hank
Datum
02.08.2016
Thanks to #MarcB for help

Pentaho - How to show complete date in line chart's X axis

I'm creating a Pentaho CDE dashboard and I'm having some difficulties in setup how the date is shown in a line chart. The below chart is what I have at this time:
As you can see, the X axis has numbers from 1-6, they are months. What I want do is show more information in this axis instead of simply 1, I want show "January / 2013" for example, but I have no idea of how can I achieve this.
My Mondrian schema for date dimension is this:
<Dimension type="TimeDimension" visible="true" foreignKey="data_id" highCardinality="false" name="Data">
<Hierarchy name="data" visible="true" hasAll="true">
<Table name="dimensao_data">
</Table>
<Level name="ano" visible="true" column="ano" type="Numeric" uniqueMembers="true" levelType="TimeYears" hideMemberIf="Never">
</Level>
<Level name="semestre" visible="true" column="semestre" type="Numeric" uniqueMembers="false" levelType="TimeHalfYears" hideMemberIf="Never" captionColumn="labelSemestre">
</Level>
<Level name="quarto" visible="true" column="quarto" type="Numeric" uniqueMembers="false" levelType="TimeQuarters" hideMemberIf="Never" captionColumn="labelQuarto">
</Level>
<Level name="mes" visible="true" column="mes" type="Numeric" uniqueMembers="false" levelType="TimeMonths" hideMemberIf="Never" captionColumn="labelMes">
</Level>
<Level name="dia" visible="true" column="dia" type="Numeric" uniqueMembers="false" levelType="TimeDays" hideMemberIf="Never">
</Level>
</Hierarchy>
</Dimension>
and this is the MDX I'm using to retrieve data for the chart:
SELECT NON EMPTY {[Measures].[valor]} ON COLUMNS,
NON EMPTY CrossJoin({[pagamento.forma].[moeda].MEMBERS}, {[Data.data].[mes].MEMBERS}) ON ROWS
FROM [Vendas]
WHERE {[Empresa.empresa].[MATRIZ]}
NEW INFORMATION
When I use debug mode I can see that Data.data don't comes only with month value and in String format:
[pvc.LineChart ]: DATA SOURCE SUMMARY
╔═════════╤═════════════════════╤═════════════╤══════════╗
║ Name │ pagamento.forma │ Data.data │ valor ║
╟─────────┼─────────────────────┼─────────────┼──────────╢
║ Label │ │ │ ║
╟─────────┼─────────────────────┼─────────────┼──────────╢
║ Type │ String │ String │ Numeric ║
╟─────────┼─────────────────────┼─────────────┼──────────╢
║ 1 │ "BOLETO BANCARIO" │ "1" │ 10469.15 ║
║ 2 │ "BOLETO BANCARIO" │ "2" │ 16279.45 ║
║ 3 │ "BOLETO BANCARIO" │ "3" │ 16279.45 ║
║ 4 │ "BOLETO BANCARIO" │ "4" │ 5810.3 ║
║ 5 │ "BOLETO BANCARIO" │ "5" │ 16279.45 ║
║ 6 │ "BOLETO BANCARIO" │ "6" │ 5810.3 ║
║ 7 │ "CARTÃO DE CRÉDITO" │ "1" │ 10243.57 ║
║ 8 │ "CARTÃO DE CRÉDITO" │ "2" │ 9178.03 ║
║ 9 │ "CARTÃO DE CRÉDITO" │ "3" │ 10273.08 ║
║ 10 │ "CARTÃO DE CRÉDITO" │ "4" │ 10110.4 ║
║ 11 │ "CARTÃO DE CRÉDITO" │ "5" │ 10366.3 ║
║ 12 │ "CARTÃO DE CRÉDITO" │ "6" │ 10768.75 ║
║ 13 │ "CARTÃO DE DÉBITO" │ "1" │ 15584.84 ║
║ 14 │ "CARTÃO DE DÉBITO" │ "2" │ 12400.53 ║
║ 15 │ "CARTÃO DE DÉBITO" │ "3" │ 13517.65 ║
╟─────────┼─────────────────────┼─────────────┼──────────╢
║ (15/41) │ │ │ ║
╚═════════╧═════════════════════╧═════════════╧══════════╝
So, I believe the problem is with the result of Data.data. How can I purchase the complete date to show in chart?
There are multiple ways to achieve that:
At the query level:
Define a Measure that holds the info you want to display:
With member [Measures].[Date Label] as [data].CurrentMember.Caption || " / " || Ancestor( [data].CurrentMember, [data].[ano]).Name
This should give you "2013 / January" as the output. Just filter out the columns you want to pass to the chart when defining the CDA query.
At the chart level.
You can change what the chart displays by playing around with the PostFetch of the chart. Something like
function(data){
var results = data.resultset.map(function(d){
// Tweak the contents of each line of data here
// You will want to take the value of d[0] and replace it by
// something else.
return d;
});
data.resultset = results;
return data
}
I prefer to have this type of thing done at the query level, it makes the dashboard simpler to understand and maintain. But it depends a lot on the specifics.

OnCollisionEnter doesn't trigger

using UnityEngine;
using System.Collections;
public class changedirection : MonoBehaviour {
void OnCollisionEnter(Collision col)
{
if (col.gameObject.name == "soldier")
{
GameObject go = col.gameObject;
Move move = go.GetComponent<Move>();
move.direction = -1;
}
}
}
Both objects that collide are triggers, sorry didn't program with unity for over a year
Static colliders don't collide with each other.
There was a table that showed when collision and trigger events are fired on docs.unity3d.com. But they have updated the site and now I can't find it. I still have it local. So, here it is.
Collision action matrix
Depending on the configurations of the two colliding Objects, a number of different actions can occur. The chart below outlines what you can expect from two colliding Objects, based on the components that are attached to them. Some of the combinations only cause one of the two Objects to be affected by the collision, so keep the standard rule in mind - physics will not be applied to objects that do not have Rigidbodies attached.
Collision detection occurs and messages are sent upon collision
╔═══════════╦══════════╤═══════════╤═══════════╤══════════╤═══════════╤═══════════╗
║ ║ Static │ Rigidbody │ Kinematic │ Static │ Rigidbody │ Kinematic ║
║ ║ Collider │ Collider │ Rigidbody │ Trigger │ Trigger │ Rigidbody ║
║ ║ │ │ Collider │ Collider │ Collider │ Trigger ║
║ ║ │ │ │ │ │ Collider ║
╠═══════════╬══════════╪═══════════╪═══════════╪══════════╪═══════════╪═══════════╣
║ Static ║ │ Y │ │ │ │ ║
║ Collider ║ │ │ │ │ │ ║
╟───────────╫──────────┼───────────┼───────────┼──────────┼───────────┼───────────╢
║ Rigidbody ║ Y │ Y │ Y │ │ │ ║
║ Collider ║ │ │ │ │ │ ║
╟───────────╫──────────┼───────────┼───────────┼──────────┼───────────┼───────────╢
║ Kinematic ║ │ Y │ │ │ │ ║
║ Rigidbody ║ │ │ │ │ │ ║
║ Collider ║ │ │ │ │ │ ║
╟───────────╫──────────┼───────────┼───────────┼──────────┼───────────┼───────────╢
║ Static ║ │ │ │ │ │ ║
║ Trigger ║ │ │ │ │ │ ║
║ Collider ║ │ │ │ │ │ ║
╟───────────╫──────────┼───────────┼───────────┼──────────┼───────────┼───────────╢
║ Rigidbody ║ │ │ │ │ │ ║
║ Trigger ║ │ │ │ │ │ ║
║ Collider ║ │ │ │ │ │ ║
╟───────────╫──────────┼───────────┼───────────┼──────────┼───────────┼───────────╢
║ Kinematic ║ │ │ │ │ │ ║
║ Rigidbody ║ │ │ │ │ │ ║
║ Trigger ║ │ │ │ │ │ ║
║ Collider ║ │ │ │ │ │ ║
╚═══════════╩══════════╧═══════════╧═══════════╧══════════╧═══════════╧═══════════╝
Trigger messages are sent upon collision
╔═══════════╦══════════╤═══════════╤═══════════╤══════════╤═══════════╤═══════════╗
║ ║ Static │ Rigidbody │ Kinematic │ Static │ Rigidbody │ Kinematic ║
║ ║ Collider │ Collider │ Rigidbody │ Trigger │ Trigger │ Rigidbody ║
║ ║ │ │ Collider │ Collider │ Collider │ Trigger ║
║ ║ │ │ │ │ │ Collider ║
╠═══════════╬══════════╪═══════════╪═══════════╪══════════╪═══════════╪═══════════╣
║ Static ║ │ │ │ │ Y │ Y ║
║ Collider ║ │ │ │ │ │ ║
╟───────────╫──────────┼───────────┼───────────┼──────────┼───────────┼───────────╢
║ Rigidbody ║ │ │ │ Y │ Y │ Y ║
║ Collider ║ │ │ │ │ │ ║
╟───────────╫──────────┼───────────┼───────────┼──────────┼───────────┼───────────╢
║ Kinematic ║ │ │ │ │ │ ║
║ Rigidbody ║ │ │ │ Y │ Y │ Y ║
║ Collider ║ │ │ │ │ │ ║
╟───────────╫──────────┼───────────┼───────────┼──────────┼───────────┼───────────╢
║ Static ║ │ │ │ │ │ ║
║ Trigger ║ │ Y │ Y │ │ Y │ Y ║
║ Collider ║ │ │ │ │ │ ║
╟───────────╫──────────┼───────────┼───────────┼──────────┼───────────┼───────────╢
║ Rigidbody ║ │ │ │ │ │ ║
║ Trigger ║ Y │ Y │ Y │ Y │ Y │ Y ║
║ Collider ║ │ │ │ │ │ ║
╟───────────╫──────────┼───────────┼───────────┼──────────┼───────────┼───────────╢
║ Kinematic ║ │ │ │ │ │ ║
║ Rigidbody ║ Y │ Y │ Y │ Y │ Y │ Y ║
║ Trigger ║ │ │ │ │ │ ║
║ Collider ║ │ │ │ │ │ ║
╚═══════════╩══════════╧═══════════╧═══════════╧══════════╧═══════════╧═══════════╝
Layer-Based Collision Detection
In Unity 3.x we introduce something called Layer-Based Collision Detection, and you can now selectively tell Unity GameObjects to collide with specific layers they are attached to. For more information click here.

Selecting a row with specific value from a group?

From the table below, if Project.Date group has a Fail and Success, I'd like to keep the Fail row, but if single row (like the rest,) then keep that row regardless of Status. For example I'd like to keep the first row, discard the second and keep the rest on the table below.
╔═════════╦══════════╦═════════╗
║ PROJECT ║ DATE ║ STATUS ║
╠═════════╬══════════╬═════════╣
║ HLM ║ 20130422 ║ Fail ║
║ HLM ║ 20130422 ║ Success ║
║ HLM ║ 20130423 ║ Fail ║
║ HLM ║ 20130424 ║ Success ║
║ HLM ║ 20130425 ║ Fail ║
║ HLM ║ 20130426 ║ Success ║
╚═════════╩══════════╩═════════╝
WITH records
AS
(
SELECT [Project], [Date], [Status],
ROW_NUMBER() OVER (PARTITION BY [Project], [Date]
ORDER BY [Status]) rn
FROM TableName
)
SELECT [Project], [Date], [Status]
FROM records
WHERE rn = 1
SQLFiddle Demo
OUTPUT
╔═════════╦══════════╦═════════╗
║ PROJECT ║ DATE ║ STATUS ║
╠═════════╬══════════╬═════════╣
║ HLM ║ 20130422 ║ Fail ║
║ HLM ║ 20130423 ║ Fail ║
║ HLM ║ 20130424 ║ Success ║
║ HLM ║ 20130425 ║ Fail ║
║ HLM ║ 20130426 ║ Success ║
╚═════════╩══════════╩═════════╝