what I tried was this
with basequery as(
SELECT dbo.PatientMedications.PatientID as IDnum,
dbo.PatientMedications.VisitDate as VDate, dbo.Drugs.abbreviation as
drugA, dbo.MedicationStatusLookup.Abbreviation as medA
FROM (dbo.PatientMedications LEFT JOIN dbo.Drugs ON
dbo.PatientMedications.DrugID = dbo.Drugs.drugId) LEFT JOIN
dbo.MedicationStatusLookup ON dbo.PatientMedications.StatusID =
dbo.MedicationStatusLookup.StatusLookupID
WHERE (((dbo.Drugs.abbreviation)='abat' Or
(dbo.Drugs.abbreviation)='adal' Or (dbo.Drugs.abbreviation)='allo' Or
(dbo.Drugs.abbreviation)='aur' Or (dbo.Drugs.abbreviation)='aza' Or
(dbo.Drugs.abbreviation)='chlq' Or (dbo.Drugs.abbreviation)='colch' Or
(dbo.Drugs.abbreviation)='ctz' Or (dbo.Drugs.abbreviation)='cya' Or
(dbo.Drugs.abbreviation)='etan' Or (dbo.Drugs.abbreviation)='glm' Or
(dbo.Drugs.abbreviation)='hcq' Or (dbo.Drugs.abbreviation)='i-mtx' Or
(dbo.Drugs.abbreviation)='infl' Or (dbo.Drugs.abbreviation)='lefl' Or
(dbo.Drugs.abbreviation)='mtx' Or (dbo.Drugs.abbreviation)='pred' Or
(dbo.Drugs.abbreviation)='ritux' Or (dbo.Drugs.abbreviation)='ssz' Or
(dbo.Drugs.abbreviation)='tcz'))
)
select
abat,adal,allo,aur,aza,chlq,colch,ctz,cya,etan,glm,hcq,[i-mtx],infl,lefl,mtx,pred,ritux,ssz,tcz
FROM basequery
PIVOT (count(medA) for IDnum in
([abat],[adal],[allo],[aur],[aza],[chlq],[colch],[ctz],[cya],[etan],[glm],[hcq],[i-mtx],[infl],[lefl],[mtx],[pred],[ritux],[ssz],[tcz]))
as abbrevi
it works but will not give them what they want.
they have records such as
patientid Visitdate Drug Type DrugFlag
0000000 01/01/2001 abat N
0000000 01/01/2001 ssz T
they are wanting
patientid abat cya ssz
0000000 N T
a pivot must be an aggrigate and will not use first or last so this
runs but only produces 0 in all positions but I might have something
else wrong.
thanks for your help this is more than I expected (sure wish I could
have just cut and pasted) lol
On Tue, 08 Jan 2013 21:38:15 +0100, Erland Sommarskog
Post by Erland SommarskogPost by unknownWe have some sql databases that we have always used access as the
front end and have a lot of queries that work fine.
Now they have decided to convert all this to just sql with no front
end.
No front end? Are end users going to use Managment Studio or what?
Post by unknownps some of these are crosstab queries and I have never seen one in
sql.
So here is the scoop: writing a crosstab query in standard SQL is not
a problem - as long as you know what columns you will produce. A result
set in SQL (and I mean SQL in general, not just T-SQL) is a table, and
a table has a fixed number of columns with a fixed data type. This is
not strange at all, since each column is supposed to model a unique
attribute.
Say that you want to show sales per month for a year per product and
SELECT ProductID, Region,
Jan = SUM(CASE WHEN datepart(MONTH, SaleDate) = 1 THEN Amt END),
Feb = SUM(CASE WHEN datepart(MONTH, SaleDate) = 2 THEN Amt END),
...
Dec = SUM(CASE WHEN datepart(MONTH, SaleDate) = 12 THEN Amt END)
FROM Sales
WHERE SaleDate >= '20120101'
AND SaleDate < '20130101'
GROUP BY ProductID, Region
In this example I assume that you needed to sum anyway, but if you just want
to display you can use MIN or MAX. Everything else is the same.
Bob Barrows mentioned the PIVOT keyword, but I recommend that you stay
away from it. For an example like the above, the code gets a little
shorter, but it is less flexible. For instance, if you want to add a
total column for the entire year, this is a no-brainer with the example
above. Furthermore, the above pattern works on any DBMS, while PIVOT
is proprietary to SQL Server.
However, in many cases, you don't know beforehand how many columns or what
the names of the columns will be. Access has a solution for this in the
TRANSFORM keyword. There is no built-in solution for this in SQL Server,
and it is a concept that is entirely non-relational.
It pops up in the forums about daily, and a search on "Dynamic Crosstab"
will give you both good and bad solutions. I have some examples and a
http://www.sommarskog.se/dynamic_sql.html#Crosstab
However, as I said, this is non-relational, and there is reason to put
this in the frontend if possible. I am told that SSRS (SQL Server Reporting
Services) has it built-in, but I am not an SSRS guy myself.