I am new to SQL. I am trying to create an over view using these 2 tables Actual sale Week_Year Target sale To combine into this resulting view with Targets and Actuals: where column Target is ‘Total to be sold’ spread linearly between the available weeks. How can I achieve this using SQL Server? A…
Tag: sql-server
Can’t return column table when the table have just FK?
When I choose a table when it doesn’t have any Primary Key, my query returns nothing. So what’s the problem in this query? This is result when I have pkin my table: And this is result when I haven’t pkin my table: Here is the query: Answer As you are putting INNER JOIN with PRIMARY KEY const…
Select every second record then determine earliest date
I have table that looks like the following I have to select every second record per PatientID that would give the following result (my last query returns this result) I then have to select the record with the oldest date which would be the following (this is the end result I want) What I have done so far: I h…
SQL Order By in Custom Sequence [closed]
Closed. This question needs details or clarity. It is not currently accepting answers. Want to improve this question? Add details and clarify the problem by editing this post. Closed 2 years ago. Improve this question I have data in this order How can I sort this data in this order? Answer To do this requires…
SQL Server XML Parsing Null Result
Hello I have procedure like below USE [DB] GO SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO ALTER PROCEDURE [dbo].[AdminTestSaveAnswer] @refQuestion xml AS BEGIN SET NOCOUNT ON; SELECT …
SQL Server: select records, not linked to another table
I have a table: and a linked table: Relationship between these 2 tables: I want to select all records from CollectionSiteAddress, which are not linked to CollectionSite (neither AddressId nor RemittanceId). Which request should I use? I tried: but it selects all records from CollectionSiteAddress Answer You a…
Summarize aggregations into friendly text
This is my table in SQL Server : and I want this output in SQL Server (return this Text with Query not function): Does anyone know how I can do this? Answer Modern Versions If on SQL Server 2017 or better, you can also do it this way (and this prevents you from having to know in advance and hard-code
Show all varchar values with PIVOT
I need to show all varchar values with pivot but I’m not sure if that is possible? I have OCCUPATIONS table: Pivot query: Query result: Above query gives only 1 record in each column but I want to get all. Answer You ‘ll need an extra column, e.g. with ROW_NUMBER():
Group by column and multiple Rows into One Row multiple columns
Please help me this: I want to group by column TestType, but Result should be splitted into colunms if they have same TestType I want SQL to return data in this format Result# column should be dynamic because each TestType has many Results Answer As I mention in the comments, what you need here is a PIVOT or …
SQL Pivot count incomplete and complete
First of let me start by saying that my SQL know how is pretty limited. So I have below table Now I would like to create a pivot in SQL that looks like so Now PN from second column can repeat from …