I'm working on a mysql query using the Visual Studio tableAdapter Query Builder, so far i'm getting this message (see image). Although the Query Builder executes the data given but cannot generate the columns needed in my dataset. I want to know what i'm doing wrong when my query works fine on my code.
Here is the text code as requested.
SELECT CONCAT(hgen.GENDESC, ',', CASE WHEN hdmhdr.brandname IS NULL THEN '' ELSE CONCAT(' ', hdmhdr.brandname) END, CASE WHEN hdmhdr.dmdnost IS NULL
THEN '' ELSE CONCAT(' ', CONVERT(hdmhdr.dmdnost, `char`(14))) END, CASE WHEN hstre.STREDESC IS NULL THEN '' ELSE hstre.STREDESC END,
CASE WHEN hform.FORMDESC IS NULL THEN '' ELSE CONCAT(' ', hform.FORMDESC) END, CASE WHEN hroute.rtedesc IS NULL THEN '' ELSE CONCAT(' ',
hroute.rtedesc) END) AS itemdesc, COUNT(DISTINCT hrxoissue.issuedte) AS 'Total Count per RX', SUM(hrxoissue.qty) AS 'Quantity', hrxoissue.dmdcomb,
hcharge.chrgdesc
FROM hdmhdr LEFT OUTER JOIN
hroute ON hdmhdr.rtecode = hroute.rtecode LEFT OUTER JOIN
hstre ON hdmhdr.strecode = hstre.strecode INNER JOIN
hdruggrp ON hdruggrp.grpcode = hdmhdr.grpcode INNER JOIN
hform ON hform.formcode = hdmhdr.formcode INNER JOIN
hgen ON hgen.gencode = hdruggrp.gencode INNER JOIN
hrxoissue ON hdmhdr.dmdcomb = hrxoissue.dmdcomb INNER JOIN
hcharge ON hrxoissue.chrgcode = hcharge.chrgcode
WHERE hdmhdr.dmdstat = 'A' AND hrxoissue.issuedte >= '2023-02-14 00:00:00' AND hrxoissue.issuedte <= '2023-02-14 23:59:00'
GROUP BY 1
Related
I am using below query to fetch the report for 10 different ids using union-all.Is there any way to reduce the query ?
SELECT
sc.consentid,
CASE
WHEN ue.userid IS NULL THEN 'No'
ELSE 'Yes'
END ,
sc.firstname,
sc.lastname,
CASE
WHEN SUM(time_on_page) > 0 THEN ROUND(SUM(time_on_page) / 60, 2)
ELSE 'No videos watched'
END AS timeonpage,
'ID1' ,
CASE
WHEN COUNT(DISTINCT COALESCE(article, video)) > 0 THEN COUNT(DISTINCT COALESCE(article, video))
ELSE 'no video in list watched'
END AS countvideoswatched
FROM
studentconsent AS sc
LEFT JOIN
useremails AS ue USING (email)
LEFT JOIN
masterstats_innodb AS m ON ue.userid = m.user_id
WHERE
(sc.email LIKE '%.it'
AND (sc.consentid IS NOT NULL
AND m.id IS NULL)
OR (m.id IS NOT NULL
AND m.timestamp >= '2020-12-01'
AND (video IN (SELECT
objectid
FROM
playlists
JOIN
playlisttoobjects USING (playlistid)
WHERE
unique_id = 'ID1'
AND objecttype = 'article'))))
GROUP BY sc.consentid
union all (query2 with differentid)....so on
Here I need to use union-all command to combine all 10 select queries with different id.Any way to reduce it?
SQL Code is mentioned here , please have a look at this.
SELECT concat('{"workbenchStatsByUser": [', GROUP_CONCAT(js SEPARATOR ','))
FROM
(
SELECT json_object('username', workbenchStatsByUser, 'workbenchStatsByStatus',
JSON_ARRAY(GROUP_CONCAT( json_object( 'status', TempStatus, 'count', IFNULL(count, 0) ) SEPARATOR ','))) AS Js
FROM
(
SELECT
CASE WHEN username IS NULL THEN 'Not Assigned'
ELSE CONCAT(username,' - ', rolename)
END AS workbenchStatsByUser,
TempStatus,
COUNT(TempStatus) AS count
FROM users u
INNER JOIN tempOrgId t ON u.OrgId = t.OrgId
CROSS JOIN TempStatus ts
LEFT JOIN roles r ON r.id = u.roleid
LEFT JOIN vtask vt ON u.username = vt.updatedby AND ts.TempStatus = vt.status
GROUP BY workbenchStatsByUser, TempStatus
) A
GROUP BY workbenchStatsByUser
) B;
Username is null even-though their is a proper value.
Try changing
CASE WHEN username IS NULL THEN 'Not Assigned'
ELSE CONCAT(username,' - ', rolename)
END AS workbenchStatsByUser,
to
COALESCE(CONCAT(username,' - ', rolename), 'Not Assigned') AS workbenchStatsByUser,
The sum operation is called more than once in my query and so the result that appears in column B2_ESTOQUE becomes inconsistent, maybe the cause of this is the amount of records that return in select and left join operations. That is an assumption.
Is there an error in the query below? Is the left join operation being used incorrectly?
SELECT
SB1.B1_FILIAL, SB1.B1_COD, SB1.B1_DESC, SB1.B1_TIPO, SB1.B1_UM, SB1.B1_SEGUM, SB1.B1_GRUPO, SB1.B1_CONV, SB1.B1_TIPCONV, SB1.B1_PRV1,
SB1.B1_PESO, SB1.B1_CODBAR, SB1.B1_QE, SB1.B1_MSBLQL, SB1.D_E_L_E_T_, SB1.R_E_C_N_O_, SB1.MD5, SB1.DATA_UPDATE,
(SELECT BM_DESC FROM PADRAO.PORTAL_SBM001 WHERE BM_GRUPO = SB1.B1_GRUPO LIMIT 0,1) as B1_GRUPO_DESCRICAO,
(SUM(IFNULL(SB2.B2_QATU, 0)) - SUM(IFNULL(SB2.B2_RESERVA, 0)) - SUM(IFNULL(SB2.B2_QPEDVEN, 0))) - SUM(IFNULL(SC6.C6_QTDVEN, 0)) AS B2_ESTOQUE
FROM PADRAO.PORTAL_SB1001 SB1
LEFT JOIN PADRAO.PORTAL_SB2001 SB2
ON SB1.B1_COD = SB2.B2_COD
LEFT JOIN PADRAO.PORTAL_SC6001_NEW SC6
ON SB1.B1_COD = SC6.C6_PRODUTO
AND (SC6.C6_NUM IS NULL OR SC6.C6_NUM = '')
AND SB2.B2_LOCAL IN ('07','08')
WHERE (SB1.D_E_L_E_T_ IS NULL OR SB1.D_E_L_E_T_ = '')
AND (SB2.D_E_L_E_T_ IS NULL OR SB2.D_E_L_E_T_ = '')
AND (SC6.D_E_L_E_T_ IS NULL OR SC6.D_E_L_E_T_ = '')
AND (SB1.B1_MSBLQL IS NULL OR SB1.B1_MSBLQL <> '1')
GROUP BY SB1.B1_COD;
I wasn't sure what all the checks for isnull and '' were about in the where clauses. I used coalesce to change null's to empty string and eliminated the values in the subqueries where clause instead of in the outermost where clause; but perhaps you wanted to keep the left joins Nulls which is why you checked for emptying string and null...
Nevertheless here's what I have as a UNTESTED query. Notice I converted the two left joined tables to sum the values before the join and group by the joined keys.
SELECT
SB1.B1_FILIAL
, SB1.B1_COD
, SB1.B1_DESC
, SB1.B1_TIPO
, SB1.B1_UM
, SB1.B1_SEGUM
, SB1.B1_GRUPO
, SB1.B1_CONV
, SB1.B1_TIPCONV
, SB1.B1_PRV1
, SB1.B1_PESO
, SB1.B1_CODBAR
, SB1.B1_QE
, SB1.B1_MSBLQL
, SB1.D_E_L_E_T_
, SB1.R_E_C_N_O_
, SB1.MD5
, SB1.DATA_UPDATE
,(SELECT BM_DESC FROM PADRAO.PORTAL_SBM001 WHERE BM_GRUPO = SB1.B1_GRUPO LIMIT 0,1) as B1_GRUPO_DESCRICAO
, SB2.mSum - SB6.mSUM as B2_ESTOQUE
FROM PADRAO.PORTAL_SB1001 SB1
LEFT JOIN (SELECT SB2.B2_COD, SUM(IFNULL(B2_QATU, 0) - IFNULL(B2_RESERVA, 0) - IFNULL(B2_QPEDVEN, 0)), AS mSum
FROM PADRAO.PORTAL_SB2001
WHERE B2_LOCAL IN ('07','08')
AND coalesce(SB2.D_E_L_E_T_,'')=''
GROUP BY SB2.B2_COD) SB2
ON SB1.B1_COD = SB2.B2_COD
LEFT JOIN (SELECT SUM(IFNULL(SC6.C6_QTDVEN, 0)) mSUM, C6_PRODUTO
FROM PADRAO.PORTAL_SC6001_NEW SC6
WHERE coalesce(SC6.C6_NUM,'') = ''
AND coalesce(SC6.D_E_L_E_T_,'') = ''
GROUP BY C6_PRODUTO)
ON SB1.B1_COD = SC6.C6_PRODUTO
WHERE (SB1.D_E_L_E_T_ IS NULL OR SB1.D_E_L_E_T_ = '')
AND (SB1.B1_MSBLQL IS NULL OR SB1.B1_MSBLQL <> '1')
GROUP BY SB1.B1_COD;
Is there a way in SQL to set a default return value when NULL is returned for part of the results?
Here is my SQL:
SELECT p.id, p.title, concat( u1.meta_value, ' ', u2.meta_value ) as fullname, concat( r.name, ', ', c.name ) as location
FROM modules_profiles p
LEFT JOIN moonlight_usermeta u1 ON p.user_id = u1.user_id AND u1.meta_key = 'first_name'
LEFT JOIN moonlight_usermeta u2 ON p.user_id = u2.user_id AND u2.meta_key = 'last_name'
LEFT JOIN modules_regions r ON r.id = p.region_id
LEFT JOIN modules_countries c ON c.id = p.country_id
WHERE p.certification IN ( 'certified' ) AND p.country_id IN ( 2 )
ORDER BY p.user_id ASC
There are times when there is no region_id set for a given profile; therefore, NULL is returned for location for that respective user_id, even though we do have a country's name (c.name).
Is there a way in this case to just return the c.name only?
Use COALESCE() function like below, it will return the first non NULL value provided in list
COALESCE(col_name, 'default_value')
For your case, do
COALESCE(region_id, c.name)
I think, you are specifically talking about the part
concat( r.name, ', ', c.name ) as location
You can modify this using CASE expression as well
case when r.name is not null and c.name is not null
then concat( r.name, ', ', c.name ) else c.name end as location
You want to use MySQL's IFNULL(value, default) function.
Coalesce could help you
COALESCE(region_id, 'default value')
I have a query that looks like this:
SELECT
app.application_id,
j.job_number,
j.job_id,
j.job_title,
j.job_city,
j.job_state,
p.person_id AS candidate_id,
p.first_name,
p.last_name,
app.start_date,
ope1.percent_complete,
MAX(CASE
WHEN r.role_display_name = 'ENG - Recruiter' THEN
(SELECT CASE WHEN COUNT(last_name) = 0 THEN
'Unassigned'
ELSE
COUNT(last_name)
END AS uname
FROM users
JOIN job_roles ON job_roles.user_id = users.user_id
WHERE job_id = j.job_id
AND role_id = r.role_id
)
ELSE '' END) AS role_3
My problem is that COUNT(last_name) will not return 0, because there are no records returned, so there is no value of NULL. All makes sense, however I have tried wrapping it in IFNULL(), ISNULL() and none of them seem to fix this problem. How can I get it to return 0 when there are no records? Do I need another subquery inside the COUNT() aggregate? I would really like to not use another subquery....
If understand correctly what you want you can try to rewrite it this way
SELECT ...
,MAX(CASE WHEN r.role_display_name = 'ENG - Recruiter'
THEN COALESCE(NULLIF(
(
SELECT COUNT(last_name)
FROM users JOIN job_roles
ON job_roles.user_id = users.user_id
WHERE job_id = j.job_id
AND role_id = r.role_id
), 0), 'Unassigned')
ELSE ''
END) as role_3
...