I have a table that displays a dealer's name, the number of blocks they have outstanding and the last date an entry was made. It looks like this.
Dealer
Blocks
Last Date
Dealer 1
10
09-Feb-23
Dealer 2
-5
24-Jan-23
The problem is that if the dealer has more than one entry in the master table where this data is being drawn from, my query does not display the Last Date. Here is the query I am currently using.
SELECT Dealer,
SUM(CAST([Total Blocks] AS integer)), MAX(date) AS [Last Date]
FROM {{query1.data}}
WHERE Dealer = {{select1.value}}
OR {{select1.value == null}}
GROUP BY Dealer
ORDER BY Dealer
What do I need to change so that any dealers with multiple entries have the Last Date displayed?
I have searched the Internet for several days and have tried different changes to my query but nothing has worked.
Related
I'm trying to write a query which is confusing me.
In essence, what I'm trying to look for is check the customer, look for the last 3 days where jobs were done and if each day has a failure, then it performs an action on that customer.
So customer A) Could have had jobs all week (I'll use this week as an example), 20/10 (Failure), 19/10 (Failure), 18/10 (Failure) would work
And Customer B) only has jobs fortnightly (20/10) (Failure), 05/10 (Failure), 20/09, (Failure)
What I am confused about, is I am not sure how to filter on the orders where I'm not looking at the orders themselves, but instead 3 seperate days where orders have been done
I was thinking of a top 3 dates, but the customer could have multiple jobs in a day and I need to find all of the orders for the last 3 days where jobs were done
SELECT distinct TOP 3 DATEPART(DAY,od.datetimeCreated), of.uniqueID FROM order.
data od LEFT JOIN order.dataFailure of ON of.orderID = od.orderID
This gives me something similar to what I want, however I still want to see all of the data for those 3 days
Could anyone give me some pointers on how I could go about this?
Sample Data:
Not sure what data would help with this issue, in essence, when the orderID from order.data is inside order.dataFailure, then we consider it a failure, else if it joins as a null, it hasn't failed.
As for dates, I compare the dates on a field called datetimeCreated and datetimeFailed, and then group it by a customer account code
Desired Results:
I need to find the last 3 days where orders were done for that customer, this could be 3 orders a day for the last 3 days, or 1 order a week for the last 3 weeks, and I'm looking to see if there is a failure on each of those days (Being is there a row in the order.datafailure table for each day of the last 3 days)
In this image I have filtered on the customer,
the query needs to be able to look and see that there has been a failure on 2020-03-12, then check the next day, 2019-12-13, also failure and 2015-07-13, no failure
I want to display the customer name and its sum of all the bills he paid between specific dates. I also want the whole list of customers between the 2 given dates with sum of their total bills.
In list what I want is first Customer Name then its regarding sum of all the bill totals.
I have tried many options for it one where I am getting the sum of that bill only every bill is appearing separately while I want to group it by the customer thats all.
Raw query is working in phpMyAdmin but I failed to convert it to Laravel Eloquent. I want to make this raw query to work using Eloquent.
Query:
select customer_id,sum(total),sum(totalcgst),sum(totalsgst),
sum(totaligst) from sales where
date BETWEEN '2018-03-01' and '2018-03-31'
group by customer_id
This is the result I am getting in database
Try this:
ORM_NAME::selectRaw('customer_id, sum(total), sum(totalcgst), sum(totalsgst)')->whereBetween('date', ['2018-03-01', '2018-03-01'])->groupBy('customer_id)->get();
I'm trying to create a measure that displays buckets of AR values from our accounting system. The billing table is structured in a way where a row is created for each financial transaction related to a bill: one record shows the billed amount and a separate record, related by a bill key, shows one or more payments to the bill. I am able to create multiple measures that sum the bill amounts and break these out by Current, 30, 60, 90 and 120 day buckets (admittedly, this is new to me so even this measure might not be right):
AR Current =
VAR EndDate = TODAY()
VAR StartDate = EndDate - 29
RETURN
CALCULATE(
[Billings],
DATESBETWEEN(
'Date'[Date], StartDate, EndDate
)
)
The issue I have is trying to sum up the collections. The collections are not aged, but the total amounts collected for a specific bill need to be subtracted from the total billings in each aging bucket. Regardless of the date range, I need to get a list of all Bill Keys for each bucket, use those keys to sum the values for the Collections column, and subtract from the total within a bucket.
The only equivalent I can think of is a SQL query:
--This example gets the billed amount for anything less than 30 days old)
SELECT BILLKEY, SUM(BILLAMOUNT) BILLAMOUNT
FROM tblBilling
WHERE BILLDATE BETWEEN GETDATE()-30 AND GETDATE()
GROUP BY BILLKEY
-- This gets the corresponding collections:
SELECT BILLKEY, SUM(COLECTION) COLLECTION
FROM tblBilling
WHERE BILLKEY IN (
SELECT BILLKEY FROM tblBillings WHERE BILLDATE BETWEEN GETDATE()-30 AND GETDATE()
)
GROUP BY BILLKEY
Any help would be greatly appreciated.
Thanks,
Eric
I have 4 columns in the table Shipping: CustomerID, CustomerName, Shipping Address and Invoice Date. Customer ID is the unique identifier in this table.
I have a 6 month date period, 02-13-2014 to 08-13-2014, that holds 60,000 records. Each record has those four columns and they are NON-nullable columns. I need to split this 6 month period into 7 day week blocks of time spanning the whole 6 months, and then count the number of records that appear more than once with the same CustomerID and Shipping Address per the 7 day weeks.
My end goal is to find how many of our customers get deliverys more than once a week to the same shipping address. If there is any record that appears more than once with the same customer ID and shipping address, it will mean that it is a delivery.
The kicker is, I don't care if there are multiple records with the same customer ID and shipping address if they are on the same day. I still need to know a record is there for that day, but I don't care how many are there.
Something along these lines might get you started:
SELECT ShippingAddress, DATEPART(wk, InvoiceDate), COUNT(*) AS countForWeek
FROM shipping
GROUP BY ShippingAddress, DATEPART(wk, InvoiceDate)
HAVING COUNT(*) > 1
I have a table named "Historical_Stock_Prices" in a MS Access database. This table has the columns: Ticker, Date1, Open1, High, Low, Close1, Volume, Adj_Close. The rows consist of the data for each ticker for every business day.
I need to run a query from inside my VB.net program that will return a table in my program that displays the growth rates for each quarter of every year for each ticker symbol listed. So for this example I would need to find the growth rate for GOOG in the 4th quarter of 2012.
To calculate this manually I would need to take the Close Price on the last BUSINESS day of the 4th quarter (12/31/2012) divided by the Open Price of the first BUSINESS day of the 4th quarter (10/1/2012). Then I need to subtract by 1 and multiply by 100 in order to get a percentage.
The actual calculation would look like this: ((707.38/759.05)-1)*100 = -6.807%
The first and last days of each quarter may vary due to weekend days.
I cannot come up with the correct syntax for the SQL statement to create a table of Growth Rates from a table of raw Historical Prices. Can anyone help me with the SQL statment?
Here's how I would approach the problem:
I'd start by creating a saved query Access named [Stock_Price_with_qtr] that calculates the year and quarter for each row:
SELECT
Historical_Stock_Prices.*,
Year([Date1]) AS Yr,
Switch(Month([Date1])<4,1,Month([Date1])<7,2,Month([Date1])<10,3,True,4) AS Qtr
FROM Historical_Stock_Prices
Then I'd create another saved query in Access named [Qtr_Dates] that finds the first and last business days for each ticker and quarter:
SELECT
Stock_Price_with_qtr.Ticker,
Stock_Price_with_qtr.Yr,
Stock_Price_with_qtr.Qtr,
Min(Stock_Price_with_qtr.Date1) AS Qtr_Start,
Max(Stock_Price_with_qtr.Date1) AS Qtr_End
FROM Stock_Price_with_qtr
GROUP BY
Stock_Price_with_qtr.Ticker,
Stock_Price_with_qtr.Yr,
Stock_Price_with_qtr.Qtr
That would allow me to use the following query in VB.NET (or C#, or Access itself) to calculate the quarterly growth rates:
SELECT
Qtr_Dates.Ticker,
Qtr_Dates.Yr,
Qtr_Dates.Qtr,
(([Close_Prices]![Close1]/[Open_Prices]![Open1])-1)*100 AS Qtr_Growth
FROM
(
Historical_Stock_Prices AS Open_Prices
INNER JOIN Qtr_Dates
ON (Open_Prices.Ticker = Qtr_Dates.Ticker)
AND (Open_Prices.Date1 = Qtr_Dates.Qtr_Start)
)
INNER JOIN
Historical_Stock_Prices AS Close_Prices
ON (Qtr_Dates.Ticker = Close_Prices.Ticker)
AND (Qtr_Dates.Qtr_End = Close_Prices.Date1)