SQL - Split values from one column into two separate columns - sql

I am trying to take the value of column Quantity and split the values where Quantity is less than 500 and put those records into the LessThan500 column, for the records that are greater than 500 into the GreaterThan500 column.
I have a select statement that does this, but the table does not get updated properly
SUM(CASE WHEN Quantity < 500 THEN CAST(( Quantity*10 ) AS INT)
ELSE ''
END) as Quantityx10
, SUM(CASE WHEN Quantity < 500 THEN CAST(( Quantity*10 ) AS INT)
ELSE ''
END) AS '<500'
, '' AS '>=500'
USE FET
-- Create a new template table
IF OBJECT_ID('dbo.FuelSales', 'U') IS NOT NULL
DROP TABLE dbo.FuelSales;
CREATE TABLE FuelSales
(
TransactionType varchar(1) null,
TransactionID int,
CustomerID int null,
TransactionDate date null,
EntryTimeofDay datetime null,
UserTranNumber varchar(15) null,
AircraftNumber varchar(10) null,
CompanyName varchar(40) null,
NumNameCode varchar(30) null,
Description varchar(40) null,
Quantity decimal(18,6) null,
LessThan500 decimal(18, 6) null,
GreaterThan500 decimal(18,6) null,
);
INSERT INTO FuelSales(TransactionType, TransactionID, CustomerID, TransactionDate, EntryTimeofDay,UserTranNumber,AircraftNumber,CompanyName,NumNameCode,Description,Quantity,LessThan500,GreaterThan500)
SELECT ci.TransactionType
, ci.TransactionID
, ci.CustomerID
, ci.TransactionDate
, ci.EntryTimeofDay
, ci.UserTranNumber
, d.AircraftNumber
, c.CompanyName
, d.NumNameCode
, d.Description
, Quantity
FROM [TFBO7].[dbo].[CustInv] ci
INNER JOIN [TFBO7].[dbo].[Cust] c
ON c.CustomerID=ci.CustomerID
INNER JOIN [TFBO7].[dbo].[CustIDet] d
ON ci.TransactionID=d.TransactionID
WHERE ci.TransactionDate between '20180701' and '20180731' and d.TransactionTypeID='1'

I'm not sure why would you do this permanently on the table and not just do the separation as an ad hoc query when needed but assuming it's SQL Server it would go something like this (in the INSERT statement you're already doing):
...
, Quantity
-- 500 has to go somewhere so I put it in the first category
, LessThan500 = CASE WHEN Quantity <= 500 THEN Quantity ELSE NULL END
, GreaterThan500 = CASE WHEN Quantity > 500 THEN Quantity ELSE NULL END
FROM [TFBO7].[dbo].[CustInv] ci
...

You can add a computed column to the table. Just do:
alter table fuelsales add lessthan500 as (case when quantity <= 500 then quantity end);
alter table fuelsales add greaterthan500 as (case when quantity > 500 then quantity end);
This column will appear in the table when you query the table. However, it is not stored and it is "updated" automatically when quantity changes.

Related

sql query issue: warehouse aging inventory when negative entries

I found a warehouse aging inventory example online (see modified code below).
Everything works fine if the entry type for purchase (0) is positive and for sales (1) is negative. But if the values are inverse (because of cancellation) then the results will be wrong.
Example: There are four entries, three of them are purchase entries but as you can see the second one has been canceled that's why the quantity is negative.
The total sum of column RemainingQty must be 0 in that case but result is 1699.
What do I have to change in my SQL query?
Thanks for any advice.
DECLARE #ItemLedgerEntry TABLE
(
id INT IDENTITY(1, 1) NOT NULL PRIMARY KEY ,
ItemNo INT NOT NULL, --references another item
Qty FLOAT NOT NULL, --quantity
EntryType INT NOT NULL, --type=0 bought, type=1 sold
PostingDate DATETIME NOT NULL -- transaction date
);
INSERT #ItemLedgerEntry
( ItemNo, qty, EntryType, PostingDate )
VALUES ( 1999, 1700, 0, '10-06-2021'),
( 1999, -1700, 0, '29-06-2021'),
( 1999, 1, 0, '03-08-2021'),
( 1999, - 1, 1, '09-08-2021');
WITH Sold
AS ( SELECT IT.[ItemNo] ,
SUM(IT.Qty) AS TotalSoldQty
FROM #ItemLedgerEntry IT
WHERE It.[EntryType] =1
GROUP BY ItemNo
),
Bought
AS ( SELECT IT.* ,
(
SELECT SUM(RS.Qty)
FROM #ItemLedgerEntry RS
WHERE RS.[EntryType] =0 AND RS.[ItemNo] = IT.[ItemNo] AND RS.[PostingDate] <= IT.[PostingDate]
) AS RunningBoughtQty
FROM #ItemLedgerEntry IT
WHERE IT.[EntryType] = 0
)
SELECT
B.[ItemNo],
B.[PostingDate],
B.[EntryType],
S.TotalSoldQty,
B.RunningBoughtQty,
B.RunningBoughtQty + S.TotalSoldQty AS RunningDifferenceQty,
CASE WHEN (B.RunningBoughtQty) + (S.TotalSoldQty) <0
THEN 0
ELSE B.RunningBoughtQty + S.TotalSoldQty
END AS RunningRemainingQty,
CASE WHEN B.RunningBoughtQty + S.TotalSoldQty < 0 THEN 0
WHEN B.RunningBoughtQty + S.TotalSoldQty > B.Qty THEN B.Qty
ELSE B.RunningBoughtQty + S.TotalSoldQty
END AS RemainingQty
FROM Bought B
inner JOIN Sold S ON B.[ItemNo] = S.[ItemNo]

dynamic grouping by in sql server

i want to make a stored procedure like makeGroupBy(#a int,#b int,#c int). The inputs are 0 or 1 to decide which columns to group by. My attempt so far is below:
-- exec makeGroupBy 0,1,0
-- exec makeGroupBy 0,1,1
create proc makeGroupBy(#product_id int = 0,#city_id int = 1,#date_key
int = 0) as
begin
declare #tbl as table(product_id int, city_id int, date_key int, amount
float)
insert into #tbl
values(1,1,1,10),
(1,1,1,10),
(1,2,1,5),
(2,2,3,15),
(2,1,3,20),
(3,1,1,25)
select case isnull(#product_id,0) when 0 then 0 else product_id end
,case isnull(#city_id,0) when 0 then 0 else city_id end
,case isnull(#date_key,0) when 0 then 0 else date_key end
, sum(amount) amount from #tbl
group by case isnull(#product_id,0) when 0 then 0 else product_id end
,case isnull(#city_id,0) when 0 then 0 else city_id end
,case isnull(#date_key,0) when 0 then 0 else date_key end
end
I don't know if it's possible but what I want is to omit the unwanted columns (inputs with value 0) in the result set.
Assuming that your sql-server version is greater or equal than 2008.
select
product_id
,city_id
,date_key
,sum(amount) as total_amount
from #tbl
group by grouping sets (
(product_id, city_id, date_key)
, (product_id,city_id)
, (product_id, date_key)
, (city_id, date_key)
, (product_id)
, (city_id)
, (date_key))
having concat(iif(grouping_id(product_id)=0,1,0),iif(grouping_id(city_id)=0,1,0),iif(grouping_id(date_key)=0,1,0)) = concat(#product_id, #city_id, #date_key)
order by concat(iif(grouping_id(product_id)=0,1,0),iif(grouping_id(city_id)=0,1,0),iif(grouping_id(date_key)=0,1,0))
It seems that a view will probably suit best for this case
create view [view_name]
as
select
product_id
,city_id
,date_key
,sum(amount) as amount
,concat(iif(grouping_id(product_id)=0,1,0),iif(grouping_id(city_id)=0,1,0),iif(grouping_id(date_key)=0,1,0)) as grp_key
from #tbl
group by grouping sets (
(product_id, city_id, date_key)
, (product_id,city_id)
, (product_id, date_key)
, (city_id, date_key)
, (product_id)
, (city_id)
, (date_key))
go
Then you can query the view like
select
city_id
,date_key
,amount
from [view_name]
where grp_key = concat(0,1,1)

The INSERT statement conflicted with the constraint

I want to make a date constraint in my table (I use sql server). I want to make sure that the date in one of my columns is later than the current date and time (I know it sounds weird, but it's an assignment so I have no choice). I tried to do it this way:
ALTER TABLE sales ADD CONSTRAINT d CHECK (Date > CURRENT_TIMESTAMP);
but later when inserting DEFAULT into date column I get the following error:
The INSERT statement conflicted with the CHECK constraint "d".
The conflict occurred in database "Newsagents", table "dbo.Sales", column 'Date'.
This is the said table:
CREATE TABLE Sales (
ID INT IDENTITY(1,1) NOT NULL ,
ClientID INT REFERENCES Client(ClientID),
ProductNumber CHAR(10) REFERENCES Product(ProductNumber),
Quantity INT NOT NULL,
Price FLOAT NOT NULL ,
Date TIMESTAMP NOT NULL,
PRIMARY KEY ( ID )
and this how I insert my data into Sales column and get the constraint conflict:
DECLARE #counter INT
DECLARE #quantity int
DECLARE #prodNum varchar(20)
SET #counter = 0
WHILE #counter < 10
BEGIN
SET #quantity = (select FLOOR(RAND()*100))
SET #prodNum = (select TOP 1 ProductNumber from Product Order by NEWID())
insert into Sales (ClientID, ProductNumber, Quantity, Price, Date )
values(
(select TOP 1 ClientID from Client Order by NEWID()),
(select #prodNum),
(select #quantity),
((select #quantity)*(select TOP 1 Price from Product where ProductNumber = #prodNum)),
DEFAULT
)
SET #counter = #counter + 1
END
Is there a different way to do this? Or am I doing something wrong?
ALTER TABLE sales ADD CONSTRAINT d CHECK (Date > GETDATE());
change the Date column to datetime

Using SQL MERGE with vars instead of a table

take the following table (a very simplified example):
CREATE TABLE [dbo].[tbl_Order_Lines] (
[LineID] [int] IDENTITY(1, 1) NOT NULL ,
[OrderID] [int] NOT NULL ,
[StockCode] [varchar](20) NOT NULL ,
[Quantity] [smallint] NOT NULL
)
I have a proc that controls insertions into this table, but at one point you come to the infamous "UPSERT" scenario.
Let’s assume my procedure has the following vars:
#OrderID INT ,
#StockCode VARCHAR(20) ,
#Quantity SMALLINT
I currently do as follows:
IF ( NOT EXISTS ( SELECT *
FROM [dbo].[tbl_Order_Lines]
WHERE [OrderID] = #OrderID
AND [StockCode] = #StockCode )
)
INSERT INTO [dbo].[tbl_Order_Lines]
( [OrderID] ,
[StockCode] ,
[Quantity]
)
VALUES ( #OrderID ,
#StockCode ,
#Quantity
)
ELSE
UPDATE [dbo].[tbl_Order_Lines]
SET Quantity = #Quantity
WHERE [OrderID] = #OrderID
AND [StockCode] = #StockCode
My intention is to do away with this old method and use the MERGE statement - however i'm struggling to get my head round the MERGE statement, this is what i have so far:
MERGE dbo.tbl_Order_Lines
USING (
VALUES
( #Quantity
) ) AS Source ( Quantity )
ON dbo.tbl_Order_Lines.OrderID = #OrderID AND StockCode = #StockCode
WHEN MATCHED THEN
UPDATE SET Quantity = source.Quantity
WHEN NOT MATCHED THEN
INSERT (
OrderID ,
StockCode ,
Quantity
) VALUES
( #OrderID ,
#StockCode ,
Source.Quantity
);
My Question(s):
My attempt at this MERGE seems to work - yet it looks VERY messy and confusing - is there a better way of writing this?
How would i modify this MERGE statement to DELETE matching rows (based on OrderID & StockCode) if #Quantity = 0
You could tighten up the last part by using the special $action keyword.
Case $action
When 'INSERT' Then 'OK_ADDED'
When 'UPDATE' Then 'OK_UPDATED'
When 'DELETE' Then 'OK_REMOVED'
End
$action
Is available only for the MERGE statement. Specifies a column of type nvarchar(10) in the OUTPUT clause in a MERGE statement that returns one of three values for each row: 'INSERT', 'UPDATE', or 'DELETE', according to the action that was performed on that row.
Output Clause.
OK, this is what i came up with:
MERGE dbo.tbl_Order_Lines
USING ( VALUES ( #Quantity ) ) AS Source ( Quantity )
ON dbo.tbl_Order_Lines.OrderID = #OrderID AND StockCode = #StockCode
WHEN MATCHED AND #Quantity > 0 THEN
UPDATE SET Quantity = source.Quantity
WHEN MATCHED AND #Quantity <= 0 THEN
DELETE
WHEN NOT MATCHED AND #Quantity > 0 THEN
INSERT (
OrderID ,
StockCode ,
Quantity
)
VALUES
( #OrderID ,
#StockCode ,
Source.Quantity
)
OUTPUT
COALESCE(Inserted.LineID, Deleted.LineID) AS ResultID ,
CASE WHEN Deleted.LineID IS NULL
AND Inserted.LineID IS NOT NULL THEN 'OK_ADDED'
WHEN Deleted.LineID IS NOT NULL
AND Inserted.LineID IS NOT NULL THEN 'OK_UPDATED'
WHEN Deleted.LineID IS NOT NULL
AND Inserted.LineID IS NULL THEN 'OK_REMOVED'
END AS ResultDesc
INTO #tbl_LineChanges ( ResultID, ResultDesc );
Would still love to know if there is a tider way of writing this!

sql query serial number

I have written a stored procedure in SQL Server 2000. I want a serial number for output table.
So when I run this stored proc I get this error:
An explicit value for the identity column in table
'#tmpSearchResults1' can only be specified when a column list is used
and IDENTITY_INSERT is ON.
I have tried with set IDENTITY_INSERT #tmpSearchResults1 on
Create Procedure dbo.usp_mobile_All_KeyWord(#searchkey varchar(30))
AS
CREATE TABLE #tmpSearchResults
(
property_id varchar(255),
property_number varchar(255),
auction_date_reason varchar(255)
)
INSERT INTO #tmpSearchResults
SELECT
p.property_id, p.property_number, p.auction_date_reason
FROM
Pr p
INNER JOIN
Au a ON p.auction_id = a.auction_id
INNER JOIN
PrAdd pa ON p.property_id = pa.property_id
INNER JOIN state AS s ON s.state_id=pa.state
where
(
(p.archive = 'N'
AND
a.show_on_site = 'Y'
AND
(
(
((p.auction_date >= CONVERT(datetime, CONVERT(varchar, GETDATE(), 103), 103) and (p.auction_date_reason is null or p.auction_date_reason = ''))
or
(p.auction_date <= CONVERT(datetime, CONVERT(varchar, GETDATE(), 103), 103) and ( p.auction_date_reason = 'Accepting Offers' )))
and
pa.property_address_type_id = 1 )) )
and
(state_abbreviation=#searchkey or s.state_name like '%'+''+ #searchkey +''+'%' or city like '%'+''+ #searchkey +''+'%' or pa.address1 like '%'+''+ #searchkey +''+'%'
or pa.address2 like '%'+''+ #searchkey +''+'%')
)
)
CREATE TABLE #tmpSearchResults1
(
i1 int identity,
property_id varchar(255),
property_number varchar(255),
auction_date_reason varchar(255)
)
insert into #tmpSearchResults1
select
property_id ,
property_number,
auction_date_reason
from #tmpSearchResults
order by
case when charindex(#searchkey,state) >0 then 1000 else 0 end desc,
case when charindex(#searchkey,statename) >0 then 1000 else 0 end desc,
case when charindex(#searchkey,city) >0 then 1000 else 0 end desc,
case when charindex(#searchkey,address2) >0 then 1000 else 0 end desc,
case when charindex(#searchkey,address1) >0 then 1000 else 0 end desc,
case when charindex(#searchkey,short_description) >0 then 1000 else 0 end desc
select * from #tmpSearchResults1
Plz do help me
The error code is very very very clear.
The relevant portion is ...when a column list is used....
You need to specify your column list in the INSERT statement.
INSERT INTO #tmpSearchResults
(i1,
property_id,
property_number,
auction_date_reason)
SELECT
p.property_id, p.property_number, p.auction_date_reason
FROM...
First, there is a comma too much in the SELECT part of your second statement:
insert into #tmpSearchResults1
select
property_id ,
property_number,
auction_date_reason , <-- THIS ONE!!
from #tmpSearchResults
The last column of a SELECT statement must be without a comma.
So this would be correct:
insert into #tmpSearchResults1
select
property_id ,
property_number,
auction_date_reason
from #tmpSearchResults
Second, did you read this part of the error message?
An explicit value [...] can only be specified when a column list is used
The "column list" part means that you have to specify the columns in the INSERT part:
insert into #tmpSearchResults1
(property_id, property_number, auction_date_reason)
select
property_id ,
property_number,
auction_date_reason
from #tmpSearchResults
You can get away with not specifying the columns when the number of columns in the SELECT statement is the same as in the table in which they should be inserted (and if the data types match).
If one of these conditions is not met, you need to specify the columns because otherwise SQL Server doesn't know which value to insert into which column.