English 中文(简体)
按语种划分的组别必须至少包含一栏非外参引
原标题:GROUP BY expression must contain at least one column that is not an outer reference
  • 时间:2012-05-24 14:34:09
  •  标签:
  • sql
  • group-by

同一议题上有许多文章, 我读过这些文章, 但尽量尝试, 我无法将这些文章应用到议题上。 我相信我忽略了这些愚蠢的事。

我想做的是让在某个特定日期和时间在床上的病人看一看。每个病人都有入院和出院日期,所以我用这些方法来确定他们是否在那个特定日期和印章;时间的床上。如果我把一切都列出来,效果会很好。现在我想做一个摘要,这样就可以在这个地点每天显示3个住院病人和1个门诊病人,等等。但是我在标题中会收到错误信息。

SELECT DISTINCT TSM950_STATION.loc_ext_id AS STATION, 
convert(varchar(10),DATEADD(DAY,-7,GETDATE()),101) AS CENSUS_DATE,
                  TSM180_CAT.cod_dtl_ds,
                  COUNT(TPM300_PAT_VISIT.vst_ext_id)
FROM         TPM300_PAT_VISIT INNER JOIN
                  TSM040_PERSON_HDR ON TPM300_PAT_VISIT.psn_int_id = TSM040_PERSON_HDR.psn_int_id INNER JOIN
                  TSM950_LOCATION_REF AS TSM950_ROOM ON TPM300_PAT_VISIT.loc_lvl_4_id = TSM950_ROOM.loc_int_id INNER JOIN
                  TSM950_LOCATION_REF AS TSM950_BED ON TPM300_PAT_VISIT.loc_lvl_5_id = TSM950_BED.loc_int_id  INNER JOIN
                  TSM950_LOCATION_REF AS TSM950_STATION ON TPM300_PAT_VISIT.loc_lvl_3_id = TSM950_STATION.loc_int_id INNER JOIN
                  TSM180_MST_COD_DTL AS TSM180_CAT ON TPM300_PAT_VISIT.pat_cat_cd = TSM180_CAT.cod_dtl_int_id
WHERE     (TSM950_STATION.loc_ext_id IN ( MS ,  OB ,  SCU ,  NURS )) AND 
        (convert(datetime,convert(varchar(10),TPM300_PAT_VISIT.adm_ts,101) +  00:00:00 ))<=convert(datetime,convert(varchar(10),DATEADD(DAY,-7,GETDATE()),101) +  23:58:00 ) AND
        (convert(datetime,convert(varchar(10),TPM300_PAT_VISIT.dschrg_ts,101) +  00:00:00 ))>=convert(datetime,convert(varchar(10),DATEADD(DAY,-7,GETDATE()),101) +  23:59:00 )
GROUP BY        TSM950_STATION.loc_ext_id, 
                 convert(varchar(10),DATEADD(DAY,-7,GETDATE()),101),
                  TSM180_CAT.cod_dtl_ds 
问题回答

问题是以下表达式是一个常数:

convert(varchar(10),DATEADD(DAY,-7,GETDATE()),101), 

简单地把这个从你的团体中移除, 所以它看起来像:

GROUP BY TSM950_STATION.loc_ext_id, TSM180_CAT.cod_dtl_ds  

您不需要同时使用 < code> DISTINCT 和 < code> gROUP by 。 根据定义,记录是不同的, 因为所有类似的记录都在同一组中...

另外,您不需要使用 GROUP 的常数

这给...

SELECT    TSM950_STATION.loc_ext_id AS STATION,
          convert(varchar(10),DATEADD(DAY,-7,GETDATE()),101) AS CENSUS_DATE,
          TSM180_CAT.cod_dtl_ds,
          COUNT(TPM300_PAT_VISIT.vst_ext_id)
FROM      TPM300_PAT_VISIT INNER JOIN
          TSM040_PERSON_HDR ON TPM300_PAT_VISIT.psn_int_id = TSM040_PERSON_HDR.psn_int_id INNER JOIN
          TSM950_LOCATION_REF AS TSM950_ROOM ON TPM300_PAT_VISIT.loc_lvl_4_id = TSM950_ROOM.loc_int_id INNER JOIN
          TSM950_LOCATION_REF AS TSM950_BED ON TPM300_PAT_VISIT.loc_lvl_5_id = TSM950_BED.loc_int_id  INNER JOIN
          TSM950_LOCATION_REF AS TSM950_STATION ON TPM300_PAT_VISIT.loc_lvl_3_id = TSM950_STATION.loc_int_id INNER JOIN
          TSM180_MST_COD_DTL AS TSM180_CAT ON TPM300_PAT_VISIT.pat_cat_cd = TSM180_CAT.cod_dtl_int_id
WHERE         (TSM950_STATION.loc_ext_id IN ( MS ,  OB ,  SCU ,  NURS ))
          AND (convert(datetime,convert(varchar(10),TPM300_PAT_VISIT.adm_ts,101) +  00:00:00 ))<=convert(datetime,convert(varchar(10),DATEADD(DAY,-7,GETDATE()),101) +  23:58:00 )
          AND (convert(datetime,convert(varchar(10),TPM300_PAT_VISIT.dschrg_ts,101) +  00:00:00 ))>=convert(datetime,convert(varchar(10),DATEADD(DAY,-7,GETDATE()),101) +  23:59:00 )
GROUP BY  TSM950_STATION.loc_ext_id, 
          TSM180_CAT.cod_dtl_ds 




相关问题
SQL SubQuery getting particular column

I noticed that there were some threads with similar questions, and I did look through them but did not really get a convincing answer. Here s my question: The subquery below returns a Table with 3 ...

难以执行 REGEXP_SUBSTR

I m 查询Oracle 10g。 我有两张表格(样本数据见下文)。 i m 试图提取一些领域

SQL Query Shortcuts

What are some cool SQL shorthands that you know of? For example, something I learned today is you can specify to group by an index: SELECT col1, col2 FROM table GROUP BY 2 This will group by col2

PHP array callback functions for cleaning output

I have an array of output from a database. I am wondering what the cleanest way to filter the values is example array Array ( [0] => Array ( [title] => title 1 ...

OracleParameter and DBNull.Value

we have a table in an Oracle Database which contains a column with the type Char(3 Byte). Now we use a parameterized sql to select some rows with a DBNull.Value and it doesn t work: OracleCommand ...

Running numbers in SQL

I have a SQL-statement like this: SELECT name FROM users WHERE deleted = 0; How can i create a result set with a running number in the first row? So the result would look like this: 1 Name_1 2 ...

How to get SQL queries for each user where env is production

I’m developing an application dedicated to generate statistical reports, I would like that user after saving their stat report they save sql queries too. To do that I wrote the following module: ...

热门标签