I want to select specific values from a table called Prescriptions. I want to select a patient that is utilizing the following medicines at a certain period in time:
This is the select that I have been using, but it returns patients that are utilizing just one or two of the medicines. I want to be able to bring just the patient that is utilizing both of the medicines:
SELECT MEDICINE, CLIENTNUMBER
FROM PRESCRIPTIONS
where
(MEDICINE like 'simvastatin %' OR
MEDICINE like 'amlodipine%')
AND
DATE between '11/23/2010' and '11/23/2010'
Group by CLIENTNUMBER, MEDICINE
Order by CLIENTNUMBER
Can anyone help? Thank you very much!
You could use two exists clauses to ensure the client has subscriptions for both medicines:
select p.MEDICINE
, p.CLIENTNUMBER
from PRESCRIPTIONS as p
where p.DATE between '11/23/2010' and '11/23/2011'
and exists
(
select *
from PRESCRIPTIONS as p2
where p2.DATE between '11/23/2010' and '11/23/2011'
and p2.CLIENTNUMBER = p.CLIENTNUMBER
and p2.MEDICINE like 'simvastatin %'
)
and exists
(
select *
from PRESCRIPTIONS as p3
where p3.DATE between '11/23/2010' and '11/23/2011'
and p3.CLIENTNUMBER = p.CLIENTNUMBER
and p3.MEDICINE like 'amlodipine %'
)
Try this:
SELECT p1.Medicine m1, p2.Medicine m2, p1.ClientNumber cn
FROM Prescriptions p1 INNER JOIN Prescriptions p2
ON p1.ClientNumber = p2.ClientNumber
WHERE (
((m1 LIKE 'simvastatin %') AND (m2 LIKE 'amlodipine%')) OR
((m2 LIKE 'simvastatin %') AND (m1 LIKE 'amlodipine%'))
AND Date BETWEEN '11/23/2010' AND '11/23/2010')
GROUP BY cn, m1
ORDER BY cn
you must create a table for your medician an then get division of prescription on it.
Related
I have a table that shows the preference of a person to a school, like this
Person(name, preference_id)
/
School(school_id, name)
each person can have interest in multiple schools(shown by preference_id), like this:
Person(John, 132)
Person(John, 133)
School(132, UVA)
School(133, UMBC)
^John would like to go to either UVA or UMBC.
I want to write a query that displays ALL the school.name s for-each person.
select person.name, school.name
from Person, School
where person.preference_id = school.school_id
;
But what I have above would return in a 1 person- 1 school name ratio. Anyway I can modify this so it's 1 person - multi-schoolname? Thanks
SELECT p.name,
LISTAGG( s.name, ',' ) WITHIN GROUP ( ORDER BY s.name ) AS preferences
FROM Person p
INNER JOIN School s
ON ( p.preference_id = s.school_id )
GROUP BY p.name
I am trying to get results of a customer from two linked servers remotely. i need to sum the points of every cust_id but am having problems with my query
SELECT sum(cust_point) as total
FROM [192.168.23.9].[POSDBV4].[dbo].[loyal_summery_branch] where cust_id='0100015388'
INNER JOIN [192.168.13.4].[POSDBV4].[dbo].[loyal_summery_branch]
ON cust_id.[192.168.23.9].[POSDBV4].[dbo].[loyal_summery_branch]=cust_id.[192.168.13.4].[POSDBV4].[dbo].[loyal_summery_branch];
I think you have your query syntax a little scrambled there. Try this.
SELECT sum(cust_point) as total
FROM [192.168.23.9].[POSDBV4].[dbo].[loyal_summery_branch] A
INNER JOIN [192.168.13.4].[POSDBV4].[dbo].[loyal_summery_branch] B ON A.cust_id=B.cust_id
WHERE cust_id='0100015388'
As you want the sum of cust_point of both of the table. Please find the query below
Select( (SELECT sum(cust_point)
FROM [192.168.23.9].[POSDBV4].[dbo].[loyal_summery_branch] where cust_id='0100015388') +
(SELECT sum(cust_point)
FROM [192.168.13.4].[POSDBV4].[dbo].[loyal_summery_branch] where cust_id='0100015388') ) as total
you can always use a UNION ALL here if you like.. this will allow you select other fields as well if you include a GROUP BY
SELECT SUM(cust_point) AS total
FROM (
SELECT cust_point
FROM [192.168.23.9].[POSDBV4].[dbo].[loyal_summery_branch]
WHERE cust_id = '0100015388'
UNION ALL
SELECT cust_point
FROM [192.168.13.4].[POSDBV4].[dbo].[loyal_summery_branch]
WHERE cust_id = '0100015388'
) t
I am having difficulties setting up the following query where I want to:
find all the WORKORDER values in the MATERIALSCOSTACT table that have one of two specific MATERIALID values.
Then from the WORKORDER table I want to weed out all the values with a 'main replace' or 'install' DESCRIPTION and then I want to make sure I am only looking at water or sewer ASSETGROUP. I want to make sure I am only looking at records that have a WOMACOST that have occured since the first of the year.
I would like to inner join the MATERIALSCOSTACT table and the WORKORDER table on their shared WORKORDERID column.
I cant figure out how to join the first statement with teh second, and in the second statement, SQL Server is giving me a syntax error on "INNER"
select workorderid from [CityWorks].[AZTECA].[materialcostact] where materialsid = '30791' or
materialsid = '30841'
Select * from [CityWorks].[AZTECA].[WORKORDER]
where description not like '%main replace%' and description not like '%install%'
and (assetgroup = 'WATER' or Assetgroup = 'SEWER')
and womatcost != 0
and ACTUALSTARTDATE > '2013-12-31 00:00:00.000'
INNER JOIN workorderid on [CityWorks].[AZTECA].[materialcostact].[workorderid] =
[CityWorks].[AZTECA].[WORKORDER].[workorderid]
group by assetgroup
Thank you very much to anyone who can help!
You just have your where clause in the wrong place. It needs to come after the joins.
Select * from [CityWorks].[AZTECA].[WORKORDER]
INNER JOIN workorderid on [CityWorks].[AZTECA].[materialcostact].[workorderid] =
[CityWorks].[AZTECA].[WORKORDER].[workorderid]
where description not like '%main replace%' and description not like '%install%'
and (assetgroup = 'WATER' or Assetgroup = 'SEWER')
and womatcost != 0
and ACTUALSTARTDATE > '2013-12-31 00:00:00.000'
group by assetgroup
You may have to alias your tables or be more explicit in your where clause if those columns occur in more than one table.
I believe this is what you want:
NB: try to use aliases, they make your code easier to read.
Select *
from [CityWorks].[AZTECA].[WORKORDER] WO
JOIN [CityWorks].[AZTECA].[materialcostact] MC
on WO.[workorderid] = MC.[workorderid]
and (MC.materialsid = 30791 or MC.materialsid = 30841)
where description not like '%main replace%' and description not like '%install%'
and (assetgroup = 'WATER' or Assetgroup = 'SEWER')
and womatcost != 0
and ACTUALSTARTDATE >= '2014-01-01'
My SQL skills aren't great hence the post.
I'm trying to get all the contact names based on a company out.
For example I have two statements:
Select Id, CompanyName, Address From Clients
Select ClientId, ContactName From Contacts
You may have many contacts to a single client
Result: (I need all the contact names in a single column)
ContactName Company Address
----------------------------------------
Johh, Steve 123 Comp 12345 Address
David,Mike, Sarah 44 Comp 111 Address
A working example would be very much appreciated.
SELECT DISTINCT (
SELECT ISNULL(ct.ContactName, '') + ', '
FROM dbo.Clients cl JOIN dbo.Contacts ct ON cl.Id = ct.ClientId
WHERE cl.ID = cl2.Id
FOR XML PATH('')) AS ContactName, CAST(cl2.Id AS nvarchar(7)) + ' ' + cl2.CompanyName AS Company, Address
FROM dbo.Clients cl2
ORDER BY 2
Demo on SQLFiddle
Firstly build all the Contact Names for a Company into a Single Column. Assuming the database to be SQL Server, I'm using a Common Table Expression to store the single column contact list. Once the CTE is built, join it with the Clients table to get the ContactNames. FOR XML is used to concatenate rows.
WITH CTEContactList(ClientID,ContactNames)
AS
(
SELECT c1.ClientID,
Names = SUBSTRING(( SELECT ', ' + c2.ContactName
FROM Contacts c2
WHERE c1.ClientID = c2.ClientID
FOR XML PATH ('')),3,8000 ))
FROM Contacts c1
GROUP BY c1.ClientID
)
SELECT
cl.ID,
cl.CompanyName,
cl.Address,
ctelist.ContactNames
FROM Clients cl
INNER JOIN CTEContactList ctelist
ON cl.ID = cteList.ClientID
Sounds like you need to do a table join.
Example: two tables here
1. Person
2. Orders
Query:
SELECT
Persons.LastName, Persons.FirstName, Orders.OrderNo
FROM Persons
INNER JOIN Orders ON Persons.P_Id = Orders.P_Id
ORDER BY Persons.LastName
You didn't specify your DBMS, so I'm assuming PostgreSQL:
select string_agg(ct.contactName, ', '), cl.companyname, cl.address
from contacts ct
join clients cl on cl.id = ct.clientId
group by cl.companyname, cl.address
I'm pulling my hair out over a subquery that I'm using to avoid about 100 duplicates (out of about 40k records). The records that are duplicated are showing up because they have 2 dates in h2.datecreated for a valid reason, so I can't just scrub the data.
I'm trying to get only the earliest date to return. The first subquery (that starts with "select distinct address_id", with the MIN) works fine on it's own...no duplicates are returned. So it would seem that the left join (or just plain join...I've tried that too) couldn't possibly see the second h2.datecreated, since it doesn't even show up in the subquery. But when I run the whole query, it's returning 2 values for some ipc.mfgid's, one with the h2.datecreated that I want, and the other one that I don't want.
I know it's got to be something really simple, or something that just isn't possible. It really seems like it should work! This is MSSQL. Thanks!
select distinct ipc.mfgid as IPC, h2.datecreated,
case when ad.Address is null
then ad.buildingname end as Address, cast(trace.name as varchar)
+ '-' + cast(trace.Number as varchar) as ONT,
c.ACCOUNT_Id,
case when h.datecreated is not null then h.datecreated
else h2.datecreated end as Install
from equipmentjoin as ipc
left join historyjoin as h on ipc.id = h.EQUIPMENT_Id
and h.type like 'add'
left join circuitjoin as c on ipc.ADDRESS_Id = c.ADDRESS_Id
and c.GRADE_Code like '%hpna%'
join (select distinct address_id, equipment_id,
min(datecreated) as datecreated, comment
from history where comment like 'MAC: 5%' group by equipment_id, address_id, comment)
as h2 on c.address_id = h2.address_id
left join (select car.id, infport.name, carport.number, car.PCIRCUITGROUP_Id
from circuit as car (NOLOCK)
join port as carport (NOLOCK) on car.id = carport.CIRCUIT_Id
and carport.name like 'lead%'
and car.GRADE_Id = 29
join circuit as inf (NOLOCK) on car.CCIRCUITGROUP_Id = inf.PCIRCUITGROUP_Id
join port as infport (NOLOCK) on inf.id = infport.CIRCUIT_Id
and infport.name like '%olt%' )
as trace on c.ccircuitgroup_id = trace.pcircuitgroup_id
join addressjoin as ad (NOLOCK) on ipc.address_id = ad.id
The typical approach to only getting the lowest row is one of the following. You didn't bother to specify what version of SQL Server you're using, what you want to do with ties, and I have little interest to try to work this into your complex query, so I'll show you an abstract simplification for different versions.
SQL Server 2000
SELECT x.grouping_column, x.min_column, x.other_columns ...
FROM dbo.foo AS x
INNER JOIN
(
SELECT grouping_column, min_column = MIN(min_column)
FROM dbo.foo GROUP BY grouping_column
) AS y
ON x.grouping_column = y.grouping_column
AND x.min_column = y.min_column;
SQL Server 2005+
;WITH x AS
(
SELECT grouping_column, min_column, other_columns,
rn = ROW_NUMBER() OVER (ORDER BY min_column)
FROM dbo.foo
)
SELECT grouping_column, min_column, other_columns
FROM x
WHERE rn = 1;
This subqery:
select distinct address_id, equipment_id,
min(datecreated) as datecreated, comment
from history where comment like 'MAC: 5%' group by equipment_id, address_id, comment
Probably will return multiple rows because the comment is not guaranteed to be the same.
Try this instead:
CROSS APPLY (
SELECT TOP 1 H2.DateCreated, H2.Comment -- H2.Equipment_id wasn't used
FROM History H2
WHERE
H2.Comment LIKE 'MAC: 5%'
AND C.Address_ID = H2.Address_ID
ORDER BY DateCreated
) H2
Switch that to OUTER APPLY in case you want rows that don't have a matching desired history entry.