Getting incorrect results in Qlikview Analyser - sql

I need help with a report I am trying to create with Olikview Analyser. The problem is I keep getting incorrect results. I think it may be related to the metrics being slightly wrong. The data source definitely has the correct figures, but when I am trying to run select queries results are not right. Here is an example. To see sales for Last Week Last Year (sales for Last week 2015)
Currently the Analyser code is as follows:
sum({$<DATE_WEEK={$(LastWeekLY)}, DATE_FYEAR {$(LastWeekYearLY)}>}POS_SALES_VALUE_GROSS)
+
sum({$<DATE_WEEK={$(LastWeekLY)}, DATE_FYEAR {$(LastWeekYearLY)}>}CS_SALES_VALUE_GROSS)
+
sum({$<DATE_WEEK={$(LastWeekLY)}, DATE_FYEAR={$(LastWeekYearLY)}>}CO_ITEM_TOTAL)
However, the figures I get are far too high. Are there any obvious errors in the code?
Thanks in advance for your help

Try
sum({$<DATE_WEEK={'$(LastWeekLY)'}, DATE_FYEAR={'$(LastWeekYearLY)'}>} numsum(POS_SALES_VALUE_GROSS,CS_SALES_VALUE_GROSS,CO_ITEM_TOTAL))
sometimes the weeks/years are stored as text and not numbers..so this will fix this issue.
If this does not work then you have a problem in your variables.

Please try
sum( {$< DATE_WEEK = {$(#LastWeekLY)} , DATE_FYEAR = {$(#LastWeekYearLY)} >} POS_SALES_VALUE_GROSS ))+
sum( {$< DATE_WEEK = {$(#LastWeekLY)} , DATE_FYEAR = {$(#LastWeekYearLY)} >} CS_SALES_VALUE_GROSS ))+
sum( {$< DATE_WEEK = {$(#LastWeekLY)} , DATE_FYEAR = {$(#LastWeekYearLY)} >} CO_ITEM_TOTAL))

Related

One Line of Code Breaks Otherwise Working Query

Everything in this query works just fine until I try to add the commented out portion (noted by '--') back into the query and run it. When I attempt to add the commented out WHERE clause back into the query body, it breaks the query. I have researched and experimented with adding the commented out line in different locations and in different ways, but nothing I have tried has worked so far.
SELECT
Count(DISTINCT a.acct_id),
CASE
WHEN b.user_seg = 'MTR' AND b.user_func = 'Sales' THEN 'MTR Sales'
WHEN b.user_seg = 'JMT' AND b.user_func = 'Sales' THEN 'JMT Sales'
THEN 'Partner Account Executive'
WHEN b.user_func IN ('Account Manager','Associate Account Manager','Outreach','Executive') THEN
'Customer Success'
WHEN b.user_func IN ('Relationship Manager') THEN 'Account Executive'
END AS sales_group
FROM qrs_access_views.mxp_sf_acct a
JOIN qrs_access_views.mxp_sf_user b
ON a.acc_crtd_by_id = b.user_id
AND a.is_sf_acct_del_y_n= 'n'
AND a.owner_id= '22573PAA3'
AND a.acct_mgr_user_id='22573PAA3'
AND b.user_seg <> '#'
--WHERE a.acc_crtd_date BETWEEN '2020-01-01' AND '2020-03-31'
LEFT JOIN grs_access_views.mxp_sf_user f
ON b.mgr_id = f.user_id
GROUP BY 2
When I run the query as above, I get a nice little grouped table kicked back to me by Teradata, but it is for all values for all time. (I just want to see the Q1 2020 numbers.)
When I add the commented out line back into the query I get some variation of the following error:
SELECT Failed. [3706] Syntax error: expected something between a string or a Unicode character literal and the 'LEFT' keyword.
Any help or assistance is greatly appreciated.
try this,
Instead of adding it to where clause use AND.
AND b.user_seg <> '#'
AND a.acc_crtd_date BETWEEN '2020-01-01' AND '2020-03-31'

Divide by zero error when converting Access to SQL Server query

I am trying to convert an Access query to one that works in SQL server. The original query in Access works perfectly well (just terribly slow).
I only changed things slightly to make it compatible with SQL server instead of Access, like changing "NOW()" to "GETDATE()" and we can no longer divide aliases.
Running this query in SQL Server:
SELECT batches.[price-group],
[development].verifier,
Count([development].company) AS SENT,
Sum([order] *- 1) AS ORDS,
Count([development].company) / Sum([order] *- 1) AS PCT
FROM [development]
INNER JOIN batches
ON [development].batch = batches.batch
WHERE (( ( [development].[mail-date] ) < Getdate() - 50 ))
GROUP BY batches.[price-group],
[development].verifier
HAVING (( ( batches.[price-group] ) = 'pgb' ))
ORDER BY batches.[price-group],
[development].verifier,
Count([development].company) DESC;
Returns this error:
Msg 8134, Level 16, State 1, Line 1 Divide by zero error encountered.
Only real change, was like I said, in Access we could do this
[ords] / [sent] AS PCT
Any help will be appreciated, I'm not sure exactly why it isn't working! Removing the converted line above, does work in SQL server without any errors.
Thank you!
Use NULLIF():
Count([development].company) / NULLIF(Sum([order] * -1), 0) AS PCT

Trying to do a simple expression in SSRS

I am trying to calculate the average price of an order. I have the following simple expression:
=( Max(Fields!Price, "Totals") - Max(Fields!Discount_Value.Value, "Totals") + Max(Fields!Tax_Value.Value, "Totals") + Max(Fields!Freight_Charges.Value, "Totals") - Max(Fields!Total_Freight_Cost.Value, "ShipCodesShipped") / Max(Fields!Total_Orders_Count.Value, "Totals"))
It does not seem to work at all and just shows up as #Error. Does anyone know what I'm doing wrong?
The first part of your expression
=( Max(Fields!Price, "Totals") ...
should be
=(Max(Fields!Price.Value, "Totals") ...

TOAD 10.6 Sql Error ORA - 01858: What is wrong with query?

Please help me identify the below issue. I have a canned query below and can't get it to run without getting this error:
SELECT * FROM TABLE(fdr_dal_txns.get_txn_trans_adjst_consol
(short_string_col('1BFV')
,'POST_DT'
,short_string_col('MCH','GP3', 'OTC')
,'01-may-2017'
,'30-june-2017'
))
WHERE trd_id_num IN ('17FHKBBSSML',
'17FHVBBRJD8')
What is obvious, is that you seem to be passing strings ('01-may-2017' is a string) where you should have passed dates. I'd suggest you to use date literals, such as
SELECT *
FROM TABLE (fdr_dal_txns.get_txn_trans_adjst_consol (
short_string_col ('1BFV'),
'POST_DT',
short_string_col ('MCH', 'GP3', 'OTC'),
DATE '2017-05-01', --'01-may-2017',
DATE '2017-06-30' --'30-june-2017'
))
WHERE trd_id_num IN ('17FHKBBSSML', '17FHVBBRJD8')
and see what happens. If it still doesn't help, you should provide much more details of what you're doing (because you told us close to nothing so far).

Comparing Date Values in Access - Data Type Mismatch in Criteria Expression

i'm having an issue comparing a date in an access database. basically i'm parsing out a date from a text field, then trying to compare that date to another to only pull newer/older records.
so far i have everything working, but when i try to add the expression to the where clause, it's acting like it's not a date value.
here's the full SQL:
SELECT
Switch(Isdate(TRIM(LEFT(bc_testingtickets.notes, Instr(bc_testingtickets.notes, ' ')))) = false, 'NOT ASSIGNED!!!') AS [Assigned Status],
TRIM(LEFT(bc_testingtickets.notes, Instr(bc_testingtickets.notes, ' '))) AS [Last Updated Date],
bc_testingtickets.notes AS [Work Diary],
bc_testingtickets.ticket_id,
clients.client_code,
bc_profilemain.SYSTEM,
list_picklists.TEXT,
list_picklists_1.TEXT,
list_picklists_2.TEXT,
list_picklists_3.TEXT,
bc_testingtickets.createdate,
bc_testingtickets.completedate,
Datevalue(TRIM(LEFT([bc_TestingTickets].[notes], Instr([bc_TestingTickets].[notes], ' ')))) AS datetest
FROM list_picklists AS list_picklists_3
RIGHT JOIN (list_picklists AS list_picklists_2
RIGHT JOIN (list_picklists AS list_picklists_1
RIGHT JOIN (bc_profilemain
RIGHT JOIN (((bc_testingtickets
LEFT JOIN clients
ON
bc_testingtickets.broker = clients.client_id)
LEFT JOIN list_picklists
ON
bc_testingtickets.status = list_picklists.id)
LEFT JOIN bc_profile2ticketmapping
ON bc_testingtickets.ticket_id =
bc_profile2ticketmapping.ticket_id)
ON bc_profilemain.id =
bc_profile2ticketmapping.profile_id)
ON list_picklists_1.id = bc_testingtickets.purpose)
ON list_picklists_2.id = bc_profilemain.destination)
ON list_picklists_3.id = bc_profilemain.security_type
WHERE ( ( ( list_picklists.TEXT ) <> 'Passed'
AND ( list_picklists.TEXT ) <> 'Failed'
AND ( list_picklists.TEXT ) <> 'Rejected' )
AND ( ( bc_testingtickets.ticket_id ) <> 4386 ) )
GROUP BY bc_testingtickets.notes,
bc_testingtickets.ticket_id,
clients.client_code,
bc_profilemain.SYSTEM,
list_picklists.TEXT,
list_picklists_1.TEXT,
list_picklists_2.TEXT,
list_picklists_3.TEXT,
bc_testingtickets.createdate,
bc_testingtickets.completedate,
DateValue(TRIM(LEFT([bc_TestingTickets].[notes], Instr([bc_TestingTickets].[notes], ' '))))
ORDER BY Datevalue(TRIM(LEFT([bc_TestingTickets].[notes], Instr([bc_TestingTickets].[notes], ' '))));
the value i'm trying to compare against a various date is this:
DateValue(Trim(Left([bc_TestingTickets].[notes],InStr([bc_TestingTickets].[notes],' '))))
if i add a section to the where clause like below, i get the Data Type Mismatch error:
WHERE DateValue(Trim(Left([bc_TestingTickets].[notes],InStr([bc_TestingTickets].[notes],' ')))) > #4/1/2012#
i've even tried using the DateValue function around the manual date i'm testing with but i still get the mismatch error:
WHERE DateValue(Trim(Left([bc_TestingTickets].[notes],InStr([bc_TestingTickets].[notes],' ')))) > DateValue("4/1/2012")
any tips on how i can compare a date in this method? i can't change any fields in the database, ect, that's why i'm parsing the date in SQL and trying to manipulate it so i can run reports against it.
i've tried googling but nothing specifically talks about parsing a date from text and converting it to a date object. i think it may be a bug or the way the date is being returned from the left/trim functions. you can see i've added a column to the end of the SELECT statement called DateTest and it's obvious access is treating it like a date (when the query is run, it asks to sort by oldest to newest/newest to oldest instead of A-Z or Z-A), unlike the second column in the select.
thanks in advance for any tips/clues on how i can query based on the date.
edit:
i just tried the following statements in my where clause and still getting a mismatch:
CDate(Trim(Left([bc_TestingTickets].[notes],InStr([bc_TestingTickets].[notes],' ')))) > #4/1/2012#
CDate(Trim(Left([bc_TestingTickets].[notes],InStr([bc_TestingTickets].[notes],' ')))) >
CDate("4/1/2012") CDate(DateValue(Trim(Left([bc_TestingTickets].[notes],InStr([bc_TestingTickets].[‌​notes],' '))))) > #4/1/2012#
i tried with all the various combinations i could think of regarding putting CDate inside of DateValue, outside, ect. the CDate function does look like what i should be using though. not sure why it's still throwing the error.
here's a link to a screenshot showing the results of the query http://ramonecung.com/access.jpg. there's two screenshots in one image.
You reported you get Data Type Mismatch error with this WHERE clause.
WHERE DateValue(Trim(Left([bc_TestingTickets].[notes],
InStr([bc_TestingTickets].[notes],' ')))) > #4/1/2012#
That makes me wonder whether [bc_TestingTickets].[notes] can ever be Null, either because the table design allows Null for that field, or Nulls are prohibited by the design but are present in the query's set of candidate rows as the result of a LEFT or RIGHT JOIN.
If Nulls are present, your situation may be similar to this simple query which also triggers the data type mismatch error:
SELECT DateValue(Trim(Left(Null,InStr(Null,' '))));
If that proves to be the cause of your problem, you will have to design around it somehow. I can't offer a suggestion about how you should do that. Trying to analyze your query scared me away. :-(
It seems like you are having a problem with the type conversion. In this case, I believe that you are looking for the CDate function.
A problem might be the order of the date parts. A test in the Immediate window shows this
?cdate(#4/1/2012#)
01.04.2012
?cdate(#2012/1/4#)
04.01.2012
Write the dates backwards in the format yyyy/MM/dd and thus avoiding inadverted swapping of days and months!
DateValue("2012/1/4")
and
CDate(#2012/1/4#)