how to get Data from sqlite table using where Cluase in iphone - iphone

I am selecting data from two tables in normal condition it works fine but if i use where clause it does not return anything.
here is the query
NSString *select =[NSString stringWithFormat:#"SELECT *from ContentMaster As ml LEFT JOIN ContentTagging As cat ON cat.ContentID = ml.ContentID where cat.ContenTagText= %#" , appDelegate.tagInput];
Here is the NSLog of query
SELECT *from ContentMaster As ml LEFT JOIN ContentTagging ON cat.ContentID = ml.ContentID where cat.ContenTagText= Tag1

put your ContenTagText in '' as it is comparing the string
NSString *select =[NSString stringWithFormat:#"SELECT * from ContentMaster As ml LEFT JOIN ContentTagging As cat ON cat.ContentID = ml.ContentID where cat.ContenTagText= \'%#\'" , appDelegate.tagInput];
The query will be:
SELECT *from ContentMaster As ml LEFT JOIN ContentTagging ON cat.ContentID = ml.ContentID where cat.ContenTagText= 'Tag1'
hope it will help.

Related

How to insert data with select query in postgresql?

How to insert data with select query sum? I tried. The result is affected success > Affected rows: 1. but row no add in the table.
CREATE TABLE Clases(Segment1_ID char(4), Segment1_Name varchar(75), CompanyID char(4), amount Float(53));
INSERT INTO Clases (Segment1_ID, Segment1_Name, CompanyID, amount)
select Segment1_ID, Segment1_Name, dataupload1h_companyid CompanyID, sum(case when Segment3_Formula = '+' then dataupload1d_amount else dataupload1d_amount * -1 end) Amount
from t_dataupload1_header
inner join t_dataupload1_detail
on dataupload1h_id = dataupload1d_id
inner join M_Account
on dataupload1d_accountid = Account_ID
and dataupload1h_companyid = Account_CompanyID
inner join M_Segment4
on Account_Segment4ID = Segment4_ID
and dataupload1h_companyid = Segment4_CompanyID
inner join M_Segment3
on Segment4_Segment3ID = Segment3_ID
and dataupload1h_companyid = Segment3_CompanyID
inner join M_Segment2
on Segment3_Segment2ID = Segment2_ID
and dataupload1h_companyid = Segment2_CompanyID
inner join M_Segment1
on Segment2_Segment1ID = Segment1_ID
and dataupload1h_companyid = Segment1_CompanyID
where dataupload1h_companyid = '1000'
group by Segment1_ID, Segment1_Name, dataupload1h_companyid
order by Segment1_ID, Segment1_Name, dataupload1h_companyid;
Please help. Thank you
If the select is returning the desired results then check whether Auto-commit is turned on in PgAdmin(I suppose you are using this) Or put COMMIT; after the insert.

understanding the logic of AND and WHERE in t-sql

I am working on Transact-SQL, Microsoft Azure.
I am trying to understand why in some join queries WHERE and AND both can be used and give the same result (or at least it seemed like the same result in my data base) and in other queries where doesn't work and and does.
Here, I am joining 2 tables:
select c.customerID, ca.addressID, ca.addressType
from salesLT.customer as c
join salesLT.customerADdress as ca
on c.customerID = ca.customerID
WHERE ca.addressType = 'Main Office'; --WHERE or AND works the same
This gives me what I was looking for = only the rows from customerAddress table where the type is 'Main Office'. Changing the word WHERE to AND gives the exact same result.
Now, the query is based on the previous, but I am trying to join 3 tables. Here the WHERE doesn't work and I have to put an AND:
select c.customerId, c.companyName, ca.addressID, ca.addressType, a.addressLine1, a. city
from salesLT.customer as c
join salesLT.customerAddress as ca
on c.customerID = ca.customerID
WHERE ca.addressType = 'Main Office' -- error: incorrect syntax near 'join'
join salesLT.address as a
on ca.addressId = a.addressID;
If I change the WHERE to AND it works. Why? What is the difference between the join of 2 tables and 3?
Thanks
If I change the WHERE to AND it works. Why?
Because using and is adding the condition to the on clause, but using where is a new clause.
Here is the full explanation: The basic syntax of a select statement when using multiple joins is:
SELECT <columns>
FROM <table 1>
JOIN <table 2> ON <condition>
JOIN <table 3> ON <condition>
WHERE <condition>
This means your second query should look like this:
select c.customerId, c.companyName, ca.addressID, ca.addressType, a.addressLine1, a.city
from salesLT.customer as c
join salesLT.customerAddress as ca on c.customerID = ca.customerID
join salesLT.address as a on ca.addressId = a.addressID
WHERE ca.addressType = 'Main Office';
If you will go to the link, you will see that a select statement syntax can get much more complicated - but for now, that basic syntax should be enough to get you started.
The second JOIN should be before the WHERE clause
select c.customerId, c.companyName, ca.addressID, ca.addressType, a.addressLine1, a. city
from salesLT.customer as c
join salesLT.customerAddress as ca
on c.customerID = ca.customerID
join salesLT.address as a
on ca.addressId = a.addressID
WHERE ca.addressType = 'Main Office'
;

Causes of duplicate results appearing in Crystal Reports

I have a crystal report using the SQL command below.
I am wondering if anyone can assist me with the reason why I am getting duplicate people appearing in my report.
At the moment I get multiple lines for a sales account manager (SAM),
I want one line per SAM.
My 1st Group Header is based on the Sector Code,
2nd group header is on command.sector,
3rd group header is on SAM
SELECT DISTINCT
'Actuals' as [Trans Type],
ER101_ACCT_ORDER_DTL.ER101_START_DATE_ISO as [Order Date],
ER101_ACCT_ORDER_DTL.ER101_ENT_DATE_ISO as [Ent Date],
year(dateadd(month,6,ER101_ACCT_ORDER_DTL.ER101_START_DATE_ISO)) as [FYR],
MONTH(DATEADD(M, 6, ER101_ACCT_ORDER_DTL.ER101_START_DATE_ISO)) as [FYP],
DATEADD(mm, DATEDIFF(mm, 0, ER101_ACCT_ORDER_DTL.ER101_START_DATE_ISO), 0) as [PStart],
DATEADD (dd, -1, DATEADD(mm, DATEDIFF(mm, 0, ER101_ACCT_ORDER_DTL.ER101_START_DATE_ISO) + 1, 0)) as [Pend],
Revenue =
Case
When ER101_ACCT_ORDER_DTL.ER101_PHASE = '1' and ER101_ACCT_ORDER_DTL.ER101_COMPL_STS = 'N'
then ER101_ACCT_ORDER_DTL.ER101_EXT_CHRG
When ER101_ACCT_ORDER_DTL.ER101_PHASE = '5' and ER101_ACCT_ORDER_DTL.ER101_COMPL_STS = 'N'
then ER101_ACCT_ORDER_DTL.ER101_EXT_CHRG
Else 0
End,
isnull(MM405_USER_NAME,'No SAM') AS [SAM],
ER101_ACCT_ORDER_DTL.ER101_DESC as [Desc],
EV200_EVENT_MASTER.EV200_EVT_ID as [EventID],
isnull(SALESACCT.EV870_TYPE,'NoSec') as [Sector],
isnull(CCRPF_ACCT_TYPE_DESC,'ZZ - No Sector') as [Sector Name],
EV130_STATUS_MASTER.EV130_STATUS_DESC as [Event Status Desc],
isnull(EV800_USER_FLD_6X,'N') as [Budgeted]
FROM ER101_ACCT_ORDER_DTL WITH (NOLOCK)
LEFT OUTER JOIN EV700_FUNC_MASTER WITH (NOLOCK)
ON ER101_ACCT_ORDER_DTL.ER101_ORG_CODE = EV700_FUNC_MASTER.EV700_ORG_CODE
AND ER101_ACCT_ORDER_DTL.ER101_EVT_ID = EV700_FUNC_MASTER.EV700_EVT_ID
AND ER101_ACCT_ORDER_DTL.ER101_FUNC_ID = EV700_FUNC_MASTER.EV700_FUNC_ID
LEFT OUTER JOIN EV800_SPACE_MASTER WITH (NOLOCK)
ON EV700_FUNC_MASTER.EV700_SPACE = EV800_SPACE_MASTER.EV800_SPACE_CODE
AND EV700_FUNC_MASTER.EV700_ORG_CODE = EV800_SPACE_MASTER.EV800_ORG_CODE
LEFT OUTER JOIN EV200_EVENT_MASTER WITH (NOLOCK)
ON ER101_ACCT_ORDER_DTL.ER101_ORG_CODE = EV200_EVENT_MASTER.EV200_ORG_CODE
AND ER101_ACCT_ORDER_DTL.ER101_EVT_ID = EV200_EVENT_MASTER.EV200_EVT_ID
LEFT OUTER JOIN EV870_ACCT_MASTER WITH (NOLOCK)
ON EV200_EVENT_MASTER.EV200_ORG_CODE = EV870_ACCT_MASTER.EV870_ORG_CODE
AND EV200_EVENT_MASTER.EV200_SLSPER = EV870_ACCT_MASTER.EV870_ACCT_CODE
LEFT OUTER JOIN EV870_ACCT_MASTER SALESACCT WITH (NOLOCK)
ON EV200_EVENT_MASTER.EV200_ORG_CODE = SALESACCT.EV870_ORG_CODE
AND EV200_EVENT_MASTER.EV200_CUST_NBR = SALESACCT.EV870_ACCT_CODE
LEFT OUTER JOIN CCRPF_ACCT_TYPES WITH (NOLOCK)
ON SALESACCT.EV870_TYPE = CCRPF_ACCT_TYPE
LEFT OUTER JOIN MM405_USER_MASTER_EXT WITH (NOLOCK)
ON EV870_ACCT_MASTER.EV870_USERID = MM405_USER_MASTER_EXT.MM405_USER_ID
LEFT OUTER JOIN EV130_STATUS_MASTER WITH (NOLOCK)
ON EV200_EVENT_MASTER.EV200_EVT_STATUS = EV130_STATUS_MASTER.EV130_STATUS_CODE
LEFT OUTER JOIN EV370_RES_MASTER WITH (NOLOCK)
ON ER101_ACCT_ORDER_DTL.ER101_ORG_CODE = EV370_RES_MASTER.EV370_ORG_CODE
AND ER101_ACCT_ORDER_DTL.ER101_NEW_RES_TYPE = EV370_RES_MASTER.EV370_NEW_RES_TYPE
AND ER101_ACCT_ORDER_DTL.ER101_RES_CODE = EV370_RES_MASTER.EV370_RES_CODE
WHERE EV200_ORG_CODE = '10' and EV200_EVT_STATUS in ('33', '34', '36', '49','50', '52') and NOT(EV200_EVT_TYPE IN ('MOS','MP','GB'))
and ER101_RES_CLASS <> '8' and ER101_ORD_TYPE = 'SO'
and not(EV370_RES_MASTER.EV370_MAJOR_GROUP in ('SRVCHR', 'EBOND', 'SCHARG','GIFTC'))
UNION
Select 'Targets' as [Trans Type],
MM012_PERIOD_STDATE as [Order Date],
GL010_ENT_STAMP as [Ent Date],
MM012_FISCAL_YR as [FYR],
MM012_FISCAL_PERIOD as [FYP],
MM012_PERIOD_STDATE as [PStart],
MM012_PERIOD_ENDATE as [Pend],
GL010_BUD_PTD as [Revenue],
'Budget SAM' as [SAM],
GL010_ACCOUNT as [Desc],
null as [Event ID],
GL001_SHORT_DESC as [Sector],
[Sector Name] =
Case
When CCRPF_ACCT_TYPE_DESC is null then 'OLD - ' + GL001_DESC
Else CCRPF_ACCT_TYPE_DESC
End,
'Budget Event Status Desc' as [Event Status Desc],
'Targets' as [Budgeted]
FROM GL010_BALANCES WITH (NOLOCK)
LEFT OUTER JOIN GL001_ACCOUNT_MASTER
WITH (NOLOCK) ON GL010_ORG_CODE = GL001_ORG_CODE
AND GL010_ACCOUNT = GL001_ACCOUNT
LEFT OUTER JOIN MM012_FISCAL_PERIODS
WITH (NOLOCK) ON GL010_ORG_CODE = MM012_ORG_CODE
AND GL010_FISCAL_YR = MM012_FISCAL_YR
AND GL010_FISCAL_PERIOD = MM012_FISCAL_PERIOD
LEFT OUTER JOIN dbo.CCRPF_ACCT_TYPES
WITH (NOLOCK) ON GL001_SHORT_DESC = CCRPF_ACCT_TYPE
Where GL001_ACCT_TYPE = 'SS' and LEFT(GL001_ACCOUNT_MASTER.GL001_ACCOUNT,3)='SEC'

how to do avg() in from count()sql

I have this T-sql to get count for each sell rep to a customer during current month
I want to ask how can apply avg function to get avg visit for current month
SELECT COUNT(VisitTracking.customerID) AS #VISIT,
MAX(VisitTracking.visitID) AS VisitID,
Customers.title AS Title,
Customers.firstName AS [First Name],
Customers.LastName AS [Last Name],
Company.companyName AS [Company Name],
MAX(VisitTracking.DateVisited) AS [Date Visited],
CONVERT(DATE, MAX(VisitTracking.nextVisit)) AS [Next Visit],
Customers.customerID
FROM VisitTracking
INNER JOIN Customers ON VisitTracking.customerID = Customers.customerID
INNER JOIN Customer_Company ON Customers.customerID = Customer_Company.customerID
INNER JOIN Company ON Customer_Company.companyID = Company.companyID
WHERE VisitTracking.DateVisited BETWEEN '11/01/2012' AND '11/31/2012'
GROUP BY Customers.title, Customers.firstName, Customers.LastName, Company.companyName, Customers.customerID
Have you tried compute?
SELECT [sParID], count([sParID]) as '#sID'
FROM [docSVsys]
group by [sParID]
order by [sParID]
compute avg(count([sParID]))
Why not a sub select? Yeah, I know, sub select are NASTY, NOT NICE and don't perform as well. But they do get the job done when producing data for reports and analysis, so, maybe:
SELECT AVG(a.#VISIT) FROM (SELECT COUNT(VisitTracking.customerID) AS #VISIT,
MAX(VisitTracking.visitID) AS VisitID,
Customers.title AS Title,
Customers.firstName AS [First Name],
Customers.LastName AS [Last Name],
Company.companyName AS [Company Name],
MAX(VisitTracking.DateVisited) AS [Date Visited],
CONVERT(DATE, MAX(VisitTracking.nextVisit)) AS [Next Visit],
Customers.customerID
FROM VisitTracking
INNER JOIN Customers ON VisitTracking.customerID = Customers.customerID
INNER JOIN Customer_Company ON Customers.customerID = Customer_Company.customerID
INNER JOIN Company ON Customer_Company.companyID = Company.companyID
WHERE VisitTracking.DateVisited BETWEEN '11/01/2012' AND '11/31/2012'
GROUP BY Customers.title, Customers.firstName, Customers.LastName, Company.companyName, Customers.customerID) AS a

Is it possible to have "not in/exists" in one table then in another table?

I'm now maintaining a big app somebody else wrote mining some data from some big government legacy systems. Basically I need a single query result to populate a gridview that takes each part number from a Tech Order and counts matching part numbers in the Fedlog table. If none found then look in the "commercial" table. The existing query currently only looks in the Fedlog table and reads as follows:
select p.*,
(select case when count(*) > 0 then 'Y' else 'N' end as SL
from tbl_fedlog where [Part Number] = p.[Part Number]) as SL
from tbl_pcms p
where p.[Tech Order] = '0B-E0C-9' order by p.Figure, p.[Index], p.Indenture
When 'N' I've got to look in the commerical table. Could I have some suggestion on the best way to go about this?
What should the result look like for the 'N' scenario - just another 'Y'/'N' answer? If so, you should be able to simply replace the 'N' expression with another scalar query against the "commercial" table.
Here is a UNION ALL between a Fedlog query and a commercial query, that checks each for existence:
SELECT p.*
, SL = 'Y'
, part_count = (SELECT COUNT(*) FROM tbl_fedlog WHERE [Part Number] = p.[Part Number])
from tbl_pcms p
where p.[Tech Order] = '0B-E0C-9'
AND EXISTS(SELECT 1 FROM tbl_fedlog WHERE [Part Number] = p.[Part Number])
UNION ALL
SELECT p.*
, SL = 'Y'
, part_count = (SELECT COUNT(*) FROM tbl_commercial WHERE [Part Number] = p.[Part Number])
from tbl_pcms p
where p.[Tech Order] = '0B-E0C-9'
AND EXISTS(SELECT 1 FROM tbl_commercial WHERE [Part Number] = p.[Part Number])
AND NOT EXISTS(SELECT 1 FROM tbl_fedlog WHERE [Part Number] = p.[Part Number])
I would need more info to address the 'N' scenario mentioned in other answer though.
If the logic is to display 'Y' when the part exists in either the Fedlog or the Commercial table and 'N' otherwise, then you could try grouping & aggregating those tables separately and (outer-)joining the aggregated result sets to tbl_pcms, like this:
SELECT
p.*,
CASE WHEN COALESCE(f.PartCount, c.PartCount) IS NULL THEN 'N' ELSE 'Y' END AS SL
FROM tbl_pcms p
LEFT JOIN (
SELECT
[Part Number],
COUNT(*) AS PartCount
FROM tbl_fedlog
GROUP BY [Part Number]
) f ON p.[Part Number] = f.[Part Number]
LEFT JOIN (
SELECT
[Part Number],
COUNT(*) AS PartCount
FROM tbl_commercial
GROUP BY [Part Number]
) c ON p.[Part Number] = c.[Part Number]
WHERE p.[Tech Order] = '0B-E0C-9'
ORDER BY
p.Figure,
p.[Index],
p.Indenture