Query to tell me if customer is in 3 queries - ms-access

I have three queries;
Q1. Returns all customer records (id, name, year amount) where the customer has spent money with us.
Q2. Returns all customers records (id,name, year of regis, number) where the customer has a telephone number.
Q3. Returns all customer records (id,name, year, amount) where the customer has a purchased a specific product.
I want a new query that will return a list of all customers ids, names if they appear in any of the 3 queries, along with the latest year. How do I do that?

First, add all the results of the 3 queries together using UNION, then group the records by id and select the first name (or any name really, because they must be all the same) and the maximum year.
Try this:
SELECT id, FIRST(name), MAX(year)
FROM (
SELECT id, name, year FROM q1
UNION
SELECT id, name, year FROM q2
UNION
SELECT id, name, year FROM q3) AS q
GROUP BY id

Related

MySQL - Average number of particular product sold on date

I need to write SQL query for "average number of particular product sold by date. On each day is sold min one product".
SELECT AVG (COUNT (PID))
FROM SOLD
GROUP BY DATE, PID;
P.S. PID means Product ID.
Is this query okay?
Should this give right answer?
Consider Using distinct count of date columns
SELECT PID,
COUNT(PID)/COUNT(distinct date_) as "Avg.Product Sold By Days"
FROM SOLD
GROUP BY PID;
You can try this sql query below. Basically, it will return the average number of 'SALES' for each product you have. It will group by each distinct product ID. Please provide us the data structure your of table and etc.
SELECT product_ID, trans_date
Sum(sales_of_product) / COUNT(DISTINCT sold_transaction) AS 'avg'
FROM SOLD
GROUP BY product_ID

Mysql select items that occur a number of times in given date range

Customer table with purchases in a date range
Having some challenge doing this.
Select all customers that made more than 2 purchases in an hour
starting from a given datetime eg.2016-01-01.13:00:00
Select all customers that made more than 2 purchases in a a day(24
hours) starting from a given datetime eg. 2016-01-01.15:30:05
Select count(name) from cust_table where count(name)
IN (BETWEEN (2016-01-01.13:00:00, 2016-01-01.14:00:00))
You need to get the identifiers (usernames or ID or so) for all users who have made more than two purchases. Something like the following should work:
SELECT name FROM purchases GROUP BY name HAVING COUNT(name) > 2
To get such rows where the purchases were made between some dates, just add a WHERE clause, so it becomes something like this
SELECT name FROM purchases WHERE pdate BETWEEN (2016-01-01.13:00:00) AND (2016-01-01.14:00:00) GROUP BY name HAVING COUNT(name) > 2
What this does is group the table rows by name with purchases between the specified dates. It then filters the groups using the HAVING COUNT(name) > 2 clause to retrieve only rows that appear more than twice.

Yearly income and expenses table

i have two tables
Income
(id,
income,
date,
userid)
and table
Expense
(id,amount,date,userid). Now I'm trying to create a View with sum of income, sum of amount in one year. Also it must be GROUPED by year and userid.
So new table (view) must have sum of income, expenses, year. Right now i am stuck on this code.
SELECT
id,
i.Prihod,
e.Rashod,
YEAR
YEAR(COALESCE(e.god, i.godi))) AS Godina ,
users_id
FROM
(
SELECT
sum(insum) as 'Prihod',
YEAR(datum) as 'Godi',
users_id
FROM
GROUP BY users_id
) as i
FULL OUTER JOIN
(
SELECT
sum(amount) as 'Rashod',
YEAR(datum) as 'God',
users_id
FROM expense
GROUP BY users_id
) as e
This is a job for the structured part of structured query language. Start with three subqueries (virtual tables).
First: A list of the years and users. You need this because MySQL doesn't have FULL OUTER JOIN capability. If you write this query correctly, it will contain one row for each user for each year you need.
SELECT DISTINCT YEAR(datum) Godi, users_id FROM income
UNION
SELECT DISTINCT YEAR(datum) Godi, users_id FROM expense
Second, a summary of the income by year and user.
SELECT sum(insum) as Prihod,
YEAR(datum) as Godi,
users_id
FROM income
GROUP BY YEAR(datum), users_id
Third, a similar sum of expenses by year and user
SELECT sum(amount) as Rashod,
YEAR(datum) as God,
users_id
FROM expense
GROUP BY YEAR(datum), users_id
Next, test these subqueries individually, standalone, to ensure you have the results you expect.
Finally, join them together as if they were tables, like so
SELECT yr.users_id, yr.Godi, inc.Prihod, exp.Rashod
FROM (
SELECT DISTINCT YEAR(datum) Godi, users_id FROM income
UNION
SELECT DISTINCT YEAR(datum) Godi, users_id FROM expense
) yr
LEFT JOIN (
SELECT sum(insum) as Prihod,
YEAR(datum) as Godi,
users_id
FROM income
GROUP BY YEAR(datum), users_id
) inc ON yr.users_id = inc.users_id AND yr.Godi = inc.Godi
LEFT JOIN (
SELECT sum(amount) as Rashod,
YEAR(datum) as God,
users_id
FROM expense
GROUP BY YEAR(datum), users_id
) exp ON yr.users_id = exp.users_id AND yr.Godi = exp.God
The query pattern here is to develop three subqueries. The first of them determines the number of rows in your final result set, because it contains one row for each desired row. The second and third queries have one, or maybe zero, rows for each row you need in your final result set. Then you join them together.

Joining tables that have no common value

I'm trying to join 3 tables for a system to keep track of what comes in and out of my shop.
The first tables is as following. (Production)
id
Amount
Date
item id
next table is: (sales)
id
date
amount
customer id
amount
the last is: (waste)
id
date
amount
reason
I haven't really found a way to join those 3 tables without using a common value they could join on. I need to order them by date so I can see the history of my income and expenses. If the 3 different tables could come with a individual value like 3 for waste 2 for sales and 1 for production would also be great.
What you want is not a join of the tables but a union: http://dev.mysql.com/doc/refman/5.0/en/union.html
Something like
(SELECT 'production' as source, id, Amount AS amount, Date AS date FROM Production)
UNION
(SELECT 'sales' as source, id, amount, date FROM sales)
UNION
(SELECT 'waste' as source, id, amount, date FROM waste)
ORDER BY date;

Mysql select with multiple total columns

I have a table named sales with 5 fields:
id
date
id_store
id_customer
price
The table customers:
customers.id
customers.name
And the table stores:
stores.id
stores.name
I want to get this result:
January, Total customer name1, Total customer name2 ..., Total store name 1, Total store name 2...
February, Total customer name1, Total customer name2 ..., Total store name 1, Total store name 2...
March, Total customer name1, Total customer name2 ..., Total store name 1, Total store name 2...
April...
is it possible?
It is possible, using JOINs and WITH ROLLUP. Basically, you'll JOIN your tables together to get the data rows you need, then GROUP BY multiple columns (in your case, probably GROUP BY MONTH(sales.date), stores.id, customers.id WITH ROLLUP). Grouping by multiple columns gives you nested groups, in the order the columns are listed. WITH ROLLUP will give you summary data for each nesting level; so in my example, you'd get totals per customer per store per month, and totals per store per month, and overall totals per month.