Invalid Column name 'Price' [closed] - sql

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 1 year ago.
Improve this question
I am trying to do a SQL If-Else Statement in SQL Server but my query displayed a error which is Invalid Column Name 'Price' but I have that column on my table. Do I need to declare it in the If-Else Statement? Here is my query:
IF Price > 2000000
BEGIN
PRINT 'Wow! The Sales amount which is 3,000,000 is much higher than the 2,000,000 amount of Year 2018';
END
ELSE
BEGIN
PRINT 'The Sales amount did not surpass the 2,000,000 of 2018';
END
I have two columns. The column names are Year and Price. I have 2018 and 2019 for my Year column. I have 2,000,000 and 3,000,000 for my Price Column.

If Price is a column in your table, you need to reference that table in some way. I'm not sure how you're looking to use Price specifically but here are a couple of possibilities.
If you're just looking for at least one record in that table where Price>2000000 then you could do something like this.
IF EXISTS (SELECT 1 from tbl WHERE Price>2000000)
BEGIN
...
Or if you want to sum that column, you could add the sum to a variable and then check the variable.
declare #price INT
select #price=sum(Price) from tbl
IF #price>2000000
BEGIN
...

Related

How to merge two or more rows where some columns have same values and some have different values? [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 5 months ago.
Improve this question
Sample Data output after joining 2 different tables on ProductNumber
ProductNumber
QuantityOnHand
VendorID
1
5
401
1
5
501
I want to write a query that would return this output
ProductNumber
QuantityOnHand
VendorID
1
10
401 & 501
I'm still pretty new to SQL. Stuck on a homework problem here. I don't really know which aggregated function to use to make it work. Sum() but I don't want to add the vendorIDs. Concat() but they're from the same column name.
If it is SQL Server, then you need STRING_AGG function for cancatenating the same column in different rows and SUM for totalling:
SELECT ProductNumber,
SUM(QuantityOnHand),
STRING_AGG(VendorID, ' & ')
FROM Product
GROUP BY ProductNumber
I assumed the table name is Product, as you did not provide one. You can update it to your actual table name.

SQL incremental data [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 1 year ago.
Improve this question
i am trying to get only incremental records on one of the table SQL.
Example Table1--need to insert data 10 times per day from some source data. Suppose I have inserted 10 records in the morning. Another new records came after one hour. SO total will be 20 records. And it keep going more and more through all day. And I need to run script to show only increment part. SO whatever I show in the morning should not include next run. Basically each time main table updated , i need to run script to show only updated new rows not old exiting rows.
Add a batch number to to the data and then you can query the data only returning the latest batch. Something like this.
During Insert
DECLARE #Batch INT = (SELECT MAX(BatchNo)+1 as NextBatchNo FROM myTable)
IF #Batch IS NULL
SET #Batch = 1
INSERT INTO myTable (firstColumn, secondColumn, anotherColumn, BatchNo)
SELECT firstColumn, secondColumn, anotherColumn, #Batch
FROM mySourceDataTable
To get the latest rows inserted
SELECT * FROM myTable
WHERE Batch = (SELECT MAX(Batch) FROM myTable)
Query the table to display output for the last 24 days, using the Date function.
If you have oracle database then you can use flashback feature to get this data.
Link for more details
https://docs.oracle.com/cd/B19306_01/backup.102/b14192/flashptr002.htm

Picking unique records in SQL [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 4 years ago.
Improve this question
Say I have a table with multiple records of peoples name, I draw out a prize winner every month. What is a query in SQL that I can use so that I pick up a unique record every month and the person does not get picked on the next month. I do not want to delete the record of that person.
create a new column as a flag named anything like 'prizeFlag' make it boolean take only 0 and 1 or anything as you like, make it's default value is 0 means not get a prize yet
when you select a random column update this filed with 1 means take a prize
when you select a random column next month, Add a condition in WHERE Clause say the prizeFlag not equal 1 to avoid duplication
One should store whether a person has already won. A date would make sense to allow people after say 10 years to win again.
ALTER TABLE ADD won DATE;
A portable way would be to use a random number function outside the SQL.
SELECT MIN(id), MAX(id), COUNT(*) FROM persons
With a random number one can get the next valid ID.
SELECT MIN(ID) FROM persons WHERE won NOT IS NULL AND ID >= ?
int randomno = minId + new Random().nextInt(maxId - minId);
preparedStatement.setInt(1, randomno)
UPDATE persons SET won = CURRENT_DATE WHERE ID = ?

SQL Server : find rows that specific column value not change in last 24 hour [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 7 years ago.
Improve this question
This is my sample table
I want get last row of BB because money column of BB not changed in last 24 hour ...
What is the right query for this in SQL Server ?
SELECT Early.*
FROM sampleTable Early
JOIN sampleTable Older
ON Early.Name = Older.Name
AND Early.Money = Older.Money
AND DATEDIFF ( day , Older.time, Early.time) >= 1
Take note if only one record, there is nothing to compare and wont appear on the query.
You have to make a "after update" trigger and in the trigger insert the records to temp_table including a update datetime column. This will allow to track each update for each row.
This is MySQL trigger example
MySQL
create trigger ai_table_trigger for each row
begin
insert into temp_table(record_id,update_datetime)values(old.id,now());
end

Replace one column by values of the same column in anther table in SQL server [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
I have two tables, one big and one small. Both contain columns ID and EffectiveDate.
The bigger table has more other columns and of course more rows than the smaller table.
Under the condition that ID for both tables are the same, the EffectiveDate column is earlier in the small table than the big table. I want to replace the EffectiveDate in the big table by the value of the EffectiveDate column from the small table.
What should I do?
Seems like a very basic SQL query....
UPDATE bt
SET EffectiveDate = st.EffectiveDate
FROM dbo.BiggerTable bt
INNER JOIN dbo.SmallerTable st ON bt.ID = st.ID
-- maybe you also need this condition, if not *ALL* EffectiveDate values in the
-- smaller table are indeed before the values in the bigger table
WHERE st.EffectiveDate < bt.EffectiveDate