Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to add a user defined column with a single value to a SQL query

Tags:

sql

I currently have a SQL query that produces a table with around 10M rows. I would like to append this table with another column that has the same entry for all 10M rows.

As an example consider the following toy query

SELECT PRODUCT_ID, ORDER_QUANTITY
FROM PRODUCT_TABLE
GROUP BY SALES_DAY

And say that is produces the following table

 PRODUCT_ID ORDER_QUANTITY`     
      1          10
      2          12
      3          14

How can I change this query so that it produces the following table, where every entry in USER_VALUE is 999.

 PRODUCT_ID ORDER_QUANTITY USER_VALUE  
      1           10         999
      2           12         999
      3           14         999

I realize that there may be several answers here... but I suppose that it would help to know the method that would be produce the table with the smallest file size (I assume this would require specifying the type of data beforehand).

like image 540
Berk U. Avatar asked Jun 25 '13 10:06

Berk U.


People also ask

How can I add just one column value in SQL?

To insert values into specific columns, you first have to specify which columns you want to populate. The query would look like this: INSERT INTO your_table_name (your_column_name) VALUES (the_value);

How do I add a column in SQL based on condition?

In Microsoft SQL Server, we can change the order of the columns and can add a new column by using ALTER command. ALTER TABLE is used to add, delete/drop or modify columns in the existing table. It is also used to add and drop various constraints on the existing table.


3 Answers

Like this:

SELECT PRODUCT_ID, ORDER_QUANTITY, 999 as USER_VALUE FROM PRODUCT_TABLE GROUP BY SALES_DAY 
like image 78
Gordon Linoff Avatar answered Sep 23 '22 06:09

Gordon Linoff


You can pass it in the SELECT, for example:

SELECT PRODUCT_ID, ORDER_QUANTITY, 999 AS USER_VALUE FROM PRODUCT_TABLE GROUP BY SALES_DAY 
like image 38
Sklivvz Avatar answered Sep 24 '22 06:09

Sklivvz


you can use

SELECT PRODUCT_ID, ORDER_QUANTITY, user_value=999
    FROM PRODUCT_TABLE
    GROUP BY SALES_DAY
like image 41
Dinup Kandel Avatar answered Sep 20 '22 06:09

Dinup Kandel