SQL - Combine another SELECT query with a JOIN - sql-server

I found this post on stackoverflow, Add a summary row with totals
What I'm trying to accomplish is just that, but with the converted DURATION field I have listed in this WITH statement below. I'm trying to TOTAL up the Durations column for that day(24 hr period). I don't know if it's possible. Let me know. Thank you!
;WITH dupes AS
(
SELECT
CALLER_PHONE, DIALED_PHONE
FROM
dbo.PBXDATA
GROUP BY
CALLER_PHONE, DIALED_PHONE
)
SELECT
c.CALL_TIME, c.SALES_REP, c.CALL_TYPE, c.FLAG1,
COALESCE(NULLIF(c.FLAG3, 'NULL'),'') AS FLAG3,
ISNULL(dupes.CALLER_PHONE, '') + ISNULL(dupes.DIALED_PHONE,'') AS PHONE,
CONVERT(VARCHAR(8), c.DURATION, 108) AS DURATION
FROM
dupes
JOIN
dbo.PBXDATA c ON dupes.CALLER_PHONE = c.CALLER_PHONE
OR dupes.DIALED_PHONE = c.DIALED_PHONE
WHERE
(c.SALES_REP LIKE 'Doug%' OR
c.SALES_REP LIKE 'Nick%' OR
c.SALES_REP LIKE 'Bob%' OR
c.SALES_REP LIKE 'Joe%' OR
c.SALES_REP LIKE 'John%')
AND (c.CALL_TIME >= DATEADD(DAY, 0, DATEDIFF(DAY, 0, CURRENT_TIMESTAMP)))
AND (c.CALL_TIME < DATEADD(DAY, 1, DATEDIFF(DAY, 0, CURRENT_TIMESTAMP)))
AND DURATION = (SELECT CAST(DATEADD(S, SUM(DATEDIFF(S, '00:00:00', DURATION)), '00:00:00') AS TIME)
FROM dbo.PBXDATA)
ORDER BY
c.CALL_TIME;

If you just want an overall total for the Duration in your dupes table, you can just sum your Duration there.
;WITH dupes AS
(
SELECT CALLER_PHONE, DIALED_PHONE, convert(varchar(8), SUM(c.DURATION), 108) AS Total_Time
FROM dbo.PBXDATA
GROUP BY CALLER_PHONE, DIALED_PHONE
)
And add , Total_Time to your SELECT statement.
If you have multiple days in your query, you would need to add the date field in the dupes and add it as part of the JOIN's ON clause.

Related

SQL Server: multiple select statements from the same table in one query

I'm working on two queries that pull data from one table. I can get them to run how I want individually, but I'd like to combine them into one if possible. I've tried the solution in [Multiple COUNT SELECTS from the same table in one query but wasn't able to get it figured out. Below is what I'm working on:
--goback 30 days start at midnight
declare #daysgoingback as int
set #daysgoingback = 90
declare #olderdate as datetime
set #olderdate = DATEADD(Day, -#daysgoingback, DATEDIFF(Day, 0, GetDate()))
--today at 11:59pm
declare #today as datetime
set #today = dateadd(ms, -3, (dateadd(day, +1, convert(varchar, GETDATE(), 101))))
print #today
--these are the two queries I'd like to combine:
select
avg(x.LogAlerts*1.0 / #daysgoingback) as 'Avg number of log alerts'
from
(select count(*) as LogAlerts
from message_log_table
where msg_timestamp between #olderdate and #today) X
select
avg(x.MessagesDeleted*1.0 / #daysgoingback) as 'Avg number of messages deleted'
from
(select count(*) as MessagesDeleted
from message_log_table
where msg_details like '%message deleted%'
and msg_timestamp between #olderdate and #today) X
"AVG" is an aggregate function, which averages multiple rows. Your "select count(*)" subquery always returns exactly one row, so you are not really using the AVG function as it's intended.
You can combine the two like this:
select count(*) * 1.0 / #daysgoingback as 'Avg number of log alerts'
, count(case when msg_details like '%message deleted%' then 1 end) * 1.0 / #daysgoingback as 'Avg number of messages deleted'
from message_log_table
where msg_timestamp between #olderdate and #today;
Make each query into a CTE, and then CROSS JOIN them.
Both queries only return a single-cell result, so there's no need to get fancy here.

T-SQL - get only latest row for selected condition

I have table with measurement with column SERIAL_NBR, DATE_TIME, VALUE.
There is a lot of data so when I need them to get the last 48 hours for 2000 devices
Select * from MY_TABLE where [TIME]> = DATEADD (hh, -48, #TimeNow)
takes a very long time.
Is there a way not to receive all the rows for each device, but only the latest entry? Would this speed up the query execution time?
Assuming that there is column named deviceId(change as per your needs), you can use top 1 with ties with window function row_number:
Select top 1 with ties *
from MY_TABLE
where [TIME]> = DATEADD (hh, -48, #TimeNow)
Order by row_number() over (
partition by deviceId
order by Time desc
);
You can simply create Common Table Expression that sorts and groups the entries and then pick the latest one from there.
;WITH numbered
AS ( SELECT [SERIAL_NBR], [TIME], [VALUE], row_nr = ROW_NUMBER() OVER (PARTITION BY [SERIAL_NBR] ORDER BY [TIME] DESC)
FROM MY_TABLE
WHERE [TIME]> = DATEADD (hh, -48, #TimeNow) )
SELECT [SERIAL_NBR], [TIME], [VALUE]
FROM numbered
WHERE row_nr = 1 -- we want the latest record only
Depending on the amount of data and the indexes available this might or might not be faster than Anthony Hancock's answer.
Similar to his answer you might also try the following:
(from MSSQL's point of view, the below query and Anthony's query are pretty much identical and they'll probably end up with the same query plan)
SELECT [SERIAL_NBR] , [TIME], [VALUE]
FROM MY_TABLE AS M
JOIN (SELECT [SERIAL_NBR] , max_time = MAX([TIME])
FROM MY_TABLE
GROUP BY [SERIAL_NBR]) AS L -- latest
ON L.[SERIAL_NBR] = M.[SERIAL_NBR]
AND L.max_time = M.[TIME]
WHERE M.DATE_TIME >= DATEADD(hh,-48,#TimeNow)
Your listed column values and your code don't quite match up so you'll probably have to change this code a little, but it sounds like for each SERIAL_NBR you want the record with the highest DATE_TIME in the last 48 hours. This should achieve that result for you.
SELECT SERIAL_NBR,DATE_TIME,VALUE
FROM MY_TABLE AS M
WHERE M.DATE_TIME >= DATEADD(hh,-48,#TimeNow)
AND M.DATE_TIME = (SELECT MAX(_M.DATE_TIME) FROM MY_TABLE AS _M WHERE M.SERIAL_NBR = _M.SERIAL_NBR)
This will get you details of the latest record per serial number:
Select t.SERIAL_NBR, q.FieldsYouWant
from MY_TABLE t
outer apply
(
selct top 1 t2.FieldsYouWant
from MY_TABLE t2
where t2.SERIAL_NBR = t.SERIAL_NBR
order by t2.[TIME] desc
)q
where t.[TIME]> = DATEADD (hh, -48, #TimeNow)
Also, worth sticking DATEADD (hh, -48, #TimeNow) into a variable rather than calculating inline.

Calculate same day start/end dates as 0 days if another occurrence already exists

I have a query where there are instances where a "phase" starts and ends on the same day - this is calculated as 1 day. If, however, another "phase" starts and ends on the same day against the same ref. no. and period no., then I'd like to calculate this as 0 days.
Example:
**Ref. Period. Phase StDt EndDt**
013 3 KAA 01/01/16 01/01/16 - This is one day
013 3 TAA 02/01/16 03/01/16 - this is 2 days
013 3 KAT 01/01/16 01/01/16 - **would like this to be counted as 0 day**
013 3 TTA 04/04/16 04/04/16 - this is one day
I would like this unique calculation to be done in the data grouped by Ref. And Period numbers. This is a tricky one....
Thanks
Try this.
I am assuming that you are using TSQl (Not sure a you have also tagged SQL.
;WITH cte_result(ID,Ref, Period,Phase,StDt,EndDt) AS
(
SELECT 1,'013' ,3,'KAA',CAST('01/01/16'AS DATETIME),CAST('01/01/16'AS DATETIME) UNION ALL
SELECT 2,'013' ,3,'TAA','01/02/16','01/03/16' UNION ALL
SELECT 3,'013' ,3,'KAT','01/01/16','01/01/16' UNION ALL
SELECT 4,'013' ,3,'TTA','04/04/16','04/04/16')
,cte_PreResult AS
(
SELECT ROW_NUMBER() OVER (PARTITION BY CAST(StDt AS DATE), CAST(EndDt AS DATE) ORDER BY ID) AS [Order],
Ref,
Period,
Phase,
StDt,
EndDt
FROM cte_result
)
SELECT Ref,
Period,
Phase,
StDt,
EndDt,
CASE
WHEN [Order] <> 1
THEN '0 Day(s)'
ELSE CAST(DATEDIFF(dd, StDt, EndDt) + 1 AS VARCHAR(10)) + ' Day(s)'
END AS Comment
FROM cte_PreResult
If there is no ID column then select some column to order by, probably Phase so replace ID with Phase as here ROW_NUMBER() OVER (PARTITION BY StDt,EndDt ORDER BY ID) AS [Order], if there is no candidate column to order by then try this
;WITH cte_result(ID,Ref, Period,Phase,StDt,EndDt) AS
(
SELECT 1,'013' ,3,'KAA',CAST('01/01/16'AS DATETIME),CAST('01/01/16'AS DATETIME) UNION ALL
SELECT 2,'013' ,3,'TAA','01/02/16','01/03/16' UNION ALL
SELECT 3,'013' ,3,'KAT','01/01/16','01/01/16' UNION ALL
SELECT 4,'013' ,3,'TTA','04/04/16','04/04/16')
,cte_PreResult AS
(
SELECT ROW_NUMBER() OVER (PARTITION BY CAST(StDt AS DATE), CAST(EndDt AS DATE) ORDER BY (SELECT NULL)) AS [Order],
Ref,
Period,
Phase,
StDt,
EndDt
FROM cte_result
)
SELECT Ref,
Period,
Phase,
StDt,
EndDt,
CASE
WHEN [Order] <> 1
THEN '0 Day(s)'
ELSE CAST(DATEDIFF(dd, StDt, EndDt) + 1 AS VARCHAR(10)) + ' Day(s)'
END AS Comment
FROM cte_PreResult
This expression should work on the SSRS side:
=IIF(Fields!StartDate.Value=Fields!EndDate.Value AND Fields!Phase.Value <> LOOKUPSET(Fields!StartDate.Value &"_" & Fields!EndDate.Value,Fields!StartDate.Value & "_" & Fields!EndDate.Value,Fields!Phase.Value,"DatasetName").GetValue(0),0,DATEDIFF("D",Fields!StartDate.Value,Fields!EndDate.Value)+1)
It will return a value of 1 for the first phase returned by the dataset. If the phase-date range combinations are not unique within the grouping, this will not work as written, but you should be able to modify accordingly.
Also, if the rows are sorted differently between SSRS and the dataset, it may not be the first row that appears that gets the 1.
The below did the trick! Basically, I'm using count aggregate to count the number of instances where phases start and end on the same day PER Ref and period. Then, for any where there are more than 1, I just use simple case statments to count the first one as 1 and any subsequent ones as 0. I'm creating the below as a subquery in the joins as a left outer join:
LEFT OUTER JOIN
(SELECT TOP (100) PERCENT Period, Ref,
CONVERT(date, PhaseStartDate) AS stdt, CONVERT(date, PhaseEndDate) AS enddt,
COUNT(*)
AS NoOfSameDayPhases,
MIN(PhaseSequence) AS FirstPhSeq
FROM Phases AS Phases_1
WHERE (CONVERT(date, PhaseStartDate) =
CONVERT(date, PhaseEndDate))
GROUP BY VoidPeriod, Ref, CONVERT(date,
PhaseStartDate), CONVERT(date, PhaseEndDate)) AS SameDayPH ON CONVERT(date,
PhaseEndDate) = SameDayPH.enddt AND CONVERT(date,
PhaseStartDate) = SameDayPH.stdt AND
VoidPeriod = SameDayPH.VoidPeriod AND SameDayPH.Ref =
VoidPhases.Ref

Alternative to cursor when applying a list of values to a where clause?

What's an alternative to getting a distinct number of dates, say all the dates for September:
9/1/2016
9/2/2016
9/3/2016
and apply each value to a query. Say something like:
Select GuitarId,GuitarBrand
From GuitarSales
Where GuitarDate = #date
I don't want to use a cursor, is there an alternative to doing this?
I tried a CTE but even then I'd have to apply the cursor for each date.
If you want all the dates for a month you can use
Select GuitarId,GuitarBrand
From GuitarSales
Where month(GuitarDate) = 9
and year(GuitarDate) = 2016;
If I understand you correctly, you need a list of all dates in September. This is a quick solution to get a gapless list of all days in September: In your query you can use this as source and LEFT JOIN your actual data.
WITH RunningNumbers AS
(
SELECT TOP(30) ROW_NUMBER() OVER(ORDER BY (SELECT NULL))-1 AS Nr
FROM sys.objects
)
SELECT {d'2016-09-01'}+Nr AS RunningDate
FROM RunningNumbers
There are many examples, how you can create a tally table on the fly. Small numbers (like 30 in this example) can be taken easily from any table with sufficient rows.
If you need this more often you might think about a Numbers-Table
a related question: https://stackoverflow.com/a/39387790/5089204
create a persitant numbers table with a lot of usefull side data: https://stackoverflow.com/a/32474751/5089204
Assuming you have an index on GuitarDate here is a way you can create a SARGable where predicate so you can still leverage the speed of using an index seek.
declare #date datetime = '2016-09-10' --just to demonstrate starting with September 10, 2016
select gs.GuitarId
, gs.GuitarBrand
From GuitarSales gs
where gs.GuitarDate >= dateadd(month, datediff(month, 0, #date), 0) --beginning of the month for #date
and gs.GuitarDate < dateadd(month, datediff(month, 0, #date) + 1, 0) --beginning of next month

In SQL, Compare Two Text Columns in Two Different Databases

SQL newb here.
I'm trying to use a UNION to compare some text type columns (thought this would be the easiest way):
SELECT
ResultText
FROM [db1].[dbo].tblTransactions
WHERE GWClientID = 122 AND Created > DATEADD(YEAR, -3, GETDATE())
UNION
SELECT
ResultText
FROM [db2].[dbo].tblTransactions
WHERE GWClientID = 122 AND Created > DATEADD(YEAR, -3, GETDATE())
However I get the error:
The data type text cannot be used as an operand to the UNION,
INTERSECT or EXCEPT operators because it is not comparable.
Why does this happen? What I use UNION ALL.I don't get the error but that's not what I want.
Why don't text types work and how can I compare the text columns?
Thanks for the help!
You can compare the columns by joining them using RIGHT JOIN and assigning common fields for each query with the ROW_NUMBER().
Note: I used MS SQL SERVER 2012
SELECT A.ResultText, B.ResultText FROM
(
SELECT
ROW_NUMBER() OVER (ORDER BY ResultText) AS row,
ResultText
FROM [db1].[dbo].tblTransactions
WHERE GWClientID = 122 AND Created > DATEADD(YEAR, -3, GETDATE())
)AS A
RIGHT JOIN
(
SELECT
ROW_NUMBER() OVER (ORDER BY ResultText) AS row,
ResultText
FROM [db2].[dbo].tblTransactions
WHERE GWClientID = 122 AND Created > DATEADD(YEAR, -3, GETDATE())
)AS B
ON A.row=B.row
Kind of assumes each "subquery" only returns a single row. Not sure how you're planning to use this information though.
select
case
when A.ResultText = B.ResultText then 'Same'
else 'Not the same'
end as Comparison
from
(
SELECT cast(ResultText as varchar(max)) as ResultText
FROM db1.dbo.tblTransactions
WHERE GWClientID = 122 AND Created > DATEADD(YEAR, -3, GETDATE())
) as A,
(
SELECT cast(ResultText as varchar(max)) as ResultText
FROM db2.dbo.tblTransactions
WHERE GWClientID = 122 AND Created > DATEADD(YEAR, -3, GETDATE())
) as B
You could also do an INNER JOIN ON A.ResultText = B.ResultText if you only want values(/pairs) that match up.

Resources