sum function based on unique date - mysql

actually i want a total value based on unique date
SELECT t2.stateName,
t3.`districtName`,t1.fingerDate,COUNT(a.arg1),COUNT(a.arg2),COUNT(a.arg3),COUNT(a.arg4),COUNT(a.arg5),COUNT(a.arg6),COUNT(a.arg7), COUNT(a.arg8),(COUNT(a.arg1) + COUNT(a.arg2) + COUNT(a.arg3) + COUNT(a.arg4) + COUNT(a.arg5) + COUNT(a.arg6) + COUNT(a.arg7))AtotalScreeningArg
FROM tbl_user AS t1
LEFT JOIN tbl_state AS t2 ON t1.addressState = t2.stateId
LEFT JOIN `tbl_district` AS t3 ON t1.addressDistrict = t3.`districtId`
LEFT JOIN (SELECT userId,
CASE WHEN occupation = "Truckers" OR occupation = "Drivers"
THEN occupation END arg1,
CASE WHEN (occupation = "Migrant")
THEN occupation END arg2,
CASE WHEN (hrg IS NULL OR hrg = " ") AND (`arg` IS NULL OR `arg` = " ")
THEN "Student" END arg3,
CASE WHEN (occupation = "Daily Wage")
THEN occupation END arg4,
CASE WHEN (occupation = "Salaried" OR occupation = "self Employed" OR occupation ="Unemployed" OR occupation = "Other")
THEN occupation END arg5,
CASE WHEN (ARG = "Female Partner (FPHRG)" OR ARG = "Partner / Spouse of FSW")
THEN ARG END arg6,CASE WHEN ARG = "Female Partner (FPARG)" THEN ARG END arg7,
CASE WHEN ARG = "TG (F-M)" THEN ARG END arg8,CASE WHEN hrg = "MSM" THEN hrg END arg9,
CASE WHEN hrg = "TG(M_F)" THEN hrg END arg10,CASE WHEN hrg = "FSW" THEN hrg END arg11,CASE WHEN hrg = "IDU" THEN hrg END arg12 FROM `tbl_user`
WHERE fingerDate IS NOT NULL OR fingerDate != " ")a ON t1.userId = a.userId
WHERE `t1`.`deleted` = "N" AND t1.userType = "user" AND `t1`.`fingerDate` >= '2018-11-01' AND `t1`.`fingerDate` <= '2018-12-01' AND t1.addressState = '34' AND t1.addressDistrict = '614' GROUP BY t1.userId
it gives result but not for unique date

Related

Pass Parameters from Excel sheet to Power Query from MS SQL

I am using Excel 365.
I have a Power Query (PQ) that reads off a MS SQL database and I need to set parameters for that query from a table in the same Excel workbook.
I created the parameters in PQ, when I feed them manually the query works
But when I link those parameters to my Excel table get the following error message
Formula.Firewall: Query 'SQL_Query' (step 'Source') references other queries or steps, so it may not directly access a data source. Please rebuild this data combination.
I changed the security settings but it did not work.
I've seen several posts that work but in instances where both data sources come directly only from Excel.
let
Source = Sql.Database("www.com", "db", [Query="
DECLARE #START_DATE AS DATE
DECLARE #END_DATE AS DATE
DECLARE #HYP AS VARCHAR(4)
SET #START_DATE = convert(datetime, " & Start_Dte & ",101)
SET #END_DATE = convert(datetime, " & End_Dte & ",101)
SET #HYP = " & Hyp & " -- = Query_Parameters('ParameterTable', 'Store')
select
case WHEN view1.STORE_NAME <> 'NULL' then view1.STORE_NAME ELSE view2.STORE end as [Store Name],
case WHEN view1.Store_Street <> 'NULL' then view1.Store_Street ELSE view2.Store_Street end as [Store Street],
case WHEN view1.Store_City <> 'NULL' then view1.Store_City ELSE view2.Store_City end as [Store City],
case WHEN view1.Store_State <> 'NULL' then view1.Store_State ELSE view2.[Store State] end as [Store State],
case WHEN view1.Store_zip <> 'NULL' then view1.Store_zip ELSE view2.Store_ZIP end as [Store ZIP],
case WHEN view1.Store <> 'NULL' then view1.Store ELSE view2.Store end as [Store],
case WHEN view1.ZIP_CODE <> 'NULL' then view1.ZIP_CODE ELSE view2.ZIP_CODE end as [ZIP Code],
case WHEN view2.Distance ='[1-5 MILE]' then '[0-5 miles]' ELSE
case WHEN view2.Distance ='[6-10 MILE]' then '[06-10 miles]' ELSE
case WHEN view2.Distance ='[+50 MILE]' then '[50+ miles]' ELSE
case WHEN view1.Distance <> 'NULL' then replace(view1.Distance,'MILES','miles') ELSE replace(view2.Distance,'MILES','miles')
end end
end
end as Distance,
case WHEN view1.Provider <> 'NULL' then view1.Provider ELSE view2.Provider end as Provider,
isnull(view1.Location,'New / Old') as Location,
case WHEN view1.[Lead Source] <> 'NULL' then view1.[Lead Source] ELSE '' end as [Lead Source],
isnull(view1.[YEAR],'') as [Year],
isnull(view1.[month],'') as [Month],
isnull(view1.[Opps],'') as [Opps],
isnull(view1.[Compass_Sales],'') as [Compass Sales],
case WHEN view2.TC_Terrain='Yes' THEN 'Yes' ELSE 'No' END as [TC Terrain]
from (
----Opps & Sales by Provider and by ZIP ---------
SELECT
b.STORE_NAME,
f.STREET1 as Store_Street,
g.CITY as Store_City,
g.STATE as Store_State,
g.ZIP_CODE as Store_ZIP,
b.STORE_STORE_ID as Store,
a.CONTACT_ZIP as ZIP_CODE,
a.DISTANCE as [Distance],
a.media_name as [Provider],
d.Location_name as [Location],
ccategory_name as [Lead Source],
YEAR(TRANSACTION_DATE) as [YEAR],
MONTH(TRANSACTION_DATE) as [MONTH],
SUM(Opportunities) as Opps,
SUM(Compass_Sales) as Compass_Sales
FROM [DB].[fact].[FACT_SALES_TRAFFIC_DETAIL] a
left join db.dim.dim_store b with(nolock) on b.store_id = a.store_id
left join db.dim.dim_lead_category c with(nolock)on a.SOURCE = ccategory
left join db.dim.dim_Location d with(nolock)on a.Location_Key = d.Location_key
left join db.dim.dim_market e with(nolock)on b.market_id = e.market_id
left join DB.DIM.DIM_STORE f with(nolock) on b.STORE_STORE_ID = f.STORE_STORE_ID
left join db.dim.dim_zipcode g with(nolock) on f.ZIP = g.ZIP_CODE
WHERE b.store_Store_id in (#HYP)
and a.Source = 'e'
and TRANSACTION_DATE between #START_DATE and #END_DATE
and a.MEDIA_NAME='TC'
GROUP BY
b.STORE_NAME,
b.STORE_STORE_ID,
d.Location_name,
a.media_Name,
ccategory_name,
YEAR(TRANSACTION_DATE),
MONTH(TRANSACTION_DATE),
CONTACT_ZIP,
a.DISTANCE,
f.STREET1,
g.CITY,
g.ZIP_CODE,
g.STATE) as view1 full outer join (
---True Car ZIP code Terrain------
SELECT
b.STORE_name as STORE,
b.STORE_STORE_ID as [Store],
b.STREET1 as [Store_Street],
c.CITY as [Store_City],
c.STATE as [Store State],
b.ZIP as [Store_ZIP],
INCLUDED_ZIP AS ZIP_CODE,
DISTANCE AS Distance,
'TC' AS Provider,
'Yes' as TC_Terrain
FROM BITESTDB.TIMLINM.TC_ZIPCODE a
left join DB.DIM.DIM_STORE b with(nolock) on b.STORE_id = a.STORE_id
left join db.dim.dim_zipcode c with(nolock) on c.ZIP_CODE= b.ZIP
WHERE STORE_STORE_ID = #HYP AND INCLUDED_ZIP <> '') as view2
on view1.ZIP_CODE = view2.zip_code
ORDER BY Distance
", MultiSubnetFailover=true])
in
Source
The easy way: open Power Query, then File > Options > CURRENT FILE > Privacy > Ignore

How to update multiple rows value in one query in particular column

UPDATE job_seekers SET js_email =
CASE js_email
WHEN "Wbsirota#gmail.com" THEN "bsirota#gmail.com"
WHEN "#nautiyalanuj#gmail.com"THEN "nautiyalanuj#gmail.com"
WHEN ".agiletestanalyst​#gmail.com" THEN "agiletestanalyst​#gmail.com"
END ) WHERE js_email IN ("wbsirota#gmail.com",
"#nautiyalanuj#gmail.com",
".agiletestanalyst​#gmail.com");
UPDATE
job_seekers
SET
js_email = CASE WHEN js_email = 'Wbsirota#gmail.com' THEN
'bsirota#gmail.com' WHEN js_email = '#nautiyalanuj#gmail.com' THEN
'nautiyalanuj#gmail.com' WHEN js_email = '.agiletestanalyst​
#gmail.com' THEN 'agiletestanalyst​#gmail.com' ELSE js_email END
WHERE
js_email IN ("wbsirota#gmail.com","#nautiyalanuj#gmail.com",
".agiletestanalyst​#gmail.com");
EXAMPLE IS BELOW
UPDATE
dbo.TestStudents
SET
LASTNAME = CASE WHEN LASTNAME = 'AAA' THEN 'BBB'
WHEN LASTNAME = 'CCC' THEN 'DDD'
WHEN LASTNAME = 'EEE' THEN 'FFF' ELSE LASTNAME END
WHERE
LASTNAME IN ('AAA', 'CCC', 'EEE');

Getting the result from the first matched condition and not consider result from the next matched condition

I have a requirement to get the result from the first matched condition and if it finds result in that level then return from there or go to Next level to
find the result .
Any help appreciated.
This is a prioritization query (with ties). One method uses dense_rank():
select rsc.*
from (select rsc.*,
dense_rank() over (order by case when rsc.entityId = :accountId and rsc.entityTypeCode = 'A' then 1
when rsc.entityId = :companyId and rsc.entityTypeCode = 'C' then 2
when rsc.entityId = :issuerId and rsc.entityTypeCode = 'I' then 3
else 4
end) as seqnum
from CurrentRuleSetContents rsc
where (rsc.entityId = :accountId and rsc.entityTypeCode = 'A') or
(rsc.entityId = :companyId and rsc.entityTypeCode = 'C') or
(rsc.entityId = :issuerId and rsc.entityTypeCode = 'I') or
(rsc.industryCode = :industryCode)
) rsc
where seqnum = 1;

CASE statement not updating Temp Table

In the UPDATE statement below, the InspectionChg, MileageChg, FuelChg and FreightChg columns are not updating to temp table. Goal is to aggregate charges. I have tried several variations and I cannot get Temp Table Columns to update.
UPDATE #TTable
SET ChargeCode = id.cht_itemcode,
InspectionChg = CASE WHEN LEFT(id.cht_itemcode, 3) = 'INS' THEN
InspectionChg+id.ivd_charge ELSE InspectionChg+0 END,
MileageChg = CASE WHEN LEFT(id.cht_itemcode, 4) = 'MILE' THEN MileageChg+id.ivd_charge
ELSE MileageChg+0 END,
FuelChg = CASE WHEN LEFT(id.cht_itemcode, 4) = 'FUEL' THEN
FuelChg+id.ivd_charge ELSE FuelChg+0 END,
FreightChg = CASE WHEN LEFT(id.cht_itemcode, 2) = 'LH' THEN FreightChg+id.ivd_charge
ELSE FreightChg+0 END,
Rate = 1
FROM #TTable
INNER JOIN invoicedetail as id
on #TTable.OrderNumber = id.Ord_hdrnumber
What is wrong?
UPDATE TT
SET TT.ChargeCode = id.cht_itemcode ,
TT.InspectionChg = ( CASE WHEN LEFT(id.cht_itemcode, 3) = 'INS'
THEN InspectionChg + id.ivd_charge
ELSE InspectionChg + 0
END ) ,
TT.MileageChg = ( CASE WHEN LEFT(id.cht_itemcode, 4) = 'MILE'
THEN MileageChg + id.ivd_charge
ELSE MileageChg + 0
END ) ,
TT.FuelChg = ( CASE WHEN LEFT(id.cht_itemcode, 4) = 'FUEL'
THEN FuelChg + id.ivd_charge
ELSE FuelChg + 0
END ) ,
TT.FreightChg = ( CASE WHEN LEFT(id.cht_itemcode, 2) = 'LH'
THEN FreightChg + id.ivd_charge
ELSE FreightChg + 0
END ) ,
TT.Rate = 1
FROM #TTable AS TT
INNER JOIN invoicedetail AS id ON TT.OrderNumber = id.Ord_hdrnumber

Subquery returns more than 1 row sql query

I had such an error can explain what I did wrong?
I only add this sql query:
(? = (select travel_region_id from relationships where travel_id = travels.id))
error
ActiveRecord::StatementInvalid (Mysql::Error: Subquery returns more than 1 row: select count(*) from travel_start_days, cars, travels
where travels.id = travel_start_days.travel_id and travels.id = travel.car_id and travel_start_days.day > adddate(curdate(), interval '2' day) and (7 = (select travel_region_id from relationships where travel_id = travels.id)) and '2013-08-16' <= travel_start_days.day):
Update Whis is method create query
def conditions
where = ''
param = []
if #region && #region != ''
where += 'travels.region_id = ?'
param += [#region.to_i]
end
if #car && #car != ''
where += ' and ' if where != ''
where += 'cars.id = ?'
param += [#car.to_i]
end
if #relation && #relation != ''
where += ' and ' if where != ''
where += '(? = (select travel_region_id from relationships where travel_id = travels.id))'
param += [#relation.to_i]
end
if #start_port && #start_port != ''
where += ' and ' if where != ''
where += '(? = (select location_id from travel_days where travel_id = travel_start_days.travel_id and day_no = 1 order by arrival asc limit 1))'
param += [#start_port.to_i]
end
return where == '' ? nil : ["travel_start_days.day > adddate(curdate(), interval ? day) and " + where] + [#criteria[5]] + param
end
The issue in this part of conditions:
(7 = (select travel_region_id from relationships where travel_id = travels.id))
Obviously this subquery returns more than one travel_region_id just replace = with IN
(7 IN (select travel_region_id from relationships where travel_id = travels.id))
If you are getting more than 1 row from subquery, then add group by clause to your subquery
SELECT travel_region_id FROM relationships
WHERE travel_id = travels.id
GROUP BY travel_region_id