in MySQL query if I pass:
case guides.Gud_Publish_Date
when null then "Unscheduled"
else "Forth Coming Titles"
end
then it is considering all is null even the Gud_Publish_Date has value also. the full SQL statement is
SELECT guides.Gud_Id
, guides.Gud_Image
, guides.Gud_SubEditor
, guides.Gud_Reprint_Status
, guides.Gud_Publish_Date
, guides.Gud_Img_Chk
, guides.Gud_Published
, guides.Gud_View
, (
CASE guides.Gud_Publish_Date
WHEN NULL
THEN "Unscheduled"
ELSE "Forth Coming Titles"
END
) AS Schedules
FROM guides
try using IF
SELECT guides.Gud_Id
, guides.Gud_Image
, guides.Gud_SubEditor
, guides.Gud_Reprint_Status
, guides.Gud_Publish_Date
, guides.Gud_Img_Chk
, guides.Gud_Published
, guides.Gud_View
, IF(guides.Gud_Publish_Date IS NULL,'Unscheduled','Forth Coming Titles')
AS Schedules
FROM guides
or if you really want CASE
SELECT guides.Gud_Id
, guides.Gud_Image
, guides.Gud_SubEditor
, guides.Gud_Reprint_Status
, guides.Gud_Publish_Date
, guides.Gud_Img_Chk
, guides.Gud_Published
, guides.Gud_View
, (
CASE
WHEN guides.Gud_Publish_Date IS NULL
THEN 'Unscheduled'
ELSE 'Forth Coming Titles'
END
) AS Schedules
FROM guides
I found this -a couple of months- old post. Using COALESCE option as intended by Rajan, you can do,
SELECT guides.Gud_Id
, guides.Gud_Image
, guides.Gud_SubEditor
, guides.Gud_Reprint_Status
, guides.Gud_Publish_Date
, guides.Gud_Img_Chk
, guides.Gud_Published
, guides.Gud_View
, CASE COALESCE(guides.Gud_Publish_Date, 0)
WHEN 0 THEN 'Unscheduled'
ELSE 'Forth Coming Titles'
END AS Schedules
FROM guides
The code above assumes that guides.Gud_Publish_Date cannot take the value 0, which I can do because it is a date. If that weren't the case you can change 0 for another value that it cannot take; maybe your favorite float like 3.1415 or a null identifier 'null'.
SELECT guides.Gud_Id
, guides.Gud_Image
, guides.Gud_SubEditor
, guides.Gud_Reprint_Status
, guides.Gud_Publish_Date
, guides.Gud_Img_Chk
, guides.Gud_Published
, guides.Gud_View
, (
CASE WHEN guides.Gud_Publish_Date IS NULL
THEN "Unscheduled"
ELSE "Forth Coming Titles"
END
) AS Schedules
FROM guides
Try this
SELECT guides.Gud_Id
, guides.Gud_Image
, guides.Gud_SubEditor
, guides.Gud_Reprint_Status
, guides.Gud_Publish_Date
, guides.Gud_Img_Chk
, guides.Gud_Published
, guides.Gud_View
, coalesce((
CASE guides.Gud_Publish_Date
WHEN NULL
THEN 'Unscheduled'
ELSE 'Forth Coming Titles'
END
), 'Unscheduled') AS Schedules
FROM guides
Related
I have created sample database
create table items (Order_date date, frame char(2) , group_name varchar(50) , order_qty int(4) , receive_qty int(4));
insert into items(Order_date , frame , group_name , order_qty ,receive_qty) values
("2021-3-10" , "am" , "books" , 4280 , 4000),
("2021-3-12" , "pm" , "notebooks" , 3259 , 3100),
("2021-3-14" , "pm" , "erasers" , 2828 , 2500),
("2021-3-15" , "am" , "books" , 3147 , 3088),
("2021-3-16" , "pm" , "notebooks" , 2897 , 2700),
("2021-3-19" , "am" , "notebooks" , 4793 , 3030),
("2021-3-21" , "pm" , "erasers" , 3317 , 3100);
#Query to get Fill Rate
select group_name , (sum(receive_qty)/sum(order_qty)*100) as Fill_Rate
from items
group by group_name;
#Query to get Fill Rate when frame is AM
select group_name , (sum(receive_qty)/sum(order_qty)*100) as AM_Fill_Rate
from items
where frame = "am"
group by group_name;
#Query to get Fill Rate post 16 mar 2021
select group_name , (sum(receive_qty)/sum(order_qty)*100) as Fill_Rate_post_16Mar
from items
where Order_date >= "2021-03-16"
group by group_name;
How can I get Fill_Rate , AM_Fill_Rate and Fill_Rate_post_16Mar in single table in single run??
since i can get all 3 by individually running above 3 queries , 3 times
Please suggest !
select group_name ,
(sum(receive_qty)/sum(order_qty)*100) as Fill_Rate ,
(sum(case when frame = "am" then receive_qty else 0 end)/sum(case when frame = "am" then order_qty else 0 end)*100) as AM_Fill_Rate
(sum(case when Order_date >= "2021-03-16" then receive_qty else 0 end)/sum(case when Order_date >= "2021-03-16" then order_qty else 0 end)*100) as Fill_Rate_post_16Mar
from items
group by group_name;
this was working previously and now I cannot figure out where the error is. trying to create a design view table qry
all table and field references are named properly
SELECT
Union.[HARD DOLLAR ENTRY AREA]
, Union.Service
, tblAcctCodes.QtyRollUpTo
, tblAcctCodes.AcctCode
, tblAcctCodes.Desc
, Union.UOM
, SUM(Union.QTY) AS SumOfQTY
, Union.UR
, Union.MH
, tblAcctCodes.MH
, tblAcctCodes.[Is Terminal]
, [is terminal] & [qty] AS Expr1
, tblAcctCodes.[WBS Code] AS CBS
, tblAcctCodes.Level
, tblAcctCodes.[Calc]
, [Calc] & [QTY] AS Expr2
FROM
[Union]
RIGHT JOIN
tblAcctCodes
ON
Union.[Account Code] = tblAcctCodes.AcctCode
GROUP BY
Union.[HARD DOLLAR ENTRY AREA]
, Union.Service
, tblAcctCodes.QtyRollUpTo
, tblAcctCodes.AcctCode
, tblAcctCodes.Desc
, Union.UOM
, Union.UR
,
UNION
.MH
, tblAcctCodes.MH
, tblAcctCodes.[Is Terminal]
, [is terminal] & [qty]
, tblAcctCodes.[WBS Code]
, tblAcctCodes.Level
, tblAcctCodes.[Calc]
, [Calc] & [QTY]
HAVING
(
(
(
[is terminal] & [qty]
)
<>"Yes"
OR
(
[is terminal] & [qty]
)
IS NULL
)
)
ORDER BY
tblAcctCodes.QtyRollUpTo
;
Your originally posted code has a space too much:
Union.UR,
UNION .MH,
tblAcctCodes.MH
Also, don't use a reserved word (UNION) for a query name.
We need result from Table based on datetime filter. But it working slow in mysql. I can't implement indexing on date & timestamp columns due to it will slow our insertion/updation. So can you please suggest any alternate for select the data quickly based on date and datetime filter with better performance.
SQL Query :
SELECT *
FROM (
SELECT id
, title
, language
, lang_code
, financial
, fname
, lname
, mname
, mname_br
, suffix
, CASE WHEN DOB='0000-00-00' THEN NULL ELSE DOB END AS DOB
, street
, street2
, postal_code
, zip_ext
, city
, state
, country_code
, phone_home
, phone_biz
, phone_biz_ext
, phone_contact
, phone_cell
, status
, CASE WHEN date='0000-00-00 00:00:00' THEN NULL ELSE
CAST(date as datetime) END AS date
, sex
, referrer
, referrerID
, providerID
, ethnoracial
, pid
, temp_key
, primary_care
, default_facility
, created_by
, patientStatus
, primary_care_id
, Sec_HCFA
, noBalanceBill
, erx_entry
, erx_patient_id
, athenaID
, CASE WHEN licenseDate='0000-00-00 00:00:00' THEN NULL ELSE licenseDate end as licenseDate
, race
, otherRace
, ethnicity
, otherEthnicity
, primary_care_phy_name
, primary_care_phy_id
, CASE WHEN dod_patient='0000-00-00' THEN NULL ELSE dod_patient END AS dod_patient--
, locked--
, co_man_phy--
, co_man_phy_id--
, vip--
, External_MRN_1--
, External_MRN_2--
, External_MRN_3--
, External_MRN_4
, as_id
, CASE WHEN acc_statement_date='0000-00-00' THEN acc_statement_date END AS acc_statement_date
, CASE WHEN timestamp='0000-00-00 00:00:00' THEN NULL ELSE timestamp END AS timestamp
, api_id
, fmh_pt_status
, race_code
, ethnicity_code
, patient_payer
, CASE WHEN date='0000-00-00 00:00:00' THEN NULL ELSE date END AS transfer_created
,CASE WHEN timestamp='0000-00-00 00:00:00' THEN NULL ELSE timestamp END AS transfer_updated
,CASE WHEN date > '2020-11-10 00:00:00' THEN 'new' ELSE 'changed' END AS flagfield
,CASE WHEN date='0000-00-00 00:00:00' THEN NULL ELSE date END AS sortdate
FROM patient_data
WHERE (date > '2020-11-10 00:00:00' or timestamp > '2019-04-01 19:53:57-04')
AND month(date) > 0)t
ORDER BY flagfield desc,
sortdate;
)
id Column has indexing in the table
Get rid of
SELECT *
FROM (
)t
it adds nothing, except to slow things down.
Let's focus on
SELECT id
FROM patient_data
WHERE (date > '2020-11-10 00:00:00'
or timestamp > '2019-04-01 19:53:57-04')
AND month(date) > 0
Does that shortened query run "too slow"? If not, then we can use that as a derived table to see if that will speed it up. If so, then we will need to get into UNION and indexing.
The SQL generated my code looks like this -
INSERT INTO [Item] (
Vendor_num
, item
, [Desc]
, Pack
, item_size
, unit_of_sale
, allowance
, points
, Promo
, WS_Cost
, WS_Unit_cost
, Retailer_Price
, Retail
, Cust_Use
, Show_Price
, GP
, Min
, Max
, Book_Number
, Del1_date
, Del2_date
, Del3_date
, Del4_date
, Del5_date
, Del6_date
, Del7_date
, Del8_date
, Del9_date
, Del10_date
, Del11_date
, Del12_date
, Del13_date
, Del14_date
, Del15_date
, warehouse
, Purchase_Group
, GPM
, Material_Number
, PM
) VALUES (
'11'
, '300681'
, 'item description'
, '60'
, 'BOX'
, 'BOX'
, '3'
, '20'
, 'Y'
, '0'
, null
, '0'
, null
, '03652'
, null
, null
, null
, null
, '832'
, '6/2/2014 12:00:00 AM'
, '6/30/2014 12:00:00 AM'
, '7/28/2014 12:00:00 AM'
, '8/18/2014 12:00:00 AM'
, null
, null
, null
, null
, null
, null
, null
, null
, null
, null
, null
, '1'
, null
, null
, null
, null
)
(forgive all those null values, they're frequently not null in these inserts, so the statement needs to be flexible enough when they're not)
I'm getting a generic error --
An unhandled exception of type 'System.Data.OleDb.OleDbException' occurred in System.Data.dll
Additional information: Syntax error in INSERT INTO statement.
The statement works fine when copied directly into a new query in Access, but when firing through .NET and OLE DB, it fails miserably.
Any ideas?
Min and Max are reserved words in Access, and should be quoted if you use them as field names or you may encounter errors;
If a reserved word is already in use, you can avoid error messages by surrounding each occurrence of the word with brackets ([ ]). However, the best solution is to change the name to a nonreserved word.
In other words, used as field names, they should be quoted as [Min] and [Max].
I have used the same query to transfer from one database to another for ages, but suddenly I am getting the error
"Operand should contain 1 column(s)"
Here is my SQL:
INSERT INTO lightand_expressionengine.exp_channel_data (
entry_id
, site_id
, channel_id
, field_id_1
, field_ft_1
, field_id_3
, field_ft_3
, field_id_7
, field_ft_7
, field_id_8
, field_ft_8
, field_id_9
, field_ft_9
, field_id_11
, field_ft_11
, field_id_12
, field_ft_12
, field_id_13
, field_ft_13
, field_id_14
, field_ft_14
, field_id_16
, field_ft_16
, field_id_17
, field_ft_17
)
SELECT (
entry_id
, site_id
, channel_id
, field_id_1
, field_ft_1
, field_id_3
, field_ft_3
, field_id_7
, field_ft_7
, field_id_8
, field_ft_8
, field_id_9
, field_ft_9
, field_id_11
, field_ft_11
, field_id_12
, field_ft_12
, field_id_13
, field_ft_13
, field_id_14
, field_ft_14
, field_id_16
, field_ft_16
, field_id_17
, field_ft_17
)
FROM lightand_expressionengineold.exp_channel_data
Can anyone see what the issue is? I cant figure it out.
Remove the opening and closing brackets i.e. (...) from the SELECT. I.e. try the following SQL.
INSERT INTO lightand_expressionengine.exp_channel_data (
entry_id
, site_id
, channel_id
, field_id_1
, field_ft_1
, field_id_3
, field_ft_3
, field_id_7
, field_ft_7
, field_id_8
, field_ft_8
, field_id_9
, field_ft_9
, field_id_11
, field_ft_11
, field_id_12
, field_ft_12
, field_id_13
, field_ft_13
, field_id_14
, field_ft_14
, field_id_16
, field_ft_16
, field_id_17
, field_ft_17
)
SELECT
entry_id
, site_id
, channel_id
, field_id_1
, field_ft_1
, field_id_3
, field_ft_3
, field_id_7
, field_ft_7
, field_id_8
, field_ft_8
, field_id_9
, field_ft_9
, field_id_11
, field_ft_11
, field_id_12
, field_ft_12
, field_id_13
, field_ft_13
, field_id_14
, field_ft_14
, field_id_16
, field_ft_16
, field_id_17
, field_ft_17
FROM lightand_expressionengineold.exp_channel_data