sql-view

Prepend table name to each column in a result set in SQL? (Postgres specifically)

爷,独闯天下 提交于 2019-11-30 19:28:01
How can I get the label of each column in a result set to prepend the name if its table? I want this to happen for queries on single tables as well as joins. Example: SELECT first_name, last_name FROM person; I want the results to be: | person.first_name | person.last_name | |-------------------|------------------| | Wendy | Melvoin | | Lisa | Coleman | I could use "AS" to define an alias for each column, but that would be tedious. I want this to happen automatically. SELECT first_name AS person.first_name, last_name AS person.last_name FROM person; The reason for my question is that I am

Combine results of two unrelated queries into single view

回眸只為那壹抹淺笑 提交于 2019-11-30 15:36:22
Is it possible to combine the results of two separate (unrelated) sql queries into a single view. I am trying to total some figures for users and count the views for videos this month to display on a dashboard. i.e., select count(*) from video where monthname(views) = 'May'; and select sum(sessions) from user where user_id = 6; I would like to create a view that combines that contains these two results. Is this possible? SELECT t2.total_session, t1.watch_count FROM (SELECT 1 AS common_key, count(*) AS watch_count FROM video WHERE monthname(views) = 'May') AS t1 JOIN (SELECT 1 AS common_key,

SQL - CTE vs VIEW

别说谁变了你拦得住时间么 提交于 2019-11-30 12:58:59
My question here is what is the difference between CTE and View in SQL . I mean in which case I should use the CTE and which case the View . I know that both are some kind of virtual tables but I can't differentiate their use. I found a similar question here but it's about performance. Update 1: For example: I have a database filled with trades( tbl_trade ). I need to select from 3.5 millions records only the trades that was opened the current month until current time and then manipulate the data(with different queries on the virtual table - this looks like View). The problem here is that I

Using COALESCE in SQL view

我怕爱的太早我们不能终老 提交于 2019-11-30 07:11:26
I need to create a view from several tables. One of the columns in the view will have to be composed out of a number of rows from one of the table as a string with comma-separated values. Here is a simplified example of what I want to do. Customers: CustomerId int CustomerName VARCHAR(100) Orders: CustomerId int OrderName VARCHAR(100) There is a one-to-many relationship between Customer and Orders. So given this data Customers 1 'John' 2 'Marry' Orders 1 'New Hat' 1 'New Book' 1 'New Phone' I want a view to be like this: Name Orders 'John' New Hat, New Book, New Phone 'Marry' NULL So that

Table-Valued Function(TVF) vs. View

三世轮回 提交于 2019-11-30 06:12:31
问题 What's the difference between table-valued functions and views? Is there something you can do with 1 that's hard or impossible to do with the other? Or does the difference lie in efficiency? 回答1: A parameterless inline TVF and a non materialized View are very similar. A few functional differences that spring to mind are below. Views Accepts Parameters - No Expanded out by Optimiser - Yes Can be Materialized in advance - Yes (through indexed views) Is Updatable - Yes Can contain Multiple

Combine results of two unrelated queries into single view

本秂侑毒 提交于 2019-11-29 21:05:24
问题 Is it possible to combine the results of two separate (unrelated) sql queries into a single view. I am trying to total some figures for users and count the views for videos this month to display on a dashboard. i.e., select count(*) from video where monthname(views) = 'May'; and select sum(sessions) from user where user_id = 6; I would like to create a view that combines that contains these two results. Is this possible? 回答1: SELECT t2.total_session, t1.watch_count FROM (SELECT 1 AS common

How do read-only database views fit into the repository pattern?

假如想象 提交于 2019-11-29 20:44:09
Example: Your database has a SQL view named "CustomerOrdersOnHold". This view returns a filtered mix of specific customer and order data fields. You need to fetch data from this view in your application. How does access to such a view fit into the repository pattern? Would you create a "CustomerOrdersOnHoldRepository"? Is a read-only view such as this considered an aggregate root? Mohamed Abed I would prefer separating the read repository, preferably even change its name to Finder or Reader, the repository is meant for Domain usage not for querying read-only data, you can refer to this article

Creating a View using stored procedure

狂风中的少年 提交于 2019-11-29 16:06:02
This questions have asked few times before, unfortunately I did not get an answer to my questions. Well I have two SQL ( SQL SERVER 2008 ) tables, Employee and Employee expens, where Employee Id is the Primary key and the foreign key respectively. Employee table columns, 1. Employee Id (P Key) 2. Manager 3. Location 4. Join Date 5. Name Employee Expense table columns, 1. Expense Id (P Key) 2. Employee Id (F key) 3. Expense Type 4. Expense Amount 5. Expense Date. Question is, I want to create a view to be used in a SharePoint web part, where I will query both table, So my requirement is to

MYSQL UPDATE with IN and Subquery

China☆狼群 提交于 2019-11-29 12:51:46
问题 Hi i have tables like this : table entry : id | total_comments _____________________ 1 | 0 2 | 0 3 | 0 4 | 0 table comments : id | eid | comment _____________________ 1 | 1 | comment sdfd 2 | 1 | testing testing 3 | 1 | comment text 4 | 2 | dummy comment 5 | 2 | sample comment 6 | 1 | fg fgh dfh Query i write : UPDATE entry SET total_comments = total_comments + 1 WHERE id IN ( SELECT eid FROM comments WHERE id IN (1,2,3,4,5,6)) Results i get is : table entry : id | total_comments ____________

Using COALESCE in SQL view

≡放荡痞女 提交于 2019-11-29 09:06:20
问题 I need to create a view from several tables. One of the columns in the view will have to be composed out of a number of rows from one of the table as a string with comma-separated values. Here is a simplified example of what I want to do. Customers: CustomerId int CustomerName VARCHAR(100) Orders: CustomerId int OrderName VARCHAR(100) There is a one-to-many relationship between Customer and Orders. So given this data Customers 1 'John' 2 'Marry' Orders 1 'New Hat' 1 'New Book' 1 'New Phone' I