I have the following mySql select statement it returns the below result and battling to get the result I am after.
select `tvshow`.`idShow` AS `idShow`,`tvshow`.`c00` AS `ShowName`,
if(count(distinct `episode`.`c12`),
count(distinct `episode`.`c12`),0) AS `TotalSeasons`,
if(count(`episode`.`c12`),count(`episode`.`c12`),0) AS `TotalEpisodeCount`
from
((((`tvshow`
left join `tvshowlinkpath` ON ((`tvshowlinkpath`.`idShow` = `tvshow`.`idShow`)))
left join `path` ON ((`path`.`idPath` = `tvshowlinkpath`.`idPath`)))
left join `episode` ON ((`episode`.`idShow` = `tvshow`.`idShow`)))
left join `files` ON ((`files`.`idFile` = `episode`.`idFile`)))
group by `tvshow`.`idShow`
having (count(`episode`.`c12`) > 0)
Select Result
I am trying to get a 4th column that would have the seasons listed in it e.g "Season 1,Season 2,Season 3"
I can get the the data I need by running the following select
select distinct c12 from episode where idShow = 1
It returns the following.
So i thought I could use the replace to change the results to read "Season1" but not sure how to get it to just return one string containing "Seasin1,Season2,Season3" and then add it to the select statement at the top of the view and bring it all together?
The Result I am trying to get(used Photoshop for this just so you could get the idea)
Just add GROUP_CONCAT(episode.c12) as additional column:
select `tvshow`.`idShow` AS `idShow`,`tvshow`.`c00` AS `ShowName`,
if(count(distinct `episode`.`c12`),
count(distinct `episode`.`c12`),0) AS `TotalSeasons`,
if(count(`episode`.`c12`),count(`episode`.`c12`),0) AS `TotalEpisodeCount`,
`GROUP_CONCAT(episode.c12)` as `Seasons`
from
((((`tvshow`
left join `tvshowlinkpath` ON ((`tvshowlinkpath`.`idShow` = `tvshow`.`idShow`)))
left join `path` ON ((`path`.`idPath` = `tvshowlinkpath`.`idPath`)))
left join `episode` ON ((`episode`.`idShow` = `tvshow`.`idShow`)))
left join `files` ON ((`files`.`idFile` = `episode`.`idFile`)))
group by `tvshow`.`idShow`
having (count(`episode`.`c12`) > 0)
See http://dev.mysql.com/doc/refman/5.0/en/group-by-functions.html#function_group-concat for the documentation of this MySQL specific function.
Related
The following query returns what is displayed in the attached image.
What I thought would be very simple has turned out to be very complex. All I simply want to do is now total up the result into one row and column. In this case the sum would be 161. How do I do this? I've literally tried everything. I hope I've provided enough information.
SELECT
TRUNCATE
(
SUM(
`assignment`.`percentage_achieved` *(
SELECT
`unitComponentWeighting`.`percentage_weighting` / 100
FROM
`unitComponentWeighting`
WHERE
`assignment`.`assignment_component_id` = `unitComponentWeighting`.`component_lookup_id`
LIMIT 1
)
),
2
) AS `unit_percentage_grade`
FROM
`assignment`
LEFT JOIN `assignmentType` ON `assignment`.`assignment_type_id` = `assignmentType`.`id`
LEFT JOIN `assignmentComponentLookup` ON `assignmentComponentLookup`.`id` = `assignment`.`assignment_component_id`
LEFT JOIN `unit` ON `unit`.`id` = `assignment`.`unit_id`
LEFT JOIN `assignmentSequence` ON `assignmentSequence`.`id` = `assignment`.`assignment_sequence_id`
LEFT JOIN `yearGroup` ON `yearGroup`.`id` = `unit`.`year_group_id`
WHERE
`yearGroup`.`id` = 1
GROUP BY
`assignment`.`unit_id`
try removing this part
" GROUP BY
assignment.unit_id "
I have the following table structure:
tbl_catalogue_state
In tbl_catalogue there is a part number 58674 that has three states in the tbl_catalogue_state_lk table. Here is the result when I run a query inner joining the three tables.
As expected there are multiple rows returned.
Is there a way to only return one row having the values for each catalgue_state_id on the same row?
I would also like the ability to ignore a row for example:
select tbl_catalogue.catalogue_part, tbl_catalogue_state.catalogue_state_id from tbl_catalogue
inner join tbl_catalogue_state_lk on tbl_catalogue.catalogue_id = tbl_catalogue_state_lk.catalogue_id
inner join tbl_catalogue_state on tbl_catalogue_state_lk.catalogue_state_id = tbl_catalogue_state.catalogue_state_id
where tbl_catalogue_state_lk.catalogue_state_id <> 1;
The above select still returns two rows.
UPDATE
I was able to use GROUP_CONCAT:
select tbl_catalogue.catalogue_part, GROUP_CONCAT(tbl_catalogue_state.catalogue_state_id) as cat_state from tbl_catalogue
inner join tbl_catalogue_state_lk on tbl_catalogue.catalogue_id = tbl_catalogue_state_lk.catalogue_id
inner join tbl_catalogue_state on tbl_catalogue_state_lk.catalogue_state_id = tbl_catalogue_state.catalogue_state_id
where tbl_catalogue_state_lk.catalogue_state_id <> 1
group by tbl_catalogue.catalogue_id;
My issue is the above statement still returns a row. I need it to return nothing.
I was able to use not exists:
select tc.catalogue_part, GROUP_CONCAT(tcs.catalogue_state_id) as cat_state from tbl_catalogue as tc
inner join tbl_catalogue_state_lk as tcsl on tc.catalogue_id = tcsl.catalogue_id
inner join tbl_catalogue_state as tcs on tcsl.catalogue_state_id = tcs.catalogue_state_id
where
not exists
(
select tcsl2.catalogue_state_id from tbl_catalogue_state_lk as tcsl2
where tcsl2.catalogue_state_id = 6 and tcsl2.catalogue_id = tc.catalogue_id
)
and
not exists
(
select tcsl3.catalogue_state_id from tbl_catalogue_state_lk as tcsl3
where tcsl3.catalogue_state_id = 1 and tcsl3.catalogue_id = tc.catalogue_id
)
and
not exists
(
select tcsl3.catalogue_state_id from tbl_catalogue_state_lk as tcsl3
where tcsl3.catalogue_state_id = 2 and tcsl3.catalogue_id = tc.catalogue_id
)
group by tc.catalogue_id;
I have this query which i want to get rank from the data on my database
set #urut:=0;
set #rankhrg:=0;
select #urut:=#urut+1 as urut, a.id_tender, b.nama_tender, b.nomor_tender, b.tgl_close1 as tgl_close,
(SELECT rankhrg
from (select sum(tot_harga) as hrg_twr, id_rekanan, id_tender, #rankhrg:=#rankhrg+1 as rankhrg from tb_real_barang where id_tender = s.id_tender group by id_rekanan) as rank_harga
left join tb_master_tender s on s.id_tender = b.id_tender
where rank_harga.id_rekanan = a.id_rekanan
order by rank_harga.hrg_twr asc) as ranking
from tb_real_tender a
left join tb_master_tender b on a.id_tender = b.id_tender
where a.id_rekanan = 1
order by convert(a.id_tender,unsigned) desc
i want to pass id_tender into the select inside the select when i want to get rankhrg :
select sum(tot_harga) as hrg_twr, id_rekanan, id_tender,
#rankhrg:=#rankhrg+1 as rankhrg
from tb_real_barang
where id_tender = s.id_tender
group by id_rekanan
but I always get error that said that s.id_tender is unknown in where clause.
can someone guide me how to pass the parameter into that insert?
thank you :)
You are not joining with that table tb_master_tender and neither it's present in outer query FROM clause. So, you need to do a JOIN separately for that inner query like below
select sum(trb.tot_harga) as hrg_twr,
trb.id_rekanan,
trb.id_tender,
#rankhrg:=#rankhrg+1 as rankhrg
from tb_real_barang trb
left join tb_master_tender s on trb.id_tender = s.id_tender
group by trb.id_rekanan
Using this query
SELECT DISTINCT(job_primary.id) AS id
FROM `job_primary`
LEFT JOIN `job_skill` ON `job_skill`.`job_id` = `job_primary`.`id`
LEFT JOIN `job_facts` ON `job_facts`.`job_id` = `job_primary`.`id`
LEFT JOIN `job_location` ON `job_location`.`job_id` = `job_primary`.`id`
LEFT JOIN `job_people` ON `job_people`.`job_id` = `job_primary`.`id`
LEFT JOIN `job_sec` ON `job_sec`.`job_id` = `job_primary`.`id`
AND job_sec.exp_start_date > UNIX_TIMESTAMP()
INNER JOIN `user_skills` ON `user_skills`.`skill_id` = `job_skill`.`skill_id`
AND user_skills.user_id = 1
WHERE job_primary.posted_by != 1
I am getting only the id value. But I want to get the rest of the values with distinct id. Don't think about rest of the codes. The problem is here, if I use:
select *, job_primary.id as id
to select code then I get all data. But it is not distinct. So if I used
SELECT *, distinct(job_primary.id) as id
or
SELECT distinct(job_primary.id), * as id
but it is showing errors.
I think your confussion is what to query itself, nixon1333. What do you want to ask to the database? If you want just one row per id, group'em by with SELECT * FROM ... GROUP BY job_primary.id
I have a Microsoft SQL Server 2008 query that returns data from three tables using a left outer join. Many times, there is no data in the second and third tables and so I get a null which I think is the default for left outer join. Is there a way to replace the default values in the select statement? I have a workaround in that I can select into a table variable but it feels a little dirty.
SELECT iar.Description, iai.Quantity, iai.Quantity * rpl.RegularPrice as 'Retail',
iar.Compliance FROM InventoryAdjustmentReason iar
LEFT OUTER JOIN InventoryAdjustmentItem iai on (iar.Id = iai.InventoryAdjustmentReasonId)
LEFT OUTER JOIN Item i on (i.Id = iai.ItemId)
LEFT OUTER JOIN ReportPriceLookup rpl on (rpl.SkuNumber = i.SkuNo)
WHERE iar.StoreUse = 'yes'
I would like the Quantity and RegularPrice to default to zero if possible.
That's as easy as
IsNull(FieldName, 0)
Or more completely:
SELECT iar.Description,
ISNULL(iai.Quantity,0) as Quantity,
ISNULL(iai.Quantity * rpl.RegularPrice,0) as 'Retail',
iar.Compliance
FROM InventoryAdjustmentReason iar
LEFT OUTER JOIN InventoryAdjustmentItem iai on (iar.Id = iai.InventoryAdjustmentReasonId)
LEFT OUTER JOIN Item i on (i.Id = iai.ItemId)
LEFT OUTER JOIN ReportPriceLookup rpl on (rpl.SkuNumber = i.SkuNo)
WHERE iar.StoreUse = 'yes'
In case of MySQL or SQLite the correct keyword is IFNULL (not ISNULL).
SELECT iar.Description,
IFNULL(iai.Quantity,0) as Quantity,
IFNULL(iai.Quantity * rpl.RegularPrice,0) as 'Retail',
iar.Compliance
FROM InventoryAdjustmentReason iar
LEFT OUTER JOIN InventoryAdjustmentItem iai on (iar.Id = iai.InventoryAdjustmentReasonId)
LEFT OUTER JOIN Item i on (i.Id = iai.ItemId)
LEFT OUTER JOIN ReportPriceLookup rpl on (rpl.SkuNumber = i.SkuNo)
WHERE iar.StoreUse = 'yes'
MySQL
COALESCE(field, 'default')
For example:
SELECT
t.id,
COALESCE(d.field, 'default')
FROM
test t
LEFT JOIN
detail d ON t.id = d.item
Also, you can use multiple columns to check their NULL by COALESCE function.
For example:
mysql> SELECT COALESCE(NULL, 1, NULL);
-> 1
mysql> SELECT COALESCE(0, 1, NULL);
-> 0
mysql> SELECT COALESCE(NULL, NULL, NULL);
-> NULL
For Oracle you can use:
NVL(columnName,deafultValue) :- NVL is used to convert a null value to a default value in the query output. eg. If you want to replace null values with 'NA' then use something like this.
SELECT NVL(columnName,'NA') FROM tableName