Debit v Credit
Hi, Can someone assist me? I am trying to produce a total by month and year for all months for this year and last year even if there are no records present. I also need my second query to be taken from the first query to give me a total of debits v
credits.
How do i join the two queries together to get my results?
Query 1
SELECT
th
.addressIdForPrimary
,MONTH(th.TransactionDate)
AS
'Month'
,YEAR(th.TransactionDate)
AS
'Year'
,CONVERT(nvarchar(4),YEAR(th.TransactionDate))
+
RIGHT(N'0'
+
CONVERT(nvarchar(2),
MONTH(th.TransactionDate)),
2)
AS
'MonthYear'
,Quantity
=
(Case
when
sum(th.TotalQuantity)
is
null
then 0
else
sum(th.TotalQuantity)
End)
, Total
=
(Case
when
SUM(tf.TotalNettRounded)
is
null
then 0
else
SUM(tf.TotalNettRounded)
End)
FROM
transactionheader
as th
INNER
JOIN
transactionfigures
as tf
ON tf.transactionheaderId
= th.Id
AND th.CompanyDivisionId
= 1
AND tf.CategoryIdForCurrency
= 62
INNER
JOIN
WorkflowStage
ON WorkflowStage.Id
= th.WorkflowStageId
AND WorkflowStage.Id
IN
('48')
WHERE
th
.addressIdForPrimary
= 7319
GROUP
BY
th
.addressIdForPrimary
,YEAR(th.TransactionDate)
,MONTH(th.TransactionDate)
ORDER
BY th.addressIdForPrimary
Query 2
SELECT
th
.addressIdForPrimary
,MONTH(th.TransactionDate)
AS
'Month'
,YEAR(th.TransactionDate)
AS
'Year'
,CONVERT(nvarchar(4),YEAR(th.TransactionDate))
+
RIGHT(N'0'
+
CONVERT(nvarchar(2),
MONTH(th.TransactionDate)),
2)
AS
'MonthYear'
,Quantity
=
(Case
when
sum(th.TotalQuantity)
is
null
then 0
else
sum(th.TotalQuantity)
End)
, Total
=
(Case
when
SUM(tf.TotalNettRounded)
is
null
then 0
else
SUM(tf.TotalNettRounded)
End)
FROM
as th
INNER
JOIN
tf ON tf.transactionheaderId
= th.Id
AND th.CompanyDivisionId
= 1
AND tf.CategoryIdForCurrency
= 62
INNER
JOIN
WorkflowGroupStage
ON WorkflowGroupStage.WorkflowStageId
= th.WorkflowStageId
AND WorkflowGroupStage.WorkflowGroupId
IN
('14')
WHERE
th
.addressIdForPrimary
= 7319
GROUP
BY
th
.addressIdForPrimary
,YEAR(th.TransactionDate)
,MONTH(th.TransactionDate)
ORDER
BY th.addressIdForPrimary
,YEAR(th.TransactionDate)
,MONTH(th.TransactionDate)
M Stoker
November 25th, 2010 11:37am
Hi LordLucan,
Firstly, you need to define a date range for both quiries, fro example Jan - Jun 2010.
You could download a calendar table from the web (search google).
Define two parameters - @DateFrom and @DateTo.
Selecting [yyyymm column] from a calendar tabe gives all rows (year-month) for that date range (you need year because date range could be Oct 2009 - Mar 2010).
Declare a temp table with the following columns -[yyyymm column] (to hold your rows headers), [debit], [credit]
Insert into temp table SELECT DISTINCT [yyyymm column] FROM [calendar table] where [date] between @DateFrom and @DateTo.
Update columns [debit] and [credit] for [yyyymm column].
SELECT * from temp table
You are done.Sergei
Free Windows Admin Tool Kit Click here and download it now
November 25th, 2010 6:17pm