Athena query results show null values despite is not null condition in query - presto

I have the following query which I run in Athena. I would like to receive all the results that contain a tag in the 'resource_tags_aws_cloudformation_stack_name'. However, when I run the query my results show me rows where the 'resource_tags_aws_cloudformation_stack_name' is empty and I don't know what I am doing wrong.
SELECT
cm.line_item_usage_account_id,
pr.line_of_business,
cm.resource_tags_aws_cloudformation_stack_name,
SUM(CASE WHEN cm.line_item_product_code = 'AmazonEC2'
THEN line_item_unblended_cost * 0.97
ELSE cm.line_item_unblended_cost END) AS discounted_cost,
CAST(cm.line_item_usage_start_date AS DATE) AS start_day
FROM cost_management cm
JOIN prod_cur_metadata pr ON cm.line_item_usage_account_id = pr.line_item_usage_account_id
WHERE cm.line_item_usage_account_id IN ('1234504482')
AND cm.resource_tags_aws_cloudformation_stack_name IS NOT NULL
AND cm.line_item_usage_start_date
BETWEEN date '2020-01-01'
AND date '2020-01-30'
GROUP BY cm.line_item_usage_account_id,pr.line_of_business, cm.resource_tags_aws_cloudformation_stack_name, CAST(cm.line_item_usage_start_date AS DATE), pr.line_of_business
HAVING sum(cm.line_item_blended_cost) > 0
ORDER BY cm.line_item_usage_account_id

I modified my query to exclude ' ' and that seems to work:
SELECT
cm.line_item_usage_account_id,
pr.line_of_business,
cm.resource_tags_aws_cloudformation_stack_name,
SUM(CASE WHEN cm.line_item_product_code = 'AmazonEC2'
THEN line_item_unblended_cost * 0.97
ELSE cm.line_item_unblended_cost END) AS discounted_cost,
CAST(cm.line_item_usage_start_date AS DATE) AS start_day
FROM cost_management cm
JOIN prod_cur_metadata pr ON cm.line_item_usage_account_id = pr.line_item_usage_account_id
WHERE cm.line_item_usage_account_id IN ('1234504482')
AND NOT cm.resource_tags_aws_cloudformation_stack_name = ' '
AND cm.line_item_usage_start_date
BETWEEN date '2020-01-01'
AND date '2020-01-30'
GROUP BY cm.line_item_usage_account_id,pr.line_of_business, cm.resource_tags_aws_cloudformation_stack_name, CAST(cm.line_item_usage_start_date AS DATE), pr.line_of_business
HAVING sum(cm.line_item_blended_cost) > 0
ORDER BY cm.line_item_usage_account_id

You can try space use case as below
AND Coalesce(cm.resource_tags_aws_cloudformation_stack_name,' ') !=' '
Or if you have multiple spaces try. The below query is not good if spaces required in actual data
AND Regexp_replace(cm.resource_tags_aws_cloudformation_stack_name,' ') is not null
Adding to this you may also have special char like CR or LF in data. Although its rare scenario

Related

Prefix DateTime variable

I am new to cognos 10.2 report studio at the moment.
I need to declare the prefix date time in my sql in order to make my union queries works.
I've tested a few datetime declarations but it seems not working and i keep getting the server returned an unrecognizable query framework response.
I've tried some of the codes which i found in some cognos forum as per shown in below.
Codes that i've tried
1. '1970-01-01T00:00:00.000000000' as invdate
2. todate(null) as invdate
/********** This below is my code ***********/
select
'fstgld' as wso,
0 as pono,
'nosh' as shpm,
'gld' as DocType,
0 as DocNo,
'gl' as item,
trim(tffst305.dim2) as ItemGroup,
tffst305.year as fy,
tffst305.perd as period,
'fst' as slsordtype,
'finbg' as finbg,
0 as Qty,
tffst305.leac as leac,
0 as Sales,
tffst305.fdah-tffst305.fcah as Cost
current_date as invdate <------this is the part where i keep getting error as i need to declare a prefix datetime
From tffst305
WHERE
tffst305.ptyp = 1 and
tffst305.budg ='ACT' and
tffst305.company_nr = 810
union all
select
cisli310.orno as wso,
cisli310.pono as pono,
cisli310.shpm as shpm,
cisli310.tran as DocType,
cisli310.idoc as DocNo,
cisli310.item as item,
tdsls411.citg as ItemGroup,
tfgld018.year as fy,
tfgld018.vprd as period,
cisli310.sotp as slsordtype,
tccom112.cfcg as finbg,
cisli310.dqua as Qty,
'inv' as leac,
cisli310.amth(1) as Sales,
0 as Cost,
cisli305.idat as invdate <--- extracted from the table field
From cisli310
RIGHT OUTER JOIN cisli305 ON cisli310.tran = cisli305.tran and
cisli310.idoc = cisli305.idoc
LEFT OUTER JOIN tdsls411 ON cisli310.orno=tdsls411.orno and
cisli310.pono=tdsls411.pono
LEFT OUTER JOIN tccom112 ON cisli305.ofbp = tccom112.itbp
inner join tfgld018 on cisli310.tran = tfgld018.ttyp and cisli310.idoc =
tfgld018.docn
WHERE
cisli310.sotp in ('SSP', 'SPL', 'SWK') and cisli310.amth(1) <>0 and
cisli305.company_nr=810 and
cisli310.company_nr=810 and
tdsls411.company_nr=810 and
tfgld018.company_nr=810 and
tccom112.company_nr=810
The field of the record is a datetime datatype such as 2009-07-03 03:08:03pm
Try replacing current_date with # timestampMask ( $current_timestamp , 'yyyy-dd-mm' ) # You can add other date or time portions as needed
timestampMask ( string_expression1 , string_expression2 )
Returns "string_expression1", representing a timestamp with time zone, trimmed to the format specified in "string_expression2".
The format in "string_expression2" must be one of the following: 'yyyy', 'mm', 'dd', 'yyyy-mm', 'yyyymm', 'yyyy-mm-dd', 'yyyymmdd', 'yyyy-mm-dd hh:mm:ss', 'yyyy-mm-dd hh:mm:ss+hh:mm', 'yyyy-mm-dd hh:mm:ss.ff3', 'yyyy-mm-dd hh:mm:ss.ff3+hh:mm', 'yyyy-mm-ddThh:mm:ss', 'yyyy-mm-ddThh:mm:ss+hh:mm', 'yyyy-mm-ddThh:mm:ss.ff3+hh:mm', or 'yyyy-mm-ddThh:mm:ss.ff3+hh:mm'.The macro functions that return a string representation of a timestamp with time zone show a precision of 9 digits for the fractional part of the seconds by default. The format options allow this to be trimmed down to a precision of 3 or 0.

How concatenating selective query results as string in Neo4j?

Initial situation
I’ve written a working Cypher query, which returns four distinct quantities.
MATCH
<complex satement>
WITH
count(DISTINCT typeA) AS amountA,
count(DISTINCT typeB) AS amountB,
count(DISTINCT typeC) AS amountC,
count(DISTINCT typeD) AS amountD
RETURN
amountA, amountB, amountC, amountD;
Target solution
Instead of a four-column table I want to return a single text string now, where all four quantities are concatenated including a descriptive label. However the quantity must only be part of the string, if its amount is greater than zero.
╒════════════════════════════════════════════════════╕
│"formattedQuantities" │
╞════════════════════════════════════════════════════╡
│"amountA: 123456, amountC: 9876543, amountD: 2018" │
└────────────────────────────────────────────────────┘
(Because the value of amountB is 0, it is omitted in the result.)
I use this Cyper query for several millions of rows. Because of a performance impact apprehension I don’t want to create and call a custom plugin.
So, how can I return the quantities as string with Cypher and Neo4j? Can you please give me an advice how to solve this challenge? Many thanks in advance for pointing me into the right direction!
approach to the problem / preliminary result
Cypher statement:
MATCH
<complex satement>
WITH
count(DISTINCT typeA) AS amountA,
count(DISTINCT typeB) AS amountB,
count(DISTINCT typeC) AS amountC,
count(DISTINCT typeD) AS amountD
WITH
['amountA: ', amountA, ', amountB: ', amountB, ', amountC: ', amountC, ', amountD: ', amountD] AS quantities
RETURN
reduce(result = toString(head(quantities)), n IN tail(quantities) | result + n) AS formattedQuantities;
Result:
╒═════════════════════════════════════════════════════════════════╕
│"formattedQuantities" │
╞═════════════════════════════════════════════════════════════════╡
│"amountA: 123456: 1, amountB: 0, amountC: 9876543, amountD: 2018"│
└─────────────────────────────────────────────────────────────────┘
Still open:
filtering of amountB because of value 0
You want to use the FILTER function
MATCH
<complex satement>
WITH
count(DISTINCT typeA) AS amountA,
count(DISTINCT typeB) AS amountB,
count(DISTINCT typeC) AS amountC,
count(DISTINCT typeD) AS amountD
// Reformat to list
WITH
[{name:'amountA', value:amountA}, {name:'amountB', value:amountB}, {name:'amountC', value:amountC}, {name:'amountD', value:amountD}] AS quantities
// Filter out 0's
WITH filter(x IN quantities WHERE x.value > 0) AS quantities
// Convert list to string
RETURN
reduce(result = quantities[0].name + ": " + quantities[0].value, n IN tail(quantities) | result + ", " + n.name + ": " + n.value) AS formattedQuantities; AS formattedQuantities;
Note that this returns null if all values are 0 (null + string = null)

Remove decimal places from varchar(32) result

I'm using SQL and windows batch script to download inventory from our POS and then upload it to a 3rd party platform. The file is successfully downloading and uploading, but the 3rd party platform is quite finicky on formatting. Specifically, it won't accept decimal place for the column titled "Quantity".
I've searched and tried various different approaches but can't seem to find one that works. The tricky aspect to this sql is that i had to use a join in order to create a title row and I'm using the format varchar(32) I've posted my sql below, any suggestions?
set nocount ON
SELECT CAST('sku' as VARCHAR(32)) AS sku,
CAST('quantity' as VARCHAR(32)) AS quantity
UNION
SELECT CAST(IM_BARCOD.BARCOD AS
VARCHAR(32)) as sku, case when
IM_INV.QTY_AVAIL > 0 then
CAST(IM_INV.QTY_AVAIL AS VARCHAR(32)) else
CAST(0 as VARCHAR(32)) END as quantity
FROM IM_BARCOD INNER JOIN IM_INV ON
IM_INV.ITEM_NO = IM_BARCOD.ITEM_NO INNER
JOIN IM_PRC ON IM_INV.ITEM_NO =
IM_PRC.ITEM_NO INNER JOIN
IM_ITEM ON IM_INV.ITEM_NO = IM_ITEM.ITEM_NO
UNION
SELECT CAST(IM_BARCOD.BARCOD AS
VARCHAR(32)) as sku, case when
IM_INV_CELL.QTY_AVAIL > 0 then
CAST(IM_INV_CELL.QTY_AVAIL AS VARCHAR(32))
else CAST (0 as VARCHAR (32)) END as
quantity FROM IM_BARCOD INNER JOIN IM_PRC
ON IM_BARCOD.ITEM_NO = IM_PRC.ITEM_NO INNER
JOIN IM_INV_CELL ON IM_BARCOD.ITEM_NO =
IM_INV_CELL.ITEM_NO AND
IM_INV_CELL.DIM_1_UPR=IM_BARCOD.DIM_1_UPR
AND IM_INV_CELL.DIM_2_UPR =
IM_BARCOD.DIM_2_UPR AND
IM_INV_CELL.DIM_3_UPR =IM_BARCOD.DIM_3_UPR
INNER JOIN
IM_ITEM ON IM_BARCOD.ITEM_NO =
IM_ITEM.ITEM_NO

Count null columns as zeros with oracle

I am running a query with Oracle:
SELECT
c.customer_number,
COUNT(DISTINCT o.ORDER_NUMBER),
COUNT(DISTINCT q.QUOTE_NUMBER)
FROM
Customer c
JOIN Orders o on c.customer_number = o.party_number
JOIN Quote q on c.customer_number = q.account_number
GROUP BY
c.customer_number
This works beautifully and I can get the customer and their order and quote counts.
However, not all customers have orders or quotes but I still want their data. When I use LEFT JOIN I get this error from Oracle:
ORA-24347: Warning of a NULL column in an aggregate function
Seemingly this error is caused by the eventual COUNT(NULL) for customers that are missing orders and/or quotes.
How can I get a COUNT of null values to come out to 0 in this query?
I can do COUNT(DISTINCT NVL(o.ORDER_NUMBER, 0)) but then the counts will come out to 1 if orders/quotes are missing which is no good. Using NVL(o.ORDER_NUMBER, NULL) has the same problem.
Try using inline views:
SELECT
c.customer_number,
o.order_count,
q.quote_count
FROM
customer c,
( SELECT
party_number,
COUNT(DISTINCT order_number) AS order_count
FROM
orders
GROUP BY
party_number
) o,
( SELECT
account_number,
COUNT(DISTINCT quote_number) AS quote_count
FROM
quote
GROUP BY
account_number
) q
WHERE 1=1
AND c.customer_number = o.party_number (+)
AND c.customer_number = q.account_number (+)
;
Sorry, but I'm not working with any databases right now to test this, or to test whatever the ANSI SQL version might be. Just going on memory.

ERROR: COALESCE types timestamp without time zone and integer cannot be matched (Postgresql)

## PROBLEM ##
I got error from this script (Postgresql 9.3.2)
( It's OK in MS SQL Server )
SELECT
CASE COALESCE(my_date_field,0)
WHEN 0 THEN 0
ELSE 1 END
AS status
FROM
my_table
Error :COALESCE types timestamp without time zone and integer cannot be matched
Line 2 : CASE COALESCE(my_date_field,0)
## SOLVED ##
SELECT
CASE WHEN my_date_field IS NULL
THEN 0 ELSE 1 END
AS status
FROM
my_table
COALESCE accepts pretty much any number of parameters, but they should be the same data-type.
I quoted from COALESCE Function in TSQL
Zero is not a valid date. It's surprising that it works in MS SQL. You need to use a sensible date, or accept NULL.
CASE COALESCE(my_date_field, DATE '0001-01-01')
The query is a bit bizarre in general. Isn't that an incredibly longwinded and complicated way of writing IS NULL?
SELECT
my_date_field IS NULL AS status
FROM
my_table
If, per comment, you want 0 or 1, use:
SELECT
CASE WHEN my_date_field IS NULL THEN 1 ELSE 0 END AS status
FROM
my_table

Resources