Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 2 years ago.
Improve this question
I have a query that I inherited:
SELECT SUM(Credit) AS Total, Account, Date
FROM
(
SELECT DISTINCT
CONVERT(char(10), dbo.vCustomer_Invoice.Accounting_Distribution__Document_Date, 101) AS Date
, dbo.vCustomer_Invoice.Accounting_Distribution__Amount_Credit AS "Credit"
, dbo.vCustomer_Invoice.Accounting_Distribution__GL_Account AS "Account"
FROM dbo.vCustomer_Invoice
WHERE CONVERT(char(10), dbo.vCustomer_Invoice.Accounting_Distribution__Document_Date, 101) = '11/03/2020' AND (dbo.vCustomer_Invoice.Accounting_Distribution__GL_Account LIKE '4000%' OR dbo.vCustomer_Invoice.Accounting_Distribution__GL_Account LIKE '4100-700-%')
)
AS D
Group By Account, Date;
This gives me a total by each GL_Account for a date. I'd like to now add a column that Sums by Date. I have in the past used a UNION ALL but I can't get that to work with this query configuration.
Any help would be appreciated.
You can use SUM() window function for this new column:
SELECT SUM(Credit) AS Total,
Account,
Date,
SUM(SUM(Credit)) OVER (PARTITION BY Date) AS Total_By_Date
FROM (
SELECT DISTINCT
CONVERT(CHAR(10), Accounting_Distribution__Document_Date,101) AS Date,
Accounting_Distribution__Amount_Credit AS Credit,
Accounting_Distribution__GL_Account AS Account
FROM dbo.vCustomer_Invoice
WHERE CONVERT(CHAR(10), Accounting_Distribution__Document_Date, 101) = '11/03/2020'
AND (Accounting_Distribution__GL_Account LIKE '4000%' OR Accounting_Distribution__GL_Account LIKE '4100-700-%')
) AS D
GROUP BY Account, Date;
Related
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 4 years ago.
Improve this question
I'm trying to generate a report that only shows clients that are age 65 years and up. I'm wondering what the SQL equivalent of months_between is and also how to correctly limit the report to clients 65+ years-old. I've included my statement below. Any assistance is appreciated. Thank you.
SELECT person.idFamily AS Family_ID, person.id AS Person_ID,(SELECT person.firstName+ ', ' + person.lastName) AS Name, person.Race AS Race, person.Ethnicity as Ethnicity, family.capidCounty AS County, Person.BirthDate AS [DateofBirth], trunc(months_between(sysdate,DateofBirth)/12))AS Age
FROM Family
LEFT JOIN person ON family.Id = person.idFamily
This should work for getting people older than 65 years and their age. DATEDIFF calculates the difference between two dates and DATEADD can add/subtract from dates.
select *, DATEDIFF(year,BirthDate,getdate()) as Age from Person where BirthDate < DATEADD(year, -65, getdate())
More about DATEDIFF and DATEADD
Well something like this should work in SQL Server:
SELECT p.idFamily AS Family_ID, p.id AS Person_ID,
(p.firstName + ', ' + p.lastName) AS Name, p.Race AS Race, p.Ethnicity as Ethnicity,
f.capidCounty AS County, p.BirthDate AS [DateofBirth],
datediff(month, dateofbirth, getdate()) / 12 as age
FROM Family f LEFT JOIN
person p
ON f.Id = p.idFamily;
This is not an exact match, but the logic is probably good enough. Note that the age will increment on the first of the month of the month with the birthday.
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 6 years ago.
Improve this question
I've got a problem. I just wanna calc the balance of my table.
my select statement is:
select date, ammount, ?? as Balance
from table
where accountnr = 123
order by date
Output should look like this:
Date Ammount Balance
07/02/2016 -145.55 945.65
25/01/2016 349.45 1091.20
11/11/2015 340.25 741.75
30/09/2015 369.10 401.50
05/04/2015 32.40 32.40
I tried so long, with different ways without luck.
You can do it in a single table scan (i.e. without any joins or correlated sub-queries) with an analytical query:
SELECT "date",
amount,
SUM( amount ) OVER ( ORDER BY "date" ) AS balance
FROM your_table;
If there are multiple accounts in the table then:
SELECT account_number,
"date",
amount,
SUM( amount ) OVER ( PARTITION BY account_number ORDER BY "date" ) AS balance
FROM your_table;
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 7 years ago.
Improve this question
I have a table to stores expiry dates of credit cards, below is the sample schema for table.
ExpiryDate Varchar(10)
Sample Data in Table is as follows:-
'08/10'
'09/11'
'08/16'
'10/17'
how can i find out if a card's date is expired?
I am not sure what you are expected, but You can use string manipulation with substring to get records those are not expired
Select *
FROM supportContacts
WHERE LEFT(ExpiryDate,2) >= MONTH(GETDATE()) AND RIGHT(ExpiryDate,2) >= RIGHT(YEAR(GETDATE()),2)
If you want expired card list then use this
Select *
FROM supportContacts
WHERE LEFT(ExpiryDate,2) < MONTH(GETDATE()) AND RIGHT(ExpiryDate,2) <= RIGHT(YEAR(GETDATE()),2)
SQL DEMO
With such an awful data structure you are forced to kludge this together. Here is one way of doing it.
with BadData as
(
select '08/10' as SemiDate union all
select '09/11' union all
select '08/16' union all
select '10/17'
)
select *
from BadData
where cast(replace(STUFF(SemiDate, 4, 0, '01-20'), '/', '-') as DATE) < CAST(getdate() as DATE)
If you stored the ExpirationDate as a date instead of a string this would be simple. I sure hope you aren't storing the credit card number alongside this.
select *
from billinginfo
where substring(expirydate,1,charindex('/',expirydate)-1) < month(getdate())
and '20'+ substring(expirydate,charindex('/',expirydate)+1, 2) <= year(getdate())
You can try this.
You want something like this:
select * from BillingInfo
where
cast(left(expiryDate, 2) as int) --take the month portion of the expiry date
< select datepart(mm, getdate()) --expiry month must be before current month
and
cast(right(expiryDate, 2) as int) --take the year portion of the expiry date
<= select datepart(yy, getdate()) % 1000 --expiry year must also be during or before current year
The % 1000 converts the year to two-digit format.
I guess you want this:
SELECT TOP 4 *
FROM BillingInfo
WHERE ExpiryDate in ('08/10','09/11','08/16','10/17')
This will find you the 4 items with those dates.
Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 8 years ago.
Improve this question
I have a requirement, there is a table CR. Type is a field here. Now it has values like A,B,C. I need separate counts for CR records having type A in month Jan,Feb,...Dec 2013.Same for Type B and C in SQL SERVER 2008. A, B, C will be column headers and count of each for a month-year combination comes under it.
Can someone help me here?
You need google first before asking this...
SELECT * FROM
(SELECT type, datepart(mm, date) month, count(1) cn
FROM CR
WHERE datepart(yyyy, date) = '2013'
GROUP BY type, datepart(mm, date)
) AS t
PIVOT(MIN(cn) FOR type IN ([A], [B], [C])) AS m
Assuming that the CR table have a datetime field called dat you can GROUP BY MONTH in this way :
SELECT DATEPART(Month, dat) AS month, Type
FROM CR
WHERE DATEPART(Year, dat) = 2013
GROUP BY Type , DATEPART(Year, dat), DATEPART(Month, dat)
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 9 years ago.
Improve this question
SELECT
VEHICLE_ID,
DRIVER_ID,
SUM(AMOUNT) as Total_Amount,
SUM(ODOMETER) as Total_Odometer,
SUM(LTR) As Total_Ltr
FROM Fuel_Filling
WHERE
COMPANY_ID = #COMPANY_ID
AND VEHICLE_ID = #VEHICLE_ID
AND DATE_TIME BETWEEN #FROM_DATE AND #TO_DATE
GROUP BY VEHICLE_ID, DRIVER_ID
I want to select ODOMETER Between Dates
If ODOMETER is truly the odometer reading then this should work:
SELECT
VEHICLE_ID,
DRIVER_ID,
SUM(AMOUNT) as Total_Amount,
MAX(ODOMETER) - MIN(ODOMETER) as DistanceTravelled,
SUM(LTR) As Total_Ltr
FROM Fuel_Filling
WHERE
COMPANY_ID = #COMPANY_ID
AND VEHICLE_ID = #VEHICLE_ID
AND DATE_TIME BETWEEN #FROM_DATE AND #TO_DATE
GROUP BY VEHICLE_ID, DRIVER_ID