Trying to INSERT to based on CASE statement with SELECT VALUE - sql

I Have a list of of SpellIDs with corresponding M values.
Given by the statement
SELECT TOP(1000) Spell_ID, MAX(Episode_Order) as M
From [dbo].[Client_MidEssex_Inpatient_Episodes_Landing]
GROUP BY Spell_ID
output
What I want to is to INSERT on the Episode Order Column on my Inpatients_episodes_table, a value depending on the M column from the select statement, on the corresponding spell_IDs. (Each row has a spell ID and Episode order value)
The logic is:For each spell ID value in the table Check the corresponding spellID in the select statement and -> check M value. Then Compare M value from the select statement to the Episode Order value from the table. Based on this comparison, insert either a discharge or admittance code
I am trying to relate the select statement and the table both on spell_IDs, but I'm not too how.
So far I have this:
UPDATE Inpatient_Episodes_Landing SET Episode_Ward = Null;
INSERT INTO [dbo].[Inpatient_Episodes_Landing] ([Episode_Ward])
SELECT
(Case when Episode_Order=01 then Admission_Ward_Code
when Episode_Order = (SELECT Spell_ID, MAX(Episode_Order) as M
From [dbo].[Client_MidEssex_Inpatient_Episodes_Landing]
where Spell_ID = (select SpellID from [dbo].[Inpatient_Episodes_Landing])
GROUP BY Spell_ID ) then Discharge_Ward_Code
else Admission_Ward_Code
END)
FROM [dbo].[Client_MidEssex_Inpatient_Episodes_Landing]
Select TOP (1000) SpellID,Episode_Number,Episode_Ward from Inpatient_Episodes_Landing
Any help would be great, thank you!

Related

Case Statement Illegal Expression SQL

I don't get where the error msg 'Illegal expression in WHEN clause of CASE expression' comes from by running this snippet in Teradata SQL
CASE
WHEN f_ev1.PROCESS_NO IN
(
SELECT
numbers.PROCESS_NO
FROM numbers
WHERE
numbers.FLAG = 1
GROUP BY 1 --no duplicates
) THEN 2
END AS Status,
If I write IN (some numbers) it works just fine. But it is a list that consists of 50 unique values that can possibly change over time.
My Suggestion is that you can load the fifty values into another table and you can join that table with the main table
table_50 - Table with 50 unique values
main_tbl - Main table
insert into table_50 ( process_no ) SELECT numbers.PROCESS_NO FROM numbers WHERE numbers.FLAG = 1 GROUP BY 1 ;
Select case when b.process_no is null then 2 end as status from main_table a
left join table_50 b
on a.process_no= b.process_no
From a performance perspective, this is more effective and you can also update the values in the table_50 as per your requirement

Check for same character value in column

I'm trying to verify when a column has all the same values for the same group. Here is a sample of my table data:
So using this data, for example. I want to check to see if all values of Status is the same for every row with the same TPID. So TPID 60210 should result with True since both items have a Status of A. However, TPID 60061 should result in false since two of the Line_Item show A and the rest P.
I intend to update a different table using this information, setting its status using a CASE statement. But I'm at a loss how to check against this column to find the values I desire.
;WITH CTE_Count
AS
(
SELECT TPID, COUNT(DISTINCT Status) CNT
FROM TableName
GROUP BY TPID
)
UPDATE AnotherTableName
SET ColumnName = (
CASE WHEN CTE_Count.CNT = 1 -- all row has same status
THEN SomeValue
ELSE SomeOtherValue END
)
FROM AnotherTableName
INNER JOIN CTE_Count ON ...

I am trying to return a certain values in each row which depend on whether different values in that row are already in a different table

I'm still a n00b at SQL and am running into a snag. What I have is an initial selection of certain IDs into a temp table based upon certain conditions:
SELECT DISTINCT ID
INTO #TEMPTABLE
FROM ICC
WHERE ICC_Code = 1 AND ICC_State = 'CA'
Later in the query I SELECT a different and much longer listing of IDs along with other data from other tables. That SELECT is about 20 columns wide and is my result set. What I would like to be able to do is add an extra column to that result set with each value of that column either TRUE or FALSE. If the ID in the row is in #TEMPTABLE the value of the additional column should read TRUE. If not, FALSE. This way the added column will ready TRUE or FALSE on each row, depending on if the ID in each row is in #TEMPTABLE.
The second SELECT would be something like:
SELECT ID,
ColumnA,
ColumnB,
...
NEWCOLUMN
FROM ...
NEWCOLUMN's value for each row would depend on whether the ID in that row returned is in #TEMPTABLE.
Does anyone have any advice here?
Thank you,
Matt
If you left join to the #TEMPTABLE you'll get a NULL where the ID's don't exist
SELECT ID,
ColumnA,
ColumnB,
...
T.ID IS NOT NULL AS NEWCOLUMN -- Gives 1 or 0 or True/false as a bit
FROM ... X
LEFT JOIN #TEMPTABLE T
ON T.ID = X.ID -- DEFINE how the two rows can be related unquiley
You need to LEFT JOIN your results query to #TEMPTABLE ON ID, this will give you the ID if there is one and NULL if there isn't, if you want 1 or 0 this would do it (For SQL Server) ISNULL(#TEMPTABLE.ID,0)<>0.
A few notes on coding for performance:
By definition an ID column is unique so the DISTINCT is redundant and causes unnecisary processing (unless it is an ID from another table)
Why would you store this to a temporary table rather than just using it in the query directly?
You could use a union and a subquery.
Select . . . . , 'TRUE'
From . . .
Where ID in
(Select id FROM #temptable)
UNION
SELECT . . . , 'FALSE'
FROM . . .
WHERE ID NOT in
(Select id FROM #temptable)
So the top part, SELECT ... FROM ... WHERE ID IN (Subquery), does a SELECT if the ID is in your temptable.
The bottom part does a SELECT if the ID is not in the temptable.
The UNION operator joins the two results nicely, since both SELECT statements will return the same number of columns.
To expand on what someone else was saying with Union, just do something like so
SELECT id, TRUE AS myColumn FROM `table1`
UNION
SELECT id, FALSE AS myColumn FROM `table2`

Oracle SQL: Returning a Record even when a specific value doesn't exist

I have a query where I'm trying to pull some values from a table where a specific ID is queried for. If that value doesn't exist, I would still like the query to return a record that only has that ID value I was looking for. Here's what I've tried so far.
Select attr.attrval, attr.uidservicepoint, sp.servicepointid
From bilik.lssrvcptmarketattr attr
Join bilik.lsmarketattrtype type on attr.uidmarketattrtype = type.uidmarketattrtype AND
type.attrtype IN ('CAPACITY_REQUIREMENT_KW') and TO_CHAR( attr.starttime , 'mm/dd/yyyy')in ('05/01/2011')
Right Outer Join bilik.lsservicepoint sp on attr.uidservicepoint = sp.uidservicepoint
Where sp.servicepointid in ('RGE_R01000051574382') Order By sp.servicepointid ASC
In this example, I'm trying to look for RGE_R01000051574382. If that doesn't exist in table SP.servicepointid, I want it to still return the 'RGE_R01000051574382' in a record with nulls for the other values I'm pulling. Normally, when I'm running this, I will be pulling about 1000 specific values at a time.
If anyone has any insight that they can give on this, it would be greatly appreciated. Thanks so much!
If I understand correctly, you just need to move the WHERE clause into the JOIN clause.
select attr.attrval,
attr.uidservicepoint,
sp.servicepointid
from bilik.lssrvcptmarketattr attr
join bilik.lsmarketattrtype type on attr.uidmarketattrtype = type.uidmarketattrtype
and type.attrtype in ('CAPACITY_REQUIREMENT_KW')
and TO_CHAR(attr.starttime, 'mm/dd/yyyy') in ('05/01/2011')
right outer join bilik.lsservicepoint sp on attr.uidservicepoint = sp.uidservicepoint
and sp.servicepointid in ('RGE_R01000051574382')
order by sp.servicepointid
I think you're saying you want to have a record returned, with the servicepointid column populated, but all others null?
In that case, use a union.
select ...your query without order by...
and sp.servicepointid = 'RGE_R010000515743282'
union
select null, null, 'RGE_R010000515743282'
from dual
where not exists (select 'x' from (...your query without order by...))
Here's a complete example:
create table test (id number, val varchar2(10));
insert into test (id, val) values (1, 'hi');
select id,
val
from test
where id = 1
union
select 1,
null
from dual
where not exists (select 'x'
from test
where id = 1)

Count(*) with 0 for boolean field

Let's say I have a boolean field in a database table and I want to get a tally of how many are 1 and how many are 0. Currently I am doing:
SELECT 'yes' AS result, COUNT( * ) AS num
FROM `table`
WHERE field = 1
UNION
SELECT 'no' AS result, COUNT( * ) AS num
FROM `table`
WHERE field = 0;
Is there an easier way to get the result so that even if there are no false values I will still get:
----------
|yes | 3 |
|no | 0 |
----------
One way would be to outer join onto a lookup table. So, create a lookup table that maps field values to names:
create table field_lookup (
field int,
description varchar(3)
)
and populate it
insert into field_lookup values (0, 'no')
insert into field_lookup values (1, 'yes')
now the next bit depends on your SQL vendor, the following has some Sybase (or SQL Server) specific bits (the outer join syntax and isnull to convert nulls to zero):
select description, isnull(num,0)
from (select field, count(*) num from `table` group by field) d, field_lookup fl
where d.field =* fl.field
you are on the right track, but the first answer will not be correct. Here is a solution that will give you Yes and No even if there is no "No" in the table:
SELECT 'Yes', (SELECT COUNT(*) FROM Tablename WHERE Field <> 0)
UNION ALL
SELECT 'No', (SELECT COUNT(*) FROM tablename WHERE Field = 0)
Be aware that I've checked Yes as <> 0 because some front end systems that uses SQL Server as backend server, uses -1 and 1 as yes.
Regards
Arild
This will result in two columns:
SELECT SUM(field) AS yes, COUNT(*) - SUM(field) AS no FROM table
Because there aren't any existing values for false, if you want to see a summary value for it - you need to LEFT JOIN to a table or derived table/inline view that does. Assuming there's no TYPE_CODES table to lookup the values, use:
SELECT x.desc_value AS result,
COALESCE(COUNT(t.field), 0) AS num
FROM (SELECT 1 AS value, 'yes' AS desc_value
UNION ALL
SELECT 2, 'no') x
LEFT JOIN TABLE t ON t.field = x.value
GROUP BY x.desc_value
SELECT COUNT(*) count, field FROM table GROUP BY field;
Not exactly same output format, but it's the same data you get back.
If one of them has none, you won't get that rows back, but that should be easy enough to check for in your code.