I have a table named items_list:
id item_name required_number group_id alcoholic alt_item dislciamer alt_disclaimer
---------------------------------------------------------------------------------------------
1 Draft Beer 1 24 true Appetizer Local Brand null
2 Burger 3 24 false null null null
3 Margarita 10 24 true Street Corn null Only one
4 Alcohol 1 10 true Burger null null
5 Frito Lay 3 10 false null null null
And I want to update the item_name with the alt_item IF the alcoholic is TRUE AND it's not NULL.
I tried this query
UPDATE items_list rl
SET item_name = rl2.alt_item AND disclaimer = rl2.alt_disclaimer
FROM items_list rl2
WHERE rl.required_number = rl2.required_number AND rl.group_id = 24 AND rl.alt_item IS NOT NULL AND rl.alcoholic = TRUE;
But I get: Query Error: error: argument of AND must be type boolean, not type character varying
The only boolean I have is the alcoholic option but I don't know why is throwing that error, do I need to change something on my query?
Here's a DB Fiddle for example: https://www.db-fiddle.com/f/7BvUh2FzkDhZhjqFfy1niT/4
Try to remove AND in the SET clause:
UPDATE items_list rl
SET item_name = rl2.alt_item,
disclaimer = rl2.alt_disclaimer
FROM items_list rl2
WHERE rl.required_number = rl2.required_number
AND rl.group_id = 24
AND rl.alt_item IS NOT NULL
AND rl.alcoholic = TRUE;
Related
I am new to Power BI, I am facing issue where I want to create a new column based on the latest date and the id column
ID LogCreationDate Points What I want
1001 12-Oct-2022 5 null
1001 17-Oct-2022 2 2
1001 13-Oct-2022 7 null
1001 07-Aug-2022 2 null
1002 03-Sept-2022 2.1 null
1002 22-Sept-2022 5 null
1002 04-Oct-2022 1 1
1002 01-Aug-2022 1.2 null
1003 05-Nov-2022 3.5 3.5
1003 01-Nov-2022 6.6 null
In Above table, I want to calculate "What I want" column using DAX, not using power query
Try this Calculated Column:
=
REPT(
Table1[Points],
CALCULATE(
LASTDATE( Table1[LogCreationDate] ),
ALLEXCEPT( Table1, Table1[ID] )
) = Table1[LogCreationDate]
)
I think This is the best job for the measure, not the calc. column:
Please use this code:
what I want =
VAR TblSSS =
ADDCOLUMNS (
YourTbl,
"MaxDate",
CALCULATE (
LASTDATE ( YourTbl[LogCreationDate] ),
ALLEXCEPT ( YourTbl, YourTbl[ID] )
)
)
RETURN
MAXX ( TblSSS, IF ( [LogCreationDate] = [MaxDate], [Points], "null" ) )
If we test it on a table visual, It returns:
Note: After putting all fields and measure into suitable places, do not forget to click the down-pointing arrow on the id field, and pick 'show values with no data'.
I am having issues sorting some dates in 3 different ranges of dates and return a values according to the correct range. I am hoping you can give me a efficent and clean way of doing it.
I have 6 different dates that I get from a SQL Table. Those dates are then stored in variables. All the dates can also be a Null value. My dates are seperated in 3 date ranges. I want to return an indication of what ranges I am in by using the earliest start Date in all of my ranges. The date of the correct range must also be smaller than the current Date. A date Range can also consist of only an End Date. In that case, we considered that the range end at the end date and is active before that. We select the earliest end date that is close to the current Date in that case.
Return 0 if all the date are null
Range #1(Category #1) X Start Date and X end Date Return 1
Range #2(Category #2) Y Start Date and Y end Date Return 2
Range #3(Category #3) Z Start Date and Z end Date Return 3
EDIT
Ex#1 XStart = December 10 , XEnd = December 15
YStart = December 12 , Yend = December 13
ZStart = December 9 , ZEnd = Null
Expected result would be Z Category
Ex#2 XStart = December 8 , XEnd = December 15
YStart = NULL , Yend = NULL
ZStart = December 9 , ZEnd = Null
Expected result would be X Category
Ex#3XStart = NULL , XEnd = December 15
YStart = NULL , Yend = NULL
ZStart = December 9 , ZEnd = Null
Expected result would be X Category
Ex#4 XStart = December 10 , XEnd = December 15
YStart = NULL , Yend = NULL
ZStart = December 9 , ZEnd = Null
Expected result would be Z Category
Is there a more efficent way than doing a lot of IF statements ? I am having difficulty handling all of those conditions and checks. Here is a snippet of what I have so far.
--Return 0 is not Condition is Applicable
ALTER PROCEDURE [dbo].[HO_GetReason]
#HOID INT
AS
BEGIN
Declare #IsHOIDReal INT = (SELECT ID from T_HO where id = #HOID)
Declare #XStartDate Datetime
Declare #XEndDate Datetime
Declare #YStartDate Datetime
Declare #YEndDate Datetime
Declare #ZStartDate Datetime
Declare #ZEndDate Datetime
CREATE TABLE #tmpT_HO_Withhold (
ID INT NOT NULL,
XStartDate Datetime null,
XEndDate Datetime null,
YStartDate Datetime null,
YEndDate Datetime null,
ZStartDate Datetime null,
ZEndDate Datetime null,
PRIMARY KEY CLUSTERED (ID)
)
IF (#IsHOIDReal IS NOT NULL)
BEGIN
INSERT INTO #tmpT_HO_Withhold
SELECT T_HO.ID,
XStartDate ,
XEndDate ,
YStartDate ,
YEndDate ,
ZStartDate ,
ZEndDate
FROM dbo.T_HO
WHERE ID = #HOID
SET #XStartDate = (Select TOP 1 XStartDate from #tmpT_HO_Withhold)
SET #XEndDate = (Select TOP 1 XEndDate from #tmpT_HO_Withhold)
SET #YStartDate = (Select TOP 1 YStartDate from #tmpT_HO_Withhold)
SET #YEndDate = (Select TOP 1 YEndDate from #tmpT_HO_Withhold)
SET #ZStartDate = (Select TOP 1 ZStartDate from #tmpT_HO_Withhold)
SET #ZEndDate = (Select TOP 1 ZEndDate from #tmpT_HO_Withhold)
IF(#XStartDate IS NULL AND #YStartDate IS NULL AND #ZStartDate IS NULL)
BEGIN print 'NO CONDITION' Select 0 as 'HO_GetReason' END
ELSE IF (#XStartDate IS NOT NULL AND #YStartDate IS NULL AND #ZStartDate IS NULL) BEGIN print '1' Select 1 as 'HO_GetReason'END
ELSE IF (#XStartDate IS NOT NULL AND #YStartDate IS NULL AND #ZStartDate IS NULL) BEGIN print '2' Select 2 as 'HO_GetReason'END
ELSE IF (#XStartDate IS NULL AND #YStartDate IS NULL AND #ZStartDate IS NOT NULL) BEGIN print '3' Select 3 as 'HO_GetReason'END
END
DROP TABLE #tmpT_HO_Withhold END
Notes regarding efficient and clean:
Complex conditional are not in the inefficient category. It can fall into the hard to read category and maintain, but they are a pretty quick operation.
Example: That second "else if" looks strangely like the first "else if". Code will not be reached.
Creating and destroying the temp table will be the slowest part of your stored procedure.
Temp tables using #tablename are not concurrency safe in stored procedure, you can end up with odd schema altered errors in some cases.
You can get to the same results by swapping most of that with:
SELECT
#XStartDate = XStartDate ,
#XEndDate = XEndDate ,
#YStartDate = YStartDate ,
#YEndDate = YEndDate ,
#ZStartDate = ZStartDate ,
#ZEndDate = ZEndDate
FROM dbo.T_HO
WHERE ID = #HOID
Id is unique based on the primary key spotted in your create table, so TOP isn't necessary in this format, no rows will leave the values as null.
Personally, once I get that conditional working (absolute final form), I would be tempted to directly adjust it to a CASE statement and set that as a PERSISTENT computed COLUMN in the base table.
ALTER TABLE dbo.T_HO ADD Reason AS (CASE WHEN XStartDate IS NOT NULL AND ... THEN ... WHEN ... THEN ... ELSE 0 END) PERSISTED
I have the following query:
SELECT null as id, null as cost, null as title FROM books LIMIT 1
It returns one single record.
Using Eloquent, I've tried
$listagem = Book::where('id','=',-1)->get();// there is no id = -1
$listagem = Livro::select('NULL as id' ,'NULL as cost', 'NULL as title')->get();
(...)
but nothing works.
What is the correct sintax?
Thanks
I am trying to get the distinct records
var records = (from entry in db.Table1
select new
{
RowID = entry.RowID,
FirstName = entry.First,
LastName = entry.Last,
Restricted = (entry.Prohibited == null || entry.Prohibited == "False") ? "Restricted" : "Not Restricted"
}).ToList();
Here RowID is an primary key. I want to get the distinct First and Last Name.
For example:
RowID First Last Prohibited ...
1 A B False
2 A B False
3 A B False
4 Z Y True
5 Z Y True
What I am trying to get here is:
RowID First Last Prohibited
1 A B False
4 Z Y True
How can I get it?
var records = (from entry in db.Table1
group entry by new {entry.First, entry.Last} into g
let entry = g.FirstOrDefault()
select new
{
RowID = entry.RowID,
FirstName = entry.First,
LastName = entry.Last,
Restricted = (entry.Prohibited == null || entry.Prohibited == "False") ? "Restricted" : "Not Restricted"
}).ToList();
By grouping the items by the combined first and last name, and then only taking the first item in each group, you effectively ensure that you're getting distinct items based on those values.
Simply you can do in this way:
var records =
(from entry in db.Table1
select new {
FirstName = entry.First,
LastName = entry.Last,
Restricted = (entry.Prohibited == null || entry.Prohibited == "False") ? "Restricted" : "Not Restricted"
}).Distinct();
If you want the rowID also, you can read select-distinct-using-linq.
im working on procedure which should transfer number of items (value #p_count) from old store to new store
SET #countOnOldStore = (SELECT "count" FROM ProductStore WHERE StoreId = #p_oldStoreId AND ProductId = #p_productID)
SET #countOnNewStore = (SELECT "count" FROM ProductStore WHERE StoreId = #p_newStoreID AND ProductId = #p_productID)
SET #ShiftedCount = #countOnOldStore - #p_count
SET #newStoreAfterShift = #countOnNewStore + #p_count
IF #ShiftedCount > 0
BEGIN
DELETE FROM ProductStore WHERE storeId = #p_oldStoreId and productID = #p_productID
INSERT INTO ProductStore (storeId,productId,"count") VALUES (#p_oldStoreId,#p_productID,#ShiftedCount)
DELETE FROM ProductStore WHERE storeId = #p_newStoreID and productID = #p_productID
INSERT INTO ProductStore (storeId,productId,"count") VALUES (#p_newStoreID,#p_productID,#newStoreAfterShift)
END
ELSE
PRINT 'ERROR'
well ... second insert is not working. I cant figure it out. It says
Cannot insert the value NULL into column 'count', table 'dbo.ProductStore'; column does not allow nulls. INSERT fails.
Can anyone see problem and explain it to me ? Its school project
It looks like your entire query should just be:
UPDATE ProductStore
SET [count] = [count] + CASE
WHEN storeId = #p_NewStoreID THEN #p_Count
ELSE -#p_Count END
WHERE
productID = #p_ProductID and
storeID in (#p_NewStoreID,#p_OldStoreID)
If either value in the following is NULL, the total will be NULL:
SET #newStoreAfterShift = #countOnNewStore + #p_count
Check both values (#countOnNewStore, #p_count) for NULL.
Looks like you are not assigning any value to #p_count, so it is NULL and so are #ShiftedCount and #newStoreAfterShift.