I want show data according date wise with geoup by User ID.
The first shows the data stored in database and second table shows how i want to show the data on front end.
This is the table in Database :-
UserID Date Working Hrs
1 2021-08-01 10
2 2021-08-01 1
3 2021-08-01 15
1 2021-08-02 11
2 2021-08-02 11
3 2021-08-02 16
1 2021-08-03 9
2 2021-08-03 10
3 2021-08-03 11
This is the table i want to create from db table :-
UserID 2021-08-01 2021-08-02 2021-08-03
1 10 11 9
2 1 11 10
3 15 16 11
If your columns are static then you can use conditional aggregation. Otherwise use pivot or dynamic.
-- MySQL(v5.8)
SELECT userid
, MAX(CASE WHEN tdate = '2021-08-01' THEN working_hours END) "2021-08-01"
, MAX(CASE WHEN tdate = '2021-08-02' THEN working_hours END) "2021-08-02"
, MAX(CASE WHEN tdate = '2021-08-03' THEN working_hours END) "2021-08-03"
FROM test
GROUP BY userid
Please check from url https://dbfiddle.uk/?rdbms=mysql_8.0&fiddle=d364ddf6f942a54beddb2ba719d1932f
Related
Somewhat new to SQL and I'm running into a bit of issue with a project. I have a table like this:
ID
subscription_ID
renewal_date
1
11
2022-01-01 00:00:00
2
11
2022-01-02 00:00:00
3
12
2022-01-01 00:00:00
4
12
2022-01-01 12:00:00
5
13
2022-01-01 12:00:00
6
13
2022-01-03 12:00:00
My goal is to return rows where the subscription_ID matches and the start_date is within or equal to a certain # of days (hours would work as well). For instance, I'd like rows where subscription_ID matches and the start_date is within or equal to 1 day such that my results from the table above would be:
ID
subscription_ID
renewal_date
1
11
2022-01-01 00:00:00
2
11
2022-01-02 00:00:00
3
12
2022-01-01 00:00:00
4
12
2022-01-01 12:00:00
Any assistance would be greatly appreciated--thanks!
If I understand correctly maybe you are trying something like:
select t.*
from test_tbl t
join ( SELECT subscription_id
, MAX(diff) max_diff
FROM
( SELECT x.subscription_id
, DATEDIFF(MIN(y.start_date),x.start_date) diff
FROM test_tbl x
JOIN test_tbl y ON y.subscription_id = x.subscription_id
AND y.start_date > x.start_date
GROUP BY x.subscription_id , x.start_date
) z
GROUP BY subscription_id
) as t1 on t.subscription_id=t1.subscription_id
where t1.max_diff<=1;
Result:
id subscription_id start_date
1 11 2022-01-01 00:00:00
2 11 2022-01-02 00:00:00
3 12 2022-01-01 00:00:00
4 12 2022-01-01 12:00:00
The subquery returns:
subscription_id max_diff
11 1
12 0
13 2
which is used on the where condition.
Demo
I am trying to calculate mrr for each month.
The DB table 'boxes' looks like this:
project_id
product_id
payment_method_id
price
interval
booked_at
canceled_at
1
1
3
19.00
1
2020-12-01 00:00:00
NULL
1
2
3
39.00
1
2020-05-01 00:00:00
2020-11-05 19:10:27
4
1
3
39.00
12
2020-05-01 00:00:00
2020-11-05 19:10:27
Payment-Interval is in months. I need to show in KPI dashboard the mrr.
Currently I have this query:
SELECT DATE_FORMAT(booked_at, '%Y-%m') AS period, sum(price)
FROM
project_boxes
GROUP BY period;
The problem with the above query is that it doesn't show between months MRR and doesn't work with canceled boxes.
What am I missing? Any help is appreciated.
I have 1 table called itemmovement : It has Item Id , Quantity In , Quantity Out , Invoice Id, Date. I need to make in one query to show how many pieces are sold and beside the sold column there will be the current on hand quantity .
itemmovement
Id itemid qtyin qtyout invid purchasereturnid date
1 1 10 2019-01-04
2 2 8 2019-01-06
3 2 2 1 2019-01-08
4 1 3 2 2019-01-12
5 2 1 2019-02-04
6 3 4 2019-03-04
7 1 1 3 2019-04-04
8 1 1 1 2019-04-14
9 3 1 2 2019-04-24
I need the query to show this result
Id itemid Sold Quantity OnHandQty
1 1 4 5
2 2 2 7
3 3 0 3
I'm Trying to use this query but not working
SELECT *
FROM
(SELECT itmv.itemid,
sum(itmv.qtyout)-sum(itmv.qtyin)
FROM itemmovement itmv
WHERE (itmv.systemdate BETWEEN '2019-01-01' AND '2019-06-01')
AND invid>0
GROUP BY itmv.itemid) AS result1,
(SELECT sum(itmv2.qtyin)-sum(itmv2.qtyout)
FROM itemmovement itmv2
WHERE itmv.itemid=itmv2.itemid
GROUP BY itmv2.itemid) AS result2
ORDER BY sum(itmv.qtyin)-sum(itmv.qtyout)
I'm getting :
Unknown column 'itmv.itemid' in 'where clause it for this syntax :
where itmv.itemid = itmv2.itemid
Here's your query.
select itemid
, sum(case when COALESCE(invid,0) > 0 then qtyout else 0 end) as Sold_Qantity
, sum(qtyin)-sum(qtyout) as OnHandQty
from itemmovement
group by itemid
I have two tables:
Table 1: planA
ID Date Count
3 2017-01-01 10
2 2017-02-03 15
10 2017-01-30 8
Table 2: planB
ID Date Value
3 2017-01-02 11
2 2017-02-04 12
21 2017-01-30 3
3 2017-02-03 33
What I want to do is to join the two tables on (ID and Date) columns.
However, on Date, I want to use the next day to the date on the table 1.
Therefore, the joined table should look like the following:
PlanA.ID PlanA.Date PlanB.Date PlanA.Count PlanB.Value
3 2017-01-01 2017-01-02 10 11
2 2017-02-03 2017-02-04 15 12
Is this even possible?
Any suggestion would be appreciated!
Yes it is possible:
select
PlanA.ID,
PlanA.Date,
PlanB.Date,
PlanA.Count,
PlanB.Value
from
PlanA inner join PlanB
on (
PlanA.ID = PlanB.ID
and
PlanA.Date + INTERVAL 1 DAY = PlanB.Date
)
if Date is a column of type date, + INTERVAL 1 DAY will return the next day of the one given, and then you can perform the join.
I have a MySQL table with the following table structure and desired output
historical_id grd_id register_type timestamp address value historical_type insertion_time
5358 2 11 2016-05-07 12:45:00 1 18.1 1 2016-05-07 13:44:58
5359 2 11 2016-05-07 12:45:00 2 51.4 1 2016-05-07 13:44:58
5360 2 11 2016-05-07 12:45:00 3 476 1 2016-05-07 13:44:58
5364 2 11 2016-05-07 13:00:00 1 18.79 1 2016-05-07 13:59:58
5365 2 11 2016-05-07 13:00:00 2 51.2 1 2016-05-07 13:59:58
5366 2 11 2016-05-07 13:00:00 3 718 1 2016-05-07 13:59:58
Desired output from query
kWh_date temp rh co2
2016-05-0712:45:00 18.1 51.4 476
2016-05-0713:00:00 18.79 51.2 718
Can anybody help as to how I get the desired output. I have tried to 'GROUP' by timestamp (not my decision to use this name and cannot change it) but it just shows me 1 set of data (ie 'address' 1). I have used 3 'SELECT' statements but that shows the timestamp repeated. Any help is much appreciated.
I have tried
SELECT `register_type`
, `timestamp`
, `address`
, `value`
FROM grdxf.historical
WHERE `register_type` = 11
GROUP
BY `timestamp`;
I have tried different queries but they end up failing also. I'm really at a lose and new to queries.
E.g.:
SELECT FROM_UNIXTIME(900 * FLOOR(UNIX_TIMESTAMP(timestamp)/900)) timestamp
, MAX(CASE WHEN address = 1 THEN value END) temp
, MAX(CASE WHEN address = 2 THEN value END) rh
, MAX(CASE WHEN address = 3 THEN value END) co2
FROM historical
WHERE register_type = 11
GROUP
BY timestamp;