The problem I am having is that my output is not being displayed correctly. For example, what should display 4.99 shows as 499. The amounts in my Stripe database are stored as BIGINT. I've tried using CAST but the decimal is input at the end of my values with trailing 00s and that is not what I want.
What I've tried:
SELECT COALESCE(card_address_country, card_country) AS "Country",
SUM(CAST(amount AS DECIMAL(19,2))) AS "Total Revenue"
FROM charges
WHERE amount_refunded = 0 and paid = true
GROUP BY COALESCE(card_address_country, card_country)
ORDER BY SUM(amount) DESC
$293,839,799 should be $2,938,397.99 and $499 = $4.99.
I've tried dividing the amounts by 100 but it results in a round whole number and the sum is no longer calculated correctly.
SELECT COALESCE(card_address_country, card_country) AS "Country",
SUM(amount/100) AS "Total Revenue"
FROM charges
WHERE amount_refunded = 0 and paid = true
GROUP BY COALESCE(card_address_country, card_country)
ORDER BY SUM(amount) DESC
Is what I am trying to achieve possible? Or am I just using the wrong functions? I was thinking it could be the way the database has been setup as well.
Related
I am working within the Stripe sigma application on Stripe's website and I am trying to calculate revenue by country. When customers fill in their payment information, they are asked to input their country but some fail to do so. The column is called card_country_address. When the payment is processed, the country where the card was issued is also recorded in a column called card_country.
When calculating the revenue, I am summing the amounts and grouping by card_country_address. However, because there are null values due to customers not filling in the correct information, I get 1 blank value in my card_address_country and an unassigned dollar amount. So, I would like to replace the null values with the value from card_country and include that into the calculation.
I read that I can replace the null values with COALESCE and I have written a statement to do so, however I am not sure how to include this correctly into a complete statement.
Here is what I have:
SELECT COALESCE(card_address_country, card_country)
FROM charges
WHERE card_address_country IS NULL
This seems to replace the null values correctly. The statement that I have to calculate the revenue by country is:
SELECT card_address_country AS "Country", sum(amount) AS "Total Revenue"
FROM charges
WHERE amount_refunded = 0 and paid = true
GROUP BY card_address_country
ORDER BY sum(amount) DESC
This is the problem I am having with this statement:
$12,934,033 isn't counted correctly because the card_address_country is null for numerous transactions.
I know some might be thinking, just group by card_country then. However, this column also contains null values and I would still have to do the same and add the missing country code from card_country_address. Also, card_address_country and card_country aren't always the same. I am using card_address_country because I would like to know where the customer purchased from.
How do I include the statement with COALESCE into the main statement so that I can distribute the amount linked to the null addressed correctly? Is that even the right way to go about this or is there a better method to replace and calculate the values?
Looks like GROUP BY COALESCE(card_address_country, card_country) will get the desired result set
SELECT COALESCE(card_address_country, card_country) AS "Country", SUM(amount) AS "Total Revenue"
FROM charges
WHERE amount_refunded = 0 and paid = true
GROUP BY COALESCE(card_address_country, card_country)
ORDER BY SUM(amount) DESC
I'm making a query to calculate the summation of total and amount columns but with the calculation of raw-materials and growth profit, I get the raw-materials summation from the items table that has the price in it and calculate the growth profit by subtracting calculated raw-materials from the amount,
The problem comes when I run the query it gives me wrong values. like sum of amount
sum(`payment`.`amount`) as total amount
should be like 1425 but the result is 107100, I don't know why it does this in the image below I showed the results, that number 107100 it way more than it should be please I’m new someone help me
This is my query
select `payment`.`date` as `Date`,
sum(`payment`.`total`) as `Total`,
sum(`payment`.`amount`) as `Paid amount`,
sum(items.price) as `R.M`,
sum(`payment`.`amount`) - sum(items.price) AS `G.P`,
`currennt_exp`.`expence` as `Current Expenses`,
sum(`payment`.`amount`)-sum(items.price) - `currennt_exp`.`expence` AS `Revenue`
from `payment`,`paymen_information`,`bill_information`,`bills`,`items`,`currennt_exp`,`appointments`
where `payment`.`id_payment` = `paymen_information`.`id_payment` and `paymen_information`.`id_bill` = `bills`.`id_bill` and bill_information.bills_id = `bills`.`id_bill` and bill_information.item_id = `items`.`id_item` and
date_format(`payment`.`date`,'%Y-%m-%d') = date_format(`currennt_exp`.`_date`,'%Y-%m-%d') and
date_format(`payment`.`date`, '%Y-%m-%d') = CURDATE()
and this is my table
total amount
135.0000 135.0000
165.0000 165.0000
375.0000 375.0000
300.0000 300.0000
450.0000 450.0000
and this is the result and its very wrong
I have a "transaction" table that has the following columns
ID TIMESTAMP USER ID DESCRIPTION AMOUNT REF_ID TYPE
The description column contains the payment platform used for example "STRIPE-ch_1745". We currently have 4 platforms all described in the reference as in the example above. What I want is to get the payment platform, the total amount processed by the platform and the count of transactions. Like this
Platform Amount Count
Stripe 100,000 78
iOS 78,000 50
My current code only gives me these values for one platform, I've been unable to structure this properly to give me the desired result. I assumed I needed nested select statements, so I wrote the code in that manner
SELECT txn_count, sum
FROM
(SELECT count(*) AS txn_count, sum(`transaction`.`amount`) AS `sum`
FROM `transaction`
WHERE (`transaction`.`type` = 'credit'
AND (`transaction`.`description` like 'stripe%')
AND str_to_date(concat(date_format(`transaction`.`timestamp`, '%Y-%m'), '-01'), '%Y-%m-%d') = str_to_date(concat(date_format(now(), '%Y-%m'), '-01'), '%Y-%m-%d'))) t1
What this gives me right now is
Txn Count Sum
311 501,000
Would appreciate some help on how to get the expected table
Try this : ( edited to remove the reference part, assuming the reference is always separated by the platform by '-' )
SELECT
LEFT(t.description,LOCATE('-',t.description) - 1) as 'Platform',
SUM(t.amount) as 'Amount',
COUNT(*) as 'Count'
FROM transaction t
GROUP BY Platform
i'm trying to find out the total amount of a particular customer in an inventory database.
the tables are :
customer, tax, invoice, line_no, branch, items, employee
i was able to calculate the total but i get multiple amounts as it calculates the amount for each tax rate in the database. i'm trying to restrict the amount depending on the branch issues it. i hope i made it clear, here is the query:
SELECT ROUND((SUM((LINE_NO.RETAIL_PRICE - LINE_NO.DISCOUNT)* LINE_NO.DEL_QTY)* (TAX.TAX_RATE))+
SUM((LINE_NO.RETAIL_PRICE - LINE_NO.DISCOUNT)* LINE_NO.DEL_QTY),2)
AS "TOTAL PAYMENT"
FROM LINE_NO, TAX, BRANCH
WHERE LINE_NO.INVOICE_INVOICE_NO IN (SELECT INVOICE.INVOICE_NO from INVOICE
WHERE(INVOICE.CUSTOMER_CUST_NO IN (SELECT CUST_NO from CUSTOMER where CUSTOMER.FNAME='JIM' )))
AND TAX.CITY = BRANCH.CITY
GROUP BY TAX.TAX_RATE;
GROUP BY should not operate on TAX_RATE here to avoid calculate total amount for each group (TAX_RATE).
SELECT ROUND((SUM((LINE_NO.RETAIL_PRICE - LINE_NO.DISCOUNT)* LINE_NO.DEL_QTY)* (TAX.TAX_RATE))+
SUM((LINE_NO.RETAIL_PRICE - LINE_NO.DISCOUNT)* LINE_NO.DEL_QTY),2)
AS "TOTAL PAYMENT"
FROM LINE_NO, TAX, BRANCH
WHERE LINE_NO.INVOICE_INVOICE_NO IN (SELECT INVOICE.INVOICE_NO from INVOICE
WHERE(INVOICE.CUSTOMER_CUST_NO IN (SELECT CUST_NO from CUSTOMER where CUSTOMER.FNAME='JIM' )))
AND TAX.CITY = BRANCH.CITY;
exactly. yes the rate can be retrieved from the branch location, which can be tracked by the Invoice number. so i added another query after:
AND TAX.CITY = BRANCH.CITY
which helped to determine the tax rate i'm looking for and got it working, thanks anyways
I am using the following query to create a report. My problem comes from the fact that I need to use COUNT(market) for the year to date (total) of each 'market' then I also need a COUNT of 'market' for only the user imputed month ($mon). I cannot find any reference to using a WHERE on only a single selected field, everything else works as intended giving me the market with the percentages and year to date totals.
I need to make the line "COUNT(market) AS Saves" only count markets who's month field equals $mon and everything else to stay as is.
SELECT market,
COUNT(market) AS Saves,
COUNT(market) / (SELECT COUNT(*) FROM savedata2013) * 100 AS Percent,
COUNT(market) AS YTD
FROM savedata2013, ticket_info
GROUP BY market
ORDER BY COUNT(market) DESC';
Example Data:
market - Saves(June) - Percent - YTD
Los Angeles - 530 - 16.5154 - 564
San Jose - 390 - 12.1523 - 415
Irvine - 371 - 11.5373 - 394
You can use an if statement inside a sum, something like...
select sum(if(field='$mon',1,0)) as something....
I've created a SQLFiddle based on what I think your database might look like, and then run the following query.
So, the DB I used is defined as a market and an enteredDate. You probably have a lot more information in your ticket_info, but it shouldn't be relevant.
create table ticket_info (market varchar(20),
enteredDate DateTime);
And the query is:
SELECT market,
sum(if(monthname(enteredDate)='June',1,0)) AS `Saves(June)`,
sum(if(monthname(enteredDate)='June',1,0))/ (SELECT count(*) FROM ticket_info WHERE year(enteredDate) = 2013) AS Percent,
sum(if(year(enteredDate)=2013,1,0)) AS YTD
FROM ticket_info
GROUP BY market
ORDER BY `Saves(June)` DESC;
The percent will be null if there are no tickets entered for the year, but this should be an edge condition.
Link to SQLFiddle
You can probably resolve the problem like this, using CASE statement:
SELECT market,
COUNT
(
CASE market
WHEN month = $mon
THEN market
ELSE NULL
END
) AS Saves,
COUNT(market) / (SELECT COUNT(*) FROM savedata2013) * 100 AS Percent,
COUNT(market) AS YTD
FROM savedata2013, ticket_info
GROUP BY market
ORDER BY COUNT(market) DESC';