How to turn multiple rows corresponding with one ID into single row - Presto - presto

I'm looking the way to convert the data below
"itemid" "attr_id" "Merk" "Berat" "Panjang" "BPOM" "TInggi" "Kadaluarsa"
3624918424 14718 "Hansaplast" "" "" "" "" ""
3624918424 22229 "" "" "" "" "" "24 Bulan"
into this one
itemid Merk Berat Panjang BPOM TInggi Kadaluarsa
3624918424 Hansaplast 24 Bulan
I have checked another questions in SO, the most popular answer is by using PIVOT function. but that function didnt exist in presto. can someone have solution using presto?

I think you can just work with GROUP BY clause to get the desired result:
SELECT itemid
,MAX(Merk) Merk
,MAX(Berat) Berat
,MAX(Panjang) Panjang
,MAX(BPOM) BPOM
,MAX(TInggi) TInggi
,MAX(Kadaluarsa) Kadaluarsa
from data_table
GROUP BY itemid
It's not clear in your example if the columns are NULL or have an empty string '' - with an empty string it might be a bit more complicated and you need something like for all columns:
,MAX(case when Merk='' then NULL else Merk end) Merk
If you have multiple entries per itemid and you want to keep all entries you can also work with array_agg:
array_agg(x) → array<[same as input]>
Returns an array created from the input x elements.

Related

Cognos Report Studio: CASE and IF Statements

I'm very new in using Cognos report studio and trying to filter some of the values and replace them into others.
I currently have values that are coming out as blanks and want to replace them as string "Property Claims"
what i'm trying to use in my main query is
CASE WHEN [Portfolio] is null
then 'Property Claims'
ELSE [Portfolio]
which is giving me an error. Also have a different filter i want to put in to replace windscreen flags to a string value rather than a number. For example if the flag is 1 i want to place it as 'Windscreen Claims'.
if [Claim Windscreen Flag] = 1
then ('Windscreen')
Else [Claim Windscreen Flag]
None of this works with the same error....can someone give me a hand?
Your first CASE statement is missing the END. The error message should be pretty clear. But there is a simpler way to do that:
coalesce([Portfolio], 'Property Claims')
The second problem is similar: Your IF...THEN...ELSE statement is missing a bunch of parentheses. But after correcting that you may have problems with incompatible data types. You may need to cast the numbers to strings:
case
when [Claim Windscreen Flag] = 1 then ('Windscreen')
else cast([Claim Windscreen Flag], varchar(50))
end
In future, please include the error messages.
it might be syntax
IS NULL (instead of = null)
NULL is not blank. You might also want = ' '
case might need an else and END at the bottom
referring to a data type as something else can cause errors. For example a numeric like [Sales] = 'Jane Doe'
For example (assuming the result is a string and data item 2 is also a string),
case
when([data item 1] IS NULL)Then('X')
when([data item 1] = ' ')Then('X')
else([data item 2])
end
Also, if you want to show a data item as a different type, you can use CAST

MATLAB: Count string occurrences in table columns

I'm trying to find the amount of words in this table:
Download Table here: http://www.mediafire.com/file/m81vtdo6bdd7bw8/Table_RandomInfoMiddle.mat/file
Words are indicated by the "Type" criteria, being "letters". The key thing to notice is that not everything in the table is a word, and that the entry "" registers as a word. In other words I need to determine the amount of words, by only counting "letters", except if it is a "missing".
Here is my attempt (Yet unsuccessful - Notice the two mentions of "Problem area"):
for col=1:size(Table_RandomInfoMiddle,2)
column_name = sprintf('Words count for column %d',col);
MiddleWordsType_table.(column_name) = nnz(ismember(Table_RandomInfoMiddle(:,col).Variables,{'letters'}));
MiddleWordsExclusionType_table.(column_name) = nnz(ismember(Table_RandomInfoMiddle(:,col).Variables,{'<missing>'})); %Problem area
end
%Call data from table
MiddleWordsType = table2array(MiddleWordsType_table);
MiddleWordsExclusionType = table2array(MiddleWordsExclusionType_table); %Problem area
%Take out zeros where "Type" was
MiddleWordsTotal_Nr = MiddleWordsType(MiddleWordsType~=0);
MiddleWordsExclusionTotal_Nr = MiddleWordsExclusionType(MiddleWordsExclusionType~=0);
%Final answer
FinalMiddleWordsTotal_Nr = MiddleWordsTotal_Nr-MiddleWordsExclusionTotal_Nr;
Any help will be appreciated. Thank you!
You can get the unique values from column 1 when column 2 satisfies some condition using
MiddleWordsType = numel( unique( ...
Table_RandomInfoMiddle{ismember(Table_RandomInfoMiddle{:,2}, 'letters'), 1} ) );
<missing> is a keyword in a categorical array, not literally the string "<missing>". That's why it appears blue and italicised in the workspace. If you want to check specifically for missing values, you can use this instead of ismember:
ismissing( Table_RandomInfoMiddle{:,1} )

Create new bit column based on string evaluation

I was looking into the function Contains but it can only be used in the Where predicate, what I'm looking for is something of the sorts
Select doc.* , IsSync = StringContains('Sync', doc.Url) from vw_Doc as doc
IsSync now would contain 1/0 or true/false, depending if the word Sync exists in the document Url.
Is this at all possible ?
Thank you for your time
Edit: StringContains is meant as a pseudo-function, it's not valid syntax
I would use case and charindex().
select
doc.* ,
case
when charindex('Sync', doc.Url) > 0
then 1
else 0
end as IsSync
from
vw_Doc as doc
Contains is not limited to a where clause, it returns a boolean that can be used in various contexts, e.g.:
select *,
case when Contains( doc.url, 'Sync' ) then 1 else 0 end as IsSync
from vw_Doc;

postgresql, select empty string

To query empty fields I have seen this answer:
Postgresql, select empty fields
(unfortunately I don't have enough reputation points to answer #wildplasser on that post, so here we go)
Wildplasser's answer:
SELECT mystr, mystr1
FROM mytable
WHERE COALESCE(mystr, '') = ''
OR COALESCE(mystr1, '') = ''
;
I am not sure I get the COALESCE method, but it also works for me this way (specific for my string data type):
SELECT mystr, mystr1
FROM mytable
WHERE mystr = '' ;
My questions are:
Does COALESCE work for any data type?
Is there any better way to query empty strings? i.e., column_value = ' '
First you need to understand the difference between NULL and "empty".
NULL is the absence of a value. Any (or at least almost any) data type can be NULL. When you have a column of type integer, and you don't want to put a value in that field, you put NULL.
"Empty" is a string/text concept. It's a string with an empty value, i.e. ''. A text field with an empty string contains a value: the empty string. It is not the same as containing NULL, i.e. no value. Other data types e.g. integer, boolean, json, whatever, can't have an empty string.
Now to COALESCE. That function works on any data type, and basically it returns the first not-NULL result of its arguments. So COALESCE(NULL, TRUE) returns TRUE because the first argument is NULL; COALESCE(FALSE, TRUE) returns FALSE because the first argument is not NULL; and COALESCE(NULL, NULL) returns NULL because there are no not-NULL arguments.
So, COALESCE(field, '') returns the value of field if it's not NULL, and otherwise returns an empty string. When used in COALESCE(field, '') = '' when trying to find any rows where field is "empty", this is basically saying "if field is NULL then use an empty string in its place, then see if it equals an empty string". This is because NULL and an empty string are not equivalent, and "you" are trying to find any rows where fields are NULL or empty.
In your version of the query, where you just do field = '', that will ONLY return results where field is actually an empty string, not where field is NULL. Which behaviour you desire is up to you.
With COALESCE you will get NULL values too in the first query.
1- In Postgresql, you can't mix datatype example here, but you can use the function to_char to mix values
2- I don't understand your question
I think based on the definition of coalesce itself as
"The COALESCE() function returns the first non-null value in a list."
means that it work for any data type
I don't really understand the question but i think yes its already the most efficient way to make empty string

Comparing Text boxes to be used in filtering data table c#

I want to compare two textboxes with data in a datatable and use this comparison operation to filter the datetable.
For example: I want to show all data (rows and columns) that have value x in which:
textbox1.text>x>textbox1.text
I have used "Like" operator inside string format to get the value that matches the value in the text-box completely but I could not do the required range filtering operation
Here is my code related to the specified question:
dv.RowFilter = string.Format("Type Like '%{0}%' and Gain Like" +
"'%{1}%'" +
"and Year Like'%{2}%' and MotorPower Like '%{3}%'" +
"and Profit Like '%{4}%'", textBoxType .Text,textBoxGain.Text
, textBoxYear.Text, textBoxBiggerthan.Text, textBoxKar.Text);
dataGridView1.DataSource = dv;
I have another input textbox called textBoxSmallerthan.Text
and I want to make my range for MotorPower column in datatable (datagridview) between textBoxBiggerthan.Text and textBoxSmallerthan.Text
The documentation here shows the numbers do not need be wrapped with single quote makers. So the format is:
Columnname < Number
So the final filter should be something like this:
dv.RowFilter = string.Format("Type Like '%{0}%' and Gain Like" +
"'%{1}%'" +
"and Year Like'%{2}%' and MotorPower > {3} and MotorPower < {4}" +
"and Profit Like '%{4}%'", textBoxType .Text,textBoxGain.Text
, textBoxYear.Text, textBoxSmallerthan.Text, textBoxBiggerthan.Text, textBoxKar.Text);
dataGridView1.DataSource = dv;

Resources