Postgres return JSON element - json

I have a JSON field ('ez_links') in a table ('bookings') and I wish to return a particular attribute/element ('Amount'), which represents the price of the booking.
In the example below, I want to return the value of '662', but NULL is returned.
select ez_links, ez_links-> 'Amount' AS price
FROM bookings
limit 1
Result:
ez_links
price
{"integrator": "ezlinks", "TeeTimeReserveRecord": {"Fee": "Troon App Member", "Amount": "662",...........................................}}
NULL
Thanks for your help.

This is a nested JSON,
you can do the following to access the Amount field
SELECT ez_links->'TeeTimeReserveRecord'->'Amount' as price FROM bookings
limit 1 ;
I hope this was helpful.

Related

SQL avg of comma occurrencies with condition

I have a table of events where partecipants are listed as a comma separated list of IDs:
0,4,21,33,41
I'm trying to perform the following query to retrieve the average partecipants number for event only when status is equal to 1.
I prepared the following but doesn't work, anyone can help me?
SELECT avg(case when (status = 1 then LENGTH(REPLACE(listofPartecipants, ',', '')) end) avgPartecipants FROM events;
Many thanks
This is the expression if you want to count the number of elements in a list:
SELECT avg(case when status = 1
then LENGTH(REPLACE(listofPartecipants, ',', 'XX')) - length(listofPartecipants) + 1
end) as avgPartecipants
FROM events;
More important than getting this arcane string logic right is fixing your data model. You should not be storing list of ids in a comma-delimited list. You should have a table with one row per event and per participant.

My select sum query returns 0 even though I have values in my database

$this->db->select_sum('quantity');
$query = $this->db->get('products');
return $query->result();
I don't get it why my code returns a value of 0 even though my column has values in it and when I try putting price instead of quantity it works.
Database structure:
Name Type Null Default
price decimal(25,4) No None
quantity decimal(15,4) Yes 0.0000
Database:
id name price quantity
1 Sample1 10.0000 0.0000
2 Sample2 0.0000 45.0000
You can try this code.
$this->db->select_sum('quantity', 'qty');
$query = $this->db->get('products');
return $query->row();
Add this to your views.
$total_quantity = $this->yourmodel->modelfunction()->qty;
If still can't run, you need to provide more data for us like your views and table examples. You need to give more data for ask, if you won't others see your data. You can give an example with same data.

Sorting MySQL query by a function instead of a column

If my website display the products price as the output of a function display_price(), how can I sort the query by price ?
e.g.
Product A price is 100 and base currency is USD = 100USD Product B
price is 95 and base currency is EU = 103USD
Order by price ASC; will list Product B then A, but I use this function to output the correct price:
Function display_price($products_price, $currency_value) {
$price = $products['price'] * $products['currency_value'];
return $price;
}
How can I sort my query in this situation ?
Thank you
This implies that you have a simple calculated value in your queried data:
$price = $products['price'] * $products['currency_value'];
So calculate that value in the query itself, not in PHP. Assuming the structure of your query, you'd add a calculated column:
SELECT
some_field,
some_other_field,
and_so_on,
price,
currency_value,
price * currency_value AS product_price -- add the calculated value
FROM
some_table
ORDER BY
product_price -- sort by the calculated value
No need to involve PHP at all. As far as PHP is concerned, the result of the query includes the values it needs and is in the order it needs to be.

calculate percentage of distinct records depending on its 'status' using stored procedure

I have a table site_level,I need to work on columns named 'market',status, id.
for column market , I want to calculate percentage of id depending on its status as completion or rejection.
id column may have duplicate rows.so i need distinct rows.
Ex. market 'germany' will have several id's with status accepted or rejected.
i want to calculate percentage like (total distinct id for market germany)/ (distinct id with status as accepted for germany)
I need the query result in below format.
market / count__id_accepted / count_id_rejected/ percentage_accepted / percentage_rejected
select result1.market AS market,count(result1.unique_id) AS distinct unique_id,count(result1.status='accepted' or null) as'count_acc',count(result1.status='accepted' or null)*100/count(result1.unique_id) as 'perc_accepted',count(result1.status='rejected' or null) as 'count_rej',count(result1.status='rejected' or null)*100/count(result1.unique_id) as 'perc_rejected',
count(result1.status='to be performed' or null) as 'count_tbp',count(result1.status='to be performed' or null)*100/count(result1.unique_id) as 'perc_tbp',count(result1.status='incomplete test' or null) as 'count_inco',
count(result1.status='incomplete test' or null)*100/count(result1.unique_id) as 'perc_inco' from result1
group by result1.market

What is MySQL equivalent of MS Access LAST()?

I have no idea what the following query means as I do not have any exposure to MS Access. I need to convert it into MySQL and I can't figure out what does the LAST() does
SELECT
Containers.Container_No,
Max(Containers.LastOfContainer_Date),
Max(Containers.LastOfContainer_Time),
Last(Containers.LastOfETD),
Last(Containers.LastOfContainer_Status),
Last(Containers.LastOfPickup_From),
Last(Containers.LastOfPickup_To),
Last(Containers.LastOfConsignee_Name),
Last(Containers.LastOfContract_No),
Last(Containers.LastOfSeaLNo)
FROM
Containers
WHERE
Containers.Customer_Name = 'value here"
AND
Containers.LastOfContainer_Date >='Date here'
AND
Containers.LastOfContainer_Date <= 'Date here'
GROUP BY
Containers.Container_No
Can some one explain me what does this LAST() actually do?
UPDATE
It seems the query can be changed to following:
SELECT
Containers.Container_No,
Max(Containers.LastOfContainer_Date),
Max(Containers.LastOfContainer_Time),
Containers.LastOfETD,
Containers.LastOfContainer_Status,
Containers.LastOfPickup_From,
Containers.LastOfPickup_To,
Containers.LastOfConsignee_Name,
Containers.LastOfContract_No,
Containers.LastOfSeaLNo
FROM
Containers
WHERE
Containers.Customer_Name = '".$customername."'
AND
Containers.LastOfContainer_Date >='".$fromdate."'
AND
Containers.LastOfContainer_Date <= '".$todate."'
GROUP BY
Containers.Container_No
order by
Containers.LastOfETD,
Containers.LastOfContainer_Status,
Containers.LastOfPickup_From,
Containers.LastOfPickup_To,
Containers.LastOfConsignee_Name,
Containers.LastOfContract_No,
Containers.LastOfSeaLNo
The Last function returns the last value on a given query, for instance I assume that in the query you are using it would return the data of the last transaction made by that certain customer,
for instance lets say Mike Jagger bought 2things today, he will have 2 transactions with the same date, if you use the last function you would get the latest information that was inserted.
ID | Date | Product
1 12/12/2011 socks
2 12/12/2011 shirt
select last(product)
from buys;
it would output : shirt
BTW to emulate this behavior you might want to use
ORDER BY product DESC LIMIT 1
According to this page, "The Last function returns the last value from the result set of a query."
You might have to do some kind of ORDER BY x DESC LIMIT 1.