Generate an excel like report from SQL - sql-server

I am novice to SQL and
I have two tables Ticket and TicketAttributes with following Schema
CREATE TABLE [dbo].[Ticket](
[TicketID] [int] IDENTITY(1,1) NOT NULL, --Primary key
[Category] [varchar](256) NOT NULL,
[Description] [varchar](256) NULL,
[LibID] [int] NOT NULL,
[Status] [smallint] NULL,
[LogID] [int] NULL)
Ticket Attributes
CREATE TABLE [dbo].[TicketAttributes](
[TicketID] [int] NOT NULL,
[TicketAttrID] [int] IDENTITY(1,1) NOT NULL,
[AttributeID] [int] NOT NULL,
[AttributeGroup] [varchar](255) NULL,
[AttributeValue] [nvarchar](max) NULL,
[Status] [smallint] NULL,
[LogID] [int] NULL)
Where Ticket Attribute is another table that stores different attributes of a ticket like TicketStatus, TicketCategory etc..
Now I need to generate a report that looks like
TicketStatus1 TicketStatus 2 TicketStatus3
-----------------------------------------------------------------
TicketCategory1 7 3
Ticketcategory2 4
TicketCategory3 8
I want to see the count of each of the status of each ticket category.
For Eg:-
I have the following Data in TicketTable
----------------------------------------------
TicketID Name Price Date
------------------------------------------------
155 Ticket4 $20 16 Jan 2016
157 Ticket3 $300 17 Jan 2016
158 Ticket1 $100 18 Jan 2016
159 Ticket2 $500 19 Jan 2016
Now in the TicketAttribute Table
----------------------------------------------
TicketID AttributeID AttributeValue
------------------------------------------------
155 500 Joe
155 600 Reserved
155 700 Economy
155 800 San Jose
where AttributeIDs
500=Nameofthe Passenger
600= Status of Ticket
700= Class
800= Destination
Now lets say I want to see what is the count of number of active tickets in each of the class per status
Booked Cancelled PaymentPending ............
-----------------------------------------------------------------
Economy 7 3
Economy Plus 4
Business 8
Hope I am clear now.
how to go about this using SQL Query

USING PIVOT
;WITH cte AS (
SELECT
c.AttributeValue as Class
,s.AttributeValue as StatusOfTicket
FROM
Ticket t
LEFT JOIN TicketAttributes c
ON t.TicketId = c.TicketId
AND c.AttributeID = 700
LEFT JOIN TicketAttributes s
ON t.TicketId = s.TicketId
AND s.AttributeID = 600
)
SELECT *
FROM
cte
PIVOT (
COUNT(StatusOfTicket) FOR StatusOfTicket IN (Reserved,Cancelled,PaymentPending)
) p
USING Conditional Aggregation:
SELECT
c.AttributeValue as Class
,COUNT(DISTINCT CASE WHEN s.AttributeValue = 'Reserved' THEN c.TicketId END) as Reserved
,COUNT(DISTINCT CASE WHEN s.AttributeValue = 'Cancelled' THEN c.TicketId END) as Cancelled
,COUNT(DISTINCT CASE WHEN s.AttributeValue = 'PaymentPending' THEN c.TicketId END) as PaymentPending
FROM
Ticket t
LEFT JOIN TicketAttributes c
ON t.TicketId = c.TicketId
AND c.AttributeID = 700
LEFT JOIN TicketAttributes s
ON t.TicketId = s.TicketId
AND s.AttributeID = 600
GROUP BY
c.AttributeValue

Related

How to horizontally insert data into a table from another table where this data is positioned vertically?

I currently have a data in a table positioned vertically:
FILE ID: 001
RECORD 1 1111
RECORD 2 123456789012345
RECORD 3 A01 11
RECORD 4 A02 11
RECORD 5 A03 11
RECORD 6 0103050
RECORD 7 777
RECORD 8 A01 1
RECORD 9 A02 1
RECORD 10 A03 1111
RECORD 11 A04 11111
FILE ID: 002
RECORD 1 2222
RECORD 2 1234567
RECORD 3 A01 11
RECORD 4 A02 11
RECORD 5 A03 11
RECORD 6 0103050
RECORD 7 777
RECORD 8 A01 1
RECORD 9 A02 1
RECORD 10 A03 1111
RECORD 11 A04 11111
FILE ID: 003
RECORD 1 3333
RECORD 2 1234567
RECORD 3 A01 11
RECORD 4 A02 11
RECORD 5 A03 11
RECORD 6 0103050
RECORD 7 777
RECORD 8 A01 1
RECORD 9 A02 1
RECORD 10 A03 1111
RECORD 11 A04 11111
How can I insert it into another table so it is positioned horizontally, the following way:
FileID|Record1|Record2|Record3|Record4|Record5|Record6|Record7|Record8|Record9|Record10|Record11
--------------------------------------------------------------------------------------------------
001 |1111 |1111111|A01 11|A02 11|A03 11|0103050|777 |A01 1|A02 1|A03 1111|A04 11111
002 |2222 |1234567|A01 11|A02 11|A03 11|0103050|777 |A01 1|A02 1|A03 1111|A04 11111
003 |3333 |1234567|A01 11|A02 11|A03 11|0103050|777 |A01 1|A02 1|A03 1111|A04 11111
Thank's
I'm a little late to the party, but I'll give it a shot. This will only work if your data is consistent with file id followed by 11 records. First, you need to have a table with an identity column. I would make it a habit to do so when creating tables. Do your bulk insert into the following table. This will store your data with row id's which will be important later.
CREATE TABLE [dbo].[Table_1](
[TableId] [bigint] IDENTITY(1,1) NOT NULL,
[Column1] [varchar](255) NULL,
[Column2] [varchar](255) NULL
) ON [PRIMARY]
GO
Create this table for the pivoted data.
CREATE TABLE [dbo].[Table_2](
[Table2ID] [bigint] IDENTITY(1,1) NOT NULL,
[FileID] [varchar](255) NULL,
[Record1] [varchar](255) NULL,
[Record2] [varchar](255) NULL,
[Record3] [varchar](255) NULL,
[Record4] [varchar](255) NULL,
[Record5] [varchar](255) NULL,
[Record6] [varchar](255) NULL,
[Record7] [varchar](255) NULL,
[Record8] [varchar](255) NULL,
[Record9] [varchar](255) NULL,
[Record10] [varchar](255) NULL,
[Record11] [varchar](255) NULL
) ON [PRIMARY]
GO
Now to get the data from table 1 to table 2. I will use a Common Table Expression (CTE) and the LEAD function.
WITH preselect AS
(
SELECT Column1
,Column2 AS 'FileID'
,LEAD(Column2,1,0) OVER(ORDER BY TableId) AS 'Record1'
,LEAD(Column2,2,0) OVER(ORDER BY TableId) AS 'Record2'
,LEAD(Column2,3,0) OVER(ORDER BY TableId) AS 'Record3'
,LEAD(Column2,4,0) OVER(ORDER BY TableId) AS 'Record4'
,LEAD(Column2,5,0) OVER(ORDER BY TableId) AS 'Record5'
,LEAD(Column2,6,0) OVER(ORDER BY TableId) AS 'Record6'
,LEAD(Column2,7,0) OVER(ORDER BY TableId) AS 'Record7'
,LEAD(Column2,8,0) OVER(ORDER BY TableId) AS 'Record8'
,LEAD(Column2,9,0) OVER(ORDER BY TableId) AS 'Record9'
,LEAD(Column2,10,0) OVER(ORDER BY TableId) AS 'Record10'
,LEAD(Column2,11,0) OVER(ORDER BY TableId) AS 'Record11'
FROM Table_1
)
INSERT INTO Table_2
SELECT FileID,Record1,Record2,Record3,Record4,Record5,Record6,Record7
,Record8,Record9,Record10,Record11
FROM preselect
WHERE Column1 = 'FILE ID:'
I am ordering by the TableId in the LEAD function to ensure the order of the data. Then it is just a matter of getting the FileId value along with the values of the next 11 rows.
LEAD (Transact-SQL
Common Table Expression (CTE)

How could I make a series of joins work with max value when aggregates do not work in them?

I'm looking only to get classification ids which are between the valid year range in classification. I'm using left joins because NULLs should be permitted.
I have tables:
CREATE TABLE classifications (
[id] [bigint] IDENTITY(1,1) NOT NULL,
[classification_code] [varchar](20) NOT NULL,
[description] [varchar](255) NULL,
[valid_from] [int] NULL,
[valid_to] [int] NULL
--Rest of constraints...
)
insert into classifications (classification_code, description, valid_from, valid_to)
values ('05012','Classification Number 1',2007,2012),
('05012','Classification Number 1',2013,2016),
('05012','Classification Number 1',2017,2020).
('12043','Classification Number 2',2007,2010),
('12043','Classification Number 2',2011,2020),
('12345','Classification Number 3',2013,2015),
('12345','Classification Number 3',2016,2020),
('54321','Classification Number 4',2007,2009),
('54321','Classification Number 4',2010,2013),
('54321','Classification Number 4',2014,2020)
CREATE TABLE comm_info_a (
[id] [bigint] IDENTITY(1,1) NOT NULL,
[comm_code] [nchar](10) NOT NULL, /*should be unique*/
[classification_code] [nchar](6) NULL,
[thing] [nchar](6) NULL
--Rest of constraints...
)
insert into comm_info_a (comm_code, classification_code)
values ('0100100000','54321'),
('8090010000','05012'),
('5002310010','12043'),
('0987654321','54321')
CREATE TABLE comm_info_b (
[id] [bigint] IDENTITY(1,1) NOT NULL,
[comm_code] [nchar](10) NOT NULL, /*should be unique*/
[classification_code] [nchar](6) NULL
--Rest of constraints...
)
insert into comm_info_b (comm_code, classification_code)
values ('0100100000','12043'),
('8090010000','00000'),
('5002310010','05012'),
('1234567890','12345')
CREATE TABLE transactions (
[comm_code] [varchar](50) NULL,
[year] [varchar](255) NULL
--Rest of constraints...
)
insert into transactions (comm_code, year) values
('0100100000', 2013),
('0100100000', 2015),
('0100100000', 2017),
('8090010000', 2009),
('8090010000', 2010),
('8090010000', 2011),
('8090010000', 2015),
('8090010000', 2017),
('8090010000', 2018),
('5002310010', 2008),
('5002310010', 2014),
And finally:
CREATE TABLE comm (
[id] [bigint] IDENTITY(1,1) NOT NULL,
[comm_code] [varchar](20) NULL, /*should be unique*/
[fk_classification_id_a] [bigint] NULL,
[fk_classification_id_b] [bigint] NULL
--Rest of constraints...
)
I am working on a query to insert comms from transactions, and comms should have unique comm_code
The query is as follows:
INSERT INTO comm
(comm_code,
fk_classification_id_a,
fk_classification_id_b)
SELECT comm_code,
ca.id,
cb.id,
MAX(year)
FROM transactions t
LEFT JOIN comm_info_a mia ON mia.comm_code=t.comm_code
LEFT JOIN comm_info_b mib ON mib.comm_code=t.comm_code
--these next two joins obviously do not work so I'm looking for something like it. Treat them as 'pseudo-code'
LEFT JOIN classifications ca ON ca.classification_code=mia.classification_code AND
MAX(t.year) BETWEEN ca.valid_from AND ca.valid_to
LEFT JOIN classifications cb ON cb.classification_code=mib.classification_code AND
MAX(t.year) BETWEEN cb.valid_from AND cb.valid_to
-- end of the two joins
WHERE NOT EXISTS
(SELECT DISTINCT comm_code FROM comm)
GROUP BY
t.comm_code
t.classification_code
So in the end I'm looking to get something like this as a result:
comm_code | fk_classification_id_a | fk_classification_id_b
-----------|------------------------|-----------------------
0100100000 | 5 | 10
8090010000 | 3 | NULL
5002310010 | 5 | 2
Please note that the comm_code is unique in this table!! Therefore: i want the comms on the newest transactions (thus the aggegate max year), and they should have the ids of the classification that the transaction year is in.
The real query is much more complex and longer but this pretty much covers all bases. Take a look into what is commented. I understand that it should be doable with a sub query of some sort, and I've tried, but so far I haven't found a way to pass aggregates to subqueries.
How could I tackle this problem?
Revised answer uses a common table expression to calculate the maximum year per comm_code and to exclude the comm_codes not wanted in the final result. After that the joins to the classification tables are straight forward as we have the comm_max_year value on each row to use in the joins.
with transCTE as (
select
t.*
, max(t.year) over(partition by comm_code) comm_max_year
from transactions t
left join comm on t.comm_code = comm.comm_code -- this table not in sample given
where comm.comm_code IS NULL -- use instead of NOT EXISTS
)
SELECT DISTINCT
t.comm_code
, ca.id as fk_classification_id_a
, cb.id as fk_classification_id_b
, t.comm_max_year
FROM transCTE t
LEFT JOIN comm_info_a mia ON mia.comm_code = t.comm_code
LEFT JOIN classifications ca ON mia.classification_code = ca.classification_code
AND t.comm_max_year BETWEEN ca.valid_from AND ca.valid_to
LEFT JOIN comm_info_b mib ON mib.comm_code = t.comm_code
LEFT JOIN classifications cb ON mib.classification_code = cb.classification_code
AND t.comm_max_year BETWEEN cb.valid_from AND cb.valid_to
ORDER BY
t.comm_code
;
GO
comm_code | fk_classification_id_a | fk_classification_id_b | comm_max_year
:--------- | :--------------------- | :--------------------- | :------------
0100100000 | 10 | 5 | 2017
5002310010 | 5 | 2 | 2014
8090010000 | 3 | null | 2018
Demo at dbfiddle here
CREATE TABLE transactions (
[comm_code] [varchar](50) NULL,
[year] [varchar](255) NULL
--Rest of constraints...
)
insert into transactions (comm_code, year) values
('0100100000', 2013),
('0100100000', 2015),
('0100100000', 2017),
('8090010000', 2009),
('8090010000', 2010),
('8090010000', 2011),
('8090010000', 2015),
('8090010000', 2017),
('8090010000', 2018),
('5002310010', 2008),
('5002310010', 2014)
;
GO
11 rows affected
CREATE TABLE classifications (
[id] [bigint] IDENTITY(1,1) NOT NULL,
[classification_code] [varchar](20) NOT NULL,
[description] [varchar](255) NULL,
[valid_from] [int] NULL,
[valid_to] [int] NULL
--Rest of constraints...
)
insert into classifications (classification_code, description, valid_from, valid_to)
values ('05012','Classification Number 1',2007,2012),
('05012','Classification Number 1',2013,2016),
('05012','Classification Number 1',2017,2020),
('12043','Classification Number 2',2007,2010),
('12043','Classification Number 2',2011,2020),
('12345','Classification Number 3',2013,2015),
('12345','Classification Number 3',2016,2020),
('54321','Classification Number 4',2007,2009),
('54321','Classification Number 4',2010,2013),
('54321','Classification Number 4',2014,2020)
;
GO
10 rows affected
CREATE TABLE comm_info_a (
[id] [bigint] IDENTITY(1,1) NOT NULL,
[comm_code] [nchar](10) NOT NULL, /*should be unique*/
[classification_code] [nchar](6) NULL,
[thing] [nchar](6) NULL
--Rest of constraints...
);
GO
✓
insert into comm_info_a (comm_code, classification_code)
values ('0100100000','54321'),
('8090010000','05012'),
('5002310010','12043'),
('0987654321','54321')
;
GO
4 rows affected
CREATE TABLE comm_info_b (
[id] [bigint] IDENTITY(1,1) NOT NULL,
[comm_code] [nchar](10) NOT NULL, /*should be unique*/
[classification_code] [nchar](6) NULL
--Rest of constraints...
);
GO
✓
insert into comm_info_b (comm_code, classification_code)
values ('0100100000','12043'),
('8090010000','00000'),
('5002310010','05012'),
('1234567890','12345');
GO
4 rows affected

How to get the year wise employee as per the promotion

How to get the year wise employee as per the promotion.
Example:
Employee Number 'A-001' Join on '01-07-2013' then the O/P will be showing in image.
As Employee Number 'A-001' there is no promotion in 2014 then in Last_Designation,Promoted_Designation, Last_Gross and Promoted_Gross
need to be same as 2013 or previous one.
Below is my SQL Query with Data.
CREATE TABLE [dbo].[Employee](
[Emp_No] [numeric](18, 0) NULL,
[Emp_Number] [nvarchar](50) NULL,
[Emp_Name] [nvarchar](50) NULL,
[Emp_JoiningDate] [date] NULL,
[Emp_ResignDate] [date] NULL,
[Emp_Status] [nvarchar](50) NULL,
[Emp_CurrentDesignation] [nvarchar](50) NULL,
[Emp_CurrentGross] [numeric](18, 0) NULL
) ON [PRIMARY]
GO
INSERT INTO [UserDB].[dbo].[Employee]
([Emp_No]
,[Emp_Number]
,[Emp_Name]
,[Emp_JoiningDate]
,[Emp_ResignDate]
,[Emp_Status]
,[Emp_CurrentDesignation]
,[Emp_CurrentGross])
VALUES
(1,'A-001','Alex','2012-07-01',null,'On Board','Trainee3',2000)
GO
INSERT INTO [UserDB].[dbo].[Employee]
([Emp_No]
,[Emp_Number]
,[Emp_Name]
,[Emp_JoiningDate]
,[Emp_ResignDate]
,[Emp_Status]
,[Emp_CurrentDesignation]
,[Emp_CurrentGross])
VALUES
(2,'A-002','Smith','2014-07-01','2015-07-01','Resigned','HR1',1500)
GO
CREATE TABLE [dbo].[Promotion](
[Prom_No] [numeric](18, 0) NULL,
[Prom_EmpNo] [numeric](18, 0) NULL,
[Last_Designation] [nvarchar](500) NULL,
[Promoted_Designation] [nvarchar](500) NULL,
[WEF_Date] [date] NULL,
[Promoted_Gross] [numeric](18, 0) NULL,
[Last_Gross] [numeric](18, 0) NULL
) ON [PRIMARY]
GO
INSERT INTO [UserDB].[dbo].[Promotion]
([Prom_No]
,[Prom_EmpNo]
,[Last_Designation]
,[Promoted_Designation]
,[WEF_Date]
,[Promoted_Gross]
,[Last_Gross])
VALUES
(1,1,'Trainee1','Trainee2','2013-11-01',1000,500)
GO
INSERT INTO [UserDB].[dbo].[Promotion]
([Prom_No]
,[Prom_EmpNo]
,[Last_Designation]
,[Promoted_Designation]
,[WEF_Date]
,[Promoted_Gross]
,[Last_Gross])
VALUES
(2,1,'Trainee2','Trainee3','2015-03-01',2000,1000)
GO
Try my script with various sample data and let me know if it not working any particular sample data.
First you can create and populate one year table which can be
permanent .In my example it is temporary.
create table #Year(yr int primary key)
insert into #Year
select (ROW_NUMBER()over(order by number)+1900) from master..spt_values
--select * from #Year
Main query start from here.
;WITH CTE
AS (
SELECT Emp_No NewEmpNo
,min(year(emp_joiningdate)) minYear
,max(year(case when Emp_ResignDate is not null then Emp_ResignDate else getdate() END)) maxYear
FROM [dbo].[Employee]
GROUP BY Emp_No
)
,CTE1
AS (
SELECT c.yr
,p.NewEmpNo
,e.*
FROM cte p
CROSS APPLY (
SELECT yr
FROM #Year c
WHERE c.yr >= minYear
AND c.yr <= maxYear
) c
LEFT JOIN (
SELECT e.*
,p.*
,year(isnull(p.WEF_Date,e.Emp_JoiningDate)) PYr
FROM dbo.employee E
LEFT JOIN [dbo].[Promotion] P ON e.Emp_No = p.Prom_EmpNo
) e ON p.NewEmpNo = e.Emp_No
AND c.yr = e.pyr
)
--select * from CTE1
,CTE2 as
(
SELECT yr
,NewEmpNo
,isnull(c.Emp_Number, prv.Emp_Number) Emp_Number
,isnull(c.Emp_Name, prv.Emp_Name) Emp_Name
,isnull(c.Emp_JoiningDate, prv.Emp_JoiningDate) Emp_JoiningDate
,isnull(c.Emp_ResignDate, prv.Emp_ResignDate) Emp_ResignDate
,isnull(c.Emp_Status, prv.Emp_Status) Emp_Status
,isnull(c.Emp_CurrentDesignation, prv.Emp_CurrentDesignation) Emp_CurrentDesignation
,isnull(c.Emp_CurrentGross, prv.Emp_CurrentGross) Emp_CurrentGross
,COALESCE(c.Last_Designation, prv.Last_Designation
,c.Emp_CurrentDesignation,prv.Emp_CurrentDesignation) Last_Designation
,COALESCE(c.Promoted_Designation, prv.Promoted_Designation
,c.Emp_CurrentDesignation,prv.Emp_CurrentDesignation) Promoted_Designation
,COALESCE(c.Last_Gross, prv.Last_Gross
,c.Emp_CurrentGross, prv.Emp_CurrentGross) Last_Gross
,COALESCE(c.Promoted_Gross, prv.Promoted_Gross
,c.Emp_CurrentGross, prv.Emp_CurrentGross) Promoted_Gross
,prv. PYr
--,prv1.*
FROM cte1 c
outer APPLY (
SELECT TOP 1 prve.*
,prvp.*
,year(isnull(WEF_Date,Emp_JoiningDate))Pyr
FROM dbo.employee prve
LEFT JOIN [dbo].[Promotion] prvp ON prve.Emp_No = prvp.Prom_EmpNo
WHERE (c.yr >= year(isnull(WEF_Date,Emp_JoiningDate))
)
AND c.NewEmpNo = prve.Emp_No
ORDER BY isnull(WEF_Date,Emp_JoiningDate) DESC
) prv
)
select
yr
,NewEmpNo
,isnull(c.Emp_Number, prv.Emp_Number) Emp_Number
,isnull(c.Emp_Name, prv.Emp_Name) Emp_Name
,isnull(c.Emp_JoiningDate, prv.Emp_JoiningDate) Emp_JoiningDate
,isnull(c.Emp_ResignDate, prv.Emp_ResignDate) Emp_ResignDate
,isnull(c.Emp_Status, prv.Emp_Status) Emp_Status
,isnull(c.Emp_CurrentDesignation, prv.Emp_CurrentDesignation) Emp_CurrentDesignation
,isnull(c.Emp_CurrentGross, prv.Emp_CurrentGross) Emp_CurrentGross
,COALESCE(c.Last_Designation, prv.Last_Designation
,c.Emp_CurrentDesignation,prv.Emp_CurrentDesignation) Last_Designation
,COALESCE(c.Promoted_Designation, prv.Promoted_Designation
,c.Emp_CurrentDesignation,prv.Emp_CurrentDesignation) Promoted_Designation
,COALESCE(c.Last_Gross, prv.Last_Gross
,c.Emp_CurrentGross, prv.Emp_CurrentGross) Last_Gross
,COALESCE(c.Promoted_Gross, prv.Promoted_Gross
,c.Emp_CurrentGross, prv.Emp_CurrentGross) Promoted_Gross
,prv. PYr
from cte2 c
outer apply(
SELECT TOP 1 prve.*
,prvp.*
,year(isnull(WEF_Date,Emp_JoiningDate))Pyr
FROM dbo.employee prve
LEFT JOIN [dbo].[Promotion] prvp ON prve.Emp_No = prvp.Prom_EmpNo
WHERE (c.yr <= year(isnull(WEF_Date,Emp_JoiningDate))
)
AND c.NewEmpNo = prve.Emp_No
ORDER BY isnull(WEF_Date,Emp_JoiningDate)
)prv
ORDER BY yr
DROP TABLE #Year
The CTE [Year] is to get the list of years from the Emp_JoiningDate and ResignDate. After that just CROSS join to the Employee table
For the promotion information (prev and promoted), use OUTER APPLY to get the last row by WEF_Date
; with
[Year] as
(
select [Year] = min(datepart(year, e.Emp_JoiningDate))
from [Employee] e
union all
select [Year] = y.[Year] + 1
from [Year] y
where [Year] < datepart(year, getdate())
)
select y.[Year], e.Emp_No, e.Emp_Number, e.Emp_Name, e.Emp_JoiningDate,
e.Emp_ResignDate, e.Emp_Status,
e.Emp_CurrentDesignation, e.Emp_CurrentGross,
Last_Designation = coalesce(p.Last_Designation, e.Emp_CurrentDesignation),
Promoted_Designation= coalesce(p.Promoted_Designation, e.Emp_CurrentDesignation),
Last_Gross = coalesce(p.Last_Gross, e.Emp_CurrentGross),
Promoted_Gross = coalesce(p.Promoted_Gross, e.Emp_CurrentGross)
from [Employee] e
cross join [Year] y
outer apply
(
select top 1 *
from [Promotion] x
where x.Prom_EmpNo = e.Emp_No
and datepart(year, x.[WEF_Date]) <= y.[Year]
order by x.WEF_Date desc
) p
where y.[Year] >= datepart(year, e.Emp_JoiningDate)
and y.[Year] <= datepart(year, isnull(e.Emp_ResignDate, getdate()))
order by y.[Year], e.Emp_No
RESULT
2013 1 A-001 Alex 2013-07-01 NULL On Board Trainee3 2000 Trainee1 Trainee2 500 1000
2014 1 A-001 Alex 2013-07-01 NULL On Board Trainee3 2000 Trainee1 Trainee2 500 1000
2014 2 A-002 Smith 2014-07-01 2015-07-01 Resigned HR1 1500 HR1 HR1 1500 1500
2015 1 A-001 Alex 2013-07-01 NULL On Board Trainee3 2000 Trainee2 Trainee3 1000 2000
2015 2 A-002 Smith 2014-07-01 2015-07-01 Resigned HR1 1500 HR1 HR1 1500 1500
2016 1 A-001 Alex 2013-07-01 NULL On Board Trainee3 2000 Trainee2 Trainee3 1000 2000
2017 1 A-001 Alex 2013-07-01 NULL On Board Trainee3 2000 Trainee2 Trainee3 1000 2000

SQL Server join query not retrieving proper result

Select the list of all dealers whose vehicles are never assigned to any customer i.e that vehicle is not present in PS3_VehicleBooking, and committed date is greater than today's date.
3 tables to be used are-
CREATE TABLE PS3_Dealer(
ID int IDENTITY(1,1) NOT NULL Primary KEY,
DealerID varchar(255) NULL,
DealerName varchar(255) NOT NULL,
ContactNo varchar(255) NOT NULL,
NoOfVehicles bigint NOT NULL,
CommittedDate date NOT NULL
);
create table PS3_Vehicle
(
ID int IDENTITY(1,1) NOT NULL Primary Key,
DealerID varchar(255) NOT NULL,
VehicleID varchar(255) UNIQUE NOT NULL,
VehicleName varchar(255) NOT NULL,
Capacity BIGINT NOT NULL
);
create table PS3_VehicleBooking
(
ID int IDENTITY(1,1) NOT NULL Primary Key,
BookingID varchar(255) NOT NULL UNIQUE,
VehicleID varchar(255) NOT NULL,
StartDate date NOT NULL,
EndDate date NOT NULL
);
PS3_Dealer values-
DealerID DealerName ContactNo NoOfVehicles CommittedDate
1 ng 9 5 2013-12-12
2 nikki 5 7 2013-12-25
3 nik 4 6 2013-10-11
4 hj 2 2 2014-11-10
5 pk 67 8 2013-10-10
PS3_Vehicle
DealerID VehicleID VehicleName Capacity
1 V1 ind 4
2 V2 innova 5
3 V3 innova 6
1 V4 ula 8
4 V5 hkk 2
5 V6 ghi 9
2 V7 bjhgi 4
PS3_VehicleBooking-
BookingID VehicleID StartDate EndDate
1 V1 2013-12-13 2013-12-17
2 V2 2013-10-11 2013-10-13
3 V3 2014-12-10 2014-12-13
4 V4 2012-10-10 2012-10-13
5 V2 2013-12-14 2013-12-18
expected outcome-
DealerID DealerName ContactNo NoOfVehicles CommittedDate
4 hj 2 2 2014-11-10
but i am getting-
DealerID DealerName ContactNo NoOfVehicles CommittedDate
4 hj 2 2 2014-11-10
2 Nikki 5 7 2013-12-25
i dont want dealer id 2 in result because V2 provided by dealer id 2 is present in PS3_Booking.
My query is-
SELECT h.DealerID,
h.DealerName,
h.ContactNo,
h.NoOfVehicles,
h.CommittedDate
FROM PS3_Dealer h
INNER JOIN(SELECT DealerID,
PS3_VehicleBooking.VehicleID
FROM PS3_Vehicle
LEFT JOIN PS3_VehicleBooking
ON PS3_Vehicle.VehicleID = PS3_VehicleBooking.VehicleID) w
ON h.DealerID = w.DealerID
WHERE w.VehicleID IS NULL
AND h.CommittedDate > GETDATE()
please correct where i am wrong?
The problem is that DealerID 2 has 2 Vehicles. One that has booking, and the other does not. When you run
SELECT DealerID,
PS3_VehicleBooking.VehicleID
FROM PS3_Vehicle
LEFT JOIN PS3_VehicleBooking
ON PS3_Vehicle.VehicleID = PS3_VehicleBooking.VehicleID
You will get one record that has DealerID 2 with NULL VehicleID and the other none NULL Vehicle ID.
You can try to the run the statement below to get the desired results (may not be the most optimal, but it is a fix to your statement):
SELECT h.DealerID,
h.DealerName,
h.ContactNo,
h.NoOfVehicles,
h.CommittedDate
FROM PS3_Dealer h
INNER JOIN(SELECT DealerID,
MAX(PS3_VehicleBooking.VehicleID) as VehicleID
FROM PS3_Vehicle
LEFT JOIN PS3_VehicleBooking
ON PS3_Vehicle.VehicleID = PS3_VehicleBooking.VehicleID
GROUP BY DealerID
) w
ON h.DealerID = w.DealerID
WHERE w.VehicleID IS NULL
AND h.CommittedDate > GETDATE()
Edited to actually answer the question
You were getting Nikki because there does exist a car for her (V7) that is not in the booking table.
This should exclude Nikki.
SELECT h.DealerID,
h.DealerName,
h.ContactNo,
h.NoOfVehicles,
h.CommittedDate
FROM PS3_Dealer d
WHERE NOT EXISTS
(SELECT *
FROM PS3_VehicleBookingb
JOIN PS3_Vehiclev ON b.vehicleID = v.VehicleID
JOIN PS3_Dealerd2 ON d2.dealerID = v.dealerID
WHERE d2.dealerID = d.dealerID)
AND CommittedDate > Current_date()
http://sqlfiddle.com/#!2/fb365/19

CTE - recursively update quantity until total consumed

I've been researching CTEs trying to determine if it's possible to recursively update inventory quantity records with an order quantity until the order quantity is consumed.
Here are the tables and records:
CREATE TABLE [dbo].[myOrder](
[Account] [float] NOT NULL,
[Item] [float] NOT NULL,
[Quantity] [float] NOT NULL
) ON [PRIMARY]
insert into dbo.myOrder values (12345, 1, 50)
CREATE TABLE [dbo].[myInventory](
[ID] [int] IDENTITY(1,1) NOT NULL,
[Account] [float] NOT NULL,
[InvDate] [numeric](18, 0) NOT NULL,
[Item] [float] NOT NULL,
[Quantity] [float] NOT NULL,
[QuantitySold] [float] NOT NULL
) ON [PRIMARY]
insert into dbo.myInventory values (12345, 111287, 1, 45, 40)
insert into dbo.myInventory values (12345, 111290, 1, 40, 0)
insert into dbo.myInventory values (12345, 111290, 1, 12, 0)
insert into dbo.myInventory values (12345, 111291, 1, 25, 0)
The record in the myOrder table indicates that an order is to be created for account 12345 for item #1, quantity 50:
Account Item Quantity
------- ---- --------
12345 1 50
The inventory table shows that we have plenty of item #1 on hand for account 12345:
ID Account InvDate Item Quantity QuantitySold
-- ------- ------- ---- -------- ------------
1 12345 111287 1 45 40
2 12345 111290 1 40 0
3 12345 111290 1 12 0
4 12345 111291 1 25 0
The goal is to start plugging in the order quantity of 50 into the inventory records until all 50 are consumed. Inventory records are ordered by the value in the InvDate column. Record 1 has 5 remaining quantity (45 - 40 = 5), which would leave us with 45 more to consume for the order. Record 2 can consume 40. Record 3 can consume the last 5. When the query completes the inventory records would look like this:
ID Account InvDate Item Quantity QuantitySold
-- ------- ------- ---- -------- ------------
1 12345 111287 1 45 45
2 12345 111290 1 40 40
3 12345 111290 1 12 5
4 12345 111291 1 25 0
Note: The inventory table stores QuantitySold, not QuantityRemaining, so you have to do the math (Quantity - QuantitySold) to determine how much quantity remains per inventory record.
I've gotten almost nowhere with the CTE. I've found plenty of examples for doing selects where you have 2 parts to your CTE - an initialization part and the recursive part UNIONed together. I could write this with a cursor, but I think it's possible to do with a CTE and I'd like to learn how.
If anyone can confirm this is possible with a CTE or explain how to set up the CTE, I'd appreciate it. Thanks!
--#inserted table mimics inserted virtual table from AFTER INSERT triggers on [dbo].[myOrder] table
DECLARE #inserted TABLE
(
[Account] [float] NOT NULL,
[Item] [float] NOT NULL,
[Quantity] [float] NOT NULL
);
INSERT #inserted
VALUES (12345, 1, 50);
WITH CteRowNumber
AS
(
SELECT inv.ID
,inv.Account
,inv.Item
,inv.Quantity
,inv.QuantitySold
,i.Quantity QuantityOrdered
,ROW_NUMBER() OVER(PARTITION BY inv.Account,inv.Item ORDER BY inv.ID ASC) RowNumber
FROM myInventory inv
INNER JOIN #inserted i ON inv.Account = i.Account
AND inv.Item = i.Item
WHERE inv.Quantity > inv.QuantitySold
), CteRecursive
AS
(
SELECT a.ID
,a.Account
,a.Item
,a.RowNumber
,CASE
WHEN a.Quantity - a.QuantitySold < a.QuantityOrdered THEN a.Quantity - a.QuantitySold
ELSE a.QuantityOrdered
END QuantitySoldNew
,CASE
WHEN a.Quantity - a.QuantitySold < a.QuantityOrdered THEN a.Quantity - a.QuantitySold
ELSE a.QuantityOrdered
END RunningTotal
FROM CteRowNumber a
WHERE a.RowNumber = 1
UNION ALL
SELECT crt.ID
,crt.Account
,crt.Item
,crt.RowNumber
,CASE
WHEN prev.RunningTotal + (crt.Quantity - crt.QuantitySold) < crt.QuantityOrdered THEN crt.Quantity - crt.QuantitySold
ELSE crt.QuantityOrdered - prev.RunningTotal
END QuantitySoldNew
,CASE
WHEN prev.RunningTotal + (crt.Quantity - crt.QuantitySold) < crt.QuantityOrdered THEN prev.RunningTotal + (crt.Quantity - crt.QuantitySold)
ELSE crt.QuantityOrdered
END RunningTotal
FROM CteRecursive prev
INNER JOIN CteRowNumber crt ON prev.Account = crt.Account
AND prev.Item = crt.Item
AND prev.RowNumber + 1 = crt.RowNumber
WHERE prev.RunningTotal < crt.QuantityOrdered
)
SELECT cte.ID
,cte.Account
,cte.Item
,cte.QuantitySoldNew
FROM CteRecursive cte;
--or CteRecursive can be used to update QuantitySold column from [dbo].[myInventory] table
--UPDATE myInventory
--SET QuantitySold = inv.QuantitySold + cte.QuantitySoldNew
--FROM myInventory inv
--INNER JOIN CteRecursive cte ON inv.ID = cte.ID;

Resources