ConcatRelated Function Returns All Values - string-concatenation

Using Allen Browne's ConcatRelated function is not returning correctly.
Example:
OrderNumber Product Types
00054001021 ROUND
00054001021 WHITE
00054001121 CONCAVE
00054001121 SCORED
00054001121 WHITE
00054001221 CAPSULE
00054001221 SCORED
00054001221 WHITE
Using this:
SELECT DISTINCT YourTable2.OrderNumber, ConcatRelated("[Product Types]","YourTable2","[OrderNumber]="& "[OrderNumber]","[Product Types]",",") AS All_Product_Types
FROM YourTable2;
Is returning all of the ProductTypes for each Order Number:
OrderNumber All_Product_Types
00054001021 CAPSULE,CONCAVE,ROUND,SCORED,SCORED,WHITE,WHITE,WHITE
00054001121 CAPSULE,CONCAVE,ROUND,SCORED,SCORED,WHITE,WHITE,WHITE
00054001221 CAPSULE,CONCAVE,ROUND,SCORED,SCORED,WHITE,WHITE,WHITE
It should be:
OrderNumber All_Product_Types
00054001021 ROUND, WHITE
00054001121 CONCAVE, SCORED, WHITE
00054001221 CAPSULE, SCORED, WHITE
What am I doing wrong?

I actually solved this myself. I needed more quotation marks (") since my Order Number field is text. In case someone else has the same issue, the query should look like this:
SELECT DISTINCT YourTable2.OrderNumber, ConcatRelated("[Product Types]","YourTable2","[OrderNumber]="""& [OrderNumber] & """","[Product Types]",",") AS All_Product_Types
FROM YourTable2;

Related

How to delete all values in a column with lowercase

I have a dataset I am working on and it has a description column where items for sale are represented in uppercase and expenses are represented in lowercase. I am trying to drop all rows with lower case.
Sample data :
invoiceid description(string datatype)
100 WHITE METAL LANTERN
200 post expenses
300 BLACK WIRE
what I want to achieve
invoiceid description
100 WHITE METAL LANTERN
300 BLACK WIRE
I tried using the following codes but I kept getting errors
from pyspark.sql.functions import upper, lower col
df4 = df3.where(col('Description').islower())
One way; extract the caps into a column. The lower case will return blank. Filter out the blanks and drop the extract column to clean df
df.withColumn('filt', regexp_extract('description(string datatype)','[A-Z]+', 0)).filter("filt != ''").drop('filt').show()
+---------+----------------------------+
|invoiceid|description(string datatype)|
+---------+----------------------------+
| 100| WHITE METAL LANTERN|
| 300| BLACK WIRE|
+---------+----------------------------+

How to compare values of two columns in Postgresql

I need to compare two columns which has below values and get a consolidated value in PostgreSQL. For an Id value, if all is green OR red, I want GREEN or RED to be returned respectively and even if one is RED, I want RED to be returned. Can someone please help? Thanks
ID STATUS
1 GREEN
1 GREEN
1 RED
2 GREEN
2 GREEN
2 GREEN
You seem to want string aggregation:
select id, string_agg(distinct status, ' or ' order by status) statuses
from mytable
group by id
If you want a single value returned, with priority given to "RED", then:
select id, max(status)
from mytable
group by id
This works because, string-wise, "RED" is greater than "GREEN". You don't specify what to do if there are more than two columns, so the answer does not address that.

How can i make this mysql query ? If possible at all

I have this table
itemname property value
------------------------------------------
A size big
A color red
B size big
B color blue
C size small
C color blue
D size small
D color blue
E size small
E color blue
Im creating a list like this: SELECT property,value,COUNT(itemname),GROUP_CONCAT(itemname) FROM table GROUP BY property,value
property value count
---------------------------------
size big 2 (A,B)
size small 3 (C,D,E)
color red 1 (A)
color blue 4 (B,C,D,E)
I want to filter the items which are BIG && SMALL && BLUE, how can i achieve this result ? (I MUST be specific about the property when addressing a value for filtering)
`SELECT
property,value,COUNT(itemname),GROUP_CONCAT(itemname)
FROM
table
GROUP BY
property,value
HAVING
( property IN ('size') && value IN ('big','small') )
&&
( property IN ('color') && value IN ('blue') )`
But this has no result, because it tries to match the row with size and color at the same time ? My desired output in this case is to avoid the item A because it is red, like this:
property value count
---------------------------------
size big 1 (B) A not here, because it is red
size small 3 (C,D,E) no change because all are blue
color red 0 (A) no red is selected, so this row should be 0 or not listed at all
color blue 4 (B,C,D,E) all the 4 are big or small and blue
Please someone help me out in this, i lost 2days wondering on the solution.
I might use CASE combined with HAVING ? Or i should address the WHERE instead somehow ?
Note: This table is actually not real, but if this question can be solved i can use it in my real tables which are a lot more complicated.
I figured a solution on my own, of course this example table is nonsense, the point is the way it had been solved.
`
SELECT
property,
value,
COUNT(t.itemname) AS count
FROM (
SELECT
itemname
FROM
table
GROUP BY
itemname
HAVING
COUNT (CASE WHEN property='size' AND value IN ('big','small') THEN 1 END) >=1
&&
COUNT (CASE WHEN property='color' AND value IN ('blue') THEN 1 END) >=1
) t
INNER JOIN
table ON table.itemname=t.itemname
GROUP BY
property,value
`

postgres coalesce fields, sum and group by

maybe someone can help me out with a postgres query.
the table structure looks like this
nummer nachname vorname cash
+-------+----------+----------+------+
2 Bert Brecht 0,758
2 Harry Belafonte 1,568
3 Elvis Presley 0,357
4 Mark Twain 1,555
4 Ella Fitz 0,333
…
How can I coalesce the fields where "nummer" are the same and sum the cash values?
My output should look like this:
2 Bert, Brecht 2,326
Harry, Belafonte
3 Elvis, Presley 0,357
4 Mark, Twain 1,888
Ella, Fitz
I think the part to coalesce should work something like this:
array_to_string(array_agg(nachname|| ', ' ||coalesce(vorname, '')), '<br />') as name,
Thanks for any help,
tony
SELECT
nummer,
string_agg(nachname||CASE WHEN vorname IS NULL THEN '' ELSE ', '||vorname END, E'\n') AS name,
sum(cash) AS total_cash
FROM Table1
GROUP BY nummer;
See this SQLFiddle; note that it doesn't display the newline characters between names, but they're still there.
The CASE statement is used instead of coalesce so you don't have a trailing comma on entries with a last name but no first name. If you want a trailing comma, use format('%s, %s',vorname,nachname) instead and avoid all that ugly string concatenation business:
SELECT
nummer, string_agg(format('%s, %s', nachname, vorname), E'\n'),
sum(cash) AS total_cash
FROM Table1
GROUP BY nummer;
If string_agg doesn't work, get a newer PostgreSQL, or mention the version in your questions so it's clear you're using an obsolete version. The query is trivially rewritten to use array_to_string and array_agg anyway.
If you're asking how to sum numbers that're actually represented as text strings like 1,2345 in the database: don't do that. Fix your schema. Format numbers on input and output instead, store them as numeric, float8, integer, ... whatever the appropriate numeric type for the job is.

Phrase Search in SQL Server 2008 (T-SQL)

I have a varchar column with 3 rows:
i eat orange,
orange,
oranges are nice
I want SELECT query to return the result in this order:
orange, oranges are nice, i eat orange
i.e. those matches that start with the 'keyword'=orange should come before those that contain the keyword which again should come before those that ends with the keyword.
How can I do this using T-SQL? I tried using the LIKE keyword but no success so far.
WHERE column LIKE '%' + keyword + '%'
ORDER BY CASE WHEN column = keyword THEN 0
WHEN column LIKE keyword + '%' THEN 1
WHEN column LIKE '%' + keyword + '%' THEN 2 END
But really, for this kind of search you want to use a full-text index.
Try the following order by clause (assuming your WHERE clause returns only matches)
ORDER BY charIndex(keyword,col_name),length(col_name)
This will put the earliest occurrence of the keyword first.