Solved

Query syntax

Posted on 2014-09-19
3
109 Views
Last Modified: 2014-09-19
Hello,

I have a table that contains rows of data for clinics.  Basically each row has the clinicid and then a load of data regarding performance.

There is also a row where the clinicid is -1 (not a valid clinic) and this holds the summarised data for the whole table.

If I want to select a single clinics data, and the summarized data what would the general syntax be?

Give clinicid of 10 I am looking for:
10, clinic specific data, -1, summerised data

Open in new window


Is the query something like the example below, or is there a better syntax

Given that the column names are clinicid, data1, data2, data3 etc.

Select 
@clinicid as ClinicId
Case clinicid = @clinicid then data1 else null end as clinicData1,
Case clinicid = @clinicid then data2 else null end as clinicData2,
Case clinicid = @clinicid then data3 else null end as clinicData3,
-1 as Summary,
Case clinicid = -1 then data1 else null end as SummaryData1,
Case clinicid = -1 then data2 else null end as SummaryData1,
Case clinicid = -1 then data3  else null end as SummaryData1
From MyTable

Open in new window

0
Comment
Question by:soozh
3 Comments
 
LVL 45

Expert Comment

by:Vitor Montalvão
ID: 40332682
Can you post the table structure and a sample of data? And also an example of the expected output?
0
 
LVL 9

Expert Comment

by:macarrillo1
ID: 40332743
I am not sure I understand what you are trying to do.  
But perhaps you can use a subquery as follows:

Select ClinicID, Data, (Select ClinicID from MyTable where ClinicID=-1)
from MyTable
Where ClinicID <>-1

The subquery can pull up your summerized data along side your other records.
0
 
LVL 11

Accepted Solution

by:
John_Vidmar earned 500 total points
ID: 40333788
If you don't mind having 2 rows:
SELECT 	*
FROM	MyTable
WHERE	clinicid IN (@clinicid,-1)

Open in new window

If both records are needed on the same row:
SELECT	*
FROM	(	SELECT 	*
		FROM	MyTable
		WHERE	clinicid = @clinicid
	) a
CROSS
JOIN	(	SELECT 	*
		FROM	MyTable
		WHERE	clinicid = -1
	) b

Open in new window

0

Featured Post

Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

Join & Write a Comment

In this article I will describe the Copy Database Wizard method as one possible migration process and I will add the extra tasks needed for an upgrade when and where is applied so it will cover all.
This article describes how to use the timestamp of existing data in a database to allow Tableau to calculate the prior work day instead of relying on case statements or if statements to calculate the days of the week.
Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.
Here's a very brief overview of the methods PRTG Network Monitor (https://www.paessler.com/prtg) offers for monitoring bandwidth, to help you decide which methods you´d like to investigate in more detail.  The methods are covered in more detail in o…

705 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now