简体   繁体   English

SELECT 语句与列上的 where 子句

[英]SELECT statement with where clause on Column

I am trying to gather some data from a table with INNER JOIN with another table.我正在尝试使用 INNER JOIN 从另一个表中收集一些数据。 What I am trying to get have 2 Types and I need to separate these 2 types in 2 different columns.我想要得到的有 2 种类型,我需要将这 2 种类型分隔在 2 个不同的列中。

In table where I am Inner joining have a column called 'Data' and another Column called 'Type' depending on the type of data I need to separate them when I inner join this table with another one.在我内部加入的表中,有一个名为“数据”的列和另一个名为“类型”的列,具体取决于数据的类型,当我将此表与另一个表进行内部连接时,我需要将它们分开。

Here is my script that I am trying to run:这是我正在尝试运行的脚本:

SELECT 
C.StoreName, 'Time' = (SELECT CST.Data from tbStoreScheduleData CST
INNER JOIN tbPrograms P2 on CST.StoreScheduleDataID = P2.StoreScheduleDayID
 where CST.Type = 'T'  ) from tbPrograms P 

INNER JOIN tbStore C on P.StoreID= C.StoreID
INNER JOIN tbStoreScheduleData CS on P.StoreScheduleDayID = CS.StoreScheduleDataID

But when I run this I get everything NULL for Time.但是当我运行它时,我得到了所有 NULL 的时间。 How can I put a where clause on a column in statement?如何在语句中的列上放置 where 子句? to only get the Type 'T' for Time value?只获取时间值的类型“T”?

In the Table Store Schedule Data, each store has entry values entered ONE is the time and other is the DAY and its separated and pulled depending on the Type.在 Table Store Schedule Data 中,每个存储都有输入的条目值,一个是时间,另一个是 DAY,它的分离和拉取取决于类型。 If you want to look at the days of the week store runs you pull the TYPE D and if you want Time you pull TYPE T. But pulling this in an inner join is something I am stuck on.如果您想查看一周中商店运行的日期,您可以拉 TYPE D,如果您想要 Time,您可以拉 TYPE T。但是将其拉入内部连接是我坚持的事情。

EDITED: THIS is what data looks like in StoreScheduleDataID已编辑:这是 StoreScheduleDataID 中的数据

+---------------------+------+--------------------+-----------+----------+
| StoreScheduleDataID | Type |        Data        | SortOrder | WeekDays |
+---------------------+------+--------------------+-----------+----------+
|                   1 | D    | Monday to Thursday |         1 | 2345     |
|                   2 | D    | Monday to Friday   |         2 | 23456    |
|                   3 | D    | Tuesday to Friday  |         3 | 3456     |
|                   4 | D    | Tuesdays           |        11 | 3        |
|                   5 | T    | 8:00AM to 2:15PM   |        90 | NULL     |
|                   6 | T    | 8:00AM to 1:00PM   |        83 | NULL     |
|                   7 | T    | 8:30AM to 1:30PM   |       108 | NULL     |
+---------------------+------+--------------------+-----------+----------+

Desired output:所需的 output:

+------------------------------------+------------------+
| StoreName           Date           |       TIME       |
+------------------------------------+------------------+
| Store1          Monday to Friday   | 8:00AM to 2:15PM |
| Store2          Monday to Thursday | 8:00AM to 2:15PM |
| Store3          Monday to Friday   | 8:00AM to 2:15PM |
+------------------------------------+------------------+

I get this with the 1st answer given:我得到了第一个答案:

+------------------------------------+------+
| StoreName           Date           | TIME |
+------------------------------------+------+
| Store1          Monday to Friday   | NULL |
| Store2          Monday to Thursday | NULL |
| Store3          Monday to Friday   | NULL |
+------------------------------------+------+

Maybe this:也许是这样:

SELECT C.StoreName
      ,MAX(CASE WHEN CST.Type = 'T' THEN  CST.Data  END) AS [Time]
      ,MAX(CASE WHEN CST.Type = 'D' THEN  CST.Data  END) AS [Date]
from tbPrograms P 
INNER JOIN tbStore C 
    on P.StoreID= C.StoreID
INNER JOIN tbStoreScheduleData CS 
    on P.StoreScheduleDayID = CS.StoreScheduleDataID
GROUP BY C.StoreName

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM