My following query shows results only if dcno is exists in dc_detail table
but what i actually need is even then if dcno is not exists in dc_detail table it should define dcno as 0 and select that results also
Thanks in advance
SELECT p.po_no as id, DATE_FORMAT(p.po_date, '%d-%m-%Y')as po_date, p.customer, p.cust_po as po_no,p.tot_ord_qty,
DATE_FORMAT(p.delivery_date, '%d-%m-%Y')as delivery_date,p.dc_status,p.inv_status,p.tot_dc_qty,p.tot_inv_qty,
GROUP_CONCAT(distinct d.dc_no SEPARATOR ', ') as dc FROM po_header p, dc_details d
where p.cust_po=d.cust_po
group by p.cust_po;
You should use left join to include headers that have no details. That will give you rows with nulls for the detail fields that don't exist.
To get the value 0 instead of null you can use coalesce function.
The relevant parts of the query are
SELECT COALESCE(d.dc_no, 0),...
FROM po_header p LEFT JOIN dc_detail d ON p.cust_po=d.cust_po
Coalesce function returns the first non null argument.
Related
I have one SQL query with INNER JOINS. I need to get all offers from table offers.
Table offers is empty now. But the following query returns one row with NULL field.
Why is it returned? How to fix that? I need to return 0 rows if table is empty.
Query:
select *, SUM(offers.price * announcement_product.amount) AS total, announcements.user_id AS creator_ann, announcements.id AS ann_id,
announcements.delivery AS deliveryAnn, announcements.payment AS
paymentAnn, SUM(announcement_product.amount) AS amount,
announcement_product.name as name_product
from `offers`
inner join `announcements` on `announcements`.`id` = `offers`.`announcement_id`
inner join `announcement_product` on `offers`.`announcement_product_id` = `announcement_product`.`id`
inner join `countries` on `countries`.`id` = `announcements`.`country`
where `offers`.`user_id` = 1 and `offers`.`status` = 1 and `offers`.`deleted_at` is null
You're using the aggregate function SUM(), but you don't have any GROUP BY clause.
When you do that you are instructing MySQL to add up all the row values in the column you mention in SUM(). It will do that even if there are no rows to add up.
For best results you should study up on the GROUP BY function and how to use it with SUM(). It's hard to guess what you want from your query.
I'm not sure, but I don't think
select *, ..
when there's multiple tables in the query is valid.
Try
select offers.*,..
This how Your select structure should be :
Select
Id,
Sku,
Sum(Onhand),
Sum(price)
From mytable
Where mytable Onhand > 0
Group by
Id,Sku
If you are going to use aggregate function such as Max,Sum,Min,....
you need to use group by for other table fields that your using in the select part.
SELECT DISTINCT a.assessement_group_id,
b.title as dataa
FROM assessment_group a
JOIN assessment_category b
WHERE a.assessement_group_id = b.group_id
I am using the join to display the data.the result are shown below
100 Partner Business Profile
99 Partner Activation
99 ajay test
100 ajaytest123
But i want this type of answer
100 Partner Business Profile,ajaytest123
99 Partner Activation,ajay test
You can use GROUP_CONCAT(DISTINCT ...) along with GROUP BY to get the output you want:
SELECT a.assessement_group_id,
GROUP_CONCAT(DISTINCT b.title)
FROM assessment_group a
INNER JOIN assessment_category b
ON a.assessement_group_id = b.group_id
GROUP BY a.assessement_group_id
SQLFiddle
By the way, I replaced your old-style implicit join syntax with an explicit INNER JOIN. It is generally considered bad practice now to put join conditions into the WHERE clause.
Try something as follows, hope this helps
select id, group_concat(`dataa` separator ',') as `dataa`
from
(
SELECT distinct a.assessement_group_id id, b.title as dataa
from assessment_group a JOIN assessment_category b
WHERE a.assessement_group_id=b.group_id
) tbl
group by id;
See the MySql GROUP_CONCAT() as part of a grouped query.
http://dev.mysql.com/doc/refman/5.7/en/group-by-functions.html
You would add a GROUP_BY assessement_group_id to the end of the query, for example.
In mySql you can use the GROUP_CONCAT function, see here more details: http://dev.mysql.com/doc/refman/5.6/en/group-by-functions.html#function_group-concat.
In your initial query you need to add GROUP_CONCAT(b.title) in select clause and also a group by after assessement_group_id.
This one will help you
SELECT A.ASSESSEMENT_GROUP_ID, GROUP_CONCAT(B.TITLE SEPARATOR ' , ') AS DATAA FROM ASSESSMENT_GROUP A, ASSESSMENT_CATEGORY B WHERE A.ASSESSEMENT_GROUP_ID=B.GROUP_ID GROUP BY A.ASSESSEMENT_GROUP_ID;
I have a query (below) which returns and ID value and two additional associated values (ReplacesReference). The query returns 2 records. I want to return one record so I need to concatenate the first "ReplacesReference" value with a comma ',' and the second "ReplacesReference" value.
my query is as follows
select
distinct(c.wiid) as ID,
a.reference as ReplacesReference
from npp_projects a,
npp_assoc b,
npp_projects c
where a.id = b.parent_id
and b.type = 'replace'
and c.id = b.child_id
and c.reference like '%EN 815%'
Its output is :
ID | ReplacesReference
====================================
CEN3406 | I.S. EN 815:1996
CEN3406 | I.S. EN 815:2004
I want to be able to return the below output:
ID | ReplacesReference
====================================
CEN3406 | I.S. EN 815:1996 , I.S. EN 815:2004
Many thanks in advance for your help - I am tearing my hair out with this one !
You want to use group by instead of select distinct. Your use of parentheses around c.wild suggests that you don't understand how select distinct works. It applies to all columns in the select list. The parentheses don't affect it. The query looks like:
select p2.wiid as ID,
group_concat(distinct p.reference separator ', ') as ReplacesReference
from npp_projects p join
npp_assoc a
on p.id = a.parent_id
npp_projects p2
on p2.id = a.child_id
where a.type = 'replace' and p2.reference like '%EN 815%'
group by p2.wild;
The changes I made:
Added the group_concat() to get the list you want.
Added the group by to replace the select distinct.
Changed the table aliases to use table abbreviations so the query is easier to follow.
Changed the join syntax to the more powerful explicit join syntax. As a simple rule: just do not use commas in the from clause.
You'd have to do something like:
SELECT id, GROUP_CONCAT(string SEPARATOR ' ') FROM table GROUP BY id;
http://dev.mysql.com/doc/refman/5.0/en/group-by-functions.html#function_group-concat
SELECT area_id,
area_name,
(select count(*) from applications
where claims_status=1 and
center_name=c.area_id) as cont
FROM apparea c where cont<>0
I am trying to get fields and relevant count from anothere table, but the above query is not working. The query is involved two different tables(apparea, applications). The above query has error and I am looking for the alternate way to achieve this.
The alias for your column cont is not available in the WHERE clause. You will want to use something similar to this:
SELECT area_id,
area_name,
cont
FROM
(
SELECT area_id,
area_name,
(select count(*)
from applications
where claims_status=1
and center_name=c.area_id) as cont
FROM apparea c
) c
where cont<>0
This can also be written using a LEFT JOIN:
select c.area_id,
c.area_name,
a.cont
from apparea c
left join
(
select count(*) cont,
center_name
from applications
where claims_status=1
group by center_name
) a
on c.area_id = a.center_name
Try this query
SELECT
c.area_id,
c.area_name,
cnt
FROM
apparea c,
(select
center_name,
count(*) AS cnt
from
applications
where
claims_status=1
GROUP BY
center_name
HAVING
count(*) > 0) cont
where
c.area_id = cont.center_name;
Got the count for each center_name and then joined table to get count for each area
Use HAVING rather than where.
As it is problem with aliases.
It is not permissible to refer to a column alias in a WHERE clause, because the column
value might not yet be determined when the WHERE clause is executed.
See Section C.5.5.4, “Problems with Column Aliases”.
http://dev.mysql.com/doc/refman/5.0/en/problems-with-alias.html
From: http://dev.mysql.com/doc/refman/5.0/en/select.html
I have an SQL LIKE:
SELECT S.*,
(SELECT I.NAME FROM institution I, inst_map IM
WHERE IM.STUDENT = S.ID AND IM.INSTITUTION = I.ID) as INSTITUTIONS
FROM student S
In this case it is possible for my subquery to return multiple records (I will get an error: Subquery returns more than 1 row).
How to show those multiple values from my subquery in one field (in my case INSTITUTIONS ) separated by commas?
All ideas are welcome.
Try this query -
SELECT s.*, GROUP_CONCAT(t.NAME) INSTITUTIONS FROM student s
LEFT JOIN (SELECT * FROM institution i
JOIN inst_map im
ON im.INSTITUTION = i.ID
) t
ON s.ID = t.STUDENT
GROUP BY s.ID
The GROUP_CONCAT function will help you to get values separated by commas.
DECLARE #List VARCHAR(5000)
SELECT #List = COALESCE(#List + ', ' + Display, Display)
FROM TestTable
Order By Display
query is taken from following link, and the article explain the query perfectly, I hope it works
http://www.mitchelsellers.com/blogs/articletype/articleview/articleid/289/creating-comma-separated-list-in-sql.aspx
P.S Its for SQL Server, i guess