Query error: Column name ICUSTAY_ID is ambiguous. Using multiple subqueries in BigQuery - sql

Hi, I receive the following query error "Query error: Column name ICUSTAY_ID is ambiguous" referred to the third last line of code (see the following code). Please can you help me? Thank you so much!
I am an SQL beginner..
WITH t AS
(
SELECT
*
FROM
(
SELECT *,
DATETIME_DIFF(CHARTTIME, INTIME, MINUTE) AS pi_recorded
FROM
(
SELECT
*
FROM
(
SELECT * FROM
(SELECT i.SUBJECT_ID, p.dob, i.hadm_id, p.GENDER, a.ETHNICITY, a.ADMITTIME, a.INSURANCE, i.ICUSTAY_ID,
i.DBSOURCE, i.INTIME, DATETIME_DIFF(a.ADMITTIME, p.DOB, DAY) AS age,
CASE
WHEN DATETIME_DIFF(a.ADMITTIME, p.DOB, DAY) <= 32485
THEN 'adult'
WHEN DATETIME_DIFF(a.ADMITTIME, p.DOB, DAY) > 32485
then '>89'
END AS age_group
FROM `project.mimic3.ICUSTAYS` AS i
INNER JOIN `project.mimic3.PATIENTS` AS p ON i.SUBJECT_ID = p.SUBJECT_ID
INNER JOIN `project.mimic3.ADMISSIONS` AS a ON i.HADM_ID = a.HADM_ID)
WHERE age >= 6570
) AS t1
LEFT JOIN
(
SELECT ITEMID, ICUSTAY_ID, CHARTTIME, VALUE, FROM `project.mimic3.CHARTEVENTS`
WHERE ITEMID = 551 OR ITEMID = 552 OR ITEMID = 553 OR ITEMID = 224631
OR ITEMID = 224965 OR ITEMID = 224966
) AS t2
ON t1.ICUSTAY_ID = t2.ICUSTAY_ID
)
)
WHERE ITEMID IN (552, 553, 224965, 224966) AND pi_recorded <= 1440
)
SELECT ICUSTAY_ID #### Query error: Column name ICUSTAY_ID is ambiguous
FROM t
GROUP BY ICUSTAY_ID;

Both t1 and t2 have a column called ICUSTAY_ID. When you join them together into a single dataset you end up with 2 columns with the same name - which obviously can't work as there would be no way of uniquely identify each column.
You need to alias these columns in you code or not include one or the other if you don't need both

Related

SQL Command to add a fields, or field with Formula

I am trying to add a field call MaxDate (the latest DOS Date from DOS column in table) which I have in code so it can show up in output but it is not working. my SQL Query is as follows.
CODING
Select distinct t.RECORD_ID, t.SEQ_NO, t.CLAIM, t.HIC_NO as HICN_MBI ,t.ID, DOS, t.ERROR_1
from Sandbox.dbo.XYZ t
inner join (
select CLAIM, max(DOS) as MaxDate
from Sandbox.dbo.XYZ
Group by CLAIM
) tm on t.CLAIM = tm.CLAIM and t.DOS = tm.MaxDate
where RECORD_ID='inf' OR RECORD_ID='REJ'
and ERROR_1 in ('004',
'001',
'002',
'003')
OUTPUT RETURN columns
will show the columns but the maxdate will not appear in output
RECORD_ID SEQ_NO CLAIM HICN_MBI ID DOS ERROR_1
What am I doing wrong?
The reason why the code you are using is not selecting the maxdate is because in the select statement you have not selected maxdate it is inside the table alias tm. I have added tm.maxDate in the select and this should work.
SELECT DISTINCT t.RECORD_ID
,t.SEQ_NO
,t.CLAIM
,t.HIC_NO AS HICN_MBI
,t.ID
,DOS
,t.ERROR_1
,tm.MaxDate
FROM Sandbox.dbo.XYZ t
INNER JOIN (
SELECT CLAIM
,max(DOS) AS MaxDate
FROM Sandbox.dbo.XYZ
GROUP BY CLAIM
) tm ON t.CLAIM = tm.CLAIM
AND t.DOS = tm.MaxDate
WHERE RECORD_ID = 'inf'
OR RECORD_ID = 'REJ'
AND ERROR_1 IN (
'004'
,'001'
,'002'
,'003'
)

How to Select * Where Everything is Distinct Except One Field

I'm trying to pull 6 records using the code below but there are some cases where the information is updated and therefore it is pulling duplicate records.
My code:
SELECT column2, count(*) as 'Count'
FROM ServiceTable p
join HIERARCHY h
on p.LOCATION_CODE = h.LOCATION
where Report_date between '2017-04-01' and '2017-04-30'
and Column1 = 'Issue '
and LOCATION = '8789'
and
( record_code = 'INCIDENT' or
(
SUBMIT_METHOD = 'Web' and
not exists
(
select *
from ServiceTable p2
where p2.record_code = 'INCIDENT'
and p2.incident_id = p.incident_id
)
)
)
The problem is that instead of the six records it is pulling eight. I would just use distinct * but the file_date is different on the duplicate entries:
FILE_DATE Incident_ID Column1 Column2
4/4/17 123 Issue Service - Red
4/4/17 123 Issue Service - Blue
4/5/17 123 Issue Service - Red
4/5/17 123 Issue Service - Blue
The desired output is:
COLUMN2 COUNT
Service - Red 1
Service - Blue 1
Any help would be greatly appreciated! If you need any other info just let me know.
If you turn your original select statement without the aggregation function into a subquery, you can distinct that on your values that are not the changing date, then select a COUNT from there. Don't forget your GROUP BY clause at the end.
SELECT Column2, COUNT(Incident_ID) AS Service_Count
FROM (SELECT DISTINCT Incident_ID, Column1, Column2
FROM ServiceTable p
JOIN HIERARCHY h ON p.LOCATION_CODE = h.LOCATION
WHERE Report_date BETWEEN '2017-04-01' AND '2017-04-30'
AND Column1 = 'Issue '
AND LOCATION = '8789'
AND
( record_code = 'INCIDENT' or
(
SUBMIT_METHOD = 'Web' and
NOT EXISTS
(
SELECT *
FROM ServiceTable p2
WHERE p2.record_code = 'INCIDENT'
AND p2.incident_id = p.incident_id)
)
)
)
GROUP BY Column2
Also, if you are joining tables it is a good practice to fully qualify the field you are selecting. Example: p.Column2, p.Incident_ID, h.LOCATION. That way, even your distinct fields are easier to follow where they came from and how they relate.
Finally, don't forget that COUNT is a reserved word. I modified your alias accordingly.
If you are using an aggregation function (count), you should use group by for the column not in the aggregation function:
SELECT column2, count(*) as 'Count'
FROM ServiceTable p
join HIERARCHY h
on p.LOCATION_CODE = h.LOCATION
where Report_date between '2017-04-01' and '2017-04-30'
and Column1 = 'Issue '
and LOCATION = '8789'
and
( record_code = 'INCIDENT' or
(
SUBMIT_METHOD = 'Web' and
not exists
(
select *
from ServiceTable p2
where p2.record_code = 'INCIDENT'
and p2.incident_id = p.incident_id
)
)
)
group by column2

Syntax error in FROM clause - MS ACCESS

I am working with a tool that would extract some data from an Access Database. So basically, i am working on a query to get this data.
Below is the code i am currently working on.
I am getting an error: Syntax error in FROM clause
I can't seem to find where the query is going wrong. I would appreciate any help! Thank youu.
EDIT: putting my actual query
SELECT table_freq.*, IIF(table_freq.txn_ctr > (table_ave_freq.ave_freq * 3), "T", "F") as suspicious_flag
FROM
(
SELECT tbl_TransactionHistory.client_num, tbl_TransactionHistory.client_name,
tbl_TransactionHistory.transaction_date, Count(tbl_TransactionHistory.client_num) AS txn_ctr
FROM tbl_TransactionHistory
GROUP BY tbl_TransactionHistory.client_num, tbl_TransactionHistory.client_name,
tbl_TransactionHistory.transaction_date
) AS table_freq
INNER JOIN
(
SELECT table_total_freq.client_num, total_txn_ctr as TotalTransactionFrequency, total_no_days as TotalTransactionDays,
(table_total_freq.total_txn_ctr)/(table_no_of_days.total_no_days) AS ave_freq
FROM
(
(
SELECT client_num, SUM(txn_ctr) AS total_txn_ctr
FROM
(
SELECT client_num, client_name, transaction_date, COUNT(client_num) AS txn_ctr
FROM tbl_TransactionHistory
GROUP BY client_num, client_name, transaction_date
) AS tabFreq
GROUP BY client_num
) AS table_total_freq
INNER JOIN
(
SELECT client_num, COUNT(txn_date) as total_no_days
FROM
(
SELECT DISTINCT(transaction_date) as txn_date, client_num
FROM tbl_TransactionHistory
ORDER BY client_num
) AS table1
GROUP BY client_num
) AS table_no_of_days
ON table_total_freq.client_num = table_no_of_days.client_num
)
) AS table_ave_freq
ON table_freq.client_num = table_ave_freq.client_num

Filter rows by count of two column values

I have following table:
Card(
MembershipNumber,
EmbossLine,
status,
EmbossName
)
with sample data
(0009,0321,'E0','Finn')
(0009,0322,'E1','Finn')
(0004,0356,'E0','Mary')
(0004,0398,'E0','Mary')
(0004,0382,'E1','Mary')
I want to retrieve rows such that only those rows should appear that have count of MembershipNumber > 1 AND count of status='E0' > 1.
For Example The query should return following result
(0004,0356,'E0','Mary')
(0004,0398,'E0','Mary')
I have the query for filtering it with MembershipNumber count but cant figure out how to filter by status='E0'. Here's the query so far
SELECT *
FROM (SELECT *,
Count(MembershipNumber)OVER(partition BY EmbossName) AS cnt
FROM card) A
WHERE cnt > 1
You can just add WHERE status = 'E0' inside your subquery:
SQL Fiddle (credit to Raging Bull for the fiddle)
SELECT *
FROM (
SELECT *,
COUNT(MembershipNumber) OVER(PARTITION BY EmbossName) AS cnt
FROM card
WHERE status = 'E0'
)A
WHERE cnt > 1
You can do it this way:
select t1.*
from card t1 left join
(select EmbossName
from card
where [status]='E0'
group by EmbossName,[status]
having count(MembershipNumber)>1 ) t2 on t1.EmbossName=t2.EmbossName
where t2.EmbossName is not null and [status]='E0'
Result:
MembershipNumber EmbossLine status EmbossName
---------------------------------------------------
4 356 E0 Mary
4 398 E0 Mary
Sample result in SQL Fiddle
try :
WITH cnt AS (
SELECT MembershipNumber, status
FROM Card
WHERE status = 'E0'
GROUP BY MembershipNumber, status
HAVING COUNT(MembershipNumber) > 1 AND COUNT(status) > 1
)
SELECT c.*
FROM Card c
INNER JOIN cnt
ON c.MembershipNumber = cnt.MembershipNumber
AND c.status = cnt.status;
You can try this:
DECLARE #DataSource TABLE
(
[MembershipNumber] SMALLINT
,[EmbossLine] SMALLINT
,[status] CHAR(2)
,[EmbossName] VARCHAR(8)
);
INSERT INTO #DataSource ([MembershipNumber], [EmbossLine], [status], [EmbossName])
VALUES (0009,0321,'E0','Finn')
,(0009,0322,'E1','Finn')
,(0004,0356,'E0','Mary')
,(0004,0398,'E0','Mary')
,(0004,0382,'E1','Mary');
SELECT [MembershipNumber]
,[EmbossLine]
,[status]
,[EmbossName]
FROM
(
SELECT *
,COUNT([MembershipNumber]) OVER (PARTITION BY [EmbossName]) AS cnt1
,SUM(IIF([status] = 'E0' , 1, 0)) OVER (PARTITION BY [EmbossName]) AS cnt2
FROM #DataSource
) DS
WHERE cnt1 > 1
AND cnt2 > 1
AND [status] = 'E0';
The idea is to add a second counter, but instead of COUNT function to use SUM function for counting only the rows that have [status] = 'E0'. Then, in the where clause we are filtering by the two counters and [status] = 'E0'.

Trying to calculate cummulative sum in tsql

What am I doing wrong here?
The result is an error, saying:
Msg 102, Level 15, State 1, Line 3 Incorrect syntax near 'order'.
Msg 156, Level 15, State 1, Line 25 Incorrect syntax near the keyword
'as'.
select *
, Antal + Normtid as Flextid
, SUM(antal) OVER (PARTITION BY transdate ORDER BY tekst)
, x = row_number() over (partition by åruge order by tekst)
from
(
select *
,
(
select b.antal
from bi.dbo.Table_pg_FlextidsopgørelseGlUdgave b
where b.tekst = bi.dbo.Table_pg_FlextidsopgørelseGlUdgave.tekst
and b.transdate = bi.dbo.Table_pg_FlextidsopgørelseGlUdgave.transdate
and b.åruge = bi.dbo.Table_pg_FlextidsopgørelseGlUdgave.åruge
and b.type = 'Normtid'
) as Normtid
from
bi.dbo.Table_pg_FlextidsopgørelseGlUdgave
where type = 'afholdt'
and tekst = 'fs'
--and åruge = '201501'
) as data
order by tekst, transdate
Regards
Peter
It is obvious that you have inappropriate version of Sql Server. Cumulative sums with order by clause like:
SUM(antal) OVER (PARTITION BY transdate ORDER BY tekst)
are only available from Sql Server 2012+.
Actually I can reproduce those errors on Sql Server 2008:
This is on Sql Server 2012:
Notice how the error message changes.
The way you are getting data from derived table is not correct..
EX:
create table
sales
(
id int
)
insert into sales
values
(1),
(2),
(3)
derived table should always have table alias and parent table should refer using from
----this is valid
select
* from
(
select id+1 as id1
from sales
) b
--this is not valid
select
*
(select
id from sales
)b
--Above is valid when you have a subquery say
select
id,(select t1.name from table t1 where t1.id=t2.id)as custname
from table t2
coming to your question..this is not valid.I see both table types are same
select *
,
(
select b.antal
from bi.dbo.Table_pg_FlextidsopgørelseGlUdgave b
where b.tekst = bi.dbo.Table_pg_FlextidsopgørelseGlUdgave.tekst
and b.transdate = bi.dbo.Table_pg_FlextidsopgørelseGlUdgave.transdate
and b.åruge = bi.dbo.Table_pg_FlextidsopgørelseGlUdgave.åruge
and b.type = 'Normtid'
) as Normtid
from
bi.dbo.Table_pg_FlextidsopgørelseGlUdgave
where type = 'afholdt'
and tekst = 'fs'
--and åruge = '201501'
So you can write something like below
select *
, Antal + Normtid as Flextid
, SUM(antal) OVER (PARTITION BY transdate ORDER BY tekst)
, x = row_number() over (partition by åruge order by tekst)
from
(
select * from
(
select b.antal
from bi.dbo.Table_pg_FlextidsopgørelseGlUdgave b
where b.tekst = bi.dbo.Table_pg_FlextidsopgørelseGlUdgave.tekst
and b.transdate = bi.dbo.Table_pg_FlextidsopgørelseGlUdgave.transdate
and b.åruge = bi.dbo.Table_pg_FlextidsopgørelseGlUdgave.åruge
and b.type = 'Normtid'
and b.type='afholdt'
and b.tekst = 'fs'
) as Normtid
order by tekst, transdate