Wildcard Parameter CR SQL Query - sql

The following code I have written works find. Outputs what I expect.
select distinct
mi_tm.st_event_time,
mi_tm.st_location,
mi_tm.st_log_class,
mi_tm.st_qty,
mi_tm.st_reason,
mi_tm.st_reason_data,
mi_tm.st_sku_code,
mi_tm.st_ulid,
mi_tm.st_user_id,
x_prod.description,
x_supplier.supplier_name
from
mi_tm, x_prod,
x_igd, x_supplier
where
mi_tm.st_date >= {?Start} and
mi_tm.st_date <= {?End} and
mi_tm.st_sku_code = x_prod.prod_code (+) and
mi_tm.st_sku_code = x_igd.prod_code (+) and
x_igd.supplier_no = x_supplier.supplier_no (+) and
mi_tm.st_sku_code like '{?SKU}' and
mi_tm.st_log_class in ('TM_ADJUST_QTY', 'TM_CREATE', 'TM_DELETE')
However for this line
mi_tm.st_sku_code like '{?SKU}' and
I would like it to be able to expect a wildcard parameter, that will return all values run between the dates. Just querying a % seems to crash my report. and I have tried adding '%' around the parameter. But I either get an invalid number error, or SQL not ended properly. I would be grateful for any help on this issue.
Edit: Place this at the bottom
If {?SKU} = '*' then
mi_tm.st_sku_code like '*'
else mi_tm.st_sku_code like '{?SKU}'

Related

SQL CASE after in WHERE condition with single or multiple where results

I have the following code running in part of excel data query getting information from excel
`
DateTable.FiscalYear IN (?-1,?)
AND DateTable.Week = ?
AND ProductInformation.Brand = 'Drunk Elephant'
AND StoreInformation.Retailer IN CASE
WHEN ? = 'Boots' THEN ('Boots')
WHEN ? = 'Cult Beauty' THEN ('Cult Beauty')
WHEN ? = 'DTC' THEN ('DTC')
WHEN ? = 'Look Fantastic' THEN ('Look Fantastic')
WHEN ? = 'Space NK' THEN ('Space NK')
ELSE (SELECT
DISTINCT StoreInformation.Retailer
FROM
Sellout
INNER JOIN
ProductInformation
ON
Sellout.EAN = ProductInformation.EAN
INNER JOIN
StoreInformation
ON
sellout.StoreNumber = StoreInformation.StoreNumber
WHERE
ProductInformation.Brand = 'Drunk Elephant')
END
`
The ? are referencing cells in excel to bring back values. The code was running originally running fine with = CASE, but the I had to add in the final else clause, which brings back multiple results, so = no longer works. And putting the IN causes it to break, even if I remove the else and have just IN CASE.
Any ideas how to can run this case in the where clause, working if the result of the CASE is either a single value or a sub-query table?
Thanks in advance!
I have tried it with = CASE and without the ELSE, which work. Adding the ELSE brings back an error saying subquery brings back more then 1 value, which is why i tried with IN instead of =.
But having just IN without the ELSE also causes an error.

How to write an Open SQL statement with substring in the JOIN ON condition? [duplicate]

I have the following select statement in ABAP:
SELECT munic~mandt VREFER BIS AB ZZELECDATE ZZCERTDATE CONSYEAR ZDIMO ZZONE_M ZZONE_T USAGE_M USAGE_T M2MC M2MT M2RET EXEMPTMCMT EXEMPRET CHARGEMCMT
INTO corresponding fields of table GT_INSTMUNIC_F
FROM ZCI00_INSTMUNIC AS MUNIC
INNER JOIN EVER AS EV on
MUNIC~POD = EV~VREFER(9).
"where EV~BSTATUS = '14' or EV~BSTATUS = '32'.
My problem with the above statement is that does not recognize the substring/offset operation on the 'ON' clause. If i remove the '(9) then
it recognizes the field, otherwise it gives error:
Field ev~refer is unknown. It is neither in one of the specified tables
nor defined by a "DATA" statement. I have also tried doing something similar in the 'Where' clause, receiving a similar error:
LOOP AT gt_instmunic.
clear wa_gt_instmunic_f.
wa_gt_instmunic_f-mandt = gt_instmunic-mandt.
wa_gt_instmunic_f-bis = gt_instmunic-bis.
wa_gt_instmunic_f-ab = gt_instmunic-ab.
wa_gt_instmunic_f-zzelecdate = gt_instmunic-zzelecdate.
wa_gt_instmunic_f-ZZCERTDATE = gt_instmunic-ZZCERTDATE.
wa_gt_instmunic_f-CONSYEAR = gt_instmunic-CONSYEAR.
wa_gt_instmunic_f-ZDIMO = gt_instmunic-ZDIMO.
wa_gt_instmunic_f-ZZONE_M = gt_instmunic-ZZONE_M.
wa_gt_instmunic_f-ZZONE_T = gt_instmunic-ZZONE_T.
wa_gt_instmunic_f-USAGE_M = gt_instmunic-USAGE_M.
wa_gt_instmunic_f-USAGE_T = gt_instmunic-USAGE_T.
temp_pod = gt_instmunic-pod.
SELECT vrefer
FROM ever
INTO wa_gt_instmunic_f-vrefer
WHERE ( vrefer(9) LIKE temp_pod ). " PROBLEM WITH SUBSTRING
"AND ( BSTATUS = '14' OR BSTATUS = '32' ).
ENDSELECT.
WRITE: / sy-dbcnt.
WRITE: / 'wa is: ', wa_gt_instmunic_f.
WRITE: / 'wa-ever is: ', wa_gt_instmunic_f-vrefer.
APPEND wa_gt_instmunic_f TO gt_instmunic_f.
WRITE: / wa_gt_instmunic_f-vrefer.
ENDLOOP.
itab_size = lines( gt_instmunic_f ).
WRITE: / 'Internal table populated with', itab_size, ' lines'.
The basic task i want to implement is to modify a specific field on one table,
pulling values from another. They have a common field ( pod = vrefer(9) ). Thanks in advance for your time.
If you are on a late enough NetWeaver version, it works on 7.51, you can use the OpenSQL function LEFT or SUBSTRING. Your query would look something like:
SELECT munic~mandt VREFER BIS AB ZZELECDATE ZZCERTDATE CONSYEAR ZDIMO ZZONE_M ZZONE_T USAGE_M USAGE_T M2MC M2MT M2RET EXEMPTMCMT EXEMPRET CHARGEMCMT
FROM ZCI00_INSTMUNIC AS MUNIC
INNER JOIN ever AS ev
ON MUNIC~POD EQ LEFT( EV~VREFER, 9 )
INTO corresponding fields of table GT_INSTMUNIC_F.
Note that the INTO clause needs to move to the end of the command as well.
field(9) is a subset operation that is processed by the ABAP environment and can not be translated into a database-level SQL statement (at least not at the moment, but I'd be surprised if it ever will be). Your best bet is either to select the datasets separately and merge them manually (if both are approximately equally large) or pre-select one and use a FAE/IN clause.
They have a common field ( pod = vrefer(9) )
This is a wrong assumption, because they both are not fields, but a field an other thing.
If you really need to do that task through SQL, I'll suggest you to check native SQL sentences like SUBSTRING and check if you can manage to use them within an EXEC_SQL or (better) the CL_SQL* classes.

Error: unexpected symbol in " ON a.guest_id"

sqldf('Select a.guest_id,case when b.guest_id is not null then 'old' else 'new' end as tagging from JDUniqueGuestid as a
left join UniqueGuestidallsource b
ON a.guest_id = b.guest_id', drv="SQLite")
After running the above code getting below mentioned error, kindly help me and resolve the issue
Error: unexpected symbol in " ON a.guest_id"
You have single quotes around the whole query, but also single quotes within the query; it's not being parsed the way you intend it to.
Depending on the larger context, something like this might work:
"Select a.guest_id, case ... 'old' else 'new' ... ON a.guest_id = b.guest_id"
or you might need to escape the single quotes with something like this:
'Select a.guest_id, case ... \'old\' else \'new\' ... ON a.guest_id = b.guest_id'
It depends on the context in which that query string appears, and how it parses quoted strings.

Excel MS Query - How to write parameter equals "" or show all in SQL query

Just finished writing an SQL script in the MS-Query and I'm having difficulty trying to get it to work.
What I'm after is the equivalent of this SQL where clause:
AND ((examplefield = #Para) or (#Para = ''))
So if parameter = something in the field, only show that or if the parameter = blank then show all results.
So far this is what I have which works fine if I want to select a particular item, now I just need to include the additional if blank show all.
AND (`'Project Master List$'`.`Type of Work`= ?)
This unfortunately doesn't work.
AND ((`'Project Master List$'`.`Type of Work`= ?) OR (? = ""))
Any suggestions?
Try a case?
Case When ?="" THEN
'docode'
WHEN ?="OtherValue" THEN
'DoCode'
Else
'DoCode '
End
The IIF Example:
iif(?="",
iif(?="OtherValue",ReturnSomethingTrue,ReturnSomethingFalse)
,ReturnSomethingTrue,ReturnsomethingFalse)
I looked back at my original question and realised I wrapped the where clause in the wrong quotes. See below
Wrong statement
AND ((`'Project Master List$'`.`Type of Work`= ?) OR (? = ""))
Correct statement
AND (`'Project Master List$'`.`Type of Work`= ? OR ? = '')
Once I'd made the change right at the end of the clause it worked.

MySQL conditional statement

Alright, so I have a query that looks like this:
SELECT
`orders`.*,
GROUP_CONCAT(
CONCAT(
`menu_items`.`name`,
' ($',
FORMAT(`menu_items`.`price`,2),
')'
) SEPARATOR '<br>'
) as `items`,
SUM(`menu_items`.`price`) as `additional`,
`children`.`first_name`,
`children`.`last_name`,
`organizations`.`base_price`
FROM
`orders`, `order_items`, `menu_items`, `children`, `organizations`
WHERE
`order_items`.`menu_item_id` = `menu_items`.`id` AND
`order_items`.`order_id` = `orders`.`id` AND
`orders`.`added_by` = {$user_id} AND
`orders`.`date` > '{$cutoff}' AND
`children`.`id` = `orders`.`child_id` AND
`organizations`.`id` = `children`.`organization_id`
GROUP BY
`orders`.`id`
I know it's a monstrosity and that some people will die before not using explicit joins. Ignoring that, however, what I wish to do is to only use the CONCAT inside the GROUP_CONCAT if the menu_items.price is greater than 0, otherwise only return menu_items.name. I have had, however, no success trying to throw an IF in there. I've read the manual but all the ways that I've tried aren't working and I'm pretty sure I'm missing something on the whole conditional statements thing.
Have you tried using something like this?
CASE WHEN 'menu_items'.'price' = 0 THEN 'menu.items'.'name' ELSE CONCAT (etc) END
Replacing the CONCAT statement of course.
Something like this should work (but I didn't test it, sorry):
GROUP_CONCAT(
CONCAT(
`menu_items`.`name`,
IF(`menu_items`.`price` > 0, -- <condition>
CONCAT(' ($', FORMAT(`menu_items`.`price`,2), ')'), -- <true-expr>
'' -- <false-expr>
)
)
SEPARATOR '<br>'
) as `items`,
The IF() function is really simple:
IF( <condition>, <true-expr>, <false-expr> )
The function has three arguments: the first is <condition>. If the condition evaluates to true, the function returns the result of <true-expr>. Else the function returns the result of <false-expr>.
Things get harder to get right when you use really long, multi-line expressions that contain parentheses and commas and so on. You just have to do it carefully. I suggest starting with more simple expressions and then build up.