(MDX) how to use current member name as another column? - sql-server

Example MDX query from https://quartetfs.com/resource-center/mdx-query-basics:
SELECT
NON EMPTY {[ASIN].[ASIN].Members} ON ROWS,
NON EMPTY {[Category].[Category].[LCD]} ON COLUMNS
FROM [Amazon]
WHERE ( [Measures].[Gross.Profit],
[Time].[ALL].[AllMember].[2011].[5],
[Brand].[Brand].[LG])
How could one repeat the ASIN field (pink column) in another column?
I tried adding [ASIN].[ASIN] to ON COLUMNS expression
SELECT
NON EMPTY {[ASIN].[ASIN].Members} ON ROWS,
NON EMPTY {[Category].[Category].[LCD],[ASIN].[ASIN]} ON COLUMNS
FROM [Amazon]
WHERE ( [Measures].[Gross.Profit],
[Time].[ALL].[AllMember].[2011].[5],
[Brand].[Brand].[LG])
Which resulted in Two sets specified in the function have different dimensionality. Adding .CurrentMember resulted in the same error.
I tried adding ASIN property through new measure:
MEMBER Measures.ASIN AS [ASIN].[ASIN].CurrentMember
SELECT
NON EMPTY {[ASIN].[ASIN].Members} ON ROWS,
NON EMPTY {[Category].[Category].[LCD],Measures.ASIN} ON COLUMNS
FROM [Amazon]
WHERE ( [Measures].[Gross.Profit],
[Time].[ALL].[AllMember].[2011].[5],
[Brand].[Brand].[LG])
Which adds a new column with null values.
What I want to see is:
______________LCD________ASIN__________
B003D4WAVW 124,420.16 B003D4WAVW
...
Is there a way to achieve this?

Try this one:
MEMBER Measures.ASIN AS [ASIN].[ASIN].CurrentMember.Member_Name
MEMBER Measures.LCD AS ([Category].[Category].[LCD],[Measures].[Gross.Profit])
SELECT
NON EMPTY {[ASIN].[ASIN].Members} ON ROWS,
NON EMPTY {[Measures].[LCD],[Measures].[ASIN]} ON COLUMNS
FROM [Amazon]
WHERE ( [Time].[ALL].[AllMember].[2011].[5],
[Brand].[Brand].[LG])
You tried to use dimension and measure member on the same axis. I've transformed this into two measures.
Tested on my own data:

Related

extracting name/value pairs into columns and values - Shopify Data In Bigquery

We use FiveTran to extract our data from shopify and store it in BigQuery. The field "properties" within the order_line table contains what looks like an array of key/value pairs. In this case name/value. The field type is string here is an example of the contents
order_line_id properties
9956058529877 [{"name":"_order_bump_rule_id","value":"4afx7cbw6"},{"name":"_order_bump_bump_id","value":"769d1996-b6fb-4bc3-8d41-c4d7125768c5"},{"name":"_source","value":"order-bump"}]
4467731660885 [{"name":"shipping_interval_unit_type","value":null},{"name":"charge_delay","value":null},{"name":"charge_on_day_of_week","value":null},{"name":"charge_interval_frequency","value":null},{"name":"charge_on_day_of_month","value":null},{"name":"shipping_interval_frequency","value":null},{"name":"number_charges_until_expiration","value":null}]
4467738738773 [{"name":"shipping_interval_unit_type","value":null},{"name":"charge_delay","value":null},{"name":"charge_on_day_of_week","value":null},{"name":"charge_interval_frequency","value":null},{"name":"charge_on_day_of_month","value":null},{"name":"shipping_interval_frequency","value":null},{"name":"number_charges_until_expiration","value":null}]
4578798600277 [{"name":"shipping_interval_unit_type","value":null},{"name":"charge_interval_frequency","value":null},{"name":"shipping_interval_frequency","value":null}]
I am trying to write a query that generate one row per record with a column for each of these name values:
shipping_interval_unit_type
charge_on_day_of_week
charge_interval_frequency
charge_on_day_of_month
subscription_id
number_charges_until_expiration
shipping_interval_frequency
and the corresponding "value". This field "properties" can contain many different "name" values and they can be in different order each time. The "name" values noted above are not always present in the "properties" field.
I've tried json functions but it doesn't seem to be properly formatted for json. I've tried unnesting it but that fails since it is a string.
Consider below approach
select * from (
select order_line_id,
json_extract_scalar(property, '$.name') name,
json_extract_scalar(property, '$.value') value
from your_table, unnest(json_extract_array(properties)) property
)
pivot (min(value) for name in (
'shipping_interval_unit_type',
'charge_on_day_of_week',
'charge_interval_frequency',
'charge_on_day_of_month',
'subscription_id',
'number_charges_until_expiration',
'shipping_interval_frequency'
))

How to query specific information from a JSON column?

I have searched Stack Overflow to get an answer to my question, but while I found many interesting cases, none of them quite address mine.
I have a column called fields in my data, that contains JSON information, such as presented below:
Row Fields
1 [{"label":"Label 1","key":"label_1","description":"Value of label_1"},{"label":"Label 2","key":"label_2","error":"Something"}]
2 [{"description":"something","label":"Row 1","key":"row_1"},{"label":"Row 2","message":"message_1","key":"row_2"}]
In essence, I have many rows of JSON that contain label and key, and bunch of other parameters like that. From every {}, I want to extract only label and key, and then (optional, but ideally) stretch every label and key in every {} to its own row. So, as a result, I would have the following output:
Row Label Key
1 Label 1 label_1
1 Label 2 label_2
2 Row 1 row_1
2 Row 2 row_2
Please note, contents of label and key within JSON can be anything (strings, integers, special characters, a mix of everything, etc. In addition, key and label can be anywhere in relation to other parameters within each {}.
Here is the Big Query SQL dummy data for convenience:
SELECT '1' AS Row, '[{"label":"Label 1","key":"label_1","description":"Value of label_1"},{"label":"Label 2","key":"label_2","error":"Something"}]' AS Fields
UNION ALL
SELECT '2' AS Row, '[{"description":"something","label":"Row 1","key":"row_1"},{"label":"Row 2","message":"message_1","key":"row_2"}]' AS Fields
I have first thought of using REGEX to isolate all the brackets and only show me information with label and key. Then, I looked into BQ Documentation of JSON functions and got very stuck on json_path parameters, specifically because their example doesn't match mine.
Consider below approach
select `row`,
json_extract_scalar(el, '$.label') label,
json_extract_scalar(el, '$.key') key
from your_table, unnest(json_extract_array(fields)) el
if applied to sample data in your question - output is

Snowflake Flatten Query for array

Snowflake Table has 1 Variant column and loaded with 3 JSON record. The JSON records is as follows.
{"address":{"City":"Lexington","Address1":"316 Tarrar Springs Rd","Address2":null} {"address":{"City":"Hartford","Address1":"318 Springs Rd","Address2":"319 Springs Rd"} {"address":{"City":"Avon","Address1":"38 Springs Rd","Address2":[{"txtvalue":null},{"txtvalue":"Line 1"},{"Line1":"Line 1"}]}
If you look at the Address2 field in the JSON , The first one holds NULL,2nd String and 3rd one array.
When i execute the flatten query for Address 2 as one records holds array, i get only the 3rd record exploded. How to i get all 2 records with exploded value in single query.
select data:address:City::string, data:address:Address1::string, value:txtvalue::string
from add1 ,lateral flatten( input => data:address:Address2 );
When I execute the flatten query for Address 2 as one records holds array, I get only the 3rd record exploded
The default behaviour of the FLATTEN table function in Snowflake will skip any columns that do not have a structure to expand, and the OUTER argument controls this behaviour. Quoting the relevant portion from the documentation link above (emphasis mine):
OUTER => TRUE | FALSE
If FALSE, any input rows that cannot be expanded, either because they cannot be accessed in the path or because they have zero fields or entries, are completely omitted from the output.
If TRUE, exactly one row is generated for zero-row expansions (with NULL in the KEY, INDEX, and VALUE columns).
Default: FALSE
Since your VARIANT data is oddly formed, you'll need to leverage conditional expressions and data type predicates to check if the column in the expanded row is of an ARRAY type, a VARCHAR, or something else, and use the result to emit the right value.
A sample query illustrating the use of all above:
SELECT
t.v:address.City AS city
, t.v:address.Address1 AS address1
, CASE
WHEN IS_ARRAY(t.v:address.Address2) THEN f.value:txtvalue::string
ELSE t.v:address.Address2::string
END AS address2
FROM
add1 t
, LATERAL FLATTEN(INPUT => v:address.Address2, OUTER => TRUE) f;
P.s. Consider standardizing your input at ingest or source to reduce your query complexity.
Note: Your data example is inconsistent (the array of objects does not have homogenous keys), but going by your example query I've assumed that all keys of objects in the array will be named txtvalue.

Count the SSRS Dataset rows after applying the filter

So I have a dataset in SSRS called "CRMDecisions". I am counting the number of rows for this Dataset using the following expression in a text-box outside of the tablix:
=CountRows("CRMDecisions")
Now, this dataset also has a number of duplicate records and i want to toggle between the unique counts and everything including the duplicate counts. I have used the below filter to hide the duplicate rows I don't want to see:
=IIF(Fields!Company.Value = Previous(Fields!Company.Value) AND
Fields!DaysAway.Value = Previous(Fields!DaysAway.Value) AND
Parameters!ShowPortfolio.Value = "False", True, False)
Right now, I get a count of all the rows no matter what filter view I am using. How can I get the count of the dataset after removing the duplicate rows?
For counting all the rows you can use (like you already did):
=CountRows("CRMDecisions")
For counting all the unique rows (without duplicate) you can use the following expression (note that you need put the field into the expression which holds the duplicates):
=CountDistinct(Fields!CompanyWithDuplicates.Value)
If you want some special counts you can always use this expression:
=Sum(IIF( 'Your True condition' , 1, 0))
=Sum(IIF(Fields!Company.Value = "MyCompany", 1, 0))

how to choose first row when use group by in web2py

I have a table in db. and when i use this code
results = db().select(db.project.ALL, orderby=db.project.id, groupby=db.project.status)
I can only choose the last row in repeats. how could i choose the first one?
The select() method will return a Rows object, which is a itterable collection of Row objects. The order of which rows will apear in your results variable will vary depending on your orderby parameter passed to your select() method. If you simply want to get a single record, which is the first one out of your select(), you could change the above code to:
results = db().select(db.project.ALL, orderby=db.project.id, groupby=db.project.status).first()
But if your intent is to reverse the order of which you're getting the rows in result, add the tilde operator (~) to your orderby parameter, which will result in adding a ORDER BY ____ DESC in relational databases. Eg:
results = db().select(db.project.ALL, orderby=~db.project.id, groupby=db.project.status)

Resources