Related
I have this table structure and I want a query that needs to be returning opposite side columns values.
CREATE TABLE TransactionDetail
(
ID NUMERIC NOT NULL PRIMARY KEY,
TransactionCode bigint,
COATitle NVARCHAR(50),
DrAmount NUMERIC,
CrAmount NUMERIC
);
INSERT INTO TransactionDetail VALUES (1, 1, 'Overtime', '2500', NULL);
INSERT INTO TransactionDetail VALUES (2, 1, 'Internship', NULL, '1500');
INSERT INTO TransactionDetail VALUES (3, 1, 'Medical', NULL, '1000');
INSERT INTO TransactionDetail VALUES (4, 2, 'Internship', '1150', NULL);
INSERT INTO TransactionDetail VALUES (5, 2, 'Overtime', NULL, '1150');
INSERT INTO TransactionDetail VALUES (6, 3, 'Overtime', '600', NULL);
INSERT INTO TransactionDetail VALUES (7, 3, 'Refreshment', '400', NULL);
INSERT INTO TransactionDetail VALUES (8, 3, 'Car Loan', '200', NULL);
INSERT INTO TransactionDetail VALUES (9, 3, 'Office Expenses', NULL, '1200');
If I pass parameter with value Overtime then it should return following rows
SELECT COATitle, DrAmount, CrAmount
FROM TransactionDetail
WHERE COATitle <> Overtime
Internship NULL 1500
Medical NULL 1000
Internship 1150 NULL
Office Expenses NULL 1200
The logic is against each transaction if the selected Account is on Debit side it should print the Credit side accounts and if the selected Account is on Credit side it should print Debit side accounts against that specific TransactionCode
The following code gives the desired result. It does that by checking to see whether the provided parameter is a debit or credit (or doesn't exist) in the current transaction, then only displays the reverse as specified.
declare #Parameter nvarchar(50) = 'Overtime'
declare #Trans TABLE
(
ID NUMERIC NOT NULL,
TransactionCode bigint,
COATitle NVARCHAR(50),
DrAmount NUMERIC,
CrAmount NUMERIC
);
INSERT INTO #Trans VALUES (1, 1, 'Overtime', '2500', NULL);
INSERT INTO #Trans VALUES (2, 1, 'Internship', NULL, '1500');
INSERT INTO #Trans VALUES (3, 1, 'Medical', NULL, '1000');
INSERT INTO #Trans VALUES (4, 2, 'Internship', '1150', NULL);
INSERT INTO #Trans VALUES (5, 2, 'Overtime', NULL, '1150');
INSERT INTO #Trans VALUES (6, 3, 'Overtime', '600', NULL);
INSERT INTO #Trans VALUES (7, 3, 'Refreshment', '400', NULL);
INSERT INTO #Trans VALUES (8, 3, 'Car Loan', '200', NULL);
INSERT INTO #Trans VALUES (9, 3, 'Office Expenses', NULL, '1200');
select TransactionCode, COATitle, DrAmount, CrAmount
from (
SELECT TransactionCode, COATitle, DrAmount, CrAmount
, case when exists (select 1 from #Trans T1 where T1.TransactionCode = T.TransactionCode and T1.COATitle = #Parameter and DrAmount is not null) then 1
when exists (select 1 from #Trans T1 where T1.TransactionCode = T.TransactionCode and T1.COATitle = #Parameter and CrAmount is not null) then -1
else 0 end TransSign
FROM #Trans T
WHERE COATitle <> #Parameter
) X
where (TransSign = -1 and DrAmount is not null)
or (TransSign = 1 and CrAmount is not null)
I have a table with 10 columns of floating values associated to a Serial Number.
Values can be entered or not in each column.
0 [zero] and NULL are not valid values.
Using SQL SERVER 2012.
My question is:
How can I check that given a specific serial number, none of these values is either 0 or NULL and return True if all values are present and False otherwise?
Please give me some suggestions.
Thank you
More than your effort deserved but I was curious. Cast to bit makes it a 1 or 0 and have to cast back to int to add.
declare #T table (int1 int, int2 int);
insert into #T values (null, null), (null, 0), (0, null), (null, 1), (10, null), (0, 0)
, (1, 0), (1, 10), (10, 0), (10, 10), (-2, -2);
with cte as
( select int1, cast(cast(isnull(int1, 0) as bit) as int) tf1
, int2, cast(cast(isnull(int2, 0) as bit) as int) tf2
from #T
)
select cte.int1, cte.int2
, iif(tf1 + tf2 = 2, 1, 0) as rr
from cte;
I have the following scenario. As you could see Levels (1) – ItemNumber (NULL) is the incoming number, which all the ones listed below are associated to the main one.
Ultimately, what I want to do is only get the following rows
Lines 2 and 4,5,6
Since row 2 doesn’t have any children rows and USDAFoodItemID is not null
And 4,5,6 The parent row 3 does have children, and since row 3 is null,
The children 4,5,6 will be spit out.
If Line 1 was populated with USDAFoodItemID then it would only spit out line 1.
Sorry for my English, I am still working on it. But help is universal, and I would appreciate any help I could get.
Sorry, here is the code to produce the table. I am using MS SQL
DECLARE #HoldTable TABLE
(
Levels INT,
ItemNumber INT,
IngredientNumber INT,
NutritionalLoss FLOAT,
StandardAmountInGrams FLOAT,
SequenceNumber INT,
USDAFoodItemID NVARCHAR(5)
)
INSERT INTO #HoldTable VALUES (1, NULL, 1174258, 1.0000 ,113.3980925 ,1, NULL)
INSERT INTO #HoldTable VALUES (2, 1174258, 1174252 ,1.0000, 113.3980925, 1, 09096)
INSERT INTO #HoldTable VALUES (3, 1174252, 1006967, 1.0000, 56.69904625, 1, 01297)
INSERT INTO #HoldTable VALUES (3, 1174252, 1174251, 1.0000 ,56.69904625, 1, NULL)
INSERT INTO #HoldTable VALUES (4, 1174251, 1007555 ,1.0000 ,1.41747615625, 1 ,12078)
INSERT INTO #HoldTable VALUES (4, 1174251, 1010210, 1.0000, 26.93204696875, 1 ,NULL)
INSERT INTO #HoldTable VALUES (4, 1174251, 1010984, 1.0000, 28.349523125, 1 ,NULL)
SELECT * FROM #HoldTable
Thank you
Table Schema:
CREATE TABLE [dbo].[VMaster](
[VID] [int] IDENTITY(1,1) PRIMARY KEY NOT NULL,
[VName] [varchar](30) NOT NULL
)
GO
CREATE TABLE [dbo].[TblMaster](
[SID] [int] IDENTITY(1,1) NOT NULL Primary Key,
[VID] [int] NOT NULL,
[CreatedDate] [datetime] default (getdate()) NOT NULL,
[CharToAdd] [varchar](10) NOT NULL,
[Start] [int] NOT NULL,
[End] [int] NOT NULL
) ON [PRIMARY]
GO
CREATE TABLE [dbo].[TblDetails](
[DetailsID] [int] IDENTITY(1,1) NOT NULL Primary Key,
[SID] [int] NOT NULL,
[Sno] [int] NOT NULL,
[ConcatenatedText] [varchar](20) NOT NULL,
[isIssued] [bit] default (0) NOT NULL,
[isUsed] [bit] default (0) NOT NULL
)
GO
ALTER TABLE [dbo].[TblMaster] WITH CHECK ADD CONSTRAINT [fk_SI_id] FOREIGN KEY([VID])
REFERENCES [dbo].[VMaster] ([VID])
GO
ALTER TABLE [dbo].[TblMaster] CHECK CONSTRAINT [fk_SI_id]
GO
Sample data:
Insert into dbo.VMaster Values ('A1')
Insert into dbo.VMaster Values ('A2')
GO
Insert into dbo.TblMaster Values (1,default, 'ABC', 1, 5)
Insert into dbo.TblMaster Values (1,default, 'XYZ', 10, 20)
Insert into dbo.TblMaster Values (2,default, 'P1', 10, 15)
GO
Insert into dbo.TblDetails values(1, 1, 'ABC1', 1,0)
Insert into dbo.TblDetails values(1, 2, 'ABC2', 1,0)
Insert into dbo.TblDetails values(1, 3, 'ABC3', 1,0)
Insert into dbo.TblDetails values(1, 4, 'ABC4', 0,0)
Insert into dbo.TblDetails values(1, 5, 'ABC5', 0,0)
Insert into dbo.TblDetails values(2, 10, 'XYZ10', 1,0)
Insert into dbo.TblDetails values(2, 11, 'XYZ11', 1,0)
Insert into dbo.TblDetails values(2, 12, 'XYZ12', 1,0)
Insert into dbo.TblDetails values(2, 13, 'XYZ13', 1,0)
Insert into dbo.TblDetails values(2, 14, 'XYZ14', 1,0)
Insert into dbo.TblDetails values(2, 15, 'XYZ15', 0,0)
Insert into dbo.TblDetails values(2, 16, 'XYZ16', 0,0)
Insert into dbo.TblDetails values(2, 17, 'XYZ17', 0,0)
Insert into dbo.TblDetails values(2, 18, 'XYZ18', 0,0)
Insert into dbo.TblDetails values(2, 19, 'XYZ19', 0,0)
Insert into dbo.TblDetails values(2, 20, 'XYZ20', 0,0)
Insert into dbo.TblDetails values(3, 10, 'P110', 1,0)
Insert into dbo.TblDetails values(3, 11, 'P111', 1,0)
Insert into dbo.TblDetails values(3, 12, 'P112', 1,0)
Insert into dbo.TblDetails values(3, 13, 'P113', 1,0)
Insert into dbo.TblDetails values(3, 14, 'P114', 1,0)
Insert into dbo.TblDetails values(3, 15, 'P115', 1,0)
GO
Expected Output:
Query which I have as of now:
SELECT
TM.VID, VM.VName, TM.CharToAdd, TM.Start, TM.[End],
(SELECT COUNT(*) FROM dbo.TblDetails TD where TD.SID=TM.SID and isIssued = 0 ) Balance
FROM
dbo.TblMaster TM, dbo.VMaster VM
Where VM.VID = TM.VID
I am trying to calculate total matching records in TblDetails for each VID whose isIssued value is 0.
User could select a period (date range) for which they want to see this output. For that TblMaster.CreatedDate is what we have to use.
Also user can select a specific VID and like to see the result only for that.
Just in case if date range or VID is not provided then this has to work against the whole table! Per month there might be 500000 records in TblDetails and we will hold 12 months of data.
For the summary of the whole table your query looks OK.
It is easy to add an optional filtering on top of that.
If parameter is NULL, it will be ignored. If parameter is not NULL, it will limit the result set:
DECLARE #ParamStart datetime = '2016-01-01';
DECLARE #ParamEnd datetime = '2017-01-01';
DECLARE #ParamVID int = NULL;
SELECT
TM.VID, VM.VName, TM.CharToAdd, TM.Start, TM.[End],
(SELECT COUNT(*) FROM dbo.TblDetails TD where TD.SID=TM.SID and isIssued = 0) Balance
FROM
dbo.TblMaster AS TM
INNER JOIN dbo.VMaster AS VM ON VM.VID = TM.VID
WHERE
(TM.CreatedDate >= #ParamStart OR #ParamStart IS NULL)
AND (TM.CreatedDate < #ParamEnd OR #ParamEnd IS NULL)
AND (TM.VID = #ParamVID OR #ParamVID IS NULL)
ORDER BY TM.SID
OPTION(RECOMPILE);
To make it efficient, it should help to have this index:
CREATE NONCLUSTERED INDEX [IX_] ON [dbo].[TblDetails]
(
[isIssued] ASC,
[SID] ASC
)
And also indexes on dbo.TblMaster.CreatedDate and dbo.TblMaster.VID.
I want to insert multiple rows in a single table. How can I do this using single insert statement?
Wrap each row of values to be inserted in brackets/parenthesis (value1, value2, value3) and separate the brackets/parenthesis by comma for as many as you wish to insert into the table.
INSERT INTO example
VALUES
(100, 'Name 1', 'Value 1', 'Other 1'),
(101, 'Name 2', 'Value 2', 'Other 2'),
(102, 'Name 3', 'Value 3', 'Other 3'),
(103, 'Name 4', 'Value 4', 'Other 4');
You can use SQL Bulk Insert Statement
BULK INSERT TableName
FROM 'filePath'
WITH
(
FIELDTERMINATOR = '','',
ROWTERMINATOR = ''\n'',
ROWS_PER_BATCH = 10000,
FIRSTROW = 2,
TABLOCK
)
for more reference check
https://www.google.co.in/webhp?sourceid=chrome-instant&ion=1&espv=2&ie=UTF-8#q=sql%20bulk%20insert
You Can Also Bulk Insert Your data from Code as well
for that Please check below Link:
http://www.codeproject.com/Articles/439843/Handling-BULK-Data-insert-from-CSV-to-SQL-Server
1--> {Simple Insertion when table column sequence is known}
Insert into Table1
values(1,2,...)
2--> {Simple insertion mention column}
Insert into Table1(col2,col4)
values(1,2)
3--> {bulk insertion when num of selected collumns of a table(#table2) are equal to Insertion table(Table1) }
Insert into Table1 {Column sequence}
Select * -- column sequence should be same.
from #table2
4--> {bulk insertion when you want to insert only into desired column of a table(table1)}
Insert into Table1 (Column1,Column2 ....Desired Column from Table1)
Select Column1,Column2..desired column from #table2
You can use UNION All clause to perform multiple insert in a table.
ex:
INSERT INTO dbo.MyTable (ID, Name)
SELECT 123, 'Timmy'
UNION ALL
SELECT 124, 'Jonny'
UNION ALL
SELECT 125, 'Sally'
Check here
For MSSQL, there are two ways:(Consider you have a 'users' table,below both examples are using this table for example)
1) In case, you need to insert different values in users table. Then you can write statement like:
INSERT INTO USERS VALUES
(2, 'Michael', 'Blythe'),
(3, 'Linda', 'Mitchell'),
(4, 'Jillian', 'Carson'),
(5, 'Garrett', 'Vargas');
2) Another case, if you need to insert same value for all rows(for example, 10 rows you need to insert here). Then you can use below sample statement:
INSERT INTO USERS VALUES
(2, 'Michael', 'Blythe')
GO 10
Hope this helps.
You can use the UNION ALL function
http://blog.sqlauthority.com/2007/06/08/sql-server-insert-multiple-records-using-one-insert-statement-use-of-union-all/
We will import the CSV file into the destination table in the simplest form. I placed my sample CSV file on the C: drive and now we will create a table which we will import data from the CSV file.
DROP TABLE IF EXISTS Sales
CREATE TABLE [dbo].[Sales](
[Region] [varchar](50) ,
[Country] [varchar](50) ,
[ItemType] [varchar](50) NULL,
[SalesChannel] [varchar](50) NULL,
[OrderPriority] [varchar](50) NULL,
[OrderDate] datetime,
[OrderID] bigint NULL,
[ShipDate] datetime,
[UnitsSold] float,
[UnitPrice] float,
[UnitCost] float,
[TotalRevenue] float,
[TotalCost] float,
[TotalProfit] float
)
The following BULK INSERT statement imports the CSV file to the Sales table.
BULK INSERT Sales
FROM 'C:\1500000 Sales Records.csv'
WITH (FIRSTROW = 2,
FIELDTERMINATOR = ',',
ROWTERMINATOR='\n' );