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.
Related
I have an optional parameter in my presto SQL query I tried many articles but none has worked for me.
I need something like this:
on c.portfolio_code = x.portfolio_code
/* this is a conditional parameter, this can be a null */
and c.portfolio_code = coalesce( '{p_portfolio_code}', x.portfolio_code)
when I try ISNULL( '{p_portfolio_code}', x.portfolio_code) its saying unrecognized function error
and (c.portfolio_code = ‘{p_portfolio_code}’ or ‘{p_portfolio_code}’ is null)
and (c.portfolio_code = ‘{p_portfolio_code}’ or ‘{p_portfolio_code}’ = ‘’)
Please help I tried many approaches but of no use.
and case '{p_portfolio_code}' when 'null' then 1=1 else Portfolio_Code = '{p_portfolio_code}' end
I tried the above and it resolved my issue, when no Parameter is passed, I checked it with 'null' and it worked.
Thanks Guru Stron for your help
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}'
I have a below SQL query running in one of my project. I am struggling to understand the "as" concept here. In the result "user_key" and "user_all" are appearing as empty. Where as at the front end "user_all" is the combination of "rx.ord_by_userid" + "rx.ord_by_inst_id,"
SELECT rx.rx_id,
rx.pt_visit_id,
rx.pt_id,
pt_visit.date_time_sch,
' ' as print_dea_ind,
' ' as phys_rx_label,
rx.ord_by_userid,
rx.ord_by_inst_id,
' ' as user_key,
pt_visit.visit_inst_id,
' ' as user_all,
' ' as tp_agt_ind,
FROM rx LEFT OUTER JOIN tx_pln ON rx.tp_name = tx_pln.tp_name AND rx.tp_vers_no = tx_pln.tp_vers_no, pt_visit
WHERE ( pt_visit.pt_visit_id = rx.pt_visit_id ) and
( pt_visit.pt_id = rx.pt_id ) and
( ( rx.pt_id = :pt_id ) and
( rx.rx_id = :rx_id ) )
Thanks.
I think when they query database, they need two fields called "user_key" and "user_all" with empty value for some purpose. However, in the front end, they need to display column "user_all" with the combination of "rx.ord_by_userid" + "rx.ord_by_inst_id" because of business rule.
The meaning of "AS" is just setting the alias of any field which is needed to have a new name. In this situation, new columns "user_key" and "user_all" are set with empty value.
AS just provides the field in the data set a name, or in SQL terms, an alias. In PB, this is usually done so that the DataWindow gives it a consistent, easy name. That is all that AS does.
The other part of your mystery is how these get populated with non-blank values. You were assuming this was done in the SQL with AS, but we can assure you that is not the case. Most likely, this value is being set in a script that fires in the client after the Retrieve() (if I were to bet, I'd bet a script on the DataWindow control, maybe RetrieveRow or RetrieveEnd).
I have a query which returns a bunch of different data, however I want to have it replace all the values upon a certain condition.
What I have written below kind of gives me the result I want but not really. It creates a new column instead of replacing the other one:
SELECT
CASE
WHEN T4.[U_DestType] = '6'
THEN (SELECT
'Company Limited' AS [ShipToCode]
)
END AS [ShipToCode],
T2.[ShipToCode],
T6.[StreetS],
T6.[StreetNoS],
T6.[CityS],
T6.[ZipCodeS],
T6.[CountryS],
T5.[LicTradNum],
T2.[CardCode],
T4.[Phone1],
T4.[E_Mail],
T4.[U_DestType],
CASE
WHEN T4.[Country] = 'GB'
THEN 'EN'
ELSE T4.[Country]
END AS [Country],
T4.[U_ShortName]
FROM[...]
The end goal is to replace all of the columns with some preset values instead of just ShipToCode as above.
I tried putting an EXIST subquery after FROM too but that didn't work either.
Is this possible? I'm probably missing something very obvious.
Many thanks!
You can use an ELSE in your CASE expression to combine the two "columns":
CASE
WHEN T4.[U_DestType] = '6'
THEN (SELECT
'Company Limited' AS [ShipToCode]
)
ELSE T2.[ShipToCode]
END AS [ShipToCode],
And by the way, you didn't need to use a Sub-Select. This would work just as well and is easier to read:
CASE
WHEN T4.[U_DestType] = '6' THEN 'Company Limited'
ELSE T2.[ShipToCode]
END AS [ShipToCode],
Hoping this is something I can fix, or maybe someone can point out an obvious mistake.
I have two queries:
SELECT HIDEORSHOW FROM tblProspects WHERE PROSPECT_ID = 1261484;
HIDEORSHOW
1
SELECT PROSPECT_ID FROM tblProspects WHERE HIDEORSHOW = 1;
PROSPECT_ID
196248
199004
204190
204338
210918
211932
213332
214186
216980
218254
222420
223578
223824
224429
224390
224672
224714
227031
227481
228363
230040
238168
239230
240790
241409
243827
244553
245785
247947
248349
250426
250640
252399
252555
253610
253949
254641
255109
Sorry for the long list I just want you guys to see the madness. Is there any reason why this would happen? There is another prospect that I know of, 1257506, that has this HIDEORSHOW value and does not appear in the list.
I would lay good money that the HIDEORSHOW field is a string of some kind, and that some values have leading or trailing spaces.
This is true, due to implicit CASTing: '1' = 1 (Becomes : '1' = '1')
This is false, even with implicit CASTing: ' 1' = 1 (Becomes : ' 1' = '1')
To test this, try this query (or it's equivalent in your version of SQL)...
SELECT PROSPECT_ID FROM tblProspects WHERE CAST(HIDEORSHOW AS INT) = 1;
This will force the casting to be string => int rather than the other way around.
Or you could try this test...
SELECT '<' + HIDEORSHOW + '>', LEN(HIDEORSHOW) FROM tblProspects WHERE PROSPECT_ID = 1261484;
You'll then have more visibility on the exact value in that field.