SSRS Row Values To Column - sql

I'm creating reports using Reporting Services. I want to show some values stored in the in columns. For example let's say I have a table as;
Target | Type | Value
- - - - - - - - - - -
Store A |Type I | 4
Store A |Type II | 5
Store A |Type III | 16
Store B |Type I | 10
Store B |Type II | 25
I want to list these values as;
Target | Type I | Type II | Type III
- - - - - - - - - - - - - - - - - - -
Store A |4|5|16
Store B |10|10|NULL(or 0)
Here is how I manage the situation right now, I use join as many as I need, so I can show these values in colums. However, when the data is too big it causes too many problems as expected. I wonder if there is an easier way to solve that problem?

You can use PIVOT in your data extraction SQL query like so
select
[Target],[Type I],[Type II],[Type III]
from
(
select * from yourTbl
) src
PIVOT
(
Max([Value]) for [Type] in ([Type I],[Type II],[Type III])
)p

Either group columns in the tablix https://www.youtube.com/watch?v=zM5DRsnH3E0 or perform the grouping in sql server using pivot. https://learn.microsoft.com/en-us/sql/t-sql/queries/from-using-pivot-and-unpivot You may need a dynamic pivot if the columns are not static.

Related

SQL query to get different columns from a json string using a pattern matching

I have a json string - {"exteriorCheck":{"criteria":[{"code":"EXTERIOR_BODY","title":"Exterior - XYZ","value":5},{"code":"EXTERIOR_RIMS","title":"Exterior - ABC","value":4}],"images":[{"code":"EXTERIOR_PICTURES","keys":["share-tasks-b1c757e3-0cb6-41ea-a298-f3430aafb36c/0"]}],"comment":"i.o "},"interiorCheck":{"criteria":[{"code":"INTERIOR_SEATS","title":"Interior - Seats","value":5}
I want to create a column whenever there is "title" like for "title":"Exterior - XYZ"- the column would be Exterior - XYZ and the values would be taken from "value":5 , so 5 will be my output. Since there multiple such cases in the string- it is difficult to use substr with position. I have tried -
select
case when "json" like '%Exterior - XYZ%' then substr("JSON",89,1)
else null end as "Exterior - XYZ". But for the entire json its difficult to get the position.
Desired output:
Exterior - XYZ | Exterior - ABC | Interior - Seats
5 | 4 | 5
How to proceed using AWS Athena (considering multiple string functions wont work at athena)

SQL replace list of strings with element prefixes

in Postgres, I have a table with a column which is a list of text:
devdb=> \d txyz
Table "public.txyz"
Column | Type | Collation | Nullable | Default
---------------+--------+-----------+----------+---------
status | text | | |
lstcol | text[] | | |
and lstcol contains
devdb=> select lstcol from txyz limit 1 ;
lstcol
----------------------------------------------------------------------
{"ABCD - Company One Ltd","EFG - Second Corp."}
I want to replace each element contained in the list with the word that precedes the " - ", obtaining
{"ABCD","EFG"}
How can I achieve that?
It is fine to create another column, and then replace the original one.
My SQL isn't stellar and this project has a lot of it. Any help is deeply appreciated.
Many thanks
You can update the existing table (i.e. transform the existing column contents) like this:
update txyz
set lstcol = (select array_agg(trim(split_part(s, '-', 1))) from unnest(lstcol) s);
And it would be good to vacuum table txyz after that.
One method is a lateral join which pulls the array apart, picks out the the piece you want, and then reaggregates:
select t.*, x.ar
from txyz t cross join lateral
(select array_agg(split_part(col, ' - ', 1)) as ar
from unnest(t.lstcol) col
) x;
Here is a db<>fiddle.
You should to read official docs of Postgres on it's official site.
https://www.postgresql.org/docs/13/arrays.html - this part of Manual describes arrays and operation with ones.

Issue displaying empty value of repeated columns in Google Data Studio

I've got an issue when trying to visualize in Google Data Studio some information from a denormalized table.
Context: I want to gather all the contact of a company and there related orders in a table in Big Query. Contacts can have no order or multiple orders. Following Big Query best practice, this table is denormalized and all the orders for a client are in arrays of struct. It looks like this:
Fields Examples:
+-------+------------+-------------+-----------+
| Row # | Contact_Id | Orders.date | Orders.id |
+-------+------------+-------------+-----------+
|- 1 | 23 | 2019-02-05 | CB1 |
| | | 2020-03-02 | CB293 |
|- 2 | 2321 | - | - |
|- 3 | 77 | 2010-09-03 | AX3 |
+-------+------------+-------------+-----------+
The issue is when I want to use this table as a data source in Data Studio.
For instance, if I build a table with Contact_Id as dimension, everything is fine and I can see all my contacts. However, if I add any dimensions from the Orders struct, all info from contact with no orders are not displayed. For instance, all info from Contact_Id 2321 is removed from the table.
Have you find any workaround to visualize these empty arrays (for instance as null values)?
The only solution I've found is to build an intermediary table with the orders unnested.
The way I've just discovered to work around this is to add an extra field in my DS-> BQ connector:
ARRAY_LENGTH(fields.orders) AS numberoforders
This will return zero if the array is empty - you can then create calculated fields within DataStudio - using the "numberoforders" field to force values to NULL or zero.
You can fix this behaviour by changing a little your query on the BigQuery connector.
Instead of doing this:
SELECT
Contact_id,
Orders
FROM myproject.mydataset.mytable
try this:
SELECT
Contact_id,
IF(ARRAY_LENGTH(Orders) > 0, Orders, [STRUCT(CAST(NULL AS DATE) AS date, CAST(NULL AS STRING) AS id)]) AS Orders
FROM myproject.mydataset.mytable
This way you are forcing your repeated field to have, at least, an array containing NULL values and hence Data Studio will represent those missing values.
Also, if you want to create new calculated fields using one of the nested fields, you should check before if the value is NULL to avoid filling all NULL values. For example, if you have a repeated and nested field which can be 1 or 0, and you want to create a calculated field swaping the value, you should do:
IF(myfield.key IS NOT NULL, IF(myfield.key = 1, 0, 1), NULL)
Here you can see what happens if you check before swaping and if you don't:
Original value No check Check
1 0 0
0 1 1
NULL 1 NULL
1 0 0
NULL 1 NULL

SQL Server - Update field in all table records, using a field from the same table and values from other table

I have this scenario:
Table Territory
ID (int) - CODE (varchar) - NAME (varchar)
Data:
1 - GB - UNITED KINGDOM
2 - GB - ISLE OF MAN
3 - GB - NORTHERN IRELAND
4 - PT - PORTUGAL
5 - DE - GERMANY
6 - DE - HELGOLAND ISLAND
Table Rules:
ID (int) - TERRITORY_CODES (varchar) - TERRITORY_IDS (varchar)
1 - 'GB,PT' - NULL
2 - 'DE,PT' - NULL
I know the second table should not be like this, but I have no option to change it.
I want to fill the column TERRITORY_IDS with the IDs from the table TERRITORY separated by comma. For example:
Table Rules
ID (int) - TERRITORY_CODES (varchar) - TERRITORY_IDS (varchar)
1 - 'GB,PT' - '1,4'
2 - 'DE,PT' - '5,4'
There are several IDs for each territory code, but I want only one ID for each territory table, it could be the first one, doesn't matter.
What you are looking to do is a Bad Idea. It is a good thing that you recognize this is a bad Idea. But for those reading this question and do not understand why it is bad, this violates the First normal form (1NF) principle. Which is all columns should be atomic, meaning that they hold 1 and only 1 value.
Lets get to the nuts and bolts on how to do this Coalesce to the rescue.
Since I do not know why 'gb,pt' and 'de,pt' are grouped that way I didnt wrap this in a Cursor to go through the whole table. But you can easily wrap this in a cursor and do the entire table contents for you.
DECLARE #TERRITORY_Ids varchar(100)
SELECT #TERRITORY_Ids = COALESCE(#TERRITORY_Ids+ ', ', '') +
Id
FROM table_terrytory
WHERE code in ('gb','pt')
INSERT INTO table_rules
SELECT 'gb,pt',#TERRITORY_Ids

Google BigQuery - Parsing string data from a Bigquery table column

I have a table A within a dataset in Bigquery. This table has multiple columns and one of the columns called hits_eventInfo_eventLabel has values like below:
{ID:AEEMEO,Score:8.990000;ID:SEAMCV,Score:8.990000;ID:HBLION;Property
ID:DNSEAWH,Score:0.391670;ID:CP1853;ID:HI2367;ID:H25600;}
If you write this string out in a tabular form, it contains the following data:
**ID | Score**
AEEMEO | 8.990000
SEAMCV | 8.990000
HBLION | -
DNSEAWH | 0.391670
CP1853 | -
HI2367 | -
H25600 | -
Some IDs have scores, some don't. I have multiple records with similar strings populated under the column hits_eventInfo_eventLabel within the table.
My question is how can I parse this string successfully WITHIN BIGQUERY so that I can get a list of property ids and their respective recommendation scores (if existing)? I would like to have the order in which the IDs appear in the string to be preserved after parsing this data.
Would really appreciate any info on this. Thanks in advance!
I would use combination of SPLIT to separate into different rows and REGEXP_EXTRACT to separate into different columns, i.e.
select
regexp_extract(x, r'ID:([^,]*)') as id,
regexp_extract(x, r'Score:([\d\.]*)') score from (
select split(x, ';') x from (
select 'ID:AEEMEO,Score:8.990000;ID:SEAMCV,Score:8.990000;ID:HBLION;Property ID:DNSEAWH,Score:0.391670;ID:CP1853;ID:HI2367;ID:H25600;' as x))
It produces the following result:
Row id score
1 AEEMEO 8.990000
2 SEAMCV 8.990000
3 HBLION null
4 DNSEAWH 0.391670
5 CP1853 null
6 HI2367 null
7 H25600 null
You can write your own JavaScript functions in BigQuery to get exactly what you want now: http://googledevelopers.blogspot.com/2015/08/breaking-sql-barrier-google-bigquery.html