Data warehouse-collect_set function, NVL function, date processing

table of Contents

1.collect_set function

2.nvl function

3. Date processing function


1.collect_set function

 data:

id order-type order-number
1 aa 5
2 bb 2
1 bb 1
2 aa 3
1 aa 2

 

Requirement: Analyze the user's orders, show how many orders of different order types are displayed, one line for each user

achieve:

1. Use concat() function to connect order_type and order_number

concat(order_type,'(',order_number,')')

2. Use concat_ws() and collect_set() to merge rows

Converting a user in the above list may occupy multiple rows into the target table format where each user occupies one row, which is actually "column to row"

select user,concat_ws(',',collect_set(concat(order_type,'(',order_number,')')))  order from table group by user

order is an alias

effect:

  • De-duplicate, de-duplicate the id behind group by
  • Form a set for group by that belong to the same id, and use concat_ws to separate the elements in the set to form a string

2.nvl function

NVL (expression 1, expression 2)

nvl is a null value conversion function.

If expression 1 is a null value, NVL returns the value of expression 2, otherwise it returns the value of expression 1. The purpose of this function is to convert a null value (null) into an actual value. The value of the expression can be numeric, character, and date . But the data type of expression 1 and expression 2 must be the same type .

Application scenarios:

  •   You can set the default value if the field is empty. For example, if a person does not fill in the user name when registering the app, the default is the same as the WeChat name you registered.
  •   It can also be used for external associations (join, etc.) when there are duplicate fields in two tables but the values ​​are different, you can set the priority of the field value. For example, there are two event tables, one is the emergency table, and the other is the basic event table. Both tables have a field named event urgency. Here we can associate the two tables first, and set the event urgency. Firstly take the content of this field in the emergency event table, if it is empty then take the content of this field in the basic event table.

3. Date processing function

  • data _format function (organize dates according to format)

select data_format('2020-11-6','yyyy-mm');

  • data_add function (add and subtract dates)

The day before: select date_add(' 2020-11-6 ',-1);

The next day: select date_add(' 2020-11-6 ',1);

  • next_day function

(1) Take the next Monday of the current day

select next_day('2020-11-06','MO');

(2) Take the Monday of the current week

select date_add(next_day('2020-11-06','MO'),-7);

  • last_day function (seeking the date of the last day of the month)

select last_day('2020-10-30');

 

 

 

 

Guess you like

Origin blog.csdn.net/Poolweet_/article/details/109525639
Recommended