There was a solution provided for 1.4 MySQL query for "Sales Report by Country and State/Province" unfortunately the table structure has changed. Anyone know a query to order sales by state in 1.7.0.2? I would be most appreciative.
Here is what i came up with:
SELECT
sales_flat_order_address.region
, count(*)
FROM
`sales_flat_order` AS `main_table` INNER JOIN `sales_flat_order_address`
ON main_table.entity_id = sales_flat_order_address.parent_id
AND sales_flat_order_address.address_type="shipping"
GROUP BY
sales_flat_order_address.region
Related
Before I continue I do want to say this is FOR homework for my class. We are using SQL in Microsoft Access. With the transition to online classes because of covid-19 and my professor developing the virus last month we have not have had time to really dive much into how SQL works. I will also provide a picture of the ER Diagram I have of the database to hopefully provide a clear picture of what I'm trying to achieve. https://imgur.com/a/4QGRgRc
Now the question I'm given to solve is this:
Retrieve the phone number of the customer(s), who lives in Tyler, TX, booked two rooms on Jan 1, 2020.
The code I have written so far is as follows
SELECT CUSTOMER.Phone_No, CUSTOMER.Address, RESERVATION.Reservation_No, INCLUDES.Check_in, ROOM.Room_No
FROM CUSTOMER, RESERVATION, INCLUDES, ROOM
SELECT CUSTOMER.Phone_No, WHERE CUSTOMER.Address = 'Tyler, Tx' AND INCLUDES.Check_in < '12/31/2019' AND INCLUDES.Check_in > '01/01/2020'
I'm pretty stuck on where to go now or even if what I have so far will work. Could someone give me a point in the right direction on where I should go now?
If you just want customer info, use an aggregate query. Each field pulled must be included in GROUP BY clause:
SELECT Customer.ID, CustomerName, Phone_No FROM Customer INNER JOIN Reservation ON Customer.ID = Reservation.CustomerID WHERE Address = 'Tyler, Tx' AND Check_in BETWEEN #12/31/2019# AND #01/01/2020# GROUP BY Customer.ID, CustomerName, Phone_No HAVING Count(CustomerID) = 2;
Or take advantage of the power of nested query and IN() function:
SELECT * FROM Customer WHERE Customer.Address = 'Tyler, Tx' AND Customer.ID IN (SELECT CustomerID FROM Reservation WHERE Check_in BETWEEN #12/31/2019# AND #01/01/2020# GROUP BY CustomerID HAVING Count(*) = 2);
Or use DCount(). However, domain aggregate functions can slow query performance.
SELECT * FROM Customer WHERE Customer.Address = 'Tyler, Tx' AND DCount("*", "Reservation", "Check_in BETWEEN #12/31/2019# AND #01/01/2020# AND CustomerID=" & Customer.ID) = 2;
I created a query which selects sum of columns from a table grouped by a field in Crystal reports, now I want the result to be filtered by a date range from another table which I am unable to do. Please Help...
Here is the query
SELECT
BDETAIL.HSN,
SUM(BDETAIL.TAXABLE),
SUM(BDETAIL.SGST_V),
SUM(BDETAIL.CGST_V),
SUM(BDETAIL.TOTAL),
BDETAIL.SGST_P
FROM
BDETAIL
JOIN
BILL ON BDETAIL.BILL_ID = BILL.BILL_ID
WHERE
BILL.BDATE BETWEEN {?FROM_DATE} AND {?TO_DATE} )
GROUP BY
BDETAIL.HSN, BDETAIL.SGST_P
I finally figured it out ..here is the working query
SELECT BDETAIL.HSN,
sum(BDETAIL.TAXABLE),
Sum(BDETAIL.SGST_V),
Sum(BDETAIL.CGST_V),
Sum(BDETAIL.TOTAL),
BDETAIL.SGST_P
FROM BDETAIL INNER JOIN BILL ON
( BILL.BILL_ID = BDETAIL.BILL_ID AND
BILL.BDATE BETWEEN {?FROM_DATE} AND {?TO_DATE} )
GROUP BY BDETAIL.HSN, BDETAIL.SGST_P;
thank you all for showing interest in my issue.
Tables
ARMASTER = Customer Information
ORDERHEAD = Sales order information (Ship to, bill to)
Hey everyone! I'm still quite new to SQL and I do belive I'm picking it up fairly quick. I have been racking my brain on this for a few hours now and have asked around the office and nobody seems to have a solution.
I'm trying to identify how many times a customer account has been used as a SHIP TO location & a BILL TO location.
SELECT ARMASTER.CUSTOMER,
ARMASTER.DIVISION,
ARMASTER.STATUS,
ARMASTER.CUHEAD AS "MASTER",
COUNT (ORDERHEAD.BILLTO) AS "COUNT_BILL",
COUNT (ORDERHEAD.SHIPTO) AS "COUNT_SHIP"
FROM ARMASTER
LEFT OUTER JOIN ORDERHEAD
ON ARMASTER.CUSTOMER = ORDERHEAD.BILLTO
LEFT OUTER JOIN ORDERHEAD
ON ARMASTER.CUSTOMER = ORDERHEAD.SHIPTO
GROUP BY ARMASTER.CUSTOMER,
ARMASTER.DIVISION,
ARMASTER.STATUS,
ARMASTER.CUHEAD
And I'm not even remotley getting what I should be getting. However, when I remove one of my joins the count is exactly what it should be for either 1 or the other of them.
Any guidance would be muchly appreciated! Thank you!
I think it can also work
SELECT CUSTOMER,
DIVISION,
STATUS,
CUHEAD AS "MASTER",
(SELECT COUNT(1) FROM ORDERHEAD WHERE SHIPTO = ARMASTER.CUSTOMER ) AS "COUNT_BILL",
(SELECT COUNT(1) FROM ORDERHEAD WHERE BILLTO = ARMASTER.CUSTOMER ) AS "COUNT_SHIP"
FROM ARMASTER
I've got a query which gets the lists of customers who've made a purchase of the product for the current day
select Customer.customerName, sum(InvoiceDetail.itemPrice * InvoiceDetail.itemQuantity) as dailyPurchase from Invoice
inner join InvoiceDetail on Invoice.invoiceID = InvoiceDetail.invoiceID
inner join Customer on Invoice.customerID = Customer.customerID
inner join Item on InvoiceDetail.itemID = Item.itemID
inner join Branch on Branch.branchID = Invoice.branchID
inner join Region on Region.regionID = Branch.regionID
where
Invoice.inactive = 0 and InvoiceDetail.inactive = 0
and Item.itemTypeID = 3
and Region.regionCode = 'CR'
and cast(Invoice.invoiceDate as date) = convert(date, '01/08/2016', 103)
group by Customer.customerName
What I need is a table with a list of all the dates in the current month, listing ALL customers who have at least purchased the product ONCE. It should resemble something similar to this image here.
Any help on how to get started or a general idea of how to get the desired result, is much appreciated. Thanks!
Sample Data from Results:
customerName dailyPurchase
AGH COMMUNICATIONS 450.00
ARIEL AMARCORD SHOP 285.00
AKN COMMUNICATION 300.00
AWSDAC TELECOMMUNICATION 2850.00
BARLEY MOBILE & SERVICES 285.00
Table Structure - I'm sorry, I don't know an easier way to copy this.
First get the customers who have purchased the product atleast once this month alongwith date.
Then use pivot to get the result in the form that you want (as seen in image). Search stackoverflow for pivot in sql server, you will get good info.
Give some information about the table structure and sample data and I might be able to help you with the query to get the results.
I am working with cakePHP and am having an issues with microsoft sql server. Basically, I am pulling data about the success rates of different campains. Once of the lines calculates the 'revenue per flyer.'
SUM(o.TotalPrice) / COUNT(cc.CustomerID) AS RevPerFlyer
The issue is, cakePHP is calculating this value wrong. Here is my entire query:
SELECT
c.State,
'?' AS FlyersMailed,
COUNT(o.CampaignCustomerID) AS Orders,
SUM(o.TotalPrice) AS TotalRevenue,
SUM(o.TotalPrice) / COUNT(cc.CustomerID) AS RevPerFlyer
FROM Customer c
INNER JOIN CampaignCustomer cc
ON c.CustomerID = cc.CustomerID
LEFT JOIN CustomerOrder o
ON cc.CampaignCustomerID = o.CampaignCustomerID
WHERE 1=1
AND OrderStatusID NOT IN (4,9,10,11,13)
AND cc.CampaignID = 8
GROUP BY
State
ORDER BY
When I run this directly in sql server,I get the correct data.
Example: Here is
RevPerFlyer for the state AK: 73.924
However, when I run this same exact sql from cake, this is what I get:
RevPerFlyer for the state AK: 7.2502
Does anyone know what the problem may be? Maybe a problem with sql server?
Thanks in advance!