Joining two selects into 4 columns - sql

I am trying to join two select statements to form 4 columns in one table. My desired result would be something like this
+--------------+----------------+------------------+--------------+
| ExpectedDate | ExpectedAmount | ActualDate | ActualAmount |
+--------------+----------------+------------------+--------------+
| 01/03/2014 | 65161 | 2014-03-01 00:00 | 57 |
| 02/03/2014 | 64 | 2014-03-02 00:00 | 321651651 |
| 03/03/2014 | 8 | 2014-03-03 00:00 | 233258646 |
| 04/03/2014 | 561 | 2014-03-04 00:00 | 2321 |
| 05/03/2014 | 651651 | 2014-03-05 00:00 | 8494561 |
| 06/03/2014 | 651616131 | NULL | NULL |
| 07/03/2014 | 316548478 | NULL | NULL |
| 08/03/2014 | 646512132 | NULL | NULL |
| 10/03/2014 | 654984984 | NULL | NULL |
| 11/03/2014 | 323213218 | NULL | NULL |
| 12/03/2014 | 6464651 | NULL | NULL |
| 13/03/2014 | 313218 | NULL | NULL |
| 14/03/2014 | 849898 | NULL | NULL |
| 15/03/2014 | 3213218 | NULL | NULL |
| 16/03/2014 | 9898465 | NULL | NULL |
+--------------+----------------+------------------+--------------+
I have the following SQL at the moment, which is very close, but instead of being in the same rows, they are split up like this:
+--------------+----------------+------------------+--------------+
| ExpectedDate | ExpectedAmount | ActualDate | ActualAmount |
+--------------+----------------+------------------+--------------+
| 01/03/2014 | 65161 | NULL | NULL |
| 02/03/2014 | 64 | NULL | NULL |
| 03/03/2014 | 8 | NULL | NULL |
| 04/03/2014 | 561 | NULL | NULL |
| 05/03/2014 | 651651 | NULL | NULL |
| 06/03/2014 | 651616131 | NULL | NULL |
| 07/03/2014 | 316548478 | NULL | NULL |
| 08/03/2014 | 646512132 | NULL | NULL |
| 10/03/2014 | 654984984 | NULL | NULL |
| 11/03/2014 | 323213218 | NULL | NULL |
| 12/03/2014 | 6464651 | NULL | NULL |
| 13/03/2014 | 313218 | NULL | NULL |
| 14/03/2014 | 849898 | NULL | NULL |
| 15/03/2014 | 3213218 | NULL | NULL |
| 16/03/2014 | 9898465 | NULL | NULL |
| NULL | NULL | 2014-03-01 00:00 | 57 |
| NULL | NULL | 2014-03-02 00:00 | 321651651 |
| NULL | NULL | 2014-03-03 00:00 | 233258646 |
| NULL | NULL | 2014-03-04 00:00 | 2321 |
| NULL | NULL | 2014-03-05 00:00 | 8494561 |
+--------------+----------------+------------------+--------------+
Here is the SQL:
--DECLARE PERIOD
DECLARE #NextMonthDate VARCHAR(10)
DECLARE #currentMonthDate VARCHAR(10)
DECLARE #VarientVar VARCHAR(10)
DECLARE #ExpectedDateSample VARCHAR(10)
--SET PERIOD
SET #NextMonthDate = CONVERT(VARCHAR(4), DATEPART(YEAR, DATEADD(MONTH, -1, GETDATE())))+'-'+SUBSTRING(CONVERT(VARCHAR, DATEADD(MONTH, +1, GETDATE()), 112),5,2)
SET #currentMonthDate = CONVERT(VARCHAR(4), DATEPART(YEAR, GETDATE()))+'-'+SUBSTRING(CONVERT(VARCHAR, DATEADD(MONTH, +0, GETDATE()), 112),5,2)
SET #ExpectedDateSample = SUBSTRING(CONVERT(VARCHAR, DATEADD(MONTH, +0, GETDATE()), 112),5,2)+'/'+CONVERT(VARCHAR(4), DATEPART(YEAR, GETDATE()))
SELECT e.ExpectedDate, e.ExpectedAmount, a.ActualDate, a.ActualAmount
FROM (SELECT ExpectedDate, SUM(ExpectedPayment) ExpectedAmount
FROM Simplicity..V_PaymentPlan2
WHERE Period >= #currentMonthDate and Period < #NextMonthDate AND HyphenStatus != 'Unpaid' AND ExpectedDate LIKE '%'+#ExpectedDateSample+'%'
GROUP BY ExpectedDate) e
FULL JOIN
(SELECT CONVERT(VARCHAR, PaymentDate, 20) ActualDate, SUM(ActualPayment) ActualAmount
FROM Simplicity..V_PaymentPlan2
WHERE Period >= #currentMonthDate and Period < #NextMonthDate AND HyphenStatus != 'Unpaid' AND CONVERT(VARCHAR, PaymentDate, 20) LIKE '%'+#currentMonthDate+'%'
GROUP BY PaymentDate) a on a.ActualDate = e.ExpectedDate
ORDER BY e.ExpectedDate, a.ActualDate
Any idea how to make them line up in the same rows instead of inserting NULL and pushing them down?
I dont have any Primary Key or Foreign Key.

SELECT ExpectedDate,ExpectedAmount, ActualDate, ActualAmount FROM A a1
INNER JOIN A a2
ON a1.ExpectedDate = a2.ActualDate

Related

How to format data based on the reason column in sql

I am having Two Tables
1)Activity Table (main table)
2)History Table
Table Structure:
Activity Table:
CREATE TABLE [dbo].[CheckInCheckOutActivity](
[CheckInCheckOutId] [int] IDENTITY(1,1) NOT NULL,
[TimeTicketId] [int] NULL,
[EmployeeNbr] [varchar](50) NULL,
[NetWorkNbr] [varchar](50) NULL,
[Reason] [varchar](100) NULL,
[Type] [varchar](50) NULL,
[InsertTime] [datetime] NULL,
[UpdateTime] [datetime] NULL,
[UserTime] [datetime] NULL,
[TimeTicketDate] [datetime] NULL,
PRIMARY KEY CLUSTERED
(
[CheckInCheckOutId] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
) ON [PRIMARY]
GO
History Table:
CREATE TABLE [dbo].[CheckInCheckOutHistoryDetails](
[CheckInCheckoutHistoryId] [int] IDENTITY(1,1) NOT NULL,
[CheckinCheckOutId] [int] NULL,
[UserTime] [nvarchar](50) NULL,
[InsertTime] [datetime] NULL,
[Type] [nvarchar](50) NULL,
[Reason] [varchar](100) NULL,
PRIMARY KEY CLUSTERED
(
[CheckInCheckoutHistoryId] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
) ON [PRIMARY]
GO
Activity Table:
+-------------------+--------------+-------------+------------+------------------------------+-------------+-------------------------+-------------------------+-------------------------+-------------------------+
| CheckInCheckOutId | TimeTicketId | EmployeeNbr | NetWorkNbr | Reason | Type | InsertTime | UpdateTime | UserTime | TimeTicketDate |
+-------------------+--------------+-------------+------------+------------------------------+-------------+-------------------------+-------------------------+-------------------------+-------------------------+
| 352 | 5492822 | 10020168 | 71018655 | Initial Check In | Checked In | 2022-02-18 08:08:21.847 | 2022-02-18 08:19:49.097 | 2022-02-18 06:15:00.000 | 2022-02-18 00:00:00.000 |
| 353 | 5492822 | 10020168 | 71018655 | Check Out For Meal Break | Checked Out | 2022-02-18 08:17:31.883 | 2022-02-18 08:17:55.870 | 2022-02-18 08:00:00.000 | 2022-02-18 00:00:00.000 |
| 354 | 5492822 | 10020168 | 71018655 | Check In After Meal Break | Checked In | 2022-02-18 08:19:05.657 | 2022-02-18 08:19:30.110 | 2022-02-18 08:30:00.000 | 2022-02-18 00:00:00.000 |
| 355 | 5492822 | 10020168 | 71018655 | Check Out For Change Network | Checked Out | 2022-02-18 08:21:52.623 | 2022-02-18 08:22:23.370 | 2022-02-18 09:30:00.000 | 2022-02-18 00:00:00.000 |
+-------------------+--------------+-------------+------------+------------------------------+-------------+-------------------------+-------------------------+-------------------------+-------------------------+
2)History Table:
By Passing this Query we will be getting Usertime and Insertime from the History Table
select * from CheckInCheckOutHistoryDetails where CheckinCheckOutId=353
+--------------------------+-------------------+---------------------+-------------------------+-------------+------------------------------+
| CheckInCheckoutHistoryId | CheckinCheckOutId | UserTime | InsertTime | Type | Reason |
+--------------------------+-------------------+---------------------+-------------------------+-------------+------------------------------+
| 669 | 352 | Feb 18 2022 6:03AM | 2022-02-18 08:09:11.430 | Checked In | Initial Check In |
| 671 | 352 | Feb 18 2022 6:15AM | 2022-02-18 08:19:49.097 | Checked In | Initial Check In |
| 670 | 353 | Feb 18 2022 8:00AM | 2022-02-18 08:17:55.870 | Checked Out | Check Out For Meal Break |
| 672 | 354 | Feb 18 2022 8:30AM | 2022-02-18 08:19:30.110 | Checked In | Check In After Meal Break |
| 673 | 355 | Feb 18 2022 9:30AM | 2022-02-18 08:22:23.370 | Checked Out | Check Out For Change Network |
| 674 | 355 | Feb 18 2022 9:30AM | 2022-02-18 08:22:23.370 | Checked Out | Check Out For Change Network |
+--------------------------+-------------------+---------------------+-------------------------+-------------+------------------------------+
My Cross Join Query:
select yy.Checkincheckouthistoryid,yy.UserTime,yy.InsertTime,yy.Reason
--,yy.EmployeeNbr,yy.NetWorkNbr,yy.TimeTicketDate
from
(
select CH.CheckInCheckoutHistoryId,CH.UserTime,CH.InsertTime,CH.Reason,EmployeeNbr,NetWorkNbr,TimeTicketDate
from CheckInCheckOutHistoryDetails CH
inner join CheckInCheckOutActivity CM on CH.CheckinCheckOutId = CM.CheckinCheckOutId and CH.Reason = CM.Reason
where CH.CheckinCheckOutId in(
select CheckinCheckOutId from CheckInCheckOutActivity where EmployeeNbr=10020168 and NetWorkNbr=71018655 and TimeTicketDate='2022-02-18 00:00:00.000')
)yy
Result Of Cross Join Query:
+--------------------------+---------------------+-------------------------+------------------------------+
| Checkincheckouthistoryid | UserTime | InsertTime | Reason |
+--------------------------+---------------------+-------------------------+------------------------------+
| 669 | Feb 18 2022 6:03AM | 2022-02-18 08:09:11.430 | Initial Check In |
| 670 | Feb 18 2022 8:00AM | 2022-02-18 08:17:55.870 | Check Out For Meal Break |
| 671 | Feb 18 2022 6:15AM | 2022-02-18 08:19:49.097 | Initial Check In |
| 672 | Feb 18 2022 8:30AM | 2022-02-18 08:19:30.110 | Check In After Meal Break |
| 673 | Feb 18 2022 9:30AM | 2022-02-18 08:22:23.370 | Check Out For Change Network |
| 674 | Feb 18 2022 9:30AM | 2022-02-18 08:22:23.370 | Check Out For Change Network |
+--------------------------+---------------------+-------------------------+------------------------------+
This are all the Reason for which i need the Data:
[Initial Check In] ,[Check Out For Meal Break],[Check In After Meal Break],[First Meal Break Alert],[Check In After First Meal Break],[Second Meal Break Alert],[Check In After Second Meal Break],[Check Out For Change Network],[Check in After Change Network],[Check Out For Personal Time],[Check In After Personal Time],[Check Out For End Of Shift]
The Result Expected Format:
| [Initial Check In_UsertTime] [Initial Check In_SysTime][Check Out For Meal Break UserTime] [Check Out For Meal Break SysTime][Check In After Meal BreakUserTime] [Check In After Meal BreakSysTime][First Meal Break Alert UserTime] [First Meal Break Alert SysTime][Check In After First Meal Break UserTime] [Check In After First Meal Break SysTime][Second Meal Break Alert UserTime] [Second Meal Break Alert SysTime][Check In After Second Meal Break UserTime ] [Check In After Second Meal Break SysTime ][Check Out For Change Network UserTime] [Check Out For Change Network SysTime][Check in After Change Network UserTime] [Check in After Change Network SysTime] [Check Out For Personal Time UserTime] [Check Out For Personal Time SysTime][Check In After Personal Time UserTime] [Check In After Personal Time SysTime][Check Out For End Of Shift UserTime] [Check Out For End Of Shift SysTime]
| Feb 18 2022 6:03AM 2022-02-18 08:09:11.430 | | Feb 18 2022 8:00AM | | | 2022-02-18 08:17:55.870 | | | | Feb 18 2022 8:30AM | | | 2022-02-18 08:19:30.110 | | null | | | | | | | null | | | | | | | null | | | | | | | | | | null | | | | | | | | | | null | | | | | | | | null | | | | | | null | | | | | | | | | | | null | | | | | | null | | | | | | | | | | | | null | | | | | | | | | null | | | | | | | | null | | | | | | | | null | | | | | | | | | | null | | | | | | | null | | | | | | | | | null | | | | | | | | null | | | | | | | null
Note: If there is a user time and SystemTime for same reason then in next Row we need to display that data
When I Use Pivot I am Not getting the Result as Expected:
My Pivot Query:
select kk.InitalCheckin_UserTime,kk.Checkoutformealbreak_UserTime,kk.CheckInAfterMealBreak_UserTime,kk.FirstMealBreakAlert_UserTime,kk.CheckInAfterFirstMealBreak_UserTime,kk.SecondMealBreakAlert_UserTime,kk.CheckInAfterSecondMealBreak_UserTime,kk.CheckOutForChangeNetwork_UserTime,kk.CheckinAfterChangeNetwork_UserTime,kk.CheckOutForPersonalTime_UserTime,kk.CheckInAfterPersonalTime_UserTime,kk.CheckOutForEndOfShift_UserTime
,
rr.InitalCheckin_SysTime,rr.Checkoutformealbreak_SysTime,rr.CheckInAfterMealBreak_SysTime,rr.FirstMealBreakAlert_SysTime,rr.CheckInAfterFirstMealBreak_SysTime,rr.SecondMealBreakAlert_SysTime,rr.CheckInAfterSecondMealBreak_SysTime,rr.CheckOutForChangeNetwork_SysTime,rr.CheckinAfterChangeNetwork_SysTime,rr.CheckOutForPersonalTime_SysTime,rr.CheckInAfterPersonalTime_SysTime,rr.CheckOutForEndOfShift_SysTime
, rr.checkindate
from
(
select
InitalCheckin_UserTime=[Initial Check In],
Checkoutformealbreak_UserTime=[Check Out For Meal Break]
,CheckInAfterMealBreak_UserTime=[Check In After Meal Break]
,FirstMealBreakAlert_UserTime=[First Meal Break Alert],
CheckInAfterFirstMealBreak_UserTime=[Check In After First Meal Break],
SecondMealBreakAlert_UserTime=[Second Meal Break Alert],
CheckInAfterSecondMealBreak_UserTime=[Check In After Second Meal Break],
CheckOutForChangeNetwork_UserTime=[Check Out For Change Network],
CheckinAfterChangeNetwork_UserTime=[Check in After Change Network],
CheckOutForPersonalTime_UserTime=[Check Out For Personal Time],
CheckInAfterPersonalTime_UserTime=[Check In After Personal Time],
CheckOutForEndOfShift_UserTime=[Check Out For End Of Shift],
checkindate = getdate()
from
(
select ss.Checkincheckouthistoryid,ss.UserTime,ss.InsertTime,ss.Reason,ss.Reason1
from
(
select CH.CheckInCheckoutHistoryId,CH.UserTime,CH.InsertTime,CH.Reason,CH.Reason as Reason1
from CheckInCheckOutHistoryDetails CH
inner join CheckInCheckOutActivity CM on CH.CheckinCheckOutId = CM.CheckinCheckOutId and CH.Reason = CM.Reason
where CH.CheckinCheckOutId in(
select CheckinCheckOutId from CheckInCheckOutActivity where EmployeeNbr=10020168 and NetWorkNbr=71018655 and TimeTicketDate='2022-02-18 00:00:00.000')
)ss
)d
pivot
(
max(UserTime) for Reason in ([Initial Check In] ,[Check Out For Meal Break],[Check In After Meal Break],[First Meal Break Alert],[Check In After First Meal Break],[Second Meal Break Alert],[Check In After Second Meal Break],[Check Out For Change Network],[Check in After Change Network],[Check Out For Personal Time],[Check In After Personal Time],[Check Out For End Of Shift])
) As pivasd
) as kk left outer join
(
select
InitalCheckin_SysTime=[Initial Check In],
Checkoutformealbreak_SysTime=[Check Out For Meal Break],
CheckInAfterMealBreak_SysTime=[Check In After Meal Break],
FirstMealBreakAlert_SysTime=[First Meal Break Alert],
CheckInAfterFirstMealBreak_SysTime=[Check In After First Meal Break],
SecondMealBreakAlert_SysTime=[Second Meal Break Alert],
CheckInAfterSecondMealBreak_SysTime=[Check In After Second Meal Break],
CheckOutForChangeNetwork_SysTime=[Check Out For Change Network],
CheckinAfterChangeNetwork_SysTime=[Check in After Change Network],
CheckOutForPersonalTime_SysTime=[Check Out For Personal Time],
CheckInAfterPersonalTime_SysTime=[Check In After Personal Time],
CheckOutForEndOfShift_SysTime=[Check Out For End Of Shift],
checkindate=GETDATE()
--EmployeeNbr,NetWorkNbr,TimeTicketDate
from
(
select yy.Checkincheckouthistoryid,yy.UserTime,yy.InsertTime,yy.Reason,yy.Reason1
--,yy.EmployeeNbr,yy.NetWorkNbr,yy.TimeTicketDate
from
(
select CH.CheckInCheckoutHistoryId,CH.UserTime,CH.InsertTime,CH.Reason,CH.Reason as Reason1,EmployeeNbr,NetWorkNbr,TimeTicketDate
from CheckInCheckOutHistoryDetails CH
inner join CheckInCheckOutActivity CM on CH.CheckinCheckOutId = CM.CheckinCheckOutId and CH.Reason = CM.Reason
where CH.CheckinCheckOutId in(
select CheckinCheckOutId from CheckInCheckOutActivity where EmployeeNbr=10020168 and NetWorkNbr=71018655 and TimeTicketDate='2022-02-18 00:00:00.000')
)yy
)c
pivot
(
max(InsertTime) for Reason1 in ([Initial Check In] ,[Check Out For Meal Break],[Check In After Meal Break],[First Meal Break Alert],[Check In After First Meal Break],[Second Meal Break Alert],[Check In After Second Meal Break],[Check Out For Change Network],[Check in After Change Network],[Check Out For Personal Time],[Check In After Personal Time],[Check Out For End Of Shift])
) As piva
) as rr on kk.checkindate = rr.checkindate
group by
kk.InitalCheckin_UserTime,kk.Checkoutformealbreak_UserTime,kk.CheckInAfterMealBreak_UserTime,kk.FirstMealBreakAlert_UserTime,kk.CheckInAfterFirstMealBreak_UserTime,kk.SecondMealBreakAlert_UserTime,kk.CheckInAfterSecondMealBreak_UserTime,kk.CheckOutForChangeNetwork_UserTime,kk.CheckinAfterChangeNetwork_UserTime,kk.CheckOutForPersonalTime_UserTime,kk.CheckInAfterPersonalTime_UserTime,kk.CheckOutForEndOfShift_UserTime
,
rr.InitalCheckin_SysTime,rr.Checkoutformealbreak_SysTime,rr.CheckInAfterMealBreak_SysTime,rr.FirstMealBreakAlert_SysTime,rr.CheckInAfterFirstMealBreak_SysTime,rr.SecondMealBreakAlert_SysTime,rr.CheckInAfterSecondMealBreak_SysTime,rr.CheckOutForChangeNetwork_SysTime,rr.CheckinAfterChangeNetwork_SysTime,rr.CheckOutForPersonalTime_SysTime,rr.CheckInAfterPersonalTime_SysTime,rr.CheckOutForEndOfShift_SysTime
,rr.checkindate
Result of the Pivot Query:
+------------------------+-------------------------------+--------------------------------+------------------------------+-------------------------------------+-------------------------------+--------------------------------------+-----------------------------------+------------------------------------+----------------------------------+-----------------------------------+--------------------------------+-------------------------+------------------------------+-------------------------------+-----------------------------+------------------------------------+------------------------------+-------------------------------------+----------------------------------+-----------------------------------+---------------------------------+----------------------------------+-------------------------------+-------------------------+
| InitalCheckin_UserTime | Checkoutformealbreak_UserTime | CheckInAfterMealBreak_UserTime | FirstMealBreakAlert_UserTime | CheckInAfterFirstMealBreak_UserTime | SecondMealBreakAlert_UserTime | CheckInAfterSecondMealBreak_UserTime | CheckOutForChangeNetwork_UserTime | CheckinAfterChangeNetwork_UserTime | CheckOutForPersonalTime_UserTime | CheckInAfterPersonalTime_UserTime | CheckOutForEndOfShift_UserTime | InitalCheckin_SysTime | Checkoutformealbreak_SysTime | CheckInAfterMealBreak_SysTime | FirstMealBreakAlert_SysTime | CheckInAfterFirstMealBreak_SysTime | SecondMealBreakAlert_SysTime | CheckInAfterSecondMealBreak_SysTime | CheckOutForChangeNetwork_SysTime | CheckinAfterChangeNetwork_SysTime | CheckOutForPersonalTime_SysTime | CheckInAfterPersonalTime_SysTime | CheckOutForEndOfShift_SysTime | checkindate |
+------------------------+-------------------------------+--------------------------------+------------------------------+-------------------------------------+-------------------------------+--------------------------------------+-----------------------------------+------------------------------------+----------------------------------+-----------------------------------+--------------------------------+-------------------------+------------------------------+-------------------------------+-----------------------------+------------------------------------+------------------------------+-------------------------------------+----------------------------------+-----------------------------------+---------------------------------+----------------------------------+-------------------------------+-------------------------+
| NULL | NULL | NULL | NULL | NULL | NULL | NULL | Feb 18 2022 9:30AM | NULL | NULL | NULL | NULL | NULL | NULL | NULL | NULL | NULL | NULL | NULL | 2022-02-18 08:22:23.370 | NULL | NULL | NULL | NULL | 2022-04-25 06:59:06.163 |
| NULL | NULL | NULL | NULL | NULL | NULL | NULL | Feb 18 2022 9:30AM | NULL | NULL | NULL | NULL | NULL | NULL | 2022-02-18 08:19:30.110 | NULL | NULL | NULL | NULL | NULL | NULL | NULL | NULL | NULL | 2022-04-25 06:59:06.163 |
| NULL | NULL | NULL | NULL | NULL | NULL | NULL | Feb 18 2022 9:30AM | NULL | NULL | NULL | NULL | NULL | 2022-02-18 08:17:55.870 | NULL | NULL | NULL | NULL | NULL | NULL | NULL | NULL | NULL | NULL | 2022-04-25 06:59:06.163 |
| NULL | NULL | NULL | NULL | NULL | NULL | NULL | Feb 18 2022 9:30AM | NULL | NULL | NULL | NULL | 2022-02-18 08:09:11.430 | NULL | NULL | NULL | NULL | NULL | NULL | NULL | NULL | NULL | NULL | NULL | 2022-04-25 06:59:06.163 |
| NULL | NULL | NULL | NULL | NULL | NULL | NULL | Feb 18 2022 9:30AM | NULL | NULL | NULL | NULL | 2022-02-18 08:19:49.097 | NULL | NULL | NULL | NULL | NULL | NULL | NULL | NULL | NULL | NULL | NULL | 2022-04-25 06:59:06.163 |
| NULL | NULL | Feb 18 2022 8:30AM | NULL | NULL | NULL | NULL | NULL | NULL | NULL | NULL | NULL | NULL | NULL | NULL | NULL | NULL | NULL | NULL | 2022-02-18 08:22:23.370 | NULL | NULL | NULL | NULL | 2022-04-25 06:59:06.163 |
| NULL | NULL | Feb 18 2022 8:30AM | NULL | NULL | NULL | NULL | NULL | NULL | NULL | NULL | NULL | NULL | NULL | 2022-02-18 08:19:30.110 | NULL | NULL | NULL | NULL | NULL | NULL | NULL | NULL | NULL | 2022-04-25 06:59:06.163 |
| NULL | NULL | Feb 18 2022 8:30AM | NULL | NULL | NULL | NULL | NULL | NULL | NULL | NULL | NULL | NULL | 2022-02-18 08:17:55.870 | NULL | NULL | NULL | NULL | NULL | NULL | NULL | NULL | NULL | NULL | 2022-04-25 06:59:06.163 |
| NULL | NULL | Feb 18 2022 8:30AM | NULL | NULL | NULL | NULL | NULL | NULL | NULL | NULL | NULL | 2022-02-18 08:09:11.430 | NULL | NULL | NULL | NULL | NULL | NULL | NULL | NULL | NULL | NULL | NULL | 2022-04-25 06:59:06.163 |
| NULL | NULL | Feb 18 2022 8:30AM | NULL | NULL | NULL | NULL | NULL | NULL | NULL | NULL | NULL | 2022-02-18 08:19:49.097 | NULL | NULL | NULL | NULL | NULL | NULL | NULL | NULL | NULL | NULL | NULL | 2022-04-25 06:59:06.163 |
+------------------------+-------------------------------+--------------------------------+------------------------------+-------------------------------------+-------------------------------+--------------------------------------+-----------------------------------+------------------------------------+----------------------------------+-----------------------------------+--------------------------------+-------------------------+------------------------------+-------------------------------+-----------------------------+------------------------------------+------------------------------+-------------------------------------+----------------------------------+-----------------------------------+---------------------------------+----------------------------------+-------------------------------+-------------------------+
I am not getting the result as expected by above query
I am getting 25 rows of data which are not accurate I only need 5 rows of data
You can use the scalar functions to get the required columns. for example:
CREATE FUNCTION base.function1 (#Checkincheckouthistoryid int)
RETURNS datetime
as begin
declare #dateTime datetime = (select UsertTime from
from
(
select CH.CheckInCheckoutHistoryId,CH.UserTime,CH.InsertTime,CH.Reason,EmployeeNbr,NetWorkNbr,TimeTicketDate
from CheckInCheckOutHistoryDetails CH
inner join CheckInCheckOutActivity CM on CH.CheckinCheckOutId = CM.CheckinCheckOutId and CH.Reason = CM.Reason
where CH.CheckinCheckOutId in(
select CheckinCheckOutId from CheckInCheckOutActivity where EmployeeNbr=10020168 and NetWorkNbr=71018655 and TimeTicketDate='2022-02-18 00:00:00.000')
)yy where
Checkincheckouthistoryid = #Checkincheckouthistoryid )
end
GO
then use it as a column everywhere you access to "Checkincheckouthistoryid"

Select all columns with only two distinct columns

In my SQL Server database, I have a table with many duplicate values and I need to fetch results with distinct columns EID and YEAR and select rows containing fewer NULL values or order the table and get a final DISTINCT column EID and YEAR rows.
For example: below the table with EID = E138442 and YEAR = 2019 occurs 21 times were in this duplicate the row containing fewer null values should be fetched
+---------+------+------+------+------+------+------+------+------+------+------+------+------+------+
| EID | YEAR | JAN | FEB | MAR | APR | MAY | JUN | JUL | AUG | SEP | OCT | NOV | DEC |
+---------+------+------+------+------+------+------+------+------+------+------+------+------+------+
| E050339 | 2019 | NULL | NULL | NULL | NULL | NULL | NULL | NULL | NULL | NULL | NULL | NULL | 1 |
| E050339 | 2020 | NULL | 6 | NULL | NULL | NULL | NULL | NULL | NULL | NULL | NULL | NULL | NULL |
| E050339 | 2020 | 13 | 6 | NULL | NULL | NULL | NULL | NULL | NULL | NULL | NULL | NULL | NULL |
| E138348 | 2019 | NULL | NULL | NULL | NULL | NULL | NULL | NULL | NULL | NULL | NULL | 1 | NULL |
| E138348 | 2019 | NULL | 1 | NULL | NULL | NULL | NULL | NULL | NULL | NULL | NULL | NULL | NULL |
| e138372 | 2019 | 1 | NULL | NULL | NULL | NULL | NULL | NULL | NULL | NULL | NULL | NULL | NULL |
| E138440 | 2019 | NULL | NULL | 2 | NULL | NULL | NULL | NULL | NULL | NULL | NULL | NULL | NULL |
| E138442 | 2019 | NULL | NULL | NULL | NULL | NULL | NULL | NULL | NULL | NULL | NULL | NULL | 5 |
| E138442 | 2019 | NULL | NULL | NULL | NULL | NULL | NULL | NULL | NULL | NULL | NULL | 9 | 5 |
| E138442 | 2019 | NULL | NULL | NULL | NULL | NULL | NULL | NULL | NULL | NULL | NULL | 9 | 5 |
| E138442 | 2019 | NULL | NULL | NULL | NULL | NULL | NULL | NULL | NULL | NULL | 4 | 9 | 5 |
| E138442 | 2019 | NULL | NULL | NULL | NULL | NULL | NULL | NULL | NULL | NULL | 4 | 9 | 5 |
| E138442 | 2019 | NULL | NULL | NULL | NULL | NULL | NULL | NULL | NULL | NULL | 4 | 9 | 5 |
| E138442 | 2019 | NULL | NULL | NULL | NULL | NULL | NULL | NULL | NULL | NULL | 4 | 9 | 5 |
| E138442 | 2019 | NULL | NULL | NULL | NULL | NULL | NULL | NULL | NULL | NULL | 4 | 9 | 5 |
| E138442 | 2019 | NULL | NULL | NULL | NULL | NULL | NULL | NULL | NULL | 4 | 4 | 9 | 5 |
| E138442 | 2019 | NULL | NULL | NULL | NULL | NULL | NULL | NULL | 7 | 4 | 4 | 9 | 5 |
| E138442 | 2019 | NULL | NULL | NULL | NULL | NULL | NULL | NULL | 7 | 4 | 4 | 9 | 5 |
| E138442 | 2019 | NULL | NULL | NULL | NULL | NULL | NULL | 7 | 7 | 4 | 4 | 9 | 5 |
| E138442 | 2019 | NULL | NULL | NULL | NULL | NULL | NULL | 7 | 7 | 4 | 4 | 9 | 5 |
| E138442 | 2019 | NULL | NULL | NULL | NULL | NULL | 2 | 7 | 7 | 4 | 4 | 9 | 5 |
| E138442 | 2019 | NULL | NULL | NULL | NULL | 7 | 2 | 7 | 7 | 4 | 4 | 9 | 5 |
| E138442 | 2019 | NULL | NULL | NULL | 7 | 7 | 2 | 7 | 7 | 4 | 4 | 9 | 5 |
| E138442 | 2019 | NULL | NULL | 1 | 7 | 7 | 2 | 7 | 7 | 4 | 4 | 9 | 5 |
| E138442 | 2019 | NULL | 1 | NULL | 7 | 7 | 2 | 7 | 7 | 4 | 4 | 9 | 5 |
| E138442 | 2020 | NULL | NULL | NULL | NULL | NULL | NULL | NULL | NULL | NULL | NULL | NULL | 1 |
| E138442 | 2020 | NULL | NULL | NULL | NULL | NULL | NULL | NULL | NULL | NULL | NULL | NULL | 1 |
| E138442 | 2020 | NULL | NULL | NULL | NULL | NULL | NULL | NULL | NULL | NULL | NULL | NULL | 1 |
| E138442 | 2020 | NULL | NULL | NULL | NULL | NULL | NULL | NULL | NULL | NULL | NULL | NULL | 1 |
+---------+------+------+------+------+------+------+------+------+------+------+------+------+------+
I need a SQL query to fetch values as shown here:
+---------+------+------+------+------+------+------+------+------+------+------+------+------+------+
| EID | YEAR | JAN | FEB | MAR | APR | MAY | JUN | JUL | AUG | SEP | OCT | NOV | DEC |
+---------+------+------+------+------+------+------+------+------+------+------+------+------+------+
| E050339 | 2020 | 13 | 6 | NULL | NULL | NULL | NULL | NULL | NULL | NULL | NULL | NULL | NULL |
| E138348 | 2019 | NULL | 1 | NULL | NULL | NULL | NULL | NULL | NULL | NULL | NULL | NULL | NULL |
| e138372 | 2019 | 1 | NULL | NULL | NULL | NULL | NULL | NULL | NULL | NULL | NULL | NULL | NULL |
| E138440 | 2019 | NULL | NULL | 2 | NULL | NULL | NULL | NULL | NULL | NULL | NULL | NULL | NULL |
| E138442 | 2019 | NULL | 1 | NULL | 7 | 7 | 2 | 7 | 7 | 4 | 4 | 9 | 5 |
| E138442 | 2020 | NULL | NULL | NULL | NULL | NULL | NULL | NULL | NULL | NULL | NULL | NULL | 1 |
+---------+------+------+------+------+------+------+------+------+------+------+------+------+------+
The result table should have a final row with distinct columns EID and YEAR.
SELECT *
FROM TABLE_NAME C1
WHERE EXISTS (SELECT 1
FROM TABLE_NAME C2
WHERE C1.EID = C2.EID AND C1.YEAR = C2.YEAR
HAVING COUNT(*) = 1)
ORDER BY
c1.EID, c1.YEAR, c1.JAN, c1.FEB, c1.MAR, c1.APR,
c1.MAY, c1.JUN, c1.JUL, c1.AUG, c1.SEP, c1.OCT, c1.NOV, c1.DEC ASC;
I tried the above code but found irrelevant results
since you have no other way to distinguish members of a group and based on "select rows containing fewer NULL values " here is one way how you can do it by using ctes, its not clean but probably the only way:
with cte as (
SELECT *,
ISNULL(c1.JAN, 1) + ISNULL(c1.FEB,1) + ... + ISNULL(c1.DEC,1) AS NullCount
FROM
tablename
)
, cte2 as (
select EID , YEAR , min(NullCount) min_nullcount
from cte
group by EID , YEAR
)
select t.*
from
cte t
join cte2 tt
on t.EID = tt.EID
and t.YEAR = tt.YEAR
and t.NULLCount = tt.min_nullcount
If you have duplicate minimum null per group you can use query below :
select * from (
SELECT *,
ROW_NUMBER OVER (partition by EID , YEAR order by ISNULL(c1.JAN, 1) + ... + ISNULL(c1.DEC,1) AS rnk
FROM
tablename
) xx
WHERE rnk = 1

Joining two or more tables

I have written some code to merge two tables together. The values that are displayed are null.
+----------+--------------+-----------------------------------------------+-------------+----------+----------+------+----------+
| Movie_ID | Release_year | Movie_Title | Duration | Genre_ID | Actor_ID | Role | Movie_ID |
+----------+--------------+-----------------------------------------------+-------------+----------+----------+------+----------+
| 10001 | 1997 | Titantic | 190 minutes | 40001 | NULL | NULL | NULL |
| 10002 | 1998 | Shakesphere in Love | 123 minutes | 40002 | NULL | NULL | NULL |
| 10003 | 1999 | American Beauty | 122 minutes | 40003 | NULL | NULL | NULL |
| 10004 | 2000 | Gladiator | 155 minutes | 40004 | NULL | NULL | NULL |
| 10005 | 2001 | A beautiful Mind | 135 minutes | 40004 | NULL | NULL | NULL |
| 10006 | 2002 | Chicago | 113 minutes | 40005 | NULL | NULL | NULL |
| 10007 | 2003 | The Lord of the Rings: The return of the King | 201 minutes | 40006 | NULL | NULL | NULL |
| 10008 | 2004 | Million Dollar Baby | 132 minutes | 40007 | NULL | NULL | NULL |
| 10009 | 2005 | Crash | 112 minutes | 40008 | NULL | NULL | NULL |
| 10010 | 2006 | The Departed | 151 minutes | 40009 | NULL | NULL | NULL |
| 10011 | 2007 | No Country for Old Men | 122 minutes | 40009 | NULL | NULL | NULL |
| 10012 | 2008 | Slumdog Millionaire | 120 minutes | 40008 | NULL | NULL | NULL |
| 10013 | 2009 | The Hurt Locker | 131 minutes | 40009 | NULL | NULL | NULL |
| 10014 | 2010 | The King\s speech | 118 minutes | 40010 | NULL | NULL | NULL |
| 10015 | 2011 | The Artist | 100 minutes | 40011 | NULL | NULL | NULL |
| 10016 | 2012 | Argo | 120 minutes | 40012 | NULL | NULL | NULL |
| 10017 | 2013 | 12 Years a Slave | 134 minutes | 40004 | NULL | NULL | NULL |
| 10018 | 2014 | Birdman or The Unexpected Virtue of Ignorance | 119 minutes | 40003 | NULL | NULL | NULL |
| 10019 | 2015 | Spotlight | 129 minutes | 40008 | NULL | NULL | NULL |
| 10020 | 2016 | Moonlight | 111 minutes | 40013 | NULL | NULL | NULL |
| 10021 | 2017 | The Shape of Water | 123 minutes | 40012 | NULL | NULL | NULL |
| 10022 | 2018 | Green Book | 130 minutes | 40011 | NULL | NULL | NULL |
+----------+--------------+-----------------------------------------------+-------------+----------+----------+------+----------+
SELECT *
FROM databaseoscars.movie a
LEFT JOIN databaseoscars.`movie cast` b ON a.Movie_ID = b.Actor_ID;
I expected the output to be all data is displayed on one table.
Your problem is here: a.Movie_ID = b.Actor_ID. A movie will never be an actor. Use your movie cast table's movie ID instead:
SELECT *
FROM databaseoscars.movie m
LEFT JOIN databaseoscars.`movie cast` mc ON mc.movie_id = m.movie_id;
In MySQL and MariaDB, you could also use the USING clause:
SELECT *
FROM databaseoscars.movie m
LEFT JOIN databaseoscars.`movie cast` mc USING (movie_id);

How do i join two row in respect to date and time(hour and minute)

I want join these row that date and time same but second are different. i use max function but it did not return me proper result.
| DateandTime | t1 | t2 | t3 | t4 | t5 | t6 | t7 |
|-------------------------|------|------|------|------|------|------|------|
| 2019-08-06 15:44:04.000 | NULL | 0 | 29 | 20 | 150 | 20 | 20 |
| 2019-08-06 15:44:03.000 | 0 | NULL | NULL | NULL | NULL | NULL | NULL |
| 2019-08-06 15:43:04.000 | NULL | NULL | 29 | 20 | 150 | 20 | 20 |
| 2019-08-06 15:43:03.000 | 0 | 0 | NULL | NULL | NULL | NULL | NULL |
| 2019-08-06 15:42:04.000 | NULL | NULL | 29 | 20 | 150 | 20 | 20 |
| 2019-08-06 15:42:03.000 | 0 | 0 | NULL | NULL | NULL | NULL | NULL |
| 2019-08-06 15:41:04.000 | NULL | NULL | 29 | 20 | 150 | 20 | 20 |
| 2019-08-06 15:41:03.000 | 0 | 0 | NULL | NULL | NULL | NULL | NULL |
| 2019-08-06 15:40:04.000 | NULL | 0 | 29 | 20 | 150 | 20 | 20 |
| 2019-08-06 15:40:03.000 | 0 | NULL | NULL | NULL | NULL | NULL | NULL |
| 2019-08-06 15:39:04.000 | NULL | 0 | 29 | 20 | 150 | 20 | 20 |
| 2019-08-06 15:39:03.000 | 0 | NULL | NULL | NULL | NULL | NULL | NULL |
| 2019-08-06 15:38:04.000 | NULL | NULL | 29 | 20 | 150 | 20 | 20 |
| 2019-08-06 15:38:03.000 | 0 | 0 | NULL | NULL | NULL | NULL | NULL |
If you are using MSSQL, this following script will work. You can also use the same logic for other databases just applying appropriate DATETIME conversion for that database.
SELECT
FORMAT(CAST(DateandTime AS DATETIME), 'dd-MM.yyyy HH:mm'),
-- You can also use SUM/AVG for all columns if both row can
-- have data and based on your requirement.
MAX(t1) T1,
MAX(t2) T2,
MAX(t3) T3,
MAX(t4) T4,
MAX(t5) T5,
MAX(t6) T6,
MAX(t7) T7
FROM your_table
GROUP BY FORMAT(CAST(DateandTime AS DATETIME), 'dd-MM.yyyy HH:mm')
-- Do not required CAST(DateandTime AS DATETIME) if your
-- DateandTime column type is already DATETIME

SQL Server joining trouble

Scenario:
I am trying to build a query which has a start and end date, And the result of this query gives me the days in between and the day name. I then want to JOIN to another table which has expected pay dates and amounts. The JOINED table may have more days outside the range of the start and end date, which I want to exclude.
Progress:
I sort-of have what I want, but not in the correct output, I have created the following thus far:
DECLARE
#startDate DATETIME,
#endDate DATETIME
SET #startDate = CONVERT(VARCHAR(4), DATEPART(YEAR, DATEADD(MONTH, -1, GETDATE())))+'-'+CONVERT(VARCHAR(2), DATEPART(MONTH, DATEADD(MONTH, -1, GETDATE())))+'-21'
SET #endDate = CONVERT(VARCHAR(4), DATEPART(YEAR, DATEADD(MONTH, -1, GETDATE())))+'-'+CONVERT(VARCHAR(2), DATEPART(MONTH, DATEADD(MONTH, -0, GETDATE())))+'-20'
;WITH dates AS
(
SELECT #startdate as Date,DATENAME(Dw,#startdate) As DayName
UNION ALL
SELECT DATEADD(d,1,[Date]),DATENAME(Dw,DATEADD(d,1,[Date])) as DayName
FROM dates
WHERE DATE < #enddate
)
SELECT LEFT(CONVERT(VARCHAR(30),Date, 106), 2) + '-' + LEFT(CONVERT(VARCHAR(30),Date, 10), 2) Date,DayName, SUM(ExpectedAmount), ExpectedDate FROM dates
FULL JOIN Commissions.dbo.ThreeMonthPayment on CONVERT(VARCHAR(30),Date) = Commissions.dbo.ThreeMonthPayment.ExpectedDate
GROUP BY Date, DayName, ExpectedDate
Order by ExpectedDate
Which results in this table (Sorry so long):
+-------+-----------+------------------+--------------+
| Date | DayName | (No column name) | ExpectedDate |
+-------+-----------+------------------+--------------+
| NULL | NULL | 0 | NULL |
| 21-03 | Friday | NULL | NULL |
| 22-03 | Saturday | NULL | NULL |
| 23-03 | Sunday | NULL | NULL |
| 24-03 | Monday | NULL | NULL |
| 25-03 | Tuesday | NULL | NULL |
| 26-03 | Wednesday | NULL | NULL |
| 27-03 | Thursday | NULL | NULL |
| 28-03 | Friday | NULL | NULL |
| 29-03 | Saturday | NULL | NULL |
| 30-03 | Sunday | NULL | NULL |
| 31-03 | Monday | NULL | NULL |
| 01-04 | Tuesday | NULL | NULL |
| 02-04 | Wednesday | NULL | NULL |
| 03-04 | Thursday | NULL | NULL |
| 04-04 | Friday | NULL | NULL |
| 05-04 | Saturday | NULL | NULL |
| 06-04 | Sunday | NULL | NULL |
| 07-04 | Monday | NULL | NULL |
| 08-04 | Tuesday | NULL | NULL |
| 09-04 | Wednesday | NULL | NULL |
| 10-04 | Thursday | NULL | NULL |
| 11-04 | Friday | NULL | NULL |
| 12-04 | Saturday | NULL | NULL |
| 13-04 | Sunday | NULL | NULL |
| 14-04 | Monday | NULL | NULL |
| 15-04 | Tuesday | NULL | NULL |
| 16-04 | Wednesday | NULL | NULL |
| 17-04 | Thursday | NULL | NULL |
| 18-04 | Friday | NULL | NULL |
| 19-04 | Saturday | NULL | NULL |
| 20-04 | Sunday | NULL | NULL |
| NULL | NULL | 89466 | 01-03 |
| NULL | NULL | 86058 | 01-04 |
| NULL | NULL | 23356 | 01-05 |
| NULL | NULL | 1858 | 01-06 |
| NULL | NULL | 13597 | 02-03 |
| NULL | NULL | 55587 | 02-04 |
| NULL | NULL | 7857 | 02-05 |
| NULL | NULL | 1377 | 02-06 |
| NULL | NULL | 6947 | 03-03 |
| NULL | NULL | 49626 | 03-04 |
| NULL | NULL | 0 | 03-05 |
| NULL | NULL | 0 | 03-06 |
| NULL | NULL | 6054 | 04-03 |
| NULL | NULL | 31639 | 04-04 |
| NULL | NULL | 0 | 04-05 |
| NULL | NULL | 0 | 04-06 |
| NULL | NULL | 26421 | 05-03 |
| NULL | NULL | 28154 | 05-04 |
| NULL | NULL | 15036 | 05-05 |
| NULL | NULL | 634 | 05-06 |
| NULL | NULL | 0 | 05-07 |
| NULL | NULL | 20832 | 06-03 |
| NULL | NULL | 0 | 06-04 |
| NULL | NULL | 0 | 06-05 |
| NULL | NULL | 0 | 06-06 |
| NULL | NULL | 5406 | 07-03 |
| NULL | NULL | 12864 | 07-04 |
| NULL | NULL | 4257 | 07-05 |
| NULL | NULL | 537 | 07-06 |
| NULL | NULL | 0 | 08-03 |
| NULL | NULL | 363 | 08-04 |
| NULL | NULL | 426 | 08-05 |
| NULL | NULL | 0 | 08-06 |
| NULL | NULL | 0 | 09-03 |
| NULL | NULL | 23240 | 09-04 |
| NULL | NULL | 0 | 09-05 |
| NULL | NULL | 0 | 09-06 |
| NULL | NULL | 12670 | 10-03 |
| NULL | NULL | 6790 | 10-04 |
| NULL | NULL | 0 | 10-05 |
| NULL | NULL | 0 | 10-06 |
| NULL | NULL | 2914 | 11-03 |
| NULL | NULL | 19053 | 11-04 |
| NULL | NULL | 0 | 11-05 |
| NULL | NULL | 0 | 11-06 |
| NULL | NULL | 6402 | 12-03 |
| NULL | NULL | 0 | 12-04 |
| NULL | NULL | 0 | 12-05 |
| NULL | NULL | 0 | 12-06 |
| NULL | NULL | 4166 | 13-03 |
| NULL | NULL | 0 | 13-04 |
| NULL | NULL | 0 | 13-05 |
| NULL | NULL | 0 | 13-06 |
| NULL | NULL | 50534 | 14-03 |
| NULL | NULL | 23854 | 14-04 |
| NULL | NULL | 15435 | 14-05 |
| NULL | NULL | 4003 | 14-06 |
| NULL | NULL | 475330 | 15-03 |
| NULL | NULL | 451014 | 15-04 |
| NULL | NULL | 103210 | 15-05 |
| NULL | NULL | 19947 | 15-06 |
| NULL | NULL | 12084 | 16-03 |
| NULL | NULL | 22203 | 16-04 |
| NULL | NULL | 517 | 16-05 |
| NULL | NULL | 0 | 16-06 |
| NULL | NULL | 31423 | 17-03 |
| NULL | NULL | 32150 | 17-04 |
| NULL | NULL | 0 | 17-05 |
| NULL | NULL | 0 | 17-06 |
| NULL | NULL | 33402 | 18-03 |
| NULL | NULL | 900 | 18-04 |
| NULL | NULL | 289 | 18-05 |
| NULL | NULL | 0 | 18-06 |
| NULL | NULL | 33929 | 19-03 |
| NULL | NULL | 6942 | 19-04 |
| NULL | NULL | 0 | 19-05 |
| NULL | NULL | 0 | 19-06 |
| NULL | NULL | 161806 | 20-03 |
| NULL | NULL | 141319 | 20-04 |
| NULL | NULL | 26659 | 20-05 |
| NULL | NULL | 4695 | 20-06 |
| NULL | NULL | 21074 | 21-03 |
| NULL | NULL | 15579 | 21-04 |
| NULL | NULL | 2693 | 21-05 |
| NULL | NULL | 0 | 21-06 |
| NULL | NULL | 28401 | 22-03 |
| NULL | NULL | 46258 | 22-04 |
| NULL | NULL | 11409 | 22-05 |
| NULL | NULL | 1672 | 22-06 |
| NULL | NULL | 76562 | 23-03 |
| NULL | NULL | 66804 | 23-04 |
| NULL | NULL | 32853 | 23-05 |
| NULL | NULL | 3168 | 23-06 |
| NULL | NULL | 47008 | 24-03 |
| NULL | NULL | 35888 | 24-04 |
| NULL | NULL | 4528 | 24-05 |
| NULL | NULL | 459 | 24-06 |
| NULL | NULL | 1108747 | 25-03 |
| NULL | NULL | 543351 | 25-04 |
| NULL | NULL | 152852 | 25-05 |
| NULL | NULL | 15712 | 25-06 |
| NULL | NULL | 343379 | 26-03 |
| NULL | NULL | 117657 | 26-04 |
| NULL | NULL | 41793 | 26-05 |
| NULL | NULL | 5645 | 26-06 |
| NULL | NULL | 0 | 27-02 |
| NULL | NULL | 401110 | 27-03 |
| NULL | NULL | 87571 | 27-04 |
| NULL | NULL | 39192 | 27-05 |
| NULL | NULL | 2801 | 27-06 |
| NULL | NULL | 313274 | 28-03 |
| NULL | NULL | 92607 | 28-04 |
| NULL | NULL | 21901 | 28-05 |
| NULL | NULL | 1852 | 28-06 |
| NULL | NULL | 77999 | 29-03 |
| NULL | NULL | 27693 | 29-04 |
| NULL | NULL | 3341 | 29-05 |
| NULL | NULL | 0 | 29-06 |
| NULL | NULL | 229556 | 30-03 |
| NULL | NULL | 261036 | 30-04 |
| NULL | NULL | 9109 | 30-05 |
| NULL | NULL | 545 | 30-06 |
| NULL | NULL | 460871 | 31-03 |
| NULL | NULL | 28710 | 31-05 |
+-------+-----------+------------------+--------------+
Out of the above results, I am trying to match the ExpectedDate to the date column , so instead of seeing the above results, I would have something that looks like this: (to keep it short, i haven't created all of the days I receive from my start and end date)
+-------+-----------+------------------+--------------+
| Date | DayName | (No column name) | ExpectedDate |
+-------+-----------+------------------+--------------+
| NULL | NULL | 0 | NULL |
| 21-03 | Friday | 21074 | 21-03 |
| 22-03 | Saturday | 28401 | 22-03 |
| 23-03 | Sunday | 76562 | 23-03 |
| 24-03 | Monday | 47008 | 24-03 |
+-------+-----------+------------------+--------------+
But you can see above that the expectedDate and date column are grouped / joined nicely together. And the expectedDates that are not in the date range are not displayed.
I have been struggling with this the entire morning :( is this possible ??
Any help or links to threads that I may have missed would be great!
I am using SQL SERVER 2008
Thanks so much.
first of all the full join includes everything. from both tables. if you only want the dates from the dates cte, use left join.
secondly, the CONVERT(VARCHAR(30),Date) = Commissions.dbo.ThreeMonthPayment.ExpectedDate seems to not work. are you sure you need to convert?
i suggest you try this:
DECLARE
#startDate DATETIME,
#endDate DATETIME
SET #startDate = CONVERT(VARCHAR(4), DATEPART(YEAR, DATEADD(MONTH, -1, GETDATE())))+'-'+CONVERT(VARCHAR(2), DATEPART(MONTH, DATEADD(MONTH, -1, GETDATE())))+'-21'
SET #endDate = CONVERT(VARCHAR(4), DATEPART(YEAR, DATEADD(MONTH, -1, GETDATE())))+'-'+CONVERT(VARCHAR(2), DATEPART(MONTH, DATEADD(MONTH, -0, GETDATE())))+'-20'
;WITH dates AS
(
SELECT #startdate as Date,DATENAME(Dw,#startdate) As DayName
UNION ALL
SELECT DATEADD(d,1,[Date]),DATENAME(Dw,DATEADD(d,1,[Date])) as DayName
FROM dates
WHERE DATE < #enddate
)
SELECT LEFT(CONVERT(VARCHAR(30),Date, 106), 2) + '-' + LEFT(CONVERT(VARCHAR(30),Date, 10), 2) Date
, DayName, SUM(ExpectedAmount), ExpectedDate
FROM dates
LEFT JOIN Commissions.dbo.ThreeMonthPayment
on Date = Commissions.dbo.ThreeMonthPayment.ExpectedDate
GROUP BY
Date
, DayName
, ExpectedDate
Order by
ExpectedDate