Problem with IIF function in Microsoft Access - database

Good morning all,
I need your help !
I am working in Microsoft Access with ODBC database.
I want to fill in the blank in many fields, I have 6 columns and sometimes it happened to me that I have no items in SSCDT (sub-conditioning) , I want when I have no items in SSCDT, replace them with items in CDT (conditioning)
I created this code and since I modified the part (IIF) of SSCDT I have this error:
SELECT DISTINCT SOC1_PRO.MOTCLE AS CODPRO, SOC1_PRO.CODUNI AS UV, Min(Round((SOC1_TSC.PRXTAR*0.85),2)) AS PV1, IIF(MAX(IIF(SOC1_PRU.CODUNI='SAC', SOC1_PRU.COEFUV,' '))=' ', MAX(IIF(SOC1_PRU.CODUNI='CAR', SOC1_PRU.COEFUV,' ')) ,MAX(IIF(SOC1_PRU.CODUNI='SAC', SOC1_PRU.COEFUV,' '))) AS SSCDT, Min(SOC1_TSC.PRXTAR) AS PV2, MAX(IIF(SOC1_PRU.CODUNI='CAR', SOC1_PRU.COEFUV,' ')) AS CDT, Min(SOC1_PRO.NOMPRO) AS Désignation
FROM (SOC1_PRO AS SOC1_PRO INNER JOIN SOC1_TSC ON SOC1_PRO.CODPRO = SOC1_TSC.CODPRO) INNER JOIN SOC1_PRU ON SOC1_TSC.CODPRO = SOC1_PRU.CODPRO
WHERE (((SOC1_TSC.DATFIN)=' ') AND ((SOC1_TSC.CODSOC)='1') AND ((SOC1_TSC.DATDEB)>='20210000') AND ((SOC1_PRO.CODZN6)<>'N') AND ((SOC1_TSC.CODDEV)='EUR')) AND (SOC1_PRU.CODUNI = 'SAC' OR SOC1_PRU.CODUNI='CAR')
GROUP BY SOC1_PRO.MOTCLE, SOC1_PRO.CODUNI;
For exemple :
I hope i be clear with my explanation
Thank you ! <3

Related

Google Bigquery on reddit database to get post along with all the comments

I am new to Google Big-query, I want to extract post title,post body,comments, score and the creation date from the database for all posts that are created on or after 2010 for a subreddit, for now I have been able to query all subreddit comments using
SELECT * FROM `pushshift.rt_reddit.comments` WHERE lower(subreddit)="politics"
But my motive is to join the comments and posts table in order to generate the required results but I am not able to find how to do this, how can that be achieved? Please let me know if any further details are required. Thanks
Just a quick note ... the tables you reference seemed to stop # 2018-08-27 06:59:08 UTC - meaning you may need to find another datasource if you're looking for more recent posts/comments.
Standard SQL :
SELECT
s.title,
s.selftext,
s.score,
s.created_utc post_created_utc,
s.author,
ARRAY_AGG( STRUCT( c.body,
c.created_utc,
c.author ) ) comments
FROM
`pushshift.rt_reddit.submissions` s
LEFT OUTER JOIN
`pushshift.rt_reddit.comments` c
ON
CAST(s.id AS string) = c.link_id
WHERE
REGEXP_CONTAINS(c.subreddit, r'(?i)^politics$')
AND s.created_utc > '2009-12-31'
GROUP BY
1,
2,
3,
4,
5
LIMIT
10;
Date SQL :
SELECT
MAX(created_utc)
FROM
`pushshift.rt_reddit.submissions`
Code for fh-bigquery.reddit_comments ... works the same. Maybe use this post 2018 and earlier code pre 2018.
SELECT
s.title,
s.selftext,
s.score,
TIMESTAMP_SECONDS(s.created_utc ) post_created_utc,
s.author,
c.subreddit,
ARRAY_AGG( STRUCT( c.body,
c.created_utc,
c.author ) ) comments
FROM
`fh-bigquery.reddit_posts.20*` s
LEFT OUTER JOIN
`fh-bigquery.reddit_comments.20*` c
ON
regexp_extract(c.link_id,r'(.{6})\s*$') = s.id
WHERE
TIMESTAMP_SECONDS(s.created_utc ) between '2019-01-01' and '2019-01-03'
GROUP BY
1,
2,
3,
4,
5,
6
LIMIT
10;

In SQL Server, REPLACE function is not working properly when I use to replace with 1 space

select
replace(WBSElem, appProj, '') AS WBSElement,
replace(WBSElem + '#' + Descrip, '', '') as Descrip,
*
from
SAP_sFTP_DB.dbo.MMHE_PS_WBS
where
appProj = '1.13T0117'
order by
RecID asc
Here second replace is working fine, but the 1st replace is not working properly where I have above 100 records to change. I am getting output like nothing happened for 1st replace function. Can anybody help me?
select replace(WBSElem, rtrim(ltrim(appProj)), '') AS WBSElement,
replace(WBSElem+'#'+Descrip,'','')as Descrip, * from SAP_sFTP_DB.dbo.MMHE_PS_WBS where appProj='1.13T0117'
Due to White spaces the function did'nt work.Now its fine

Count the 'X' then arrange the value as row in SQL

I have a table in SQL that have many columns which the value of each columns in every row is either ' ' or 'X'. I need to count this 'X' for every columns which can be done by following code;
SELECT COUNT(GVI0) AS GVI0, COUNT(GVI1) AS GVI1, COUNT(GVI2) AS GVI2
FROM dbo.HullInspectionProgram
WHERE (StructureEntry='1' AND Year='2016')
The result of the query is;
GVI0 NDT0 GVI1 NDT1 GVI2 NDT2
11 11 2 4 11 11
However, (in my understanding) in order for this count value to be bind into ASP.net Chart Control with multiple series name 'GVI' and 'NDT', I need to make the column into row for the DataTable.
I try to use UNPIVOT in SQL like this;
SELECT GVI0Count
FROM (
SELECT COUNT(GVI0) AS GVI0, COUNT(GVI1) AS GVI1, COUNT(GVI2) AS GVI2
FROM dbo.HullInspectionProgram
WHERE (StructureEntry='1' AND Year='2016')
)
UNPIVOT (GVI0Count FOR ListOfColumns IN (GVI0)) AS unpivott
but it seem that the code is wrong.
How do I do this?
I think the following might work for you. At least, as a start.
SELECT *
FROM (
SELECT COUNT(GVI0) AS GVI0, COUNT(GVI1) AS GVI1, COUNT(GVI2) AS GVI2
FROM dbo.HullInspectionProgram
WHERE (StructureEntry='1' AND Year='2016')
) P
UNPIVOT (GVI0Count FOR ListOfColumns IN (GVI0, GVI1, GVI2)) AS unpivott

Missing data row in NOT IN clause

I just realised that my Auto-Price Calculation doesn't fill the Prices for ListID 4.
I inserted the Prices from the SELECT shown below.
For bugg research I executed the SELECT without the WHERE part and it shows me the example data row.
I can't find the error though, why it is not shown in the complete select (it has no entry with ListID = 4).
Someone can see my mistake?
Edit: Just tried the subselect alone, it shows no rows for the requested article. Why is the NOT IN clause unaffected by this fact?
Most likely, it's because of how you are combining the artikelnummer and the auspraegungID.
I do not think you have accounted for that fact that '100' + '0' is idential to '10' + '00'.
Instead of trying to merge two fields into one, perhaps try the following?
SELECT
*
FROM
#allArticles AS allArticles
WHERE
Artikelnummer = 'IT-810260'
AND NOT EXISTS (SELECT *
FROM KHKPreisListenArtikel
WHERE ListeID = 4
AND Artikelnummer = allArticles.Artikelnummer
AND Auspraegung = allArticles.Auspraegung
)
If that still doesn't work, then you must have corresponding records in that other table, find them like this...
SELECT
*
FROM
#allArticles AS allArticles
INNER JOIN
KHKPreisListenArtikel AS Preis
ON Preis.ListeID = 4
AND Preis.Artikelnummer = allArticles.Artikelnummer
AND Preis.Auspraegung = allArticles.Auspraegung
WHERe
allArticles.Artikelnummer = 'IT-810260'
PLEASE ALSO NOTE
Please don't include images of code, please copy the code, so that we can copy it too.
Especially when the tables/fields are in another language...
EDIT
Here is a query that will show the cause of your original query to fail.
SELECT
*
FROM
#allArticles AS allArticles
INNER JOIN
KHKPreisListenArtikel AS Preis
ON Preis.ListeID = 4
AND Preis.Artikelnummer + CONVERT(VARCHAR(50), Preis.Auspraegung)
=
allArticles.Artikelnummer + CONVERT(VARCHAR(50), allArticles.Auspraegung)
WHERE
allArticles.Artikelnummer = 'IT-810260'

Dynamic column value with group by (sql server and Linq)

i have a table Plan with following sample data
i want to aggregate the result by PlanMonth and for PlanStatus i want that if any of its (in a group) values is Drafted i get drafted in the result and Under Approval otherwise. i have done it using following query
select PlanMonth, case when Flag=1 then 'Drafted' else 'Under Approval' end as PlanStatus
from
(select p.PlanMonth, Max(CASE WHEN p.PlanStatus = 'Drafted' THEN 1 ELSE 0 END) Flag
from Plans p
group by p.PlanMonth
) inquery
i have consulted this blog post. Is there something wrong with it? Moreover, if someone can help me translate it to linq i will be grateful
The query you have will work.
With the sample data you have provided it can be simplified a bit.
select p.PlanMonth,
min(p.PlanStatus) as PlanStatus
from Plans as p
group by p.PlanMonth
This will not work if you have values in PlanStatus that is alphabetically sorted before Drafted.
Following Linq query worked for me
return (from p in db.mktDrVisitPlans
group p by p.PlanMonth into grop
select
new VMVisitPlan
{
PlanMonth = grop.Key
PlanStatus = grop.Any(x=>x.p.PlanStatus == "Drafted")?"Hold":"Under Approval",
}).ToList();

Resources