filter rows based on column values in mysql query - mysql

This is my mysql query
SELECT a.model_name,
IF( b.officially_released_year = '".$currentyear."',1,0 ) AS release_year,
IF( b.officially_released_month = '".$first_month."' OR b.officially_released_month = '".$second_month."' OR b.officially_released_month = '".$third_month."' OR b.officially_released_month = '".$currentmonth."' ,1,0) AS release_month
FROM ".TBL_CAR_ADD_MODELS." a, ".TBL_CAR_SPEC_GENERAL." b
WHERE a.model_id = b.model_id AND a.model_status = '1'
ORDER BY a.model_created_on DESC
I want to do one more filtering option in this query. I need to get the records based on release_year = 1 & release_year = 1. I have done release_year and release_month columns through IF STATEMENT in MYSQL QUERY
release_year
IF( b.officially_released_year = '".$currentyear."',1,0 ) AS release_year
release_month
IF( b.officially_released_month = '".$first_month."' OR b.officially_released_month = '".$second_month."' OR b.officially_released_month = '".$third_month."' OR b.officially_released_month = '".$currentmonth."' ,1,0) AS release_month
How do I get the records based on these values (release_month = 1 & release_year = 1) in this query? I have tried WHERE release_month = 1 AND release_year = 1 but this one returns unknown column

You could do this:
SELECT
*
FROM
(
SELECT
a.model_name,
a.model_created_on,
IF( b.officially_released_year = '".$currentyear."',1,0 ) AS release_year,
IF( b.officially_released_month = '".$first_month."' OR b.officially_released_month = '".$second_month."' OR b.officially_released_month = '".$third_month."' OR b.officially_released_month = '".$currentmonth."' ,1,0) AS release_month
FROM ".TBL_CAR_ADD_MODELS." a, ".TBL_CAR_SPEC_GENERAL." b
WHERE a.model_id = b.model_id AND a.model_status = '1'
) AS tbl
WHERE tbl.release_year=1 AND release_month=1
ORDER BY tbl.model_created_on DESC

Related

MYSQL Group by DATA NOT individually

This is My Data:
I want select data Like This ▼
I try select that in group by but is not individually data show.
and this is my code
select
date_format((select receive_contract_datetime from worklist_info where id = worklist_id), '%y.%m.%d') as receive_datetime,
(select trade_name from trade_info where id = (select worklist_trade_id from worklist_info where id = worklist_id)) as trade_name,
(select staff_name from staff_info where id =
(select account_staff_id from account_info where id =
(select worklist_account_id from worklist_info where id = worklist_id))) as worklist_writer,
date_format((select worklist_output_plan_date from worklist_info where id = worklist_id), '%y.%m.%d') as output_plan_date,
(select worklist_project_name from worklist_info where id = worklist_id) as prj_name,
worklist_sub_process_id,
count(worklist_sub_process_id),
sum(worklist_sub_state),
-- (sum(worklist_sub_process_id = 1)*2) as laser_count, worklist_sub_state
-- sum(worklist_sub_process_id = 1) as laser_count,
-- if(sum(worklist_sub_process_id = 1) > 0,count(IF(worklist_sub_process_id = 1, if(worklist_sub_state = 0,1,null), null)),-1) as laser_wait,
-- if(sum(worklist_sub_process_id = 1) > 0,count(IF(worklist_sub_process_id = 1, if(worklist_sub_state = 1,1,null), null)),-1) as laser_run,
-- if(sum(worklist_sub_process_id = 1) > 0,count(IF(worklist_sub_process_id = 1, if(worklist_sub_state = 2,1,null), null)),-1) as laser_end,
(select worklist_comment from worklist_info where id = worklist_id) as worklist_comment,
(select worklist_lot from worklist_info where id = worklist_id) as lot_number
from worklist_info_sub group by worklist_id,worklist_sub_process_id;
You seem to pivot your dataset. For this, you can use conditional aggregation:
select col_master_id,
sum(col_semi_id = 1) as col_semi_1_count,
sum(case when col_semi_id = 1 then col_state else 0 end) as col_semi_1_state,
sum(col_semi_id = 2) as col_semi_2_count,
sum(case when col_semi_id = 2 then col_state else 0 end) as col_semi_2_state,
sum(col_semi_id = 3) as col_semi_3_count,
sum(case when col_semi_id = 3 then col_state else 0 end) as col_semi_3_state,
from mytable
group by col_master_id
I don't see how your query relates to your data. This answer is based on your sample data and desired results, not on your query.

Linq to SQL generates much slower query

I've got a SQL query that when I run it in SQL Server Mgmt Studio takes 15 seconds to complete. When I translate it to linq and run it in LINQPad it takes 1:50 to complete. Have I just completely bungled the "translation" to linq?
This is the SQL:
WITH details AS (
SELECT DISTINCT SerialNumber, OrgLevel3, OrgLevel4, [Status Label]
FROM vw_PSM_AssetIntransit
WHERE yyyyww = DATENAME(yy, GETDATE()) + RIGHT('00' + DATENAME(wk, GETDATE()), 2) AND pro_status <> 'retired'
)
SELECT OrgLevel3, OrgLevel4, COUNT(DISTINCT SerialNumber) Total,
SUM(CASE WHEN [Status Label] NOT IN ('15-21 Days InTransit', '8-14 Days InTransit', 'over 21 Days InTransit') THEN 1 ELSE 0 END) Compliant
FROM details
GROUP BY OrgLevel3, OrgLevel4
ORDER BY 1, 2
and this is the LINQ I translated it to:
var now = DateTime.Now;
var dtf = System.Globalization.DateTimeFormatInfo.CurrentInfo;
var calendar = dtf.Calendar;
var yearWW = now.Year * 100 + calendar.GetWeekOfYear(now, dtf.CalendarWeekRule, dtf.FirstDayOfWeek);
var badStatusLabels = new string[] { "15-21 Days InTransit", "8-14 Days InTransit", "over 21 Days InTransit" };
var details = (
from r in Vw_PSM_AssetIntransit
where r.yyyyww == yearWW && r.Pro_status != "retired"
select new { r.SerialNumber, r.OrgLevel3, r.OrgLevel4, r.StatusLabel }
).Distinct();
var data = from r in details
group r by new { r.OrgLevel3, r.OrgLevel4 } into grp
orderby grp.Key.OrgLevel3, grp.Key.OrgLevel4
select new
{
OrgLevel3 = grp.Key.OrgLevel3,
OrgLevel4 = grp.Key.OrgLevel4,
Total = grp.Select(x => x.SerialNumber).Distinct().Count(),
Compliant = grp.Sum(x => !badStatusLabels.Contains(x.StatusLabel) ? 1 : 0)
};
Console.WriteLine(data);
This is what LINQPad shows the SQL query came out as:
-- Region Parameters
DECLARE #p0 Int = 201816
DECLARE #p1 NVarChar(1000) = 'retired'
DECLARE #p2 NVarChar(1000) = '15-21 Days InTransit'
DECLARE #p3 NVarChar(1000) = '8-14 Days InTransit'
DECLARE #p4 NVarChar(1000) = 'over 21 Days InTransit'
DECLARE #p5 Int = 1
DECLARE #p6 Int = 0
-- EndRegion
SELECT [t3].[OrgLevel3], [t3].[OrgLevel4], (
SELECT COUNT(*)
FROM (
SELECT DISTINCT [t5].[SerialNumber]
FROM (
SELECT DISTINCT [t4].[SerialNumber], [t4].[OrgLevel3], [t4].[OrgLevel4], [t4].[Status Label]
FROM [vw_PSM_AssetIntransit] AS [t4]
WHERE ([t4].[yyyyww] = #p0) AND ([t4].[pro_status] <> #p1)
) AS [t5]
WHERE ((([t3].[OrgLevel3] IS NULL) AND ([t5].[OrgLevel3] IS NULL)) OR (([t3].[OrgLevel3] IS NOT NULL) AND ([t5].[OrgLevel3] IS NOT NULL) AND ((([t3].[OrgLevel3] IS NULL) AND ([t5].[OrgLevel3] IS NULL)) OR (([t3].[OrgLevel3] IS NOT NULL) AND ([t5].[OrgLevel3] IS NOT NULL) AND ([t3].[OrgLevel3] = [t5].[OrgLevel3]))))) AND ((([t3].[OrgLevel4] IS NULL) AND ([t5].[OrgLevel4] IS NULL)) OR (([t3].[OrgLevel4] IS NOT NULL) AND ([t5].[OrgLevel4] IS NOT NULL) AND ((([t3].[OrgLevel4] IS NULL) AND ([t5].[OrgLevel4] IS NULL)) OR (([t3].[OrgLevel4] IS NOT NULL) AND ([t5].[OrgLevel4] IS NOT NULL) AND ([t3].[OrgLevel4] = [t5].[OrgLevel4])))))
) AS [t6]
) AS [Total], [t3].[value] AS [Compliant]
FROM (
SELECT SUM([t2].[value]) AS [value], [t2].[OrgLevel3], [t2].[OrgLevel4]
FROM (
SELECT
(CASE
WHEN NOT ([t1].[Status Label] IN (#p2, #p3, #p4)) THEN #p5
ELSE #p6
END) AS [value], [t1].[OrgLevel3], [t1].[OrgLevel4]
FROM (
SELECT DISTINCT [t0].[SerialNumber], [t0].[OrgLevel3], [t0].[OrgLevel4], [t0].[Status Label]
FROM [vw_PSM_AssetIntransit] AS [t0]
WHERE ([t0].[yyyyww] = #p0) AND ([t0].[pro_status] <> #p1)
) AS [t1]
) AS [t2]
GROUP BY [t2].[OrgLevel3], [t2].[OrgLevel4]
) AS [t3]
ORDER BY [t3].[OrgLevel3], [t3].[OrgLevel4]

Oracle SQL: Update a column from select

I have to update SRCFILE_FULL_COUNT_HIGH. Set new value from NOVA_HODNOTA. Here is working select.
Thank you!
UPDATE SRCFILE_ID, NOVA_HODNOTA, SRCFILE_FULL_COUNT_HIGH as STARA_HODNOTA
from
(
working select:
SELECT /*+ no_index(rh FILEINS_STATUS_FK_I) */
rh.SRCFILE_ID, rh.FILEINS_RECORD_COUNT, rh.FILEINS_EFFECTIVE_DATE, esf.SRCFILE_FULL_COUNT_HIGH,
100*(rh.FILEINS_RECORD_COUNT/esf.SRCFILE_FULL_COUNT_HIGH) as PROCENTA,
CASE
WHEN (100*(rh.FILEINS_RECORD_COUNT/esf.SRCFILE_FULL_COUNT_HIGH)) >= 80
THEN FILEINS_RECORD_COUNT*1.25
ELSE SRCFILE_FULL_COUNT_HIGH
END AS NOVA_HODNOTA
FROM ETL_SOURCE_FILE_INST rh,
(SELECT MAX(FILEINS_EFFECTIVE_DATE) AS maxdate, SRCFILE_ID
FROM ETL_SOURCE_FILE_INST
GROUP BY SRCFILE_ID) maxresults,
ETL_SOURCE_FILES esf
WHERE rh.SRCFILE_ID = maxresults.SRCFILE_ID
and rh.SRCFILE_ID = esf.SRCFILE_ID
AND rh.FILEINS_EFFECTIVE_DATE= maxresults.maxdate
AND RH.FILEINS_INCREMENTAL_FLAG = 'F'
and ESF.SRCFILE_FULL_COUNT_FLAG = 'Y'
and RH.FILEINS_STATUS = 'COMPLETE'
ORDER BY SRCFILE_ID ASC
END of select
)
WHERE STARA_HODNOTA = SRCFILE_FULL_COUNT_HIGH
SET STARA_HODNOTA = NOVA_HODNOTA
;
merge into
ETL_SOURCE_FILE_INST i
using
(
SELECT /*+ no_index(rh FILEINS_STATUS_FK_I) */
rh.SRCFILE_ID, rh.FILEINS_RECORD_COUNT, rh.FILEINS_EFFECTIVE_DATE, esf.SRCFILE_FULL_COUNT_HIGH,
100*(rh.FILEINS_RECORD_COUNT/esf.SRCFILE_FULL_COUNT_HIGH) as PROCENTA,
CASE
WHEN (100*(rh.FILEINS_RECORD_COUNT/esf.SRCFILE_FULL_COUNT_HIGH)) >= 80
THEN FILEINS_RECORD_COUNT*1.25
ELSE SRCFILE_FULL_COUNT_HIGH
END AS NOVA_HODNOTA
FROM ETL_SOURCE_FILE_INST rh,
(SELECT MAX(FILEINS_EFFECTIVE_DATE) AS maxdate, SRCFILE_ID
FROM ETL_SOURCE_FILE_INST
GROUP BY SRCFILE_ID) maxresults,
ETL_SOURCE_FILES esf
WHERE rh.SRCFILE_ID = maxresults.SRCFILE_ID
and rh.SRCFILE_ID = esf.SRCFILE_ID
AND rh.FILEINS_EFFECTIVE_DATE= maxresults.maxdate
AND RH.FILEINS_INCREMENTAL_FLAG = 'F'
and ESF.SRCFILE_FULL_COUNT_FLAG = 'Y'
and RH.FILEINS_STATUS = 'COMPLETE'
) t on (t.SRCFILE_ID = i.SRCFILE_ID)
when matched then
update
set
i.SRCFILE_FULL_COUNT_HIGH = t.NOVA_HODNOTA
where
i.SRCFILE_FULL_COUNT_HIGH = t.SRCFILE_FULL_COUNT_HIGH
;

Why the query is giving result while the destCurrency is not 0 -

SELECT *
FROM xyz
WHERE applyat = 'anita'
AND applyAt = 'Create'
AND country = '50'
AND dest_country = '108'
AND originCurrency = 'EUR'
AND destCurrency = 0 AND isEnable = 'Y'
AND agentID = 0
AND Module = 'abc'
ORDER BY ruleID DESC
You should add quotes as it is a varchar
SELECT * FROM xyz WHERE applyat = 'anita' AND applyAt = 'Create' AND country = '50' AND dest_country = '108' AND originCurrency = 'EUR' AND destCurrency = '0' AND isEnable = 'Y' And agentID = 0 AND Module = 'abc' ORDER BY ruleID DESC

How to perform multiple calculations with in a single query

I have a situation where in i have to get the data from an Year Ago , Previous Month and Current Month. What is the best way to achieve this ?
I have a table which contains the year,month and data in it. In the below query have added a filter
c.ReportMonth = DATENAME(month, #12MonthsAgo) and c.ReportYear = Year(#12MonthsAgo)
This is for an year ago. In the same way if i have to get the previous month and current month, can i do that with in the same query by setting the filters ? how do we do that ?
Is there a better way other than i end up writing 3 select queries and then putting the select to a tmp table and later merging the tables ?
create table #TPTABLE
(
KPIName varchar(150)
,MetricName Varchar(200)
,MetricId INT
,DataSource varchar(50)
,[AnYearAgo] Float
,[PreviousMonth] float
,[CurrentMonth] float
);
insert into #TPTABLE
(KPIName,MetricName,MetricId,DataSource,[AnYearAgo])
SELECT
p.KPIName
,p.MetricName
,p.MetricId
,p.DataSource
,c.Value as [AnYearAgo]
FROM [IntegratedCare].[report].[KPIMetricDetails] p
LEFT JOIN [IntegratedCare].[report].[KPIMectricValues] c
ON p.[MetricId] = c.MetricId
WHERE c.ReportMonth = DATENAME(month, #12MonthsAgo) and c.ReportYear = Year(#12MonthsAgo)
ORDER BY KPI_Id ASC, [MetricId] ASC
SELECT
p.KPIName
,p.MetricName
,p.MetricId
,p.DataSource
,c.Value
,c2.Value
,c3.Value
FROM [IntegratedCare].[report].[KPIMetricDetails] p
LEFT JOIN [IntegratedCare].[report].[KPIMectricValues] c
ON p.[MetricId] = c.MetricId
AND c.[CommissionerCode] = COALESCE(NULLIF(#Commissioner, ''), c.[CommissionerCode])
ANd ReportMonth = DATENAME(month, #12MonthsAgo) and c.ReportYear = Year(#12MonthsAgo)
LEFT JOIN [IntegratedCare].[report].[KPIMectricValues] c2
ON p.[MetricId] = c2.MetricId
AND c2.[CommissionerCode] = COALESCE(NULLIF(#Commissioner, ''), c2.[CommissionerCode])
ANd c2.ReportMonth = DATENAME(month, #PreviousMonth) and c2.ReportYear = Year(#PreviousMonth)
LEFT JOIN [IntegratedCare].[report].[KPIMectricValues] c3
ON p.[MetricId] = c3.MetricId
AND c3.[CommissionerCode] = COALESCE(NULLIF(#Commissioner, ''), c3.[CommissionerCode])
ANd c3.ReportMonth = DATENAME(month, #PreviousMonth) and c3.ReportYear = Year(#PreviousMonth)
ORDER BY p.KPI_Id ASC, p.[MetricId] ASC
I think what you need is this:
insert into #TPTABLE
(KPIName,MetricName,MetricId,DataSource,[AnYearAgo])
SELECT
KPIName
,MetricName
,MetricId
,DataSource
,[AnYearAgo]
,[PreviousMonth]
,[CurrentMonth]
FROM (
SELECT
KPIName
,MetricName
,MetricId
,DataSource
,KPI_Id
,sum(case when c.ReportMonth = DATENAME(month, #12MonthsAgo) and c.ReportYear = Year(#12MonthsAgo) then c.Value else 0 end) as [AnYearAgo]
,sum(case when c.ReportMonth = DATENAME(month, #PreviousMonth) and c.ReportYear = Year(#PreviousMonth) then c.Value else 0 end) as [PreviousMonth]
,sum(case when c.ReportMonth = DATENAME(month, #CurrentMonth) and c.ReportYear = Year(#CurrentMonth) then c.Value else 0 end) as [CurrentMonth]
FROM [IntegratedCare].[report].[KPIMetricDetails] p
LEFT JOIN [IntegratedCare].[report].[KPIMectricValues] c
ON p.[MetricId] = c.MetricId
GROUP BY KPIName, MetricName, MetricId, DataSource, KPI_Id
ORDER BY KPI_Id ASC, [MetricId] ASC