我需要基于同一张表中不同条件的两个不同列值

时间:2019-07-15 11:43:53

标签: mysql sql

标题

我想每天获取每个“储罐ID”的第一条记录的总和,以及每天明智的“收据”的总和,并且特定月份的整个数据将受到限制

android:layout_weight
<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">

    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="1"
        android:background="@android:color/holo_blue_dark"
        >

        <ImageView
            android:layout_width="100dp"
            android:layout_height="100dp"
            android:id="@+id/profile_picture"
            android:src="@drawable/user_profile_picture"
            android:layout_centerInParent="true"/>

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_below="@+id/profile_picture"
            android:layout_centerHorizontal="true"
            android:layout_marginTop="10dp"
            android:text="User Email"
            android:textColor="@android:color/white"
            android:textSize="28sp" />
    </RelativeLayout>
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="1"
        android:orientation="vertical"
        android:gravity="center">

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="0dp"
            android:layout_weight="1"
            android:gravity="center_horizontal">
            <Button
                android:layout_width="200dp"
                android:layout_height="wrap_content"
                android:id="@+id/btn_change_password"
                android:layout_gravity="bottom"
                android:text="change password"/>
        </LinearLayout>
        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="0dp"
            android:layout_weight="1"
            android:gravity="center_horizontal">
            <Button
                android:layout_width="200dp"
                android:layout_height="wrap_content"
                android:id="@+id/btn_sign_out"
                android:layout_gravity="top"
                android:text="sign out"/>

        </LinearLayout>
    </LinearLayout>
</LinearLayout>

我需要上表中的此输出

SELECT
  DATE(t.DATE) AS 'Date',
  SUM(t.in_stock) AS 'Opening Stock',
  SUM(receipt) AS 'Receipt',
  SUM(in_stock + receipt)AS 'Total Stock' 
FROM diesel_tank_details t 
WHERE NOT EXISTS (
    SELECT
      1 
    FROM diesel_tank_details t2 
    WHERE
      DATE(t2.DATE) = DATE(t.DATE) 
      AND t2.tank_id = t.tank_id 
      AND t2.dt_id < t.dt_id
  )
  AND YEAR(DATE) = 2019 
  AND MONTH(DATE) = 7 
GROUP BY DATE(DATE)

2 个答案:

答案 0 :(得分:0)

查询

SELECT DATE(t.Date) as 'Date',
       SUM(t.In_Stock) as 'Opening Stock',
       SUM(t.Receipt) as 'Receipt',
       SUM(t.In_Stock + t.Receipt) as 'Total Stock'

FROM   diesel_tank_details t

WHERE  AND YEAR(t.Date) = 2019
       AND MONTH(t.Date) = 7

GROUP BY DATE(t.Date)

只需添加其他过滤器即可。我不知道DT_ID在做什么,所以我将其删除。但基本上您只是想按天分组。

答案 1 :(得分:0)

由于您需要不同的库存和收货条件,因此每种情况下必须汇总不同的行:

select s.day, s.`Opening Stock`, r.Receipt,
  (s.`Opening Stock` + s.Receipt) `Total Stock`
from (
  select g.day, sum(t.in_stock) `Opening Stock`, sum(t.receipt) receipt
  from diesel_tank_details t inner join (
    select date(date) day, tank_id, min(date) mindate
    from diesel_tank_details
    where in_stock <> 0
    group by date(date), tank_id
  ) g on g.tank_id = t.tank_id and g.mindate = t.date   
  group by g.day
) s inner join (
  select g.day, sum(t.receipt) Receipt
  from diesel_tank_details t inner join (
    select date(date) day, tank_id, min(date) mindate
    from diesel_tank_details
    where receipt <> 0
    group by date(date), tank_id
  ) g on g.tank_id = t.tank_id and g.mindate = t.date   
  group by g.day  
) r on r.day = s.day

请参见demo
结果:

| day        | Opening Stock | Receipt | Total Stock |
| ---------- | ------------- | ------- | ----------- |
| 2019-07-10 | 6000          | 6000    | 12000       |
| 2019-07-15 | 4550          | 1250    | 4550        |