Below is the query where i want total on ReadyDate , invDate means all the data have same readydate and invdate then total will be sum of all total between these two days
READYDATE Total INVDATE FIXED TWO_MAN THREE_MAN
2017-01-30 0.00 2017-02-12 NULL 1 0
2017-01-30 12102.20 2017-02-12 NULL 1 0
2017-01-30 1950.30 2017-02-12 NULL 0 1
2017-01-30 0.00 2017-03-26 NULL 1 0
2017-01-31 0.00 2017-02-12 NULL 1 0
2017-01-31 10451.90 2017-02-12 NULL 1 0
2017-01-31 1300.20 2017-02-12 NULL 0 1
2017-02-01 10451.90 2017-02-12 NULL 1 0
2017-02-01 1950.30 2017-02-12 NULL 0 1
2017-02-02 12652.30 2017-02-12 NULL 1 0
The Correct will
READYDATE Total INVDATE FIXED TWO_MAN THREE_MAN
2017-01-30 14052.00 2017-02-12 NULL 1 0
Because the total will be readydate and invdate 14052.50
My query is below
SELECT READYDATE, SUM(TOTAL) AS Total, INVDATE,
case when TOTAL='1495.38' then TOTAL else NULL END AS FIXED,
case when TOTAL<600 then '1' else '0' END AS TWO_MAN,
case when TOTAL>=600 then '1' else '0' END AS THREE_MAN
FROM orde_ WHERE CLIENTNUM='WAY' AND INVDATE>='20170101'
GROUP BY READYDATE,INVDATE,TOTAL
ORDER BY READYDATE
can you please tell me what i am doing mistake
Try this
DECLARE #tbl TABLE(
READYDATE DATE
,Total MONEY
,INVDATE DATE
)
INSERT INTO #tbl
( READYDATE
,Total
,INVDATE
)
VALUES
('2017-01-30',0.00, '2017-02-12')
,('2017-01-30',12102.20, '2017-02-12')
,('2017-01-30',1950.30, '2017-02-12')
,('2017-01-30',0.00, '2017-03-26')
,('2017-01-31',0.00, '2017-02-12')
,('2017-01-31',10451.90, '2017-02-12')
,('2017-01-31',1300.20, '2017-02-12')
,('2017-02-01',10451.90, '2017-02-12')
,('2017-02-01',1950.30, '2017-02-12')
,('2017-02-02',12652.30, '2017-02-12')
;WITH cteX
AS
(
SELECT
T.READYDATE
,T.INVDATE
,SUM(T.Total) 'Total'
FROM #tbl T
GROUP BY
T.READYDATE
,T.INVDATE
)
SELECT
X.READYDATE
,X.Total
,X.INVDATE
,case when TOTAL='1495.38' then TOTAL else NULL END AS FIXED
,case when TOTAL<600 then '1' else '0' END AS TWO_MAN
,case when TOTAL>=600 then '1' else '0' END AS THREE_MAN
FROM cteX X
ORDER BY
X.READYDATE
Related
This is the data I have in my table
id id_mst_tag value time date
1 1 100 16:18:25 2019-10-04
2 2 300 14:10:12 2019-10-04
3 1 90 18:11:22 2019-10-04
4 2 299 18:15:34 2019-10-04
5 1 98 20:13:45 2019-10-04
6 2 311 20:18:25 2019-10-04
7 3 OFF 22:13:21 2019-10-04
and I want to show it horizontally like this
id_mst_tag 14:00 16:00 18:00 20:00 22:00
1 NULL 100 90 98 NULL
2 300 NULL 299 311 NULL
3 NULL NULL NULL NULL OFF
How can I make like this ? please help
You can consider GROUP BY and CASE combination as below to get your desired output.
Note: You have Integer and Text value in the column "Value" which is not realistic. Hope there are NULL in place of OFF in the table. Anyway, you can apply the following logic with data in correct format in column "Value".
SELECT id_mst_tag,
SUM(CASE WHEN HOUR(time) = 14 THEN Value ELSE 0 END) `14:00`,
SUM(CASE WHEN HOUR(time) = 16 THEN Value ELSE 0 END) `16:00`,
SUM(CASE WHEN HOUR(time) = 18 THEN Value ELSE 0 END) `18:00`,
SUM(CASE WHEN HOUR(time) = 20 THEN Value ELSE 0 END) `20:00`,
SUM(CASE WHEN HOUR(time) = 22 THEN Value ELSE 0 END) `22:00`
FROM your_table
GROUP BY id_mst_tag
But still, if you have that "OFF" is there in the database, you can try this following option with one additional level of data casting as below-
SELECT id_mst_tag,
SUM(CASE WHEN HOUR(time) = 14 THEN (CASE WHEN Value = 'OFF' THEN 0 ELSE CONVERT(Value,UNSIGNED INTEGER) END) ELSE 0 END) `14:00`,
SUM(CASE WHEN HOUR(time) = 16 THEN (CASE WHEN Value = 'OFF' THEN 0 ELSE CONVERT(Value,UNSIGNED INTEGER) END) ELSE 0 END) `16:00`,
SUM(CASE WHEN HOUR(time) = 18 THEN (CASE WHEN Value = 'OFF' THEN 0 ELSE CONVERT(Value,UNSIGNED INTEGER) END) ELSE 0 END) `18:00`,
SUM(CASE WHEN HOUR(time) = 20 THEN (CASE WHEN Value = 'OFF' THEN 0 ELSE CONVERT(Value,UNSIGNED INTEGER) END) ELSE 0 END) `20:00`,
SUM(CASE WHEN HOUR(time) = 22 THEN (CASE WHEN Value = 'OFF' THEN 0 ELSE CONVERT(Value,UNSIGNED INTEGER) END) ELSE 0 END) `22:00`
FROM your_table
GROUP BY id_mst_tag;
My table was enough to get what I needed at the beginning of the project but now I need to expand it.
Table s_ogr
id | ad | numara | yurt_id | egitim_id | yetkili_id | yetkili_ad
Current Table s_kontrol
id | ogr_id | tur_id | durum_id | tarih | egitim_id | yurt_id
What is needed s_kontrol_k
id | ogr_id | ogr_numara |ogr_ad | tur_id | durum_id | tarih | egitim_id | yurt_id
Explanation of rows ad = name , ogr_id is student_id , yurt_id is department_id
My Sittuation; s_kontrol is full of data over 24000. I need it to expand s_kontrol_k , but new two fields should be taken from s_ogr table , by the match operation of s_ogr and s_kontrol (s_ogr.id=s_kontrol.ogr_id and s_ogr.yurt_id =s_kontrol.yurt_id). I see that my mistake is not to add the ogr_id as id of student into s_kontrol
What I need; a Helpful query that copies the data from source but how to fill new fields from the third table with right values
INSERT INTO s_kontrol_k ( id , ogr_id , tur_id , durum_id , tarih , egitim_id , yurt_id )
SELECT id , ogr_id , tur_id , durum_id , tarih , egitim_id , yurt_id FROM s_kontrol
The queries that I am having a problem that is why I need a simple table
SELECT s_kontrol.ogr_id,
s_ogr.ad,
s_kontrol.tur_id,
s_kontrol.durum_id,
SUM(case s_kontrol.durum_id WHEN 1 THEN 1 else 0 end) var,
SUM(case s_kontrol.durum_id WHEN 2 THEN 1 else 0 end) gorevli,
SUM(case s_kontrol.durum_id WHEN 3 THEN 1 else 0 end) yok,
SUM(case s_kontrol.durum_id WHEN 4 THEN 1 else 0 end) izinli,
SUM(case s_kontrol.durum_id WHEN 5 THEN 1 else 0 end) hatimde
FROM s_kontrol, s_ogr
WHERE s_kontrol.ogr_id=s_ogr.numara
AND s_kontrol.yurt_id=s_ogr.yurt_id
and s_ogr.yurt_id=?
and tarih BETWEEN ? and ?
and tur_id IN (?)
GROUP BY s_kontrol.ogr_id
Second one
select deneme.ogr_id,
deneme.ad,
sum(case deneme.tur_id WHEN 1 THEN YUZDE else 0 END) sabah ,
sum(case deneme.tur_id WHEN 2 THEN YUZDE else 0 END) ogle,
sum(case deneme.tur_id WHEN 4 THEN YUZDE else 0 END) aksam,
sum(case deneme.tur_id WHEN 5 THEN YUZDE else 0 END) yatsi,
sum(case deneme.tur_id WHEN 6 THEN YUZDE else 0 END) sohbet,
sum(case deneme.tur_id WHEN 7 THEN YUZDE else 0 END) muhtelif
FROM (
SELECT s_kontrol.ogr_id,
s_ogr.ad,tur_id,
CEILING(((SUM(case s_kontrol.durum_id WHEN 1 THEN 1 else 0 end)
+SUM(case s_kontrol.durum_id WHEN 2 THEN 1 else 0 end)
+SUM(case s_kontrol.durum_id WHEN 3 THEN 1 else 0 end)
+SUM(case s_kontrol.durum_id WHEN 4 THEN 1 else 0 end)
+SUM(case s_kontrol.durum_id WHEN 5 THEN 1 else 0 end) )
-(SUM(case s_kontrol.durum_id WHEN 3 THEN 1 else 0 end)))
/(SUM(case s_kontrol.durum_id WHEN 1 THEN 1 else 0 end)
+SUM(case s_kontrol.durum_id WHEN 2 THEN 1 else 0 end)
+SUM(case s_kontrol.durum_id WHEN 3 THEN 1 else 0 end)
+SUM(case s_kontrol.durum_id WHEN 4 THEN 1 else 0 end)
+SUM(case s_kontrol.durum_id WHEN 5 THEN 1 else 0 end) )
* 100) YUZDE
FROM s_kontrol, s_ogr
WHERE s_kontrol.ogr_id=s_ogr.numara
and s_ogr.yurt_id=?
and tarih BETWEEN ? and ?
and tur_id IN(1,2,3,4,5,6,7)
GROUP BY s_kontrol.ogr_id,s_ogr.ad, tur_id
) deneme
group by ogr_id,ad order by 1
You can try something like below.
INSERT INTO s_kontrol_k
( id , ogr_id , ogr_ad , tur_id , durum_id , tarih , egitim_id , yurt_id )
SELECT o.id , o.ogr_id , o.ogr_ad ,
s.tur_id , s.durum_id , s.tarih , s.egitim_id , s.yurt_id
FROM
s_kontrol s inner join s_ogr o on o.yurt_id =s.yurt_id;
INSERT INTO s_kontrol_k (
id, ogr_id, ogr_numara, ogr_ad, tur_id,
durum_id, tarih, egitim_id, yurt_id)
SELECT
s_kontrol.id,
s_ogr.id,
s_kontrol.ogr_id,
s_ogr.ad,
s_kontrol.tur_id,
s_kontrol.durum_id,
s_kontrol.tarih,
s_kontrol.egitim_id,
s_kontrol.yurt_id FROM
s_kontrol inner join
s_ogr WHERE
s_ogr.yurt_id = s_kontrol.yurt_id
and s_ogr.numara = s_kontrol.ogr_id
First copied the same columns
INSERT INTO s_kontrol_k (
id, ogr_numara, tur_id, durum_id, tarih,
egitim_id, yurt_id)
SELECT
id,
ogr_id,
tur_id,
durum_id,
tarih,
egitim_id,
yurt_id
FROM
s_kontrol
Then Updated values
UPDATE
s_kontrol_k
INNER JOIN s_ogr ON s_kontrol_k.ogr_numara = s_ogr.numara
and s_kontrol_k.yurt_id = s_ogr.yurt_id SET s_kontrol_k.ogr_id = s_ogr.id
UPDATE
s_kontrol_k
INNER JOIN s_ogr ON s_kontrol_k.ogr_numara = s_ogr.numara
and s_kontrol_k.yurt_id = s_ogr.yurt_id SET
s_kontrol_k.ogr_ad = s_ogr.ad
Could be updated on main table by adding new rows
I have an EMP table in SQL Server that looks something like this
ID T_Date Jid Emp_Cost Con_Cost IsActive
--------------------------------------------------------
13178 null 214 0 0 0
12797 null 214 0 55 1
11906 null 214 0 55 1
12916 null 214 0 58 1
I am executing the below query
SELECT
AVG(CASE WHEN IsActive = 1 THEN Con_Cost ELSE Emp_Cost END) cost
FROM
EMP
WHERE
Jid = 214
AND (T_Date IS NULL OR T_Date >= sysdate);
My expected cost value must be 56.
Can anybody help me with this?
Arun
Since aggregate functions ignore null values, you can use nullif() to change 0 to null like so:
select avg(nullif(case
when IsActive = 1 then Con_Cost
else Emp_Cost
end
,0)) as cost
from EMP
where Jid = 214
and (T_Date is null or T_Date >= sysdatetime());
rextester demo: http://rextester.com/NPBOP19522
returns
+------+
| cost |
+------+
| 56 |
+------+
note: I changed sysdate to sysdatetime(), though I do not know if that is the intent of your query.
AVG actually works fine, since it does an average of the sum of 0+55+55+58 divided by the number of rows in the result set.
I believe what you are looking for is:
select distinct
sum(case when IsActive = 1 then con_cost else emp_cost end) over () /
count(case when IsActive = 1 then 1 else null end) over ()
from emp
where Jid = 214
and (T_Date is null or T_Date >= sysdatetime());
or maybe are you looking for:
select avg(con_cost)
from emp
where isActive = 1
and Jid = 214
and (T_Date is null or T_Date >= sysdatetime());
This can work too. http://rextester.com/QIXA57971
SELECT
AVG(CASE WHEN IsActive = 1 THEN Con_Cost ELSE Emp_Cost END) cost
FROM
EMP
WHERE
Jid = 214
AND (T_Date IS NULL OR T_Date >= GETDATE()) AND IsActive = 1;
Result
cost
-----
56
awbno byhub entrydatetime uplopadtime
111 ho 2017-01-01 10:10:13.000 2017-01-01 10:10:13.000
222 ho 2017-01-01 18:10:13.00 2017-01-02 10:10:13.000
333 ho 2017-01-01 13:10:13.000 2017-01-03 10:10:13.000
444 ho 2017-01-01 10:10:13.000 2017-01-05 10:10:13.000
I would like to output like this way by substracting entrydatetime from uploadingdatetime:
byhub total_awbno sameday oneday twoday morethan_two_day
ho 4 1 1 1 1
I used this Query but not found right solution:-
select byhub ,
COUNT(awbno),
count(case when (datediff(day,uplopadtime,EntryDateTime)=0) then 1 end ),
count(case when (datediff(day,uplopadtime,EntryDateTime)=1) then 1 end ),
count(case when (datediff(day,uplopadtime,EntryDateTime)=2) then 1 end ),
count(case when (datediff(day, uplopadtime,EntryDateTime)>2) then 1 end )
from MyTable
group by byhub
The output of my query is:
ho 4 1 0 0 0
datediff() function in MySQL has only 2 parameters: 2 date values. The function you are looking for is TIMESTAMPDIFF() and you also need to reverse the order of the fields in the parameter list if you would like to substract entrydatetime from uploadingdatetime:
TIMESTAMPDIFF(unit,datetime_expr1,datetime_expr2)
Returns datetime_expr2 − datetime_expr1, where datetime_expr1 and
datetime_expr2 are date or datetime expressions. One expression may be
a date and the other a datetime; a date value is treated as a datetime
having the time part '00:00:00' where necessary. The unit for the
result (an integer) is given by the unit argument. The legal values
for unit are the same as those listed in the description of the
TIMESTAMPADD() function.
select byhub ,
COUNT(awbno),
count(case when timestampdiff(day,EntryDateTime,uplopadtime)=0 then 1 end ),
count(case when timestampdiff(day,EntryDateTime,uplopadtime)=1 then 1 end ),
count(case when timestampdiff(day,EntryDateTime,uplopadtime)=) then 1 end ),
count(case when timestampdiff(day,EntryDateTime,uplopadtime)>2 then 1 end )
from MyTable
group by byhub
timestampdiff(day,EntryDateTime,uplopadtime) is the same as datediff(EntryDateTime,uplopadtime)
Try this:
SELECT byhub,
COUNT(awbno) AS total_awbno,
COUNT(CASE WHEN days_diff = 0 THEN 1 END) AS sameday,
COUNT(CASE WHEN days_diff = 1 THEN 1 END) AS oneday,
COUNT(CASE WHEN days_diff = 2 THEN 1 END) AS twoday,
COUNT(CASE WHEN days_diff > 2 THEN 1 END) AS morethan_two_day
FROM (
SELECT awbno, byhub, datediff(uplopadtime,EntryDateTime) AS days_diff
FROM mytable) AS t
GROUP BY byhub;
Demo here
Check Bellow
select byhub ,
COUNT(awbno),
sum(case when (datediff(uplopadtime,EntryDateTime)=0) then 1 else 0 end ),
sum(case when (datediff(uplopadtime,EntryDateTime)=1 or datediff(uplopadtime,EntryDateTime)=-1) then 1 else 0 end ),
sum(case when (datediff(uplopadtime,EntryDateTime)=2 or datediff(uplopadtime,EntryDateTime)=-2) then 1 else 0 end ),
sum(case when (datediff(uplopadtime,EntryDateTime) > 2 or datediff(uplopadtime,EntryDateTime) < -2) then 1 else 0 end )
from MyTable
group by byhub
I've a table named log.
Table: log
ID user_id time_of_action
I want to get result for each user for each date i.e. group by date,user_id.
So, here's the expected output structure:
user_id date occurred_in_afternoon occurred_at_night total_action_count
Explanation:
occurred_in_afternoon: whether any action of a user occurred in between 12:00 PM to 4:00 PM
occurred_at_night: whether any action of a user occurred between 8:00 PM to 12:00 AM (next day)
Schema and sample data:
DROP TABLE IF EXISTS `logs`;
CREATE TABLE `logs` (
`Id` int(11) NOT NULL AUTO_INCREMENT,
`user_id` int(11) DEFAULT NULL,
`time_of_action` timestamp NULL DEFAULT NULL ON UPDATE CURRENT_TIMESTAMP,
PRIMARY KEY (`Id`)
);
INSERT INTO `logs` VALUES ('1', '71', '2016-03-10 10:07:34');
INSERT INTO `logs` VALUES ('2', '66', '2016-03-10 14:07:57');
INSERT INTO `logs` VALUES ('3', '71', '2016-03-10 22:08:27');
INSERT INTO `logs` VALUES ('4', '71', '2016-03-10 15:08:40');
And here's my current query:
SELECT
user_id,
DATE(time_of_action) `date`,
CASE WHEN time_of_action BETWEEN TIMESTAMPADD(HOUR,12,DATE(time_of_action)) AND TIMESTAMPADD(HOUR,16,DATE(time_of_action)) THEN 1 ELSE 0 END occurred_in_afternoon,
CASE WHEN time_of_action BETWEEN TIMESTAMPADD(HOUR,20,DATE(time_of_action)) AND TIMESTAMPADD(HOUR,24,DATE(time_of_action)) THEN 1 ELSE 0 END occurred_at_night,
COUNT(*) total_action_count
FROM `logs`
GROUP BY `date`,user_id
my current output:
user_id date occurred_in_afternoon occurred_at_night total_action_count
66 2016-03-10 1 0 1
71 2016-03-10 0 0 3
Expected output:
user_id date occurred_in_afternoon occurred_at_night total_action_count
66 2016-03-10 1 0 1
71 2016-03-10 1 1 3
The problem is that I am not getting the expected result. I guess occurred in afternoon value is reset by another time_of_action which doesn't lie in that afternoon region.
And is it possible to implement it in a single query?
You missed to use an aggregate function. You can use MAX() or BIT_OR() for your purpose:
SELECT
user_id,
DATE(time_of_action) `date`,
MAX(CASE WHEN time_of_action BETWEEN TIMESTAMPADD(HOUR,12,DATE(time_of_action)) AND TIMESTAMPADD(HOUR,16,DATE(time_of_action)) THEN 1 ELSE 0 END) occurred_in_afternoon,
MAX(CASE WHEN time_of_action BETWEEN TIMESTAMPADD(HOUR,20,DATE(time_of_action)) AND TIMESTAMPADD(HOUR,24,DATE(time_of_action)) THEN 1 ELSE 0 END) occurred_at_night,
COUNT(*) total_action_count
FROM `logs`
GROUP BY `date`,user_id
Update: I would also prefer a more readable version like
SELECT
user_id,
DATE(time_of_action) `date`,
BIT_OR(TIME(time_of_action) BETWEEN '12:00:00' AND '16:00:00') occurred_in_afternoon,
BIT_OR(TIME(time_of_action) BETWEEN '20:00:00' AND '23:59:59') occurred_at_night,
COUNT(*) total_action_count
FROM `logs`
GROUP BY `date`,user_id
I was thinking to have an alias of the result table that I've got through SUM in order to get Binary value for those two fields.
SELECT
t.user_id,
t.date,
CASE WHEN t.occurred_in_afternoon > 0 THEN 1 ELSE 0 END AS occurred_in_afternoon,
CASE WHEN t.occurred_at_night > 0 THEN 1 ELSE 0 END AS occurred_at_night,
t.total_action_count
FROM
(SELECT
user_id,
DATE(time_of_action) `date`,
SUM(CASE WHEN time_of_action BETWEEN TIMESTAMPADD(HOUR,12,DATE(time_of_action)) AND TIMESTAMPADD(HOUR,16,DATE(time_of_action)) THEN 1 ELSE 0 END) occurred_in_afternoon,
SUM(CASE WHEN time_of_action BETWEEN TIMESTAMPADD(HOUR,20,DATE(time_of_action)) AND TIMESTAMPADD(HOUR,24,DATE(time_of_action)) THEN 1 ELSE 0 END) occurred_at_night,
COUNT(*) total_action_count
FROM `logs`
GROUP BY `date`,user_id) t