Related
I have tried to process select query.
The query works fine in DB browser for SQLite.
However, it returns error when I apply it on C.
int result = sqlite3_prepare_v2(db,
"SELECT _id, name1, name2, name3, num1, type1, num2, typ2, " \
"num3, typ3, num4, type4, num5, type5, " \
"instr(name1||name2||name3, ?), " \
"instr(num1, ?), instr(num2, ?), instr(num3, ?), " \
"instr(num4, ?), instr(num5, ?), " \
"unicode(substr(name1|| name2|| name3, 1, 1)) " \
"FROM table1 " \
"WHERE (name1||name2||name3 LIKE ? " \
"OR name1||name3||name3 LIKE ? " \
"OR REPLACE(name1||name2||name3, ',', '') LIKE ? " \
"OR REPLACE(name1||name2||name3, ',', '') LIKE ? " \
"OR num1 LIKE ? OR name2 LIKE ? " \
"OR num3 LIKE ? OR num4 LIKE ? OR num5 LIKE ?) " \
"ORDER BY CASE " \
"WHEN ((instr(name1, ?) IS 1) AND name3 != '') THEN 1 " \
"WHEN ((instr(name3, ?) IS 1) AND name1 = '') THEN 2 " \
"WHEN ((instr(name1, ?) IS 1) AND name3 = '') THEN 2 " \
"WHEN (instr(name1||' '||name2||' '||name3, ?) IS 1) THEN 3 " \
"WHEN (instr(name1||', '||name2||', '||name3, ?) IS 1) THEN 3 " \
"ELSE 4 END, UPPER(name1||name2||name3) LIMIT 1000 " \
"collate KOREA",
-1,
&main_stmt,
0
);
char LIKEsearchWord1[52];
char LIKEsearchWord2[52];
memset(LIKEsearchWord1, '\0', 52);
memset(LIKEsearchWord2, '\0', 52);
LIKEsearchWord1[0] = '%';
LIKEsearchWord2[0] = '%';
strcat(LIKEsearchWord1, searchWord1);
strcat(LIKEsearchWord2, searchWord2);
strcat(LIKEsearchWord1, "%");
strcat(LIKEsearchWord2, "%");
if(result == SQLITE_OK)
{
sqlite3_bind_text(main_stmt, 0, searchWord1, -1, SQLITE_STATIC);
sqlite3_bind_text(main_stmt, 1, searchWord2, -1, SQLITE_STATIC);
sqlite3_bind_text(main_stmt, 2, searchWord2, -1, SQLITE_STATIC);
sqlite3_bind_text(main_stmt, 3, searchWord2, -1, SQLITE_STATIC);
sqlite3_bind_text(main_stmt, 4, searchWord2, -1, SQLITE_STATIC);
sqlite3_bind_text(main_stmt, 5, searchWord2, -1, SQLITE_STATIC);
sqlite3_bind_text(main_stmt, 6, searchWord1, -1, SQLITE_STATIC);
sqlite3_bind_text(main_stmt, 7, searchWord1, -1, SQLITE_STATIC);
sqlite3_bind_text(main_stmt, 8, searchWord1, -1, SQLITE_STATIC);
sqlite3_bind_text(main_stmt, 9, searchWord1, -1, SQLITE_STATIC);
sqlite3_bind_text(main_stmt, 10, searchWord2, -1, SQLITE_STATIC);
sqlite3_bind_text(main_stmt, 11, searchWord2, -1, SQLITE_STATIC);
sqlite3_bind_text(main_stmt, 12, searchWord2, -1, SQLITE_STATIC);
sqlite3_bind_text(main_stmt, 13, searchWord2, -1, SQLITE_STATIC);
sqlite3_bind_text(main_stmt, 14, searchWord2, -1, SQLITE_STATIC);
sqlite3_bind_text(main_stmt, 15, searchWord1, -1, SQLITE_STATIC);
sqlite3_bind_text(main_stmt, 16, searchWord1, -1, SQLITE_STATIC);
sqlite3_bind_text(main_stmt, 17, searchWord1, -1, SQLITE_STATIC);
sqlite3_bind_text(main_stmt, 18, searchWord1, -1, SQLITE_STATIC);
sqlite3_bind_text(main_stmt, 19, searchWord1, -1, SQLITE_STATIC);
result = sqlite3_step(main_stmt);
if(result != SQLITE_OK)
{
printf(SQL error: "%s", sqlite3_errmsg(db));
}
}
Above code is what I have tested in C.
The words for LIKE are correct with both % at start and end.
However, the result of sqlite3_stpe(main_stmt) is unkown error without any further information.
I don't know what is wrong.
Even though I make the query one line without backslash, it has same error.
I have a stored procedure that is getting data from some other database tables and and putting them into another table. I am running into an error
Conversion failed when converting the varchar value 'Email' to data type int.
However, I don't see where/how a data conversion would take place.
I have checked the data types for both where I am getting the email address and where I am inserting the email address. They are both set to varchar(255).
DECLARE #prefix varchar(50), #middle varchar(50), #suffix varchar(50),
#companyID int, #companyNm varchar(100),
#badge varchar(50), #title varchar(100), #salutation varchar(50),
#pplType int, #email varchar(255), #specialty int,
#mailpref int, #source int,
#add1 varchar(100), #add2 varchar(100), #city varchar(50),
#state varchar(50), #zip varchar(50), #countryID int,
#phone varchar(50), #ext varchar(50), #fax varchar(50),
#addID int, #addType int, #arrival datetime, #departure datetime,
#regType varchar(50), #mtRegID int
SELECT #regType = regType
FROM Registration
WHERE personID = #personID AND meetingCode = #meetingCode
SELECT
#prefix = [Name Prefix], #middle = Middle, #suffix = [Name Suffix],
#companyID = [Company ID], #companyNm = [Company Name],
#badge = [Badge Name], #title = Title,
#salutation = Salutation, #pplType = [People Type],
#email = Email, #specialty = [Specialty ID], #mailpref = MailPref,
#source = [Source ID]
FROM
MeetingtrakTest.dbo.tblPeople
WHERE
[Person ID] = #personID
SELECT
#add1 = Address1, #add2 = Address2, #city = City, #state = State,
#zip = Zip, #countryID = [Country Id],
#phone = Phone, #ext = Extension, #fax = Fax, #addID = [Address ID]
FROM
MeetingtrakTest.dbo.tblPeopleAddresses
WHERE
[Person ID] = #personID AND [Primary] = 1
SELECT
#arrival = [Meeting Start Date], #departure = [Meeting End Date]
FROM
MeetingtrakTest.dbo.tblMeetings
WHERE
[Meeting Code] = #meetingCode
INSERT INTO tblRegistration ([Person Id], [Pre or On-Site], [Attendee List], [Use Company Name], [Reg Date], [Use Housing Costs], [Use Itinerary Costs], Attended, [Date Entered], [Entered By], [Date Updated], [First Name], [Last Name], [Meeting Code], [Registration Code], [Name Prefix], Middle, [Name Suffix], [Company ID], [Company Name], [Badge Name], Title, Salutation, [People Type], [Email], [Specialty ID], MailPref, Source, [Address 1], [Address 2], City, State, Zip, [Country ID], Phone, Extension, Fax, [Address Id] ,[Address Type], [Arrival Date], [Departure Date])
VALUES (#personID, 1, 1, 1, GETDATE(), 0, 0, 0, GETDATE(), 'etrak', GETDATE(), #first, #last, #meetingCode, #regType, #prefix, #middle, #suffix, #companyID, #companyNm, #badge, #title, #salutation, #pplType, #email, #specialty, #mailpref, #source, #add1, #add2, #city, #state, #zip, #countryID, #phone, #ext, #fax, #addID, #addType, #arrival, #departure);
SET #mtRegID = SCOPE_IDENTITY();
I am having three columns,- and / should be used as separator result should come in new column(File Index-sub Index/Year),if sub index is blank then result should be (File Index/Year).
SELECT
[ File Index].[File Index]
, [ File Index].[Sub Index]
, [ File Index].[Financial Year]
, [ File Index].[File Index] & [Sub Index] & [Financial Year] AS [Composite Index]
FROM [File Index];
Since you have both MySQL and MS Access listed, here are solutions for both.
For MySQL, you can use a CASE statement for this:
SELECT [File Index], [Sub Index], [Financial Year],
CASE WHEN [Sub Index] IS NOT NULL
THEN Concat([File Index], '-', [Sub Index], '/', [Financial Year])
ELSE Concat([File Index], '/', [Financial Year])
END as [Composite Index]
FROM [File Index];
For MS Access, you will use Switch(...):
SELECT [File Index], [Sub Index], [Financial Year],
Switch(Not IsNull([Sub Index]),
[File Index] & '-' & [Sub Index] & '/' & [Financial Year],
IsNull([Sub Index]),
[File Index] & '/' & [Financial Year]
) as [Composite Index]
FROM [File Index];
You can use IIF function: IIF(Condition;ConcatenationIfTrue;ConcatenationIfFalse)
SELECT
[ File Index].[File Index]
, [ File Index].[Sub Index]
, [ File Index].[Financial Year]
, IIF(ISNULL([Sub Index];[ File Index].[File Index] & "/" & [Financial Year];[ File Index].[File Index] & [Sub Index] & [Financial Year]) AS [Composite Index]
FROM [File Index];
I have the following query
SELECT [Acct], [Customer], [Code], [Description], [Value], [Sale Person],
[Region], [Store Type], ISNULL([2016], 0) AS '2016', ISNULL([2017],0) AS
'2017'
FROM
(
SELECT CUCODE AS 'Acct', CUNAME AS 'Customer', STKCODE AS 'Code', STKNAME AS 'Description', SUM(OD_QTYORD) AS 'Packs Ordered'
, OD_GROSS AS 'Value', CUSORT AS 'Sale Person', LEFT(CUUSER1,3) AS 'Region', DATEPART(yyyy, OD_DATE) AS 'Year', OH_USER2 AS 'Store Type'
FROM STK_STOCK
INNER JOIN ORD_DETAIL ON STK_STOCK.STKCODE=ORD_DETAIL.OD_STOCK_CODE
INNER JOIN ORD_HEADER ON OD_ORDER_NUMBER=OH_ORDER_NUMBER
INNER JOIN SL_ACCOUNTS ON OH_ACCOUNT=CUCODE
WHERE STKCODE IN ('76958', '27225', '27221', '26962', '26959', '26961', '27226', '26963', '26960')
AND OD_QTYORD > 0
AND CUCODE != 'Z9997'
GROUP BY CUCODE, CUNAME, STKCODE, STKNAME, OD_GROSS, CUSORT, CUUSER1, OD_DATE, OH_USER2
) AS SourceTable
PIVOT
(
SUM([Packs Ordered])
FOR [Year] IN ([2016], [2017])
) AS PivotTable;
The output looks great but I want a subtotal for each 'Acct' and a Grand Total for the whole thing.
Can anyone help with this?
You can use group by grouping sets as below:
SELECT [Acct], [Customer], [Code], [Description], [Value], [Sale Person],
[Region], [Store Type], sum([2016]) as [2016], sum([2017]) as [2017] FROM
(
SELECT [Acct], [Customer], [Code], [Description], [Value], [Sale Person],
[Region], [Store Type], ISNULL([2016], 0) AS '2016', ISNULL([2017],0) AS
'2017'
FROM
(
SELECT CUCODE AS 'Acct', CUNAME AS 'Customer', STKCODE AS 'Code', STKNAME AS 'Description', SUM(OD_QTYORD) AS 'Packs Ordered'
, OD_GROSS AS 'Value', CUSORT AS 'Sale Person', LEFT(CUUSER1,3) AS 'Region', DATEPART(yyyy, OD_DATE) AS 'Year', OH_USER2 AS 'Store Type'
FROM STK_STOCK
INNER JOIN ORD_DETAIL ON STK_STOCK.STKCODE=ORD_DETAIL.OD_STOCK_CODE
INNER JOIN ORD_HEADER ON OD_ORDER_NUMBER=OH_ORDER_NUMBER
INNER JOIN SL_ACCOUNTS ON OH_ACCOUNT=CUCODE
WHERE STKCODE IN ('76958', '27225', '27221', '26962', '26959', '26961', '27226', '26963', '26960')
AND OD_QTYORD > 0
AND CUCODE != 'Z9997'
GROUP BY CUCODE, CUNAME, STKCODE, STKNAME, OD_GROSS, CUSORT, CUUSER1, OD_DATE, OH_USER2
) AS SourceTable
PIVOT
(
SUM([Packs Ordered])
FOR [Year] IN ([2016], [2017])
) AS PivotTable
) a
group by GROUPING SETS ( [Acct], [Customer], [Code], [Description], [Value], [Sale Person],
[Region], [Store Type] )
ORDER BY [Acct], [Customer], [Code], [Description], [Value], [Sale Person],
[Region], [Store Type];
Based on Kannan Kandasamy's reply I got this which worked beautifully:
SELECT (CASE
WHEN GROUPING([Acct])=0 AND
GROUPING([Customer]) = 1 AND
GROUPING([Code]) = 1 AND
GROUPING([Description]) = 1 AND
GROUPING([Sale Person]) = 1 AND
GROUPING([Region]) = 1 AND
GROUPING([Store Type]) = 1
THEN 'Total '+ [Acct]
WHEN GROUPING([Acct])=1 AND
GROUPING([Customer]) = 1 AND
GROUPING([Code]) = 1 AND
GROUPING([Description]) = 1 AND
GROUPING([Sale Person]) = 1 AND
GROUPING([Region]) = 1 AND
GROUPING([Store Type]) = 1
THEN 'Total'
ELSE [Acct]
END) AS Acct , ISNULL([Customer],'') AS 'Customer', ISNULL([Code],'') AS 'Code', ISNULL([Description],'') AS 'Description', SUM(ISNULL([Value],'')) AS 'Value', ISNULL([Sale Person],'') AS 'Sales Person',
ISNULL([Region],'') AS 'Region', ISNULL([Store Type],'') AS 'Store Type', SUM([2016]) AS '2016', SUM([2017]) AS '2017'
FROM
(
SELECT [Acct], [Customer], [Code], [Description], [Value], [Sale Person],
[Region], [Store Type], ISNULL([2016], 0) AS '2016', ISNULL([2017],0) AS
'2017'
FROM
(
SELECT CUCODE AS 'Acct', CUNAME AS 'Customer', STKCODE AS 'Code', STKNAME AS 'Description', SUM(OD_QTYORD) AS 'Packs Ordered'
, OD_GROSS AS 'Value', CUSORT AS 'Sale Person', LEFT(CUUSER1,3) AS 'Region', DATEPART(yyyy, OD_DATE) AS 'Year', OH_USER2 AS 'Store Type'
FROM STK_STOCK
INNER JOIN ORD_DETAIL ON STK_STOCK.STKCODE=ORD_DETAIL.OD_STOCK_CODE
INNER JOIN ORD_HEADER ON OD_ORDER_NUMBER=OH_ORDER_NUMBER
INNER JOIN SL_ACCOUNTS ON OH_ACCOUNT=CUCODE
WHERE STKCODE IN ('76958', '27225', '27221', '26962', '26959', '26961', '27226', '26963', '26960')
AND OD_QTYORD > 0
AND CUCODE != 'Z9997'
GROUP BY CUCODE, CUNAME, STKCODE, STKNAME, OD_GROSS, CUSORT, CUUSER1, OD_DATE, OH_USER2
) AS SourceTable
PIVOT
(
SUM([Packs Ordered])
FOR [Year] IN ([2016], [2017])
) AS PivotTable
) a
group by GROUPING SETS (( [Acct], [Customer], [Code], [Description], [Sale Person],
[Region], [Store Type]),([Acct]),());
I am having a hard time to identify (and fix) the grouping issue I have with the following query. My data has some Country fields blank and the query is ignoring them, only those with non blank values are converted to rows. I tried removing the country grouping but I get the error "You tried to execute a query that does not include the specified expression "Country" as part of an aggregate function". This is the query I have in Access:
TRANSFORM Count([passed courses].[Course Number])
SELECT [domain].[Account ID], [domain].[Account Name], [passed courses].[Learner Email Address], [passed courses].Category, [passed courses].Country, [passed courses].[Earliest Start_Date], [passed courses].[Latest End_Date]
FROM [Domain Master] AS [domain] INNER JOIN (SELECT d3.ID, d3.[Learner Email Address], d3.[Course Number], d3.[Transcript Status], d3.Domain, c2.Category, [passed levels].Country, [passed levels].[Earliest Start_Date], [passed levels].[Latest End_Date]
FROM
(SELECT [completed courses].[Learner Email Address], [completed courses].[Level], [completed courses].Category, [completed courses].[Completed Count], [completed courses].Country, [learner dates].[Earliest Start_Date], [learner dates].[Latest End_Date]
FROM
(SELECT [courses taken].[Learner Email Address], [courses taken].[Level], [courses taken].Category, Count([courses taken].ID) AS [Completed Count], [courses taken].Country
FROM
(SELECT d1.ID, d1.[Learner Email Address], d1.[Course Number], c1.[Level], c1.Category, d1.[Transcript Status], d1.Country
FROM [Data] AS d1
INNER JOIN [Courses] AS c1 ON d1.[Course Number] = c1.[Course Number]
)
AS [courses taken]
WHERE [courses taken].[Transcript Status] = "Completed"
GROUP BY [courses taken].[Learner Email Address], [courses taken].Level, [courses taken].Category, d1.Country
)
AS [completed courses]
INNER JOIN
(SELECT d2.[Learner Email Address], d2.Country, Min(d2.Start_Date) AS [Earliest Start_Date], Max(d2.End_Date) AS [Latest End_Date]
FROM [Data] AS d2
GROUP BY d2.[Learner Email Address], d2.Country
) AS [learner dates]
ON [completed courses].[Learner Email Address] = [learner dates].[Learner Email Address] AND
[completed courses].Country = [learner dates].Country
WHERE
([courses taken].Category = 'Sales') AND (
(([completed courses].Level = 1) AND ([completed courses].[Completed Count] >=5)) OR
(([completed courses].Level = 2) AND ([completed courses].[Completed Count] >=5)) OR
(([completed courses].Level = 3) AND ([completed courses].[Completed Count] >=1))
) OR
([courses taken].Category = 'Purchase') AND (
(([completed courses].Level = 1) AND ([completed courses].[Completed Count] >=5)) OR
(([completed courses].Level = 2) AND ([completed courses].[Completed Count] >=4)) OR
(([completed courses].Level = 3) AND ([completed courses].[Completed Count] >=1))
)
)
AS [passed levels]
INNER JOIN ([Data] AS d3 INNER JOIN Courses AS c2 ON ((d3.[Course Number] = c2.[Course Number])))
ON ([passed levels].Level = c2.Level) AND ([passed levels].[Learner Email Address] = d3.[Learner Email Address]) AND ([passed levels].Category = c2.Category)
) AS [passed courses] ON [domain].[Partner Domain] = [passed courses].Domain
GROUP BY [domain].[Account ID], [domain].[Account Name], [passed courses].[Learner Email Address], [passed courses].Category, [passed courses].Country, [passed courses].[Earliest Start_Date], [passed courses].[Latest End_Date]
PIVOT [passed courses].[Course Number];
Any help is appreciated!
What if you added a case statement into your query where if country = '' then return a unique GUID else return the Country column value and alias the case as Country
GUID function for access
You may need to wrap the whole query as a sub query to group by the Country aliased column though. This isn't tested, just a concept idea as I can't see any other way of grouping a country and ignoring blanks within the same select, or a less you did 2 selects and did a union instead.
Regards
Liam