如何为此方案创建查询?

时间:2017-09-21 09:05:21

标签: sql sql-server

我已经尝试了一切使预期结果显示为输出但无法运气。

示例数据库

表1

 CalendarDate Date

表2

User VarChar(50)
WDT DateTime

表1记录

 CalendarDate
 2017-01-01
 2017-01-02
 2017-01-03
 2017-01-04

表2记录

 WDT                         User
 2017-01-01 08:00:00.000     User1
 2017-01-02 08:05:00.000     USer1

预期结果

 User     CalendarDate    Status     WDT            Time
 User1    2017-01-01      Present    2017-01-01     08:00:00.000 
 User1    2017-01-02      Present    2017-01-02     08:00:00.000
 User1    2017-01-03      Absent     Null           Null
 User1    2017-01-04      Absent     Null           Null

我想显示日期,即使Table2上没有记录,如果Table2在Table1 CalendarDate上为空,则显示状态

4 个答案:

答案 0 :(得分:1)

使用左连接

fdatabaseuser.addValueEventListener(new ValueEventListener({
   @Override
   public void onDataChange(DataSnapshot dataSnapshot) {

      for (DataSnapshot list: dataSnapshot.getChildren())
      {
          Object obj= list.getKey();
          item.add(obj.toString();)

      }

      Toast.makeText(GiveActivity.this,item,Toast.LENGTH_LONG).show();

   }

   @Override
   public void onCancelled(DatabaseError databaseError) {

   }
});

如果你需要列[User]也不匹配行,你需要添加一个与users表的连接或从Table2中提取用户列表,如:

select t1.CalendarDate, t2.[User], case when t2.[User] is null then 'Absent' else 'Present' end [Status], CAST(t2.wdt as date) WDT, CAST(t2.wdt as time) [Time]
from Table1 t1
left join Table2 t2 on t1.CalendarDate = CAST(t2.wdt as date)

CalendarDate    User    Status  WDT         Time
2017-09-22      User1   Present 2017-09-22  11:23:19.2730000
2017-09-23      User1   Present 2017-09-23  11:23:19.2730000
2017-09-24      NULL    Absent  NULL        NULL
2017-09-25      NULL    Absent  NULL        NULL

答案 1 :(得分:0)

我认为您可以尝试这样来获取所有用户的列表(在这种情况下,与表用户的左联接是不够的 - 请参阅下面的示例数据):

SELECT C.USERNAME, A.CALENDARDATE
      , CASE WHEN B.USERNAME IS NULL THEN 'Absent' ELSE 'Present' END AS STATUS
      , CAST(B.WDT AS DATE) AS WDT, CAST(B.WDT AS TIME) AS TIME
FROM CALENDAR A
CROSS JOIN (SELECT DISTINCT USERNAME FROM US) C
LEFT JOIN US B ON A.CALENDARDATE = CAST(WDT AS DATE) AND C.USERNAME=B.USERNAME;

示例数据:

SET DATEFORMAT ymd

CREATE TABLE CALENDAR (CALENDARDATE DATE);
INSERT INTO CALENDAR VALUES ('2017-01-01');
INSERT INTO CALENDAR VALUES ('2017-01-02');
INSERT INTO CALENDAR VALUES ('2017-01-03');
INSERT INTO CALENDAR VALUES ('2017-01-04');

CREATE TABLE US (USERNAME VARCHAR(10), WDT DATETIME);
INSERT INTO US VALUES ('User1', '2017-01-01 08:00');
INSERT INTO US VALUES ('User1', '2017-01-02 08:05');
INSERT INTO US VALUES ('User2', '2017-01-04 08:10');

输出:

   +----------+--------------+---------+------------+------------------+
| USERNAME | CALENDARDATE | STATUS  |    WDT     |       TIME       |
+----------+--------------+---------+------------+------------------+
| User1    | 2017-01-01   | Present | 2017-01-01 | 08:00:00.0000000 |
| User1    | 2017-01-02   | Present | 2017-01-02 | 08:05:00.0000000 |
| User1    | 2017-01-03   | Absent  | NULL       | NULL             |
| User1    | 2017-01-04   | Absent  | NULL       | NULL             |
| User2    | 2017-01-01   | Absent  | NULL       | NULL             |
| User2    | 2017-01-02   | Absent  | NULL       | NULL             |
| User2    | 2017-01-03   | Absent  | NULL       | NULL             |
| User2    | 2017-01-04   | Present | 2017-01-04 | 08:10:00.0000000 |
+----------+--------------+---------+------------+------------------+

答案 2 :(得分:0)

左连接将执行,但在用户字段中返回空值,除非您对其进行硬编码

select [USER],convert( varchar(10),t1.Calenderdate,120) Calenderdate ,
 case when convert( varchar(10),t2.WDT,120)  =  convert( varchar(10),t1.Calenderdate,120) then 'Present'
 else 'Absent' end Status, convert( varchar(10),t2.WDT,120) WDT,
 CAST(t2.WDT AS TIME) AS TIME 
 from 
 #tab1 t1 LEFT join #tab2 t2 on 
 convert( varchar(10),t2.WDT,120)  =  convert( varchar(10),t1.Calenderdate,120) 

答案 3 :(得分:0)

你的意思是这样的吗?

        DECLARE @Table1 TABLE ([CalendarDate] DATE)
    DECLARE @Table2 TABLE ([User] VarChar(50), [WDT] DateTiME)

    INSERT INTO @Table1 VALUES 
    ('2017-01-01'),
    ('2017-01-02'),
    ('2017-01-03'),
    ('2017-01-04')

    INSERT INTO @Table2 VALUES
    ('User1', '2017-01-01 08:00:00.000'),
    ('User1', '2017-01-02 08:05:00.000')


    SELECT  ISNULL(t2.[User], 'User1') AS [User],
            t1.CalendarDate,
            CASE WHEN t2.[User] IS NOT NULL THEN 'Present' ELSE 'Absent' END AS Status,
            CAST(t2.WDT AS DATE) AS WDT,
            CAST(t2.WDT AS TIME) AS Time
    FROM @Table2 t2
    RIGHT JOIN @Table1 t1
    ON CAST(t2.WDT AS DATE) = t1.CalendarDate