Skip to content

Tag: sql-server-2016

How to find ‘MAIN’ master record SQL

I’m running a query: Which returns the following results: I just so happen to know these IDs and am using them for this example. How can I return results like this across the board grouping together linked companies, until I get to the ‘MAIN’ company. There are going to be several other companies where I don’t know the IDs I

Select specific columns from the stored procedure

I have a stored procedure in other database which is maintained by other team. Assume that it is currently returning 3 columns, and my system only needs those 3 columns but the other team can add few more columns for their own use which is causing my system to fail. Other database SP And below is my query, which was

Using HAVING COUNT(column name) NOT working as expected

I have the SQL statement, working in SQL Server 2016, below where I’m getting records from an INNER SELECT UNION ALL statement and in the OUTER SELECT I’m trying to extract those records that meet the HAVING COUNT code however, my code is bringing back no records. Here are the records from my INNER SELECT statement with UNION ALL: LocationSysID

SQL Server 2016: how to get a single row view

I have been trying to word this correctly, but here’s my dbfiddle Table: Expected Output: CustomerKey processdate Product_Mix Product_Mix_Expanded 101 2021-05-01 12,3,5 CCcount, CHKCount, SACount 102 2021-02-03 1,3,1 CCcount, CHKCount, SACount 103 2019-04-12 4,0,2 CCount, SACount As you can see, I used STUFF, but not sure if that’s the right approach. I need the Product Mix showing the count and

How to generate a Forecast based on last row result in SQL

I need help to develop the proposed task: I need to count the current month active employees and then generate a “forecast” for the next six months, the forecast is just the last total active from the current month over the next six months, just changing the date value on the result. Expected Result: Date_Active Location Total_Active Forecast 2021.04 127

Splitting up events that occur over the day boundary

I have a table of events with a start time and an end time, with some events that have a start time before midnight and an end time after midnight. I’d like to produce output that splits up these events at the midnight barrier so they can be counted toward their respective date. The above table could be produced by