Access "Where" Query - sql

I am trying to run some Queries in Access and I am unable to find a Query for this,
It is a Hotel Database and Fields Contain Room Type [Single,Double] and Location [London,USA]
Where Room Type is “Single” and Location is London 10% Tax and USA 20% Tax of Room Charge.

You can use an IIF expression:
SELECT IIF(room_type = 'Single', IIF(location = 'London', 0.1, 0.2), 0)
FROM my_table

Related

Calculating and displaying customer lifetime value histogram with BigQuery and Data Studio

Consider a table in Google BigQuery containing purchase records for customer. For the sake of simplicity, let's focus on the following properties:
customer_id, product_id, amount
I'd like to create a Google Data Studio report from the above data set showing a customer lifetime value histogram. The customer lifetime value is the sum of amount for any given customer. The histogram would show how many customers fall into a certain bucket by their total amount - I would define the buckets like 0-10, 10-20, 20-30 etc. value ranges.
Like this:
Finally, I'd also like to filter the histogram by product_id. When the filter is active, the histogram would show the totals for customers who - at least once - purchased the given product.
As of this moment, I think this is not possible to implement in Datastudio, but I hope I am wrong.
Things I've tried so far:
Displaying an average customer lifetime value for the whole dataset is easy, via a calculated field in Datastudio as SUM(amount) / COUNT(customer_id)
For creating a histogram, I don't see any way purely in Data Studio (based on the above data set). I think I need to create a view of the original table, consisting a single row for each customer with the total amount. The bucket assignment could be implemented either in Big Query or in Data Studio with CASE ... WHEN.
However, for the final step, i.e. creating a product filter that filters the histogram for those customers who purchased the given product, I have no clue how to approach this.
Any thoughts?
I was able to do a similar reproduction to what you describe but it's not straightforward so I'll try to detail everything. The main idea is to have two data sources from the same table: one contains customer_id and product_id so that we can filter it while the other one contains customer_id and the already calculated amount_bucket field. This way we can join it (blend data) on customer_id and filter according to product_id which won't change the amount_bucket calculations.
I used the following script to create some data in BigQuery:
CREATE OR REPLACE TABLE data_studio.histogram
(
customer_id STRING,
product_id STRING,
amount INT64
);
INSERT INTO data_studio.histogram (customer_id, product_id, amount)
VALUES ('John', 'Game', 60),
('John', 'TV', 800),
('John', 'Console', 300),
('Paul', 'Sofa', 1200),
('George', 'TV', 750),
('Ringo', 'Movie', 20),
('Ringo', 'Console', 250)
;
Then I connect directly to the BigQuery table and get the following fields. Data source is called histogram:
We add our second data source (BigQuery) using a custom query:
SELECT
customer_id,
CASE
WHEN SUM(amount) < 500 THEN '0-500'
WHEN SUM(amount) < 1000 THEN '500-1000'
WHEN SUM(amount) < 1500 THEN '1000-1500'
ELSE '1500+'
END
AS amount_bucket
FROM
data_studio.histogram
GROUP BY
customer_id
With only the latter we could already do a basic histogram with the following configuration:
Dimension is amount_bucket, metric is Record count. I made a bucket_order custom field to sort it as lexicographically '1000-1500' comes before '500-1000':
CASE
WHEN amount_bucket = '0-500' THEN 0
WHEN amount_bucket = '500-1000' THEN 1
WHEN amount_bucket = '1000-1500' THEN 2
ELSE 3
END
Now we add the product_id filter on top and a new chart with the following configuration:
Note that metric is CTD (Count Distinct) of customer_id and the Blended data data source is implemented as:
An example where I filter by TV so only George and John appear but the other products are still counted for the total amount calculation:
I hope it works for you.

How to calculate overall percentage of two items in SQL?

I have two teams A and B. A team member will enter records. B team member will evaluate the records. Each team has their own daily targets, like A's Daily Target is 30 entries and B's Daily Target is 60 evaluations.
So, in some situations A or B team member might do the both the jobs.During that, how should we calculate overall percentage against 100% ?
In clear, lets say A team member has done 15 entries out of 30 entries(target), then his individual achievement percentage is 50%.The same logic applies for B Team member too. I am able to calculate individual team member achievement percentage.
But what if a person from any team works on both the things. I can't sum up them or do average as both were doing different jobs.
Can you guys help me out with any suggestions or formulas in SQL ?
I am able to write query for calculating percentage if its single, for eg. in the picture you can observer the first case, where I can calculate percentage as that person did only entry,so his target would be 30.Then achievement percentage would be 02*100/30 %. Same applies for second one as 10*100/60 %. I got struck in the third case where the user performs both the different actions.
How should I calculate his overall percentage? Please suggest.
If I really understand what do you want, try next code:
use [your_db_name];
create table dbo.test17
(
[user] int null,
[date] datetime null,
records_entered int not null,
records_evaluated int not null
);
insert into dbo.test17
values (1, '2017-04-17', 2, 0), (2, '2017-04-17', 0, 10), (3, '2017-04-17', 5, 20)
select
[user],
[date],
(sum(records_entered) * 2 + sum(records_evaluated)) * 100.0 / 60 as target
from dbo.test17
group by
[user]
,[date]

Google BigQuery/SQL: Dividing one column by another, from separate tables

I am trying to determine medicare costs per capita in each State using Google BigQuery.
I already have population numbers for each state (represented as Total) as well as total medicare cost (Cost) in each state. I am trying to divide total cost by the population of each state.
At the moment the query runs, however every entry is null. I am admittedly a beginner with both BigQuery and SQL.
Here is my code:
SELECT State, Cost / Total AS PerCapita
FROM medicare.population, medicare.CostByState
GROUP BY State, PerCapita;
One thing that may be causing issues is that the 'State' column exists in both 'population' and 'CostByState' tables. Not sure how to address this.
Here are my tables:
population
CostByState
You seem to have data with one row per state, so you only need a JOIN.
SELECT p.State, cbs.Cost / p.Total AS PerCapita
FROM medicare.population p JOIN
medicare.CostByState cbs
ON p.state = cbs.state;
You would only need aggregation if the tables had multiple rows per state.
Indeed you need to join that.
If the relationship is one to one you're good. But if not you may need some type of aggregation
sum(cost)/sum(total) as per_capita

Query cannot be parse SELECT avg_galoon, avg_galoon

I dont know if its the right way to multiply that way, I want to multiply the avg_galoon that is sold weekly by the price of it which is 20
SELECT avg_galoon,
avg_galoon * 20 + NVL(total income , 0)
FROM customer;
total income isn't a column name from the customer table.
If you disagree and can see that column name (via methods mentioned in the comments already) then you will need to enclose that column name in the appropriate syntax for the database engine you are using i.e. [total income] for Microsofts SQL Server, `total income` for MySQL etc.
In future avoid the use of spaces in column names.

How do I enforce, that the sum of a weight parameter equals 1, grouped by part of the key?

I have two tables in my setup. One with sales persons and there income. Each sales person only know their total income. For this particular income period, they are asked to give an estimate of their income on either private, small business or large business customers. This information is entered in the second table.
Income
=================
SalesPerson
Income
Distribution
=============================
SalesPerson
CustomerType
Weight
Now, my query would look something like this:
SELECT
Income.SalesPerson,
Distribution.CustomerType,
Income.Income * Distribution.Weight as DistributedIncome
FROM
Income INNER JOIN Distribution ON
Income.SalesPerson = Distribution.SalesPerson
How would I enforce, that the SUM(Weight) = 1 for each SalesPerson in Distribution?
Normalize by the sum, according to the same criteria.
SELECT
Income.SalesPerson,
Distribution.CustomerType,
Income.Income * Distribution.Weight/(
select sum(d.weight)
from distribution d
inner join income i on i.salesperson = d.salesperson
)
as DistributedIncome
FROM
Income INNER JOIN Distribution ON
Income.SalesPerson = Distribution.SalesPerson
If somehow want to select unmodified weights that sum to 1 then I believe you've got a case of the subset sum problem and you are probably not going to be able to solve this with an SQL query.