SQLITE multiple table join with a condition - sqlite

I have these tables:
doodhiya
dhid INTEGER PRIMARY KEY NOT NULL,
dname TEXT NOT NULL,
dfname TEXT NOT NULL,
dage INTEGER NOT NULL,
dadd TEXT,
dphone INTEGER NOT NULL,
demail TEXT NOT NULL
doodhdata
dtid INTEGER PRIMARY KEY NOT NULL,
ddate INTEGER NOT NULL,
dmonth INTEGER NOT NULL,
dyear INTEGER NOT NULL,
dmilk INTEGER NOT NULL,
dprice INTEGER NOT NULL default 35 ,
dmore TEXT,
ddhid INTEGER NOT NULL
pricemilk
pid INTEGER PRIMARY KEY NOT NULL,
pmonth INTEGER NOT NULL,
pyear INTEGER NOT NULL,
milkprice INTEGER NOT NULL,
typeperson TEXT,
userid INTEGER,
gheeprice INTEGER,
defaultprice TEXT
cashdata
cashid INTEGER PRIMARY KEY NOT NULL,
cashdate INTEGER NOT NULL,
cashmonth INTEGER NOT NULL,
cashyear INTEGER NOT NULL,
cashamount INTEGER NOT NULL,
uid INTEGER NOT NULL,
utype TEXT NOT NULL,
cashtype TEXT NOT NULL,
cashmore TEXT
I want to make a monthly bill and I am succeed buy using it... but in a bill how can i show last month balance....I am trying to use it
SELECT
ddhid, dmonth, dyear, dmilk,
userid, pmonth, pyear, milkprice,
uid, cashmonth, cashyear, cashamount, utype,
SUM(dmilk) AS totalmilk,
SUM(dmilk*milkprice) AS totalamount,
SUM(cashamount) AS totalcash
FROM
doodhdata
LEFT JOIN pricemilk ON (
doodhdata.ddhid = pricemilk.userid
AND doodhdata.dmonth = pricemilk.pmonth
AND doodhdata.dyear = pricemilk.pyear
)
LEFT JOIN cashdata ON (
doodhdata.ddhid = cashdata.uid
AND doodhdata.dmonth = cashdata.cashmonth
AND doodhdata.dyear = cashdata.cashyear
)
WHERE
dmonth > '$mikdatem'
AND dyear='$mikdatey'
AND ddhid='$dhid'
But I want to use defaultprice when milkprice is NULL....how it is possible...?

Use
COALESCE(milkprice, defaultprice)
in place of milkprice in your query.
See SQLite core functions documentation.

Related

How can I modify this script to delete the records instead of showing them?

WITH CTE AS (
SELECT
user_id,
ip_addr,
MIN(reg_date) AS min_reg_date
FROM
user_info
GROUP BY
ip_addr
HAVING
COUNT(*) > 1
)
SELECT
users.id
FROM
users
JOIN user_info
ON users.id = users_info.user_id
JOIN CTE
ON user_info.ip_addr = CTE.ip_addr
AND user_info.reg_date > CTE.min_reg_date;
user_info table:
CREATE TABLE `user_info` (
`user_id` int(11) UNSIGNED NOT NULL,
`ip_addr` varchar(255) DEFAULT NULL,
`user_agent` varchar(255) DEFAULT NULL,
`reg_date` timestamp NOT NULL DEFAULT current_timestamp()
)
users table:
CREATE TABLE `users` (
`id` int(11) UNSIGNED NOT NULL,
`first_name` varchar(50) NOT NULL,
`last_name` varchar(50) NOT NULL,
`nickname` varchar(50) NOT NULL,
`email` varchar(100) NOT NULL,
`password` varchar(255) NOT NULL,
`avatar` varchar(255) NOT NULL,
`group_id` int(11) UNSIGNED DEFAULT NULL
)
I tried using EXISTS and IN keywords but it shows that the error is near "DELETE FROM..."
Should i change something else ? Please help.

How do I get my kernel execution times from the sqlite3 output of Nsight-systems?

Suppose I use NSight Systems to profile my program, and create an SQLite 3 database, as follows:
nsys profile -o /path/to/db --export=sqlite /path/to/executable --arg1=val1 --arg2
What exactly do I do now to obtain the execution times of my various kernel invocations?
The CUPTI documentation (for CUDA 11.2) says:
3.29. CUpti_ActivityKernel4 Struct Reference [CUPTI Activity API]
This activity record represents a kernel execution (CUPTI_ACTIVITY_KIND_KERNEL and CUPTI_ACTIVITY_KIND_CONCURRENT_KERNEL).
And these are two names of tables in the SQLite3 output DB. Here's how to query them:
If you just want the execution times:
sqlite3 -csv /path/to/db.sqlite 'SELECT end-start AS duration FROM CUPTI_ACTIVITY_KIND_KERNEL;'
If you also want the (demangled) kernel names, you'll need a more complex SQL query:
sqlite3 -csv /path/to/db.sqlite 'SELECT names.value AS name, end - start FROM CUPTI_ACTIVITY_KIND_KERNEL AS k JOIN StringIds AS names ON k.demangledName = names.id;'
It is also educational to run:
sqlite3 /path/to/db.sqlite
and then enter
.schema
to get the SQL creation command for all table in the schema. That would typically look like the following (with CUDA 11.2 and nsys 2020.4.3):
sqlite> .schema
CREATE TABLE StringIds (id INTEGER NOT NULL PRIMARY KEY, value TEXT NOT NULL);
CREATE TABLE ProcessStreams (globalPid INTEGER NOT NULL, filenameId INTEGER NOT NULL, contentId INTEGER NOT NULL);
CREATE TABLE SCHED_EVENTS (start INTEGER NOT NULL, cpu INTEGER NOT NULL, isSchedIn INTEGER NOT NULL, globalTid INTEGER);
CREATE TABLE COMPOSITE_EVENTS (id INTEGER NOT NULL PRIMARY KEY, start INTEGER NOT NULL, cpu INTEGER, threadState INTEGER, globalTid INTEGER, cpuCycles INTEGER NOT NULL);
CREATE TABLE UnwindMethodType (number INTEGER PRIMARY KEY, name TEXT NOT NULL);
CREATE TABLE SAMPLING_CALLCHAINS (id INTEGER NOT NULL REFERENCES COMPOSITE_EVENTS, symbol INTEGER NOT NULL, module INTEGER NOT NULL, kernelMode INTEGER, thumbCode INTEGER, unresolved INTEGER, specialEntry INTEGER, originalIP INTEGER, unwindMethod INTEGER REFERENCES UnwindMethodType(number), stackDepth INTEGER NOT NULL, PRIMARY KEY (id, stackDepth));
CREATE TABLE PROFILER_OVERHEAD (start INTEGER NOT NULL, end INTEGER NOT NULL, globalTid INTEGER, correlationId INTEGER, nameId INTEGER NOT NULL, returnValue INTEGER NOT NULL);
CREATE TABLE OSRT_API (start INTEGER NOT NULL, end INTEGER NOT NULL, eventClass INTEGER NOT NULL, globalTid INTEGER, correlationId INTEGER, nameId INTEGER NOT NULL, returnValue INTEGER NOT NULL, nestingLevel INTEGER, callchainId INTEGER NOT NULL);
CREATE TABLE OSRT_CALLCHAINS (id INTEGER NOT NULL, symbol INTEGER NOT NULL, module INTEGER NOT NULL, kernelMode INTEGER, thumbCode INTEGER, unresolved INTEGER, specialEntry INTEGER, originalIP INTEGER, unwindMethod INTEGER REFERENCES UnwindMethodType(number), stackDepth INTEGER NOT NULL, PRIMARY KEY (id, stackDepth));
CREATE TABLE CUPTI_ACTIVITY_KIND_RUNTIME (start INTEGER NOT NULL, end INTEGER NOT NULL, eventClass INTEGER NOT NULL, globalTid INTEGER, correlationId INTEGER, nameId INTEGER NOT NULL, returnValue INTEGER NOT NULL, callchainId INTEGER REFERENCES CUDA_CALLCHAINS(id));
CREATE TABLE CUPTI_ACTIVITY_KIND_MEMCPY (start INTEGER NOT NULL, end INTEGER NOT NULL, deviceId INTEGER NOT NULL, contextId INTEGER NOT NULL, streamId INTEGER NOT NULL, correlationId INTEGER, globalPid INTEGER, bytes INTEGER NOT NULL, copyKind INTEGER NOT NULL, deprecatedSrcId INTEGER, srcKind INTEGER, dstKind INTEGER, srcDeviceId INTEGER, srcContextId INTEGER, dstDeviceId INTEGER, dstContextId INTEGER, graphNodeId INTEGER);
CREATE TABLE CUPTI_ACTIVITY_KIND_SYNCHRONIZATION (start INTEGER NOT NULL, end INTEGER NOT NULL, deviceId INTEGER NOT NULL, contextId INTEGER NOT NULL, streamId INTEGER NOT NULL, correlationId INTEGER, globalPid INTEGER, syncType INTEGER NOT NULL, eventId INTEGER NOT NULL);
CREATE TABLE CUPTI_ACTIVITY_KIND_KERNEL (start INTEGER NOT NULL, end INTEGER NOT NULL, deviceId INTEGER NOT NULL, contextId INTEGER NOT NULL, streamId INTEGER NOT NULL, correlationId INTEGER, globalPid INTEGER, demangledName INTEGER NOT NULL, shortName INTEGER NOT NULL, launchType INTEGER, cacheConfig INTEGER, registersPerThread INTEGER NOT NULL, gridX INTEGER NOT NULL, gridY INTEGER NOT NULL, gridZ INTEGER NOT NULL, blockX INTEGER NOT NULL, blockY INTEGER NOT NULL, blockZ INTEGER NOT NULL, staticSharedMemory INTEGER NOT NULL, dynamicSharedMemory INTEGER NOT NULL, localMemoryPerThread INTEGER NOT NULL, localMemoryTotal INTEGER NOT NULL, gridId INTEGER NOT NULL, sharedMemoryExecuted INTEGER, graphNodeId INTEGER);
CREATE TABLE ThreadNames (nameId INTEGER NOT NULL, priority INTEGER, globalTid INTEGER);
CREATE TABLE TARGET_INFO_CUDA_GPU (vmId INTEGER NOT NULL, name TEXT NOT NULL, pciBusId TEXT, globalMemoryBandwidth INTEGER NOT NULL, globalMemorySize INTEGER NOT NULL, constantMemorySize INTEGER NOT NULL, l2CacheSize INTEGER NOT NULL, numThreadsPerWarp INTEGER NOT NULL, coreClockRate INTEGER NOT NULL, numMemcpyEngines INTEGER NOT NULL, numMultiprocessors INTEGER NOT NULL, maxIPC INTEGER NOT NULL, maxWarpsPerMultiprocessor INTEGER NOT NULL, maxBlocksPerMultiprocessor INTEGER NOT NULL, maxRegistersPerBlock INTEGER NOT NULL, maxSharedMemoryPerBlock INTEGER NOT NULL, maxThreadsPerBlock INTEGER NOT NULL, maxBlockDimX INTEGER NOT NULL, maxBlockDimY INTEGER NOT NULL, maxBlockDimZ INTEGER NOT NULL, maxGridDimX INTEGER NOT NULL, maxGridDimY INTEGER NOT NULL, maxGridDimZ INTEGER NOT NULL, computeCapabilityMajor INTEGER NOT NULL, computeCapabilityMinor INTEGER NOT NULL, deviceId INTEGER NOT NULL, pid INTEGER, maxSharedMemoryPerMultiprocessor INTEGER, maxRegistersPerMultiprocessor INTEGER);
CREATE TABLE TARGET_INFO_GPU (vmId INTEGER NOT NULL, deviceId INTEGER NOT NULL, name TEXT, busLocation TEXT, isDiscrete INTEGER);
CREATE TABLE TARGET_INFO_CUDA_NULL_STREAM (streamId INTEGER NOT NULL, hwId INTEGER NOT NULL, vmId INTEGER NOT NULL, processId INTEGER NOT NULL, deviceId INTEGER NOT NULL, contextId INTEGER NOT NULL);
CREATE TABLE TARGET_INFO_CUDA_STREAM (streamId INTEGER NOT NULL, hwId INTEGER NOT NULL, vmId INTEGER NOT NULL, processId INTEGER NOT NULL, contextId INTEGER NOT NULL, priority INTEGER NOT NULL, flag INTEGER NOT NULL);
And you can apply any SQL query to this (in SQLite's dialect of course).

create new table - missing parenthesis

need to create this but its saying missing left parenthesis?
CREATE TABLE
active_units2(agency_code varchar2(10) not null,
unit varchar2(10) not null,
supp_unit_1 varchar2(10) not null,
supp_unit_2 varchar2(10) not null,
supp_unit_3 varchar2(10) not null,
supp_reqmt varchar2(10) not null,
alt_priority varchar2(1) not null,
alt_group varchar2(1) not null,
crew_type varchar2(10) not null,
status_control varchar2(1) not null,
onduty_status varchar2(10) not null,
dependant_res_type varchar2(10) not null,
mdt_state varchar2(10) not null,
brigade int not null,
node int not null,
port int not null,
breathing_apparatus int not null,
manual_crewing int not null,
udf1 varchar2(10) not null,
udf2 varchar2(10) not null,
udf3 varchar2(10) not null,
udf4 varchar2(10) not null,
CONSTRAINT active_units2_PK PRIMARY KEY CLUSTERED (agency_code ,unit))
When you run that statement in SQL*Plus the output you get is:
CONSTRAINT active_units2_PK PRIMARY KEY CLUSTERED (agency_code ,unit))
*
ERROR at line 24:
ORA-00906: missing left parenthesis
The * indicates where the problem is - not always helpful but it is in this case. The CLUSTERED key word isn't valid in Oracle, as you can see from the syntax diagram, so you just need to remove that word.

i cannot call null data row in sqlite

Why I cannot call null data row when I use IS NULL in sqlite
select roomkeyID from roomkey where dateOfReturn is null;
CREATE TABLE ROOMKEY (
roomkeyID VARCHAR(10) not null primary key,
roomID VARCHAR(10) not null,
keyID VARCHAR(10) not null,
dateOfIssue DATETIME,
dateOfReturn DATETIME,
FOREIGN KEY (roomID) REFERENCES ROOM(roomID),
FOREIGN KEY (keyID ) REFERENCES KEY(keyID )
)
"RK000001","R106","K0003","12/17/2015"," "
"RK000090","R101","K0002","12/12/2015"," "
From the example it seems that in the field dateOfReturn you do not have NULL values, but strings with a space inside. If this is true then you have to change your query to:
select roomkeyID from roomkey where dateOfReturn is null or dateOfReturn = ' '

insert statement conflicted with the foreign key constraint?

my stored procedure is-
CREATE PROCEDURE [dbo].[usp_SetMenu](
#locationId BIGINT,
#menuId BIGINT = NULL,
#name VARCHAR(100) = NULL,
#taxable BIT = NULL,
#type VARCHAR(100) = NULL,
#dateFrom DATETIME = NULL,
#dateTo DATETIME = NULL,
#timeFrom VARCHAR(10) = NULL,
#timeTo VARCHAR(10) = NULL,
#price MONEY = NULL,
#discountPerc FLOAT = NULL,
#discTimeFrom VARCHAR(10) = NULL,
#discTimeTo VARCHAR(10) = NULL,
#textcolor varchar(10) = null,
#bodycolor varchar(10) = null,
#createdBy BIGINT = NULL,
#createdOn DATETIME = NULL,
#modifiedBy BIGINT = NULL,
#modifiedOn DATETIME = NULL,
#menuProductsXML NTEXT = NULL ,
#IsCopy VARCHAR (10) = NULL,
#CopyMenuId BIGINT = NULL,
#menuTaxXML NTEXT = NULL ,
#menuExists INT = NULL OUTPUT,
#newMenuId INT = NULL OUTPUT
)
AS
SET NOCOUNT ON
---------------------------------------------------------------------
-- Declarations of variables
---------------------------------------------------------------------
DECLARE #ptrHandle INT
---------------------------------------------------------------------
-- initialize variables
---------------------------------------------------------------------
---------------------------------------------------------------------
-- get the data
---------------------------------------------------------------------
IF(#menuId IS NULL) -- If menuid is null then create a new record
BEGIN
select #menuExists = count('x') from tblMenu
where [name] = #name and isDeleted = 0 and locationid=#locationId
if #menuExists > 0
Return
INSERT INTO tblMenu
(locationid
,[name]
,[type]
,taxable
,datefrom
,dateto
,timefrom
,timeto
,price
,discountperc
,disctimefrom
,disctimeto
,bodycolor
,textcolor
,createdby
,createdon)
VALUES
(#locationId
,#name
,#type
,#taxable
,#dateFrom
,#dateTo
,#timeFrom
,#timeTo
,#price
,#discountPerc
,#discTimeFrom
,#discTimeTo
,#bodycolor
,#textcolor
,#createdBy
,#createdOn)
SET #menuId = ##IDENTITY
END
ELSE -- If menuid is not null then update that record
select #menuExists = count('x') from tblMenu
where [name] = #name and MenuId <> #menuId and isDeleted = 0 and locationid=#locationId
if #menuExists > 0
Return
UPDATE tblMenu
SET locationid = #locationId
,[name] = #name
,[type] = #type
,taxable = #taxable
,datefrom = #dateFrom
,dateto = #dateTo
,timefrom = #timeFrom
,timeto = #timeTo
,price = #price
,discountperc = #discountPerc
,disctimefrom = #discTimeFrom
,disctimeto = #discTimeTo
,bodycolor = #bodycolor
,textcolor = #textcolor
,modifiedby = #modifiedBy
,modifiedon = #modifiedOn
WHERE menuid = #menuId
-- if menu product collection is passed then insert new records
IF(#menuProductsXML IS NOT NULL)
BEGIN
-- Clearing the old menu products and inserting new ones
DELETE tblMenuProduct WHERE menuid = #menuId
EXEC sp_xml_preparedocument #ptrHandle OUTPUT, #menuProductsXML
INSERT INTO tblMenuProduct
(menuid
,productid
,categoryid
,productprice
,createdby
,createdon)
SELECT #menuId,
ProductId,
CategoryId,
ProductPrice,
#createdBy,
#createdOn
FROM OPENXML (#ptrHandle, '/ArrayOfMenuProductEntity/MenuProductEntity', 2)
WITH(ProductId BIGINT,CategoryId BIGINT, ProductPrice MONEY)
END
if(#IsCopy = 'True')
Begin
INSERT INTO tblMenuProduct
(menuid
,productid
,categoryid
,productprice
,createdby
,createdon)
Select #menuId,productid,categoryid,productprice,#createdBy,#createdOn
From tblMenuProduct where menuid = #CopyMenuId
SET #newMenuId = #menuId
End
IF(#menuTaxXML IS NOT NULL)
BEGIN
DELETE tblMenuTaxClass WHERE menuid = #menuId
EXEC sp_xml_preparedocument #ptrHandle OUTPUT, #menuTaxXML
INSERT INTO tblMenuTaxClass
(menuid
,taxclassid
)
SELECT #menuId,
TaxClassId
FROM OPENXML (#ptrHandle, '/ArrayOfTaxClassEntity/TaxClassEntity', 2)
WITH(TaxClassId BIGINT)
END
---------------------------------------------------------------------
-- exit the sproc
---------------------------------------------------------------------
-----------------------------------------------------------------------------------------------------------------------------
SET NOCOUNT OFF
END
Exception:insert statement conflicted with the foreign key constraint
Why I am getting this exception and how can I fix this?
The primary key value wont be there.you are trying to insert a Foreign key value to the table where corresponding PK wont be there.
table1
ID(PK)
1
2
3
table2
ID1(PK) ID(FK)
1 1
2 1
3 4// Error not there in PK table

Resources