Not every row is shown in a query - sql-server

I have a query to calculate the number of transactions and the number of payments in my table. These counts are based on data in separated tables. I need to group the results by year and the week of year. The dates are coming also from a separate table. I add the week values in an union of the payments and transactions in a with clause and do afterwards the calculation.
WITH CHART_DATES
AS (
SELECT DD.CAL_YEAR, DD.WEEK_OF_YEAR
FROM DIM_DATE DD
RIGHT JOIN FACT_PAY_PAYMENT FPP ON DD.ID = FPP.REQUESTED_EXECUTION_DATE_ID
GROUP BY DD.CAL_YEAR, DD.WEEK_OF_YEAR
UNION
SELECT DD.CAL_YEAR, DD.WEEK_OF_YEAR
FROM DIM_DATE DD
RIGHT JOIN FACT_AS_BALANCE FAB ON DD.ID = FAB.BALANCE_DATE_ID
GROUP BY DD.CAL_YEAR, DD.WEEK_OF_YEAR
)
SELECT
CD.WEEK_OF_YEAR,
CD.CAL_YEAR,
COALESCE(COUNT(DISTINCT FPP.ID), 0) AS PAYMENTS,
COALESCE(COUNT(DISTINCT FAB.ACCOUNT_STATEMENT_ID), 0) AS STATEMENTS
FROM FACT_PAY_PAYMENT FPP
RIGHT JOIN DIM_DATE DD ON FPP.REQUESTED_EXECUTION_DATE_ID = DD.ID
FULL JOIN FACT_AS_BALANCE FAB ON DD.ID = FAB.BALANCE_DATE_ID
RIGHT JOIN CHART_DATES CD ON DD.CAL_YEAR = CD.CAL_YEAR
AND DD.WEEK_OF_YEAR = CD.WEEK_OF_YEAR
INNER JOIN DIM_AS_CHARACTERISTICS DAC ON FAB.BALANCE_TYPE_ID = DAC.ID
WHERE DAC.TRANSACTION_INTRADAY_FLAG = 'N'
GROUP BY CD.CAL_YEAR, CD.WEEK_OF_YEAR
I added following data in my tables:
Week 42 of 2015 has 1 payment and 2 transactions
Week 41 of 2015 has 1 payment and 0 transactions
After executing the query, I receive following result:
Wrong result
What is wrong in my query and how to fix it?

Related

Include all rows from table 1 and summed rows from another

I'm trying to sum rows into categories. I have a table (r) with the categories and a view with the transactions (v). For the result, I need all categories regardless if there is data in the table v. Here's the code I have, it only return all rows from the r table when there is corresponding data in v.
SELECT SUM(ISNULL(v.PledgeAmount, 0)) AS Range1Pledge,
COUNT(DISTINCT v.CONSTITUENT_ID) AS Range1Count,
r.RangeTitle
FROM view_CASA_KPIDonorPyramidStep2 as v RIGHT OUTER JOIN
tbl_CASA_KPI_GiftRanges as r ON
v.RangeTitle = r.RangeTitle
WHERE (v.CAMPAIGN_ID = '2018') AND (v.PledgeDate < '2/1/2018')
GROUP BY r.RangeTitle
WHERE logic was moved to LEFT JOIN condition in order to prevent filtering of categories that have no transactions
RIGHT join turned into LEFT
2/1/2018 replaced by locale independent format, assuming that it is 01 Feb 2018
SQL:
SELECT r.RangeTitle,
SUM(ISNULL(v.PledgeAmount, 0)) AS Range1Pledge,
COUNT(DISTINCT v.CONSTITUENT_ID) AS Range1Count
FROM tbl_CASA_KPI_GiftRanges as r
LEFT OUTER JOIN view_CASA_KPIDonorPyramidStep2 as v
ON v.RangeTitle = r.RangeTitle
AND ( (v.CAMPAIGN_ID = '2018') AND (v.PledgeDate < '20180201') )
GROUP BY r.RangeTitle

How to place subquery in SQL Server 2008

I have a main SQL query which returns stock quantity in dates between and I also need to return the stock as on date along with between dates result.
Main query:
SELECT
TT.TranRId, MAX(TT.TInQty) AS InQty, MAX(TT.TOutQty) AS OutQty
FROM
TReg TR, TTrans TT
WHERE
TR.TRegId = TT.TrRegId
AND TT.Stid = 2
AND TR.TransDate BETWEEN '2018-08-25' AND '2018-08-28'
GROUP BY
TT.TranRId
ORDER BY
TT.TranRId
Sub query:
(SELECT TT.TransRId, (SUM(TT.TInQty) - SUM(TT.TOutQty))
FROM TTrans TT, TReg TR
WHERE TR.TransDate <= '2018-08-24'
AND TR.TRegId = TT.TrRegId
AND TT.Stid = 2 GROUP BY TT.TranRId) --AS Stock
Please help where I should include my sub query in my main query
To get output as follows:
TransRId Stock InQty OutQty
----------------------------------
41 700 1 1000
42 800 5 500
I am not sure I am following your question 100%, if you are just looking to join it as a sub query the below logic should work.
SELECT TT.TranRId
,MAX(TT.TInQty) AS InQty
,MAX(TT.TOutQty) AS OutQty
,Stock.[Sum]
FROM TReg TR
LEFT JOIN TTrans TT
ON TR.TRegId = TT.TrRegId
LEFT JOIN (
SELECT TT.TransRId
,(SUM(TT.TInQty) - SUM(TT.TOutQty)) as Sum
FROM TTrans TT
LEFT JOIN TReg TR
ON TR.TRegId = TT.TrRegId
WHERE TR.TransDate <= '2018-08-24'
AND TT.Stid = 2
GROUP BY TT.TranRId
) AS Stock
ON Stock.TranRId = TT.TranRId
WHERE TT.Stid = 2
AND TR.TransDate BETWEEN '2018-08-25' AND '2018-08-28'
GROUP BY TT.TranRId
ORDER BY TT.TranRId
edit:
I noticed tt.TranRId and tt.Tran***s***RId if this is not a typo it would need to be corrected, if not my answer will not work for you.
If you need specific dates, including the date in the join logic along with the ID would give you the appropriate result...without knowing your data set I am not sure if the TranRId is unique...sorry about that!

How to display only the MAX results from a query

I am new to writing MS SQL queries and I am trying to display only the record with the highest field named RecordVersion.
Below is the query that works but displays all records:
SELECT
PriceCalendars.PriceProgramID,
PriceCalendars.EffectiveDateTime,
PriceSchedules.Price,
PriceSchedules.PLU,
items.Descr,
PriceSchedules.LastUpdate,
PriceSchedules.LastUpdatedBy,
PriceSchedules.RecordVersion,
PriceSchedules.PriceScheduleUniqueID
FROM
PriceCalendars
INNER JOIN PriceSchedules ON PriceCalendars.PriceProgramID = PriceSchedules.PriceProgramID
INNER JOIN items ON PriceSchedules.PLU = items.PLU
WHERE
(PriceSchedules.PLU = 'SLS10100103')
AND (PriceCalendars.EffectiveDateTime = '2016-03-22')
Here are the query results:
PriceProgramID EffectiveDateTime Price PLU Descr LastUpdate LastUpdatedBy RecordVersion PriceScheduleUniqueID
1 2016-03-22 00:00:00.000 35.00 SLS10100103 Architecture Adult from NP POS 2015-01-22 07:53:15.000 GX70,83 9 569
1 2016-03-22 00:00:00.000 32.00 SLS10100103 Architecture Adult from NP POS 2014-02-25 16:22:46.000 GX70,83 5 86180
The first line of the results has RecordVersion being 9 and the second line results is 5, I only want the higher record displaying, the one that returned RecordVersion = 9.
Every time I try to use the MAX command I get errors or the group by and I have tried every example I could find on the web but nothing seems to work.
Using MS SQL 2012.
Thanks,
Ken
Try the following query which attempts to solve your problem by ordering the returned rows by RecordVersion DESC and then SELECTs just the first row.
SELECT TOP 1
PriceCalendars.PriceProgramID,
PriceCalendars.EffectiveDateTime,
PriceSchedules.Price,
PriceSchedules.PLU,
items.Descr,
PriceSchedules.LastUpdate,
PriceSchedules.LastUpdatedBy,
PriceSchedules.RecordVersion,
PriceSchedules.PriceScheduleUniqueID
FROM
PriceCalendars
INNER JOIN PriceSchedules ON PriceCalendars.PriceProgramID = PriceSchedules.PriceProgramID
INNER JOIN items ON PriceSchedules.PLU = items.PLU
WHERE
(PriceSchedules.PLU = 'SLS10100103')
AND (PriceCalendars.EffectiveDateTime = '2016-03-22')
ORDER BY
RecordVersion DESC
All group by columns should be in select ,that's the rule of group by.How group by works is for every distinct combination of group by columns,arrange remaining columns into groups,so that any aggregation can be applied,in your case I am not sure what group by columns are unique with out test date.here is one version which use row number which gives you the output desired
Remember ,order by last updated date is the one which decides rows order and assign numbers
WITH CTE
AS
(
SELECT PriceCalendars.PriceProgramID,
PriceCalendars.EffectiveDateTime,
PriceSchedules.Price,
PriceSchedules.PLU,
items.Descr,
PriceSchedules.LastUpdate,
PriceSchedules.LastUpdatedBy,
PriceSchedules.RecordVersion,
PriceSchedules.PriceScheduleUniqueID,
ROW_NUMBER() OVER (PARTITION BY PriceSchedules.RecordVersion ORDER BY PriceSchedules.LastUpdatedBy) AS RN
FROM
PriceCalendars
INNER JOIN PriceSchedules ON PriceCalendars.PriceProgramID = PriceSchedules.PriceProgramID
INNER JOIN items ON PriceSchedules.PLU = items.PLU
WHERE
(PriceSchedules.PLU = 'SLS10100103')
AND (PriceCalendars.EffectiveDateTime = '2016-03-22')
)
SELECT * FROM CTE WHERE RN=1

Exclude certain line depending on a certain condition in t-SQL

Firstly, I have created the following stock sales SELECT statement:
SELECT
PostST.TxDate AS TxDate,
PostST.Reference AS InvNum,
Client.Name AS CustomerName,
SalesRep.Code AS SalesRep,
WhseMst.Code AS Whse,
CONCAT (StkItem.Description_1, ' - ',StkItem.Code) AS Item,
_etblLotTracking.cLotDescription AS LotNumber,
CASE
WHEN PostST.TrCodeID = 34 THEN (PostST.Quantity * 1)
WHEN PostST.TrCodeID = 30 THEN (PostST.Quantity * -1)
ELSE 0
END AS QtySold,
CAST
(
CASE
WHEN PostST.TrCodeID = 34 THEN (PostST.Credit / PostST.Quantity)
WHEN PostST.TrCodeID = 30 THEN (PostST.Debit / (PostST.Quantity * -1))
ELSE 0
END
as [money]
)
AS CustomerPrice,
StkItem.ItemGroup AS ItemGroup,
StkItem.ulIIStockType AS StockType,
YEAR (PostST.TxDate) AS Year,
DATENAME (MONTH, (PostST.TxDate)) AS Month
FROM
PostST
INNER JOIN StkItem
ON StkItem.StockLink = PostST.AccountLink
INNER JOIN PostAR
ON PostAR.cAuditNumber = PostST.cAuditNumber
INNER JOIN Client
ON Client.DCLink = PostAR.AccountLink
INNER JOIN SalesRep
ON SalesRep.idSalesRep = PostAR.RepID
INNER JOIN WhseMst
ON WhseMst.WhseLink = PostST.WarehouseID
FULL JOIN _etblLotTracking
ON _etblLotTracking.idLotTracking = PostST.iLotID
WHERE
PostST.TrCodeID IN (34, 30) AND StkItem.ItemGroup <> 'TRAN'
This query is run from Sage Evolution and data is dumped into Excel. From there I manipulate to view each item and the sales history in quantity per month and year.
My problem is as follows:
Within the data that follows, is sometimes a customer who has perhaps stopped buying a particular item. What I need is for this statement to also filter out a line by the following condition:
If the customer does not have a sale for 6 months or more, then that STOCK ITEM must be filtered out for that specific customer.
Thank you!

For LOOP in SQL Server

Total EmpId in Database = 74.
Active Days for September 2013 in Databse = 22
I want to segregate the dates when the employee was not filled the production in MIS_Opus table.
Since FULL OUTER Join was not worked, I m using the except query. Through this query, I can get the unfilled dates for each employee by passing the empid thru c# function. For that loop will going to SQL & back to C# for 74 times. Please let me know how can I get the unfilled dates for all employees at one shot in SQL itself. I m using SQL 2012 and VS 2012 c#.
Thank you,
select _Date from MIS_BM_Calendar c
where c.Month = 'September 2013' and c.DayShiftStatus = 'active'
except
select _Date from MIS_Opus o
where o.EmpId=#Empid
One way is to build a list of all employee + day combinations using a cross join. Then you can use a left join to check if there is an employee entry for that day.
select days._Date as TheDay
, emps.EmpId as EmployeeWithMissingEntry
from (
select distinct _Date
from MIS_BM_Calendar
where Month = 'September 2013'
and DayShiftStatus = 'active'
) days
cross join -- One row for each combination of employee and date
(
select distinct EmpId
from MIS_Opus
) emps
left join
MIS_Opus o
on o._Date = days._Date
and o.EmpId = emps.EmpId
where o._Date is null -- Employee entry for day not found

Resources