SELECT using Correlated Sub Queries and multiple tables - sql

I have two tables (Table1 & Table2). One including a date (tDate) column and an ID (tTillNo) column that I would like to group by. And another with values (pValue) that I would like to sum for each date and ID.
The tables can be joined using the 'tName' and 'pName' columns from each table, however the pName column may have duplicates, which I would like to include in the sum.
For the 3 sub queries I also need to include the where clauses using the 'pCode' column.
And I would like to return the columns shown here
Using the below code however returns the sub queries with incorrect totals as I am using 'MAX(a.tName)' to prevent the grouping of this field. Could anyone suggest another way of preventing this or an alternative way of going about this query?
SELECT
DATENAME(Month,a.tDate) as 'Month Name', DAY(a.tDate) as 'Day',
MONTH(a.tDate) as 'Month', YEAR(a.tDate) as 'Year', a.tTillNo,
BankedCash=(SELECT ISNULL(CAST(SUM(b.pValue) as numeric(12,2)),0)
FROM Table2 b
where MAX(a.tName)=b.pName AND pCode = 'CSH'
or pCode = 'CHQ'),
CardTransactions=(SELECT ISNULL(CAST(SUM(b.pValue) as numeric(12,2)),0)
FROM Table2 b
where MAX(a.tName)=b.pName AND b.pCode = 'CRD'),
BankingTotal=(SELECT ISNULL(CAST(SUM(b.pValue) as numeric(12,2)),0)
FROM Table2 b
where MAX(a.tName)=b.pName AND b.pCode = 'CSH' or
b.pCode = 'CHQ' or b.pCode = 'CRD')
FROM Table1 a
group by YEAR(a.tDate), MONTH (a.tDate), DATENAME(Month,a.tDate),
DAY(a.tDate), a.tTillNo
order by YEAR(a.tDate), MONTH (a.tDate), DATENAME(Month,a.tDate)
Any suggestions or article referrals would be highly appreciated. Many thanks in advance.
CREATE TABLE [dbo].[Table1](
[UniqueID] [int] IDENTITY(1,1) NOT NULL,
[tTillNo] [varchar](4) NULL,
[tName] [varchar](20) NULL,
[tDate] [datetime] NULL)
INSERT INTO Table1 (tTillNo, tName, tDate)
VALUES ('0101', '01010000001', '2018-10-30 00:00:00.000'),
('0101', '01010000002', '2018-10-30 00:00:00.000'),
('0102', '01020000001', '2018-10-30 00:00:00.000'),
('0102', '01020000002', '2018-10-30 00:00:00.000')
CREATE TABLE [dbo].[Table2](
[UniqueID] [int] IDENTITY(1,1) NOT NULL,
[pName] [varchar](20) NULL,
[pCode] [varchar](10) NULL,
[pValue] [decimal](22, 7) NULL)
INSERT INTO Table2 (pName, pCode, pValue)
VALUES ('01010000001', 'CRD', '100.0000000'),
('01010000002', 'CSH', '100.0000000'),
('01020000001', 'CHQ', '100.0000000'),
('01020000002', 'CSH', '100.0000000'),
('01020000002', 'CRD', '100.0000000'),
('01010000001', 'CSH', '100.0000000')

I think you can solve this all with a join.
select DATENAME(Month,a.tDate) as 'Month Name'
, DAY(a.tDate) as 'Day'
, MONTH(a.tDate) as 'Month'
, YEAR(a.tDate) as 'Year'
, a.tTillNo
, BankedCash=SUM(case when pCode in( 'CSH','CHQ') then pvalue else 0 end)
, [Card] = SUM(case when pCode in( 'CRD') then pvalue else 0 end)
,Total = SUM(pvalue)
from TableA a
join TableB b on a.tName=b.pName
group by YEAR(a.tDate)
, MONTH (a.tDate)
, DATENAME(Month,a.tDate)
, DAY(a.tDate)
, a.tTillNo

Related

SQL order by needs to check if DATETIME2 is not null then return them first and after order by id

I have two tables and I have trouble figuring out how to do the order by statement to fit my needs.
Basically if the FeaturedUntil column if greater than now then these should be returned first ordered by the PurchasedAt column. Most recent purchases should be first. After these everything should be ordered by the item Id column descending.
Create Table Script
create table Items(
[Id] [int] IDENTITY(1,1) NOT NULL,
[Name] nvarchar(200) null,
)
create table Feature(
[Id] [int] IDENTITY(1,1) NOT NULL,
[PurchasedAt] [datetime2](7) NOT NULL,
[FeaturedUntil] [datetime2](7) NOT NULL,
[ItemId] [int] NOT NULL,
)
Insert Script
insert into Items(Name) values ('test1')
insert into Feature(PurchasedAt, FeaturedUntil, ItemId) values (dateadd(day, -3, getdate()), dateadd(month, 1, getdate()), ##IDENTITY)
insert into Items(Name) values ('test2')
insert into Feature(PurchasedAt, FeaturedUntil, ItemId) values (dateadd(day, -2, getdate()), dateadd(month, 1, getdate()), ##IDENTITY)
insert into Items(Name) values ('test3')
insert into Feature(PurchasedAt, FeaturedUntil, ItemId) values (dateadd(day, -1, getdate()), dateadd(month, -1, getdate()), ##IDENTITY)
insert into Items(Name) values ('test4')
Select Script
select *
from Items i
left join Feature f on i.Id = f.ItemId
order by
case when f.FeaturedUntil is not null THEN f.PurchasedAt
else i.Id
end
The select should return test2 first as it's FeaturedUntil is greater than now and it is the most recently purchased, second row should be test1 as it is bought before test2. After these should be test4 and last one is test3, because these have no joining Feature table data or the FeatureUntil is not greater than now and these are order by their Item.Id descending.
SELECT *
FROM items i
LEFT JOIN feature f
ON i.id = f.itemid
ORDER BY CASE
WHEN f.featureduntil > getdate THEN purchasedat
ELSE '19000101'
END DESC,
id DESC
You need to order this in descending in order to get the most recent purchase first; the ID sort will still occur, so if you have two PurchasedAt's that are the same, it would sort those 2 by ID.
Based on what you've told us, I think this might be what you're after:
ORDER BY CASE WHEN FeaturedUntil > GETDATE THEN PurchasedAt ELSE '99991231' END ASC, --Future features first, and in date order
--(past have a silly in future date, so always last
Id; --Then ID
Try the following.
select *, case when f.FeaturedUntil is not null THEN f.PurchasedAt else NULL end AS PurchasedAtNew
from Items i
left join Feature f on i.Id = f.ItemId
order by PurchasedAtNew desc, i.Id

Tuning a query converting subquery

I was trying to change an update statement where there is a subquery in the WHERE clause to a join in order to improve performance.
DDL to sample table/data
CREATE TABLE [dbo].[Table1](
[pre_plan_id] [smallint] NULL,
[pre_type_id] [smallint] NULL,
[associate_id] [smallint] NOT NULL,
[deleted] [bit] NOT NULL
)
INSERT INTO Table1
VALUES
(NULL, NULL, -32768, 0),
(1, NULL, 2, 1),
(1, NULL, 3, 0),
(NULL, NULL, 3109, 0),
(1, NULL, 3109, 1)
CREATE TABLE [dbo].[Table2](
[type_id] [smallint] NOT NULL,
[plan_id] [smallint] NOT NULL,
[associate_id] [smallint] NOT NULL,
[time_in] [smalldatetime] NOT NULL
)
INSERT INTO Table2
VALUES
(390, 31, 3109, '2009-09-02'),
(304, 32, 3109, '2010-02-05'),
(388, 31, 3109, '2010-09-24')
The query that uses a subquery:
SELECT pre_plan_id, pre_type_id FROM Table1 WHERE pre_plan_id =1
AND associate_id NOT IN
(SELECT TOP 2 associate_id
FROM Table2 WHERE time_in= '2010-09-24 00:00:00' group by associate_id order by count(*) desc)
My attempt of converting it to JOIN
SELECT pre_plan_id
FROM (SELECT pre_plan_id, pre_type_id, rn
FROM Table1 a
LEFT JOIN
( select associate_id, Row_number() over (partition by associate_id order by count(*) desc ) rn
FROM Table2
WHERE time_in= '2010-09-24 00:00:00' Group by associate_id) b
ON a.associate_id = b.associate_id where b.rn <> 1) a
where pre_plan_id = 1
However, this displays nothing while I expect two rows; and it is happening because of b.rn <> 1
I expected it would display the NULL values when it is b.rn <> 1
Any explanation for this? Any guide on better approach of tuning the query is much appreciated.
Thanks.
You can just move the subquery to the FROM clause and use LEFT JOIN:
SELECT t1.pre_plan_id, t1.pre_type_id
FROM Table1 t1 LEFT JOIN
(SELECT TOP 2 t2.associate_id
FROM Table2
WHERE t2.time_in= '2010-09-24 00:00:00'
GROUP BY associate_id
ORDER BY COUNT(*) DESC
) t2
ON t2.associate_id = t1.associate_id
WHERE t1.pre_plan_id = 1 AND t2.associate_id IS NULL;

SQL Server SQL Statement - Updating record

I have a data as below:
I need to update Matching_id and Matching_Type by using column id, region, company, dept, subdept and amountsepend. The logic is:
Sum AmountSepend by Region, Company, Dept and SubDept. If the sum amount is 0 then Matching_Type is 'Match' and Matching_id is the combination of the id for the matched record else 'Not Match' and Matching_id is the id. **SUM means the total sum of all records for same criteria regardless the AmountSepend is positive or negative.
Another important criteria is if the transaction is single record, meaning the total count by grouping by Region, Company, Dept and SubDept is 1 then Matching type is Not Match and Matching_UID is id regardless the AmountSepend is 0 or positive/negative value. Example id 8.
Below is the output:
Here the table and data script
CREATE TABLE [dbo].[StackoverflowQuest](
[id] [int] NOT NULL,
[Region] [varchar](50) NULL,
[Company] [varchar](50) NULL,
[Dept] [varchar](50) NULL,
[SubDept] [varchar](50) NULL,
[AmountSepend] [float] NULL,
[Matching_id] [varchar](100) NULL,
[Matching_Type] [varchar](100) NULL
) ON [PRIMARY]
How could I achieved such result ? Any help/hint would be appreciate
CREATE TABLE #Table(Id INT,Region VARCHAR(100),Company INT,Dept INT,SubDept
INT,AmtSpend INT,MatchingId VARCHAR(100),MatchingType VARCHAR(100))
INSERT INTO #Table(Id ,Region ,Company , Dept ,SubDept ,AmtSpend )
SELECT 1,'NAM',12378,1,NULL,900 UNION ALL
SELECT 2,'NAM',12378,1,NULL,-900 UNION ALL
SELECT 3,'NAM',12370,1,23,1000 UNION ALL
SELECT 4,'ASA',1234,9,12,5000 UNION ALL
SELECT 5,'NAM',12370,1,23,-1000 UNION ALL
SELECT 6,'ASA',1234,9,12,800 UNION ALL
SELECT 7,'ASA',1234,9,12,-600 UNION ALL
SELECT 8,'ASA',12311,6,NULL,200
UPDATE #Table SET MatchingId = MatchIds,MatchingType = 'Match'
FROM
(
SELECT T2.Company,STUFF( ( SELECT ',' + CAST(T3.Id AS VARCHAR) FROM #Table
T3 WHERE T2.Company = T3.Company FOR XML PATH('')),1,1,'') MatchIds
FROM #Table T2
JOIN
(
SELECT T1.Company Company,SUM(T1.AmtSpend) Total
FROM #Table T1
GROUP BY T1.Company
HAVING SUM(T1.AmtSpend) = 0
)A ON A.Company = T2.Company
GROUP BY T2.Company
) A
WHERE A.Company = #Table.Company
UPDATE #Table SET MatchingId = CAST(Id AS VARCHAR),MatchingType = 'Not
Match' WHERE ISNULL(MatchingId,'') = ''
SELECT * FROM #Table

In SQL replace null value with another value

Here is my SQL Query
SELECT p.StudentID, ai.RollNo, p.FirstName, p.MiddleName, p.LastName,
om.ExamID, et.ExamName, om.SubjectID,
ISNULL(CONVERT(varchar(20),om.ObtainedMarksTheory), 'A') as 'ObtainedMarksTheory',
ISNULL(CONVERT(varchar(20),om.ObtainedPracticalMarks),'A') as 'ObtainedPracticalMarks'
FROM Students.PersonalInfo p
INNER JOIN Students.AcademicCourse ac on p.StudentID = ac.StudentID
INNER JOIN Students.AcademicInfo ai on p.StudentID=ai.StudentID
LEFT OUTER JOIN Exam.ObtainedMarkEntry om on p.StudentID = om.StudentID
LEFT JOIN Exam.ExamType et on om.ExamID = et.ExamID
WHERE ai.BatchID = '103' AND ai.SemesterID = '21' and ac.Section = '8'
This produce result as in a picture:
But I want result like this since those two students were absent in that exam
Similarly if another Exam does exists for any of three student and other are absent same procedure should repeat
Use IsNULL() Function see below example
Declare #variable varchar(MAX)
set #variable = NULL
select IsNULL(#variable,0) as A
Let's have the following sample data (it is like your sample data, just inserted in one table):
DECLARE #DataSource TABLE
(
[StudentID] TINYINT
,[RowNo] TINYINT
,[FirstName] VARCHAR(12)
,[MiddleName] VARCHAR(12)
,[LastName] VARCHAR(12)
,[ExamID] TINYINT
,[ExamName] VARCHAR(18)
,[SubjectID] TINYINT
,[ObtainedMarksTheory] VARCHAR(12)
,[ObtainedPracticalMarks] VARCHAR(12)
);
INSERT INTO #DataSource ([StudentID], [RowNo], [FirstName], [MiddleName], [LastName], [ExamID], [ExamName], [SubjectID], [ObtainedMarksTheory], [ObtainedPracticalMarks])
VALUES (101, 1, 'FN_A', 'MN_A', 'LN_A', NULL, NULL, NULL, 'A', 'A')
,(102, 2, 'FN_B', 'MN_B', 'LN_B', 28, 'First Tem2072', 97, '74.00', '56.00')
,(103, 3, 'FN_C', 'MN_C', 'LN_C', NULL, NULL, NULL, 'A', 'A');
SELECT *
FROM #DataSource;
So, I am supposing we have details only for one exam here and the question is how to replace the NULL values of ExamID, ExamName and SubjectID columns using the existing values.
The solution is to use the MAX function with OVER clause:
SELECT [StudentID]
,[RowNo]
,[FirstName]
,[MiddleName]
,[LastName]
,MAX([ExamID]) OVER() AS [ExamID]
,MAX([ExamName]) OVER() AS [ExamName]
,MAX([SubjectID]) OVER() AS [SubjectID]
,[ObtainedMarksTheory]
,[ObtainedPracticalMarks]
FROM #DataSource;
Note, that it is better to use the OVER clause as you are not going to add GROUP BY clauses in your initial query. Also, if you have details for more exams you can use the PARTITION BY clause (if you have some column(s) to distinguish the NULL rows for each exam).

Correlated join with outer fields as parameters

I need a way to join a table with a function's results. I'm not sure if it's possible and somehow I don't think it's a good idea. Let me try and explain the situation.
There is a table [Entities]:
[ID] [Description] [kWh] [kVArh] [kVAh] [OfferID] [CustomOfferID]
Another table [Data]:
[ID] [Timestamp] [Value]
And a function:
[Calc] (#offer, #customOffer, #kWh, #kVArh, #kVAh, #dtStart, #dtEnd)
So you can see there are entities, the entities' data (usage) and a function to calculate the cost.
I need to display the usage and cost of multiple entites:
[Description] [MWh] [MVA] [Cost]
[MWh] will be a SUM of all the entity's data over the period; [MVA] will be the MAX of all the data over the period; and [Cost] will SUM the [Cost] field from the sub query (function).
The query I figured would do the jobs looks like this:
SELECT [tc].[ID], [tc].[Desc]
, SUM([kWh].[Value]) / 1000 AS [MWh]
, MAX([kVAh].[Value]) / 1000 AS [MVA]
, SUM([cost].[Cost])
FROM [Tree_Cost] AS [tc]
INNER JOIN [Data] AS [kWh] ON [tc].[kWh] = [kWh].[ID]
INNER JOIN [Data] AS [kVAh] ON [tc].[kVAh] = [kVAh].[ID]
INNER JOIN
(
SELECT [tc].[ID], [Cost]
FROM [Calc] ([tc].[Offer_ID], [tc].[OfferCustom_ID], [tc].[kWh], [tc].[KVArh], [tc].[kVAh], #dtStart, #dtEnd)
) AS [cost] ON [tc].[ID] = [cost].[ID]
WHERE [tc].[Type] = 1 AND [tc].[TypeDesc] = 'GF_K_M'
AND [kWh].[Timestamp] BETWEEN #dtStart AND #dtEnd
AND [kVAh].[Timestamp] BETWEEN #dtStart AND #dtEnd
GROUP BY [tc].[ID], [tc].[Desc]
The real problem here is that I need to include the [ID] from the outer query in the result set of the inner query (function) in order to be able to join the two. Then I also need to be able to use the fields from the outer query as arguments for the inner query (function).
This is obviously not the way seeing as the [tc] identifier is not recognized in the inner query. So how am I supposed to accomplish something like this?
CREATE FUNCTION [dbo].[Calc]
( \#intOffer [int]
, \#intCustom [int]
, \#intP [int]
, \#intQ [int]
, \#intS [int]
, \#dtStart [datetime]
, \#dtEnd [datetime]
)
RETURNS TABLE
( [Entry] [nvarchar](200) NULL
, [Rate] [float] NULL
, [Unit] [nvarchar](50) NULL
, [Reading] [float] NULL
, [Cost] [float] NULL
, [DDate] [nvarchar](50) NULL
)
WITH EXECUTE AS CALLER
AS EXTERNAL NAME [OfferCalcLite].[UserDefinedFunctions].[SqlArray]
I'm not sure I understand correctly. Perhaps you can completely drop the JOIN:
INNER JOIN
(
SELECT [tc].[ID], [Cost]
FROM [Calc] ([tc].[Offer_ID], [tc].[OfferCustom_ID], [tc].[kWh], [tc].[KVArh], [tc].[kVAh], #dtStart, #dtEnd)
) AS [cost] ON [tc].[ID] = [cost].[ID]
and change:
, SUM([cost].[Cost])
into:
, SUM( [Calc] ( [tc].[Offer_ID]
, [tc].[OfferCustom_ID]
, [tc].[kWh]
, [tc].[KVArh]
, [tc].[kVAh]
, #dtStart, #dtEnd
)
) AS Cost