I have table absen with clause join, where and group
and I have this query
SELECT count( tgl ) AS total,
absen.id, pegawai.nama, pegawai.bagian,
pegawai.bagian, pegawai.tglmasuk, gaji.jenis_gaji,
gaji.jumlah, gajiharian.id_gajiharian,
gajiharian.status, gajiharian.tgldigaji,
CASE WHEN (weekday( tgl ) <=3)
THEN date( tgl + INTERVAL( 3 - weekday( tgl ) ) DAY )
ELSE date( tgl + INTERVAL( 3 +7 - weekday( tgl ) ) DAY )
END AS tglitung
FROM absen
JOIN pegawai ON pegawai.id = absen.id
JOIN gaji ON gaji.id = pegawai.id
LEFT JOIN gajiharian ON gajiharian.id = absen.id
WHERE absen.status = 'm'
AND absen.id = '15'
AND tglitung='2016-01-14'
GROUP BY absen.id, tglitung
and results in
#1054 - Unknown column 'tglitung' in 'where clause'
How do I solve that?
I want 'as tglitung' can detect as column.
Related
I have this SQL query:
SELECT (CASE WHEN count(_days) > 0 THEN 'yes' ELSE 'No' END) as availability
FROM (
SELECT count(roomTypeDay2.room_type_id) as _days
FROM room_type_day as roomTypeDay2
LEFT JOIN room_type as roomType2 on roomTypeDay2.room_type_id = roomType2.id
WHERE roomType2.accommodation_id=3
AND roomTypeDay2.date IN ( '2018-06-09 00:00:00','2018-06-10 00:00:00','2018-06-11 00:00:00')
GROUP BY roomTypeDay2.room_type_id
HAVING COUNT(roomTypeDay2.room_type_id) = 3
) as disponible
This works fine, but now, I want to filter if the first date row (2018-06-09 00:00:00) have
"min_night_stay <= 3 and release_days <=2"
parameters, but I don't know how doing it.
I try adding these lines to a query:
AND (roomTypeDay2.date = '2018-06-09 00:00:00' and
roomTypeDay2.min_night_stay <= 3 AND roomTypeDay2.release_days <= 2)
But this is not correct query.
UDPATE
SELECT (CASE WHEN count(_days) > 0 THEN 'yes' ELSE 'No' END) as availability
FROM (
SELECT count(roomTypeDay2.room_type_id) as _days
FROM room_type_day as roomTypeDay2
LEFT JOIN room_type as roomType2 on roomTypeDay2.room_type_id = roomType2.id
AND roomType2.accommodation_id=3
WHERE roomTypeDay2.date IN ( '2018-06-09 00:00:00','2018-06-10 00:00:00','2018-06-11 00:00:00')
GROUP BY roomTypeDay2.room_type_id
HAVING COUNT(roomTypeDay2.room_type_id) = 3
) as disponible
Solution
SELECT (CASE WHEN count(_days) > 0 THEN 'yes' ELSE 'No' END) as availability
FROM (
SELECT count(roomTypeDay2.room_type_id) as _days
FROM room_type_day as roomTypeDay2
LEFT JOIN room_type as roomType2 on roomTypeDay2.room_type_id = roomType2.id
WHERE roomType2.accommodation_id=3
AND roomTypeDay2.num_rooms_available > 0
AND (roomTypeDay2.date = '2018-06-12 00:00:00' AND roomTypeDay2.min_night_stay <= 2 AND roomTypeDay2.release_days <= 2 )
OR roomTypeDay2.date IN ( '2018-06-13 00:00:00','2018-06-14 00:00:00')
GROUP BY roomTypeDay2.room_type_id
HAVING COUNT(roomTypeDay2.room_type_id) = 3
) as disponible
This is your WHERE clause:
WHERE roomTypeDay2.date IN ('2018-06-09 00:00:00',
'2018-06-10 00:00:00',
'2018-06-11 00:00:00')
Now for the first date you want additional criteria. Use AND and ORwith parentheses for this:
WHERE
(
roomTypeDay2.date = '2018-06-09 00:00:00'
AND
roomTypeDay2.min_night_stay <= 3
AND
roomTypeDay2.release_days <= 2
)
OR
roomTypeDay2.date IN ('2018-06-10 00:00:00','2018-06-11 00:00:00')
SELECT
(
CASE WHEN count(_days) > 0 THEN 'yes' ELSE 'No' END
) as availability
from
(
SELECT
count(roomTypeDay2.room_type_id) as _days
FROM
room_type_day as roomTypeDay2
LEFT JOIN room_type as roomType2 on roomTypeDay2.room_type_id = roomType2.id
AND roomType2.accommodation_id = 3
WHERE
roomTypeDay2.date IN ('2018-06-10 00:00:00', '2018-06-11 00:00:00')
GROUP BY
roomTypeDay2.room_type_id
HAVING
COUNT(roomTypeDay2.room_type_id) = 3
union
SELECT
count(roomTypeDay2.room_type_id) as _days
FROM
room_type_day as roomTypeDay2
LEFT JOIN room_type as roomType2 on roomTypeDay2.room_type_id = roomType2.id
AND roomType2.accommodation_id = 3
WHERE
roomTypeDay2.date = '2018-06-09 00:00:00'
and roomTypeDay2.min_night_stay <= 3
AND roomTypeDay2.release_days <= 2
GROUP BY
roomTypeDay2.room_type_id
HAVING
COUNT(roomTypeDay2.room_type_id) = 3
) disponible
I have model like this
Model:
public function lihatgajiharian($id,$tglitung){
$status='m';
$this->db->select('count( tgl ) AS total, absen.id, pegawai.nama, pegawai.bagian, pegawai.bagian, pegawai.tglmasuk, gaji.jenis_gaji, gaji.jumlah, gajiharian.id_gajiharian, gajiharian.status, gajiharian.tgldigaji,
(CASE WHEN (weekday( tgl ) <=3) THEN date( tgl + INTERVAL( 3 - weekday( tgl ) )DAY )
ELSE date( tgl + INTERVAL( 3 +7 - weekday( tgl ) )DAY )END) AS tglitung');
$this->db->from('absen');
$this->db->join('pegawai', 'pegawai.id = absen.id');
$this->db->join('gaji', 'gaji.id = pegawai.id');
$this->db->join('gajiharian', 'gajiharian.= absen.id','left');
$this->db->where('absen.id',$id);
$this->db->where('absen.status',$status);
$this->db->group_by('absen.id');
$this->db->group_by('tglitung');
$this->db->having('tglitung', $tglitung);
$query = $this->db->get();
return $query->row();
and appear error,
Error Number: 1064
You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'absen.idWHEREabsen.id= '15' ANDabsen.status= 'm' GROUP BYabs' at line 6
but if I try run query in sql like this
Check near third join gajiharian you have put only dot there not with column add it and problem will be solved...
$status='m';
$this->db->select('count( tgl ) AS total, absen.id, pegawai.nama, pegawai.bagian, pegawai.bagian, pegawai.tglmasuk, gaji.jenis_gaji, gaji.jumlah, gajiharian.id_gajiharian, gajiharian.status, gajiharian.tgldigaji,
(CASE WHEN (weekday( tgl ) <=3) THEN date( tgl + INTERVAL( 3 - weekday( tgl ) )DAY )
ELSE date( tgl + INTERVAL( 3 +7 - weekday( tgl ) )DAY )END) AS tglitung');
$this->db->from('absen');
$this->db->join('pegawai', 'pegawai.id = absen.id');
$this->db->join('gaji', 'gaji.id = pegawai.id');
$this->db->join('gajiharian', 'gajiharian.column= absen.id','left'); //in this line problem occurs
$this->db->where('absen.id',$id);
$this->db->where('absen.status',$status);
$this->db->group_by('absen.id');
$this->db->group_by('tglitung');
$this->db->having('tglitung', $tglitung);
$query = $this->db->get();
return $query->row();
I have table absen like this:
and the table itungharian with coloumn like this:
id_itung | id | total | itungtgl
(no data)
and query join like this:
SELECT
count(tgl) AS total,
absen.id,
pegawai.nama,
pegawai.bagian,
pegawai.bagian,
pegawai.tglmasuk,
gaji.jenis_gaji,
gaji.jumlah,
itungharian.id AS id2,
itungharian.itungtgl,
CASE
WHEN (weekday(tgl) <=3 )
THEN date(tgl + INTERVAL(3 - weekday(tgl)) DAY)
ELSE date(tgl + INTERVAL(3 + 7 - weekday(tgl)) DAY)
END AS tglitung
FROM
absen
JOIN
pegawai ON pegawai.id = absen.id
JOIN
gaji ON gaji.id = pegawai.id
LEFT JOIN
itungharian ON itungharian.id = absen.id
WHERE
absen.status = 'm'
GROUP BY
absen.id,
tglitung
and show this
So I have problem how to join like this if values on cloumn id(table absen) same with itungharian.tgl and tglitung(result case when) same with itungharian.itungtgl don't show in join table
I have tried with sql like this:
SELECT
count(tgl) AS total,
absen.id,
pegawai.nama,
pegawai.bagian,
pegawai.bagian,
pegawai.tglmasuk,
gaji.jenis_gaji,
gaji.jumlah,
itungharian.id AS id2,
itungharian.itungtgl,
CASE
WHEN (weekday(tgl) <= 3)
THEN date(tgl + INTERVAL(3 - weekday(tgl)) DAY)
ELSE date(tgl + INTERVAL(3 + 7 - weekday(tgl)) DAY)
END AS tglitung
FROM
absen
JOIN
pegawai ON pegawai.id = absen.id
JOIN
gaji ON gaji.id = pegawai.id
LEFT JOIN
itungharian ON itungharian.id = absen.id
WHERE
absen.status = 'm'
AND
NOT (CASE WHEN (weekday(tgl) <= 3)
THEN DATE(tgl + INTERVAL(3 - weekday(tgl)) DAY)
ELSE DATE(tgl + INTERVAL(3 + 7 - weekday(tgl)) DAY)END) = itungharian.itungtgl
AND
NOT absen.id = itungharian.id
GROUP BY
absen.id,
tglitung
but MySQL returned an empty result set (i.e. zero rows).
SELECT a.agent, a.talktime, a.percentclosed,COALESCE(COUNT(b.dealamount),0),COALESCE(SUM(b.dealamount),0) , b.dealdate
FROM closers a
LEFT JOIN deals b ON a.agent = b.agent
WHERE b.dealdate IS NULL OR YEARWEEK( b.dealdate ) = YEARWEEK( NOW( ) )
GROUP BY a.agent
ORDER BY COALESCE( SUM( b.dealamount ) , 0 ) DESC
Can anybody help me fix this query i know its messy but im new to this,
This works fine for the current week but if i add +1 to simulate a future week everybody who has a deal the week before does not show,
Thanks,
John,
IMAGE OF QUERY'S
WEEKOFYEAR is not good in this case because it will wrap at year's end. You should use YEARWEEK().
WHERE b.dealdate IS NULL OR YEARWEEK( b.dealdate ) = YEARWEEK( NOW( ) ) +1
Got it to work!,
SELECT a.agent, a.talktime, a.percentclosed, COALESCE( COUNT( b.dealamount ) , 0 ) ,COALESCE( SUM( b.dealamount ) , 0 ) , b.dealdate
FROM closers a
LEFT JOIN deals b ON a.agent = b.agent
AND WEEKOFYEAR( b.dealdate ) = WEEKOFYEAR( NOW( ) )
WHERE b.dealdate IS NULL
OR WEEKOFYEAR( b.dealdate ) = WEEKOFYEAR( NOW( ) )
GROUP BY a.agent
ORDER BY COALESCE( SUM( b.dealamount ) , 0 ) DESC
LIMIT 0 , 30
WHERE b.dealdate IS NULL OR WEEKOFYEAR( b.dealdate ) = WEEKOFYEAR( date_add(NOW( ), interval 1 week))
use the interval feature of mysql, and the date_add or date_sub function
Reference
SELECT COUNT( sendbook.id ) AS total, SUM( sendbook.num ) AS num, (
SELECT COUNT( sendbook.id )
FROM sendbook
INNER JOIN clients ON clients.id = sendbook.clientid
WHERE sendbook.issueid = '29'
AND clients.area >1000
AND clients.area <2000
) AS area1000, (
SELECT COUNT( sendbook.id )
FROM sendbook
INNER JOIN clients ON clients.id = sendbook.clientid
WHERE sendbook.issueid = '29'
AND clients.area >2000
AND clients.area <3000
) AS area2000, (
SELECT COUNT( sendbook.id )
FROM sendbook
INNER JOIN clients ON clients.id = sendbook.clientid
WHERE sendbook.issueid = '29'
AND clients.area >3000
AND clients.area <4001
) AS area2000
FROM `sendbook`
WHERE sendbook.issueid = '29'
total num area1000 area2000 area2000
8 438 3 3 2
Do you know a way to simplify this query?
Thinks
you need to use case statement something like this :
SELECT COUNT( sendbook.id ),
(CASE
WHEN clients.area between '1000' and '2000' THEN 1
WHEN clients.area between '2000' and '3000' THEN 2
WHEN clients.area between '3000' and '4000' THEN 3
END) AS myrange FROM mytable
GROUP BY myrange