"current month" and "cumulative monthly expenditure" data within the same query [duplicate] - sql

I've posted a couple of question on this site regarding this topic and I thank you for your input. I decided to take a step back and see if I can rephrase my question a bit better.
The below code provides a Running sum across various product numbers. Unfortunately, it provides a running total regardless of the Product number. That said, I need the running total to be calculated based on the product number. See below example code and current/desired output.
Note that the month field was left out, this is a test database that I created to see if some sample code that I found would work. Now I want to see if I can take it a bit further to provide the below desired output.
Example Code:
SELECT Prod.Product, Prod.Amount, DSum("[Amount]","[Prod]","[Amount] >=" & [Amount]) AS RunTot, Prod.EndDate
FROM Prod
GROUP BY Prod.Product, Prod.Amount, Prod.EndDate;
Desired Output - Example:
Product - SumOfAmount -RunTot - month
7000XZ - $600.00 - 600 - 10/28/14
7000XZ - $500.00 - $1100 - 11/28/14
7000YT - $400.00 - $400 - 10/28/14
7000YT - $300.00 - $700 - 11/28/14
7000AB - $200.00 - $200 - 10/28/14
7000AB - $100.00 - $300- 11/28/14
7000WE - $130.00 - $130 - 10/28/14
7000WE - $121.00 - $251 - 11/28/14
Current OutPut - Example:
7000XZ - $600.00 - $600
7000XZ - $500.00 - $1100
7000YT - $400.00 - $1500
7000YT - $300.00 - $1800
7000AB - $200.00 - $2000
7000WE - $130.00 - $2130
7000WE - $121.00 - $2251
7000AB - $100.00 - $2351
Current output Report:
I played around with the input provided and didn't get the desired input. Please take a look.
![Sample Report After flowing instruction at:
https://support.office.com/en-us/article/Summing-in-reports-ad4e310d-64e9-4699-8d33-b8ae9639fbf4?CTT=1&CorrelationId=4c1bcd05-6bab-4844-8d51-47111c6ab911&ui=en-US&rs=en-US&ad=US#bmrunningsum]1

Related

SQL - Query relating to date range from one table and two other tables

I am new to SQL, starting today at college.
I need to find the telephone number of owners who took their pet for a visit in May 2020.
I have the following tables with the columns and some example data:
Visit:
Pet_ID, Vet_Id, Date, Time, Reason, Treatment
P0089 - V04 - 03/05/2020 - 17:50 - Limping - Ointment
P0001 - V04 - 07/05/2020 - 21:00 - Cut on nose and leg - Ointment
Pet:
Pet_ID, Name, Type, Breed, Gender, Born, Owner_ID, Notes
P0001 - Tiddles - Cat - Persian - F - 2009 - 2 - Has a bad temper & Scratches
P0089 - Ginger - Cat - Siamese - F - 2016 - 4 - Bad tempered
Owner:
Owner_ID, Surname, Forename, Title, Tel_Number, Address
1 - Jackson - Janet - Miss - 0141-223321 - 1 Main Street, Glasgow
2 - Singh - Raj - Mr - 0141-326535 - 22, The Hill, Glasgow
I have tried the following command.
SELECT Owner.Tel_Number
FROM Owner, Pet, Visit
WHERE Visit. Date_ BETWEEN "01/05/2020" AND "31/05/2020"
AND Visit.Pet_id = Pet.Pet_id
AND Pet.Owner_id = Owner.Owner_id
GROUP BY Owner.Tel_Number
But the result shows the telephone number for all owners? What am I doing wrong? Thank you.
EDIT: Apologies, I am using DB Browser for SQLite, not Oracle or anything other system like that. This is for college. And I have added some example data.
Always use proper, explicit, standard*, readable JOIN syntax.
However, your problem is probably the format of the date strings. This should always be in standard format. I would suggest:
SELECT o.Tel_Number
FROM Owner o JOIN
Pet p
ON p.Owner_id = o.Owner_id JOIN
Visit v
ON v.Pet_id = p.Pet_id
WHERE v.Date_ >= '2020-05-01' AND
v.Date_ < '2020-06-01'
GROUP BY o.Tel_Number

Regular Expressions contains in bigquery example

I need to write regular expressions in bigquery to match the following two under title column: I want to get exactly these two. There are some other values containing 3 Percent, but I want to get only these two.
WBC - SAV - 3 Percent Q4 FY20
Canstar - canstar.com.au - AFF: Table Listing - Cost per click - National - 1x1 - 3 percent Savings
My code is:
WHEN REGEXP_CONTAINS(title, '(?i) 3 Percent')
THEN '3% PF'
I am not getting the correct output. Can anyone please assist.
There are some other values containing 3 Percent, but I want to get only these two.
So, in this case you don't need regular expression and rather use below
WHEN title IN (
'WBC - SAV - 3 Percent Q4 FY20',
'Canstar - canstar.com.au - AFF: Table Listing - Cost per click - National - 1x1 - 3 percent Savings'
) THEN '3% PF'

VB.NET How to get the last value from a Column in a DataTable

I have a DataTable called DTPerson like this:
Name - - - - - Age - - - - - Number
John - - - - - 16 - - - - - 1234567
Mike - - - - - 25 - - - - - 1231231
I need to retrieve the last field from the Column "Name" in DTPerson.
I want label1.text = Mike
I've looked everywhere and could only find examples for Datasets so I'm not sure what to do.
Try This.
DTPerson.Rows(DTPerson.Rows.Count -1).Item("Name").ToString()

SQL Query to compare multiple rows

I'm new here, and new to SQL. I have searched, but I can't seem to find an answer to my question. Maybe you gurus can help. I have a table that has customer ID numbers and their status (number) among other things. For instance, a few lines would be like this:
Status - ACCTnum - CustName - City - State
95 - A330 - Billy Burger - Cleveland - Oh
11 - A330/Q - Billy Burger Store#2 - Cleveland - Oh
15 - B250 - Spanky - Columbus - Oh
15 - B250/Z - Spanky#2 - Springfield - OH
15 - B250/Y - Spanky#3 - Miami - FL
We see here, there is a main account number, and a sub account number, but they occupy the same field. Account A330 is billy burger, and his second store is A330/Q. The status column is for their salesman number. If the number is 95, it is a dead account. The problem is, for our purposes, the status of a main account cannot be dead if a sub account is in good standing. So what I need is a query that can basically select any records that meet the criteria: "If ACCTnum is status 95, and has sub accounts that are not status 95"
Ideally if I ran the query on the table above it should return the first two records, since A330 is status 95 and A330/Q is not. It should ignore the other records.
I have tried the INTERSECT command with no success (I assume because it only works for two different tables?). I am a total SQL n00b, be gentle ;)
select distinct c1.*
from Customers c1
inner join Customers c2 ON c2.ACCTnum LIKE c1.ACCTnum + '%'
where c1.ACCTnum.status = 95 and c2.ACCTnum <> 95

Query in Access (adding missing values)

I've got two tables, Guides and Availability. Guides is a list of guides, and Availability is a list of dates for which information is known about a particular guide. For instance 03/03/14 - Guide 3 - Available or 05/03/14 - Guide 1 - Busy. If a guide's availability on a particular day isn't know yet, there isn't an entry in Availability at all for that day.
I want to write a query that will return a list of the statuses of all the guides during the next three days. If the status isn't known it will say Unknown.
I'm sure in SQL this task is definitely doable, but I am constrained by having to write a Web App in Access 2013. This means I must use the query editor in Access. Can anyone with expertise offer some guidance? If anything is unclear in the question, do say so.
Example query output:
03/03/2013 - Guide 1 - Busy
03/03/2013 - Guide 2 - Unknown
03/03/2013 - Guide 3 - Unknown
04/03/2013 - Guide 1 - Available
04/03/2013 - Guide 2 - Available
04/03/2013 - Guide 3 - Unknown
05/03/2013 - Guide 1 - Unknown
05/03/2013 - Guide 2 - Unknown
05/03/2013 - Guide 3 - Unknown
Table: Guides
Guide 1
Guide 2
Guide 3
Table: Availability
03/03/2013 - Guide 1 - Busy
04/03/2013 - Guide 1 - Available
04/03/2013 - Guide 2 - Available
P.S.: Someone has suggested I might need to make a third table listing all the dates for the next 10 years. This wouldn't be a problem if it helps with a solution
If you are limited to Access, you won't be able to use recursive CTE's (google it to find out about them, if interested).
therefore, you would have to create a table with all dates. Provided the table is called AllDates, and the date field is Dte, the query will look like this (you may need to filter by date range to limit the number of returned rows, otherwise you will see the records for all 10 years):
SELECT Availability.Dte, Availability.Guide, Availability.Status
FROM Availability INNER JOIN Guides ON Availability.Guide = Guides.Guide
UNION ALL
SELECT AllDates.Dte, Guides.Guide, 'Unknown' AS Status
FROM AllDates, Guides
WHERE NOT EXISTS (SELECT 1 FROM Availability WHERE Availability.Dte = AllDates.Dte And Availability.Guide = Guides.Guide)
ORDER BY 1, 2;
For the web-version of Access you will need to use 2 queries. The first one (DatesGuides) will list all guides for all dates, and its syntax is as simple as:
SELECT Guides.Guide, AllDates.Dte
FROM AllDates, Guides;
The second query will be LEFT JOINED with the first one. Its syntax is:
SELECT DatesGuides.Dte, DatesGuides.Guide, Nz([Status],'Unknown') AS Expr1
FROM Availability RIGHT JOIN DatesGuides ON Availability.Dte = DatesGuides.Dte AND Availability.Guide = DatesGuides.Guide;