Join query in specific column table - sql

i have table with query i can get final price , i want join query to table as new column.
my query:
SELECT ID , Name FROM dbo.AshkhasList
select dbo.Person_Mande(40,'1398/01/01','1400/12/29',DEFAULT)
line 1 query call table with id and name , line 2 get final price as id.
i want join final price query line 2 as column name Price inside name in result.

i found my problem:
SELECT ID , Name , dbo.Person_Mande(ID,'1398/01/01','1400/12/29',DEFAULT) AS Mande FROM dbo.AshkhasList

Related

Adding a Varchar auto-increment column to an select query

I'm trying to design a database and I have grouped a certain data as follows
select Name, prod_cat, prod_country, count(*)
from table1
group by Name, prod_cat, prod_country
union
select Name, prod_cat, prod_country, count(*)
from table2
group by Name, prod_cat, prod_country
Now for the resultant data from the query above, I would like to add an auto-increment ID as a VARCHAR column which should start as 'U000001' , 'U000002' , 'U000003' , 'U000004' ... all the way to the last column
For a better database, is it better to put the resultant query into a temporary table/view or is it better to use a stored procedure as there could be more data coming in
How can I add an auto-incrementing ID VARCHAR column to the above mentioned select query? Should I use Declare and increment?
Expected result :
Prod_ID
Name
Prod_cat
Prod_country
U000001
abc
12
USA
U000002
efg
1
IND
U000003
def
3
MEX
U000004
ijk
21
CHN

How do I Use Count Function for different values of the same column in MS-SQL?

For DB StudentInfo and Table Student as follows:
CREATE TABLE Student
(
ID INT PRIMARY KEY IDENTITY(1,1),
Name nvarchar(255)
)
and inserting values:
Insert Into Student Values ('Ashok')`
executing it 3 times, and
Insert Into Student Values ('Achyut')
executing it 2 times and total 5 rows of data are inserted into the table.
I want to display a result counting the result with the name having 'Ashok' & 'Achyut'.
Generally for single values count in a column I use:
SELECT Count(Name) AS NoOfStudentHavingNameAshok
FROM Student
WHERE Name = 'Ashok'
but how to display the NoOfStudentHavingNameAshok & NoOfStudentHavingNameAchyut what query should I run?
You should include name in the select and group by name.
SELECT name, Count(*)
From Student
group by name
You can put conditions inside your COUNT() function:
select count(case when Name = 'Ashok' then 'X' end) as NoOfStudentHavingNameAshok,
count(case when Name = 'Achyut' then 'X' end) as NoOfStudentHavingNameAchyut
from Student

issue in joining two tables using UNION

I need to find target minus source table.Following is the details of tables:
Columns of Target table - customer_match :
- ID
- CUST_ID
- USER_ID - has default value 'A'
Columns of Source table - sales1 :
- id
Column of Source table - sales2
- cu_id
Mapping looks like this:
1. customer_match.ID = sales1.id
2. customer_match.CUST_ID = sales2.cu_id
3. customer_match.USER_ID = 'A'
Source tables should have a union join.
I have written query in teradata based on above requirement as:
sel
ID
, CUST_ID
, USER_ID
from customer_match
MINUS
sel
id
,'A'
from sales1
UNION
sel
cu_id
,'A'
from sales2
But this query does not satisfy my requirement as my both id and cu_id are getting mapped to only one column.
Can you please help me to correct my query as per the requirement.
You can't union two values in query. Use field name instead of it.
select
id
from sales1
UNION
select
cu_id
from sales2

query with calculated fields

I've got table
Manga
idmanga title idauthor idgenre idmagazine
and table
Author
idauthor name surname
How to get table with fields
fullname title sumofids
name+surname idmanga+idauthor+idgenre+idmagazine
I can get fullname like this
select name+' '+surname as Fullname from Author
But how to get other fields in one query?
select CONVERT(VARCHAR,idmanga)
+CONVERT(VARCHAR,idauthor)
+CONVERT(VARCHAR,idgenre)
+CONVERT(VARCHAR,idmagazine)
That should do it all in one string.
Add + ' ' + to put in spaces.
To add all the values together, your query should work to get the values for every row. If you want to group by the Name to roll the results up, use the SUM() and you'll get one row per unique name combination
create table Test ( Name varchar(10)
,idmanga int
,idauthor int
,idgenre int
,idmagazine int)
insert into Test
select 'Roger',1,2,3,4
union select 'Bob',4,5,6,7
union select 'Roger',8,9,10,11
union select 'Bob',12,13,14,15
union select 'Bill',16,17,18,19
select Name
, idmanga+idauthor+idgenre+idmagazine
from Test
select Name
, SUM(idmanga+idauthor+idgenre+idmagazine)
from Test
group by Name

SQL single table with two columns

I have a single table: Checking, with two columns: ID, Memo
ID is the primary key.
I would like a query that returns both columns: ID, Memo BUT only where Memo is DISTINCT
I can do the following to get the distinct values from Memo:
SELECT DISTINCT(memo)
FROM checking
How do I return those Memo values and their values from the ID column?
I've run in circles trying inner and outer joins but I'm failing.
Thanks for your help
Sample data:
ID Memo
1 a
2 c
3 e
4 g
5 a
6 c
The desired return:
1,a
2,c
3,e
4,g
5 and 6 would not be included because they have duplicate memo values.
Thanks again for your help.
SELECT min(id), memo
FROM checking
group by memo
If I understand you correctly you want the first (?) primary key value corresponding to each unique memo value found? (I'm assuming this because you can't logically have both unique memos and Id values because there is necessarily multiple ID values for each duplicated memo value...) If the assumption is correct this will work:
SELECT m.memo,
(SELECT TOP 1 x.id
FROM checking x
WHERE x.memo = m.memo
ORDER BY x.id) as ID
FROM checking m
GROUP BY m.memo
SELECT ID,MEMO from CHECKING WHERE ID IN (SELECT MIN(ID) FROM CHECKING WHERE MEMO IN (SELECT DISTINCT MEMO FROM CHECKING))
Though nested, this query would server the purpose!
SELECT id,memo
FROM test
WHERE id IN
(
SELECT MIN(id)
FROM test
WHERE memo IN
(
SELECT DISTINCT memo
FROM test
)
GROUP BY memo
)
select id, memo
from checking
join
( /*only where Memo is DISTINCT*/
select distinct(memo) as memo
from checking
) as m on checking.memo = m.memo