Click here to Skip to main content
15,887,821 members
Home / Discussions / Database
   

Database

 
Questionhow to use sub report in rdlc Pin
mhd.sbt6-Jan-14 22:12
mhd.sbt6-Jan-14 22:12 
Question[solved] unknown syntax of stored procedure Pin
delosb6-Jan-14 4:33
delosb6-Jan-14 4:33 
AnswerRe: unknown syntax of stored procedure Pin
Peter Leow6-Jan-14 5:00
professionalPeter Leow6-Jan-14 5:00 
GeneralRe: unknown syntax of stored procedure Pin
delosb6-Jan-14 5:12
delosb6-Jan-14 5:12 
GeneralRe: unknown syntax of stored procedure Pin
Peter Leow6-Jan-14 5:20
professionalPeter Leow6-Jan-14 5:20 
Questioni want to write a optimal query for a complex problem Pin
mhd.sbt5-Jan-14 22:32
mhd.sbt5-Jan-14 22:32 
QuestionRe: i want to write a optimal query for a complex problem Pin
Jörgen Andersson6-Jan-14 1:41
professionalJörgen Andersson6-Jan-14 1:41 
QuestionHow to loop or something a temp distinct table to sum up the totals Pin
jkirkerx3-Jan-14 10:38
professionaljkirkerx3-Jan-14 10:38 
I'm not very good at SQL, but I did write this for SQL Server 2012

I'm trying to write TSQL to get all the stuff sold on a date, distinct list of stuff based on part number, and just add up the total qty sold, and the subtotal based on price for each item.

I'm not sure if I should loop the @Temptable results using WHERE partnumber
I'm stuck here, not even sure what words to use to search for learning. I could use a pointer or suggested method to use here.
DECLARE <a href="/Members/Year">@Year</a>AS int;
DECLARE <a href="/Members/month">@Month</a>AS int;
DECLARE <a href="/Members/Day">@Day</a>AS int;
SET <a href="/Members/Year">@Year</a>= 2013;
SET <a href="/Members/month">@Month</a>= 8;
SET <a href="/Members/Day">@Day</a>= 29;

DECLARE @startDate AS DATE;
DECLARE @stopDate AS DATE;
SET @startDate = DATETIMEFROMPARTS(@Year, <a href="/Members/month">@Month</a> <a href="/Members/Day">@Day</a> 0, 0, 0, 0);
SET @stopDate = DATETIMEFROMPARTS(@Year, <a href="/Members/month">@Month</a> <a href="/Members/Day">@Day</a> 23, 59, 59, 999);

DECLARE <a href="/Members/temp">@Temp</a>TABLE(
    PartNumber VarChar(80),
    ThumbNail VarChar(250),
    Qty INT,
    Cost Decimal,
    Price Decimal       
)
INSERT INTO <a href="/Members/temp">@Temp</a>(PartNumber, ThumbNail, Qty, Cost, Price)
SELECT PartNumber, ThumbNail, Qty, Cost, Price From CompletedOrdersCart WHERE OrderDate > @startDate AND OrderDate < @stopDate

////////////////////////////////////////////////////
// Here I have a table returned with 8 items in it called <a href="/Members/temp">@Temp</a>
06-CRL10, /images/06-cr10.jpg, 1, 6.45, 10.50
06-CRL10, /images/06-cr10.jpg, 1, 6.45, 10.50
06-CRL10, /images/06-cr10.jpg, 1, 6.45, 10.50
06-CRL10, /images/06-cr10.jpg, 1, 6.45, 10.50
33-8114, /images/33114.jpg, 1, 3.45, 6.00
33-8114, /images/33114.jpg, 1, 3.45, 6.00
33-8114, /images/33114.jpg, 1, 3.45, 6.00
33-8114, /images/33114.jpg, 1, 3.45, 6.00
////////////////////////////////////////////////////

DECLARE @SummaryItems TABLE(
    PartNumber VarChar(80),
    ThumbNail VarChar(250),
    Qty INT,
    Cost Decimal,
    Price Decimal,
    SubTotal Decimal    
)
INSERT INTO @SummaryItems (PartNumber, ThumbNail, Cost, Price)
SELECT DISTINCT(PartNumber), ThumbNail, Cost, Price FROM <a href="/Members/temp">@Temp</a>

/////////////////////////////////////////////////////////
// Here I have a table returned with 2 distinct items, 
06-CRL10, /images/06-cr10.jpg, null, 6.45, 10.50, null
33-8114, /images/33114.jpg, null, 3.45, 6.00, null
/////////////////////////////////////////////////////////

// I want to add up the total qty, and create a subtotal of SUM(qty * price)
// from the table results above <a href="/Members/temp">@Temp</a>


// display the final results here
SELECT PartNumber, Thumbnail, Qty, Cost, Price, SubTotal FROM @SummaryItems;

AnswerRe: How to loop or something a temp distinct table to sum up the totals Pin
Jörgen Andersson4-Jan-14 19:00
professionalJörgen Andersson4-Jan-14 19:00 
GeneralRe: How to loop or something a temp distinct table to sum up the totals Pin
jkirkerx4-Jan-14 19:38
professionaljkirkerx4-Jan-14 19:38 
GeneralRe: How to loop or something a temp distinct table to sum up the totals Pin
Jörgen Andersson4-Jan-14 21:04
professionalJörgen Andersson4-Jan-14 21:04 
GeneralRe: How to loop or something a temp distinct table to sum up the totals Pin
jkirkerx5-Jan-14 7:57
professionaljkirkerx5-Jan-14 7:57 
GeneralRe: How to loop or something a temp distinct table to sum up the totals Pin
jkirkerx5-Jan-14 8:29
professionaljkirkerx5-Jan-14 8:29 
GeneralRe: How to loop or something a temp distinct table to sum up the totals Pin
Christian Graus5-Jan-14 16:02
protectorChristian Graus5-Jan-14 16:02 
GeneralRe: How to loop or something a temp distinct table to sum up the totals Pin
Jörgen Andersson5-Jan-14 22:19
professionalJörgen Andersson5-Jan-14 22:19 
GeneralRe: How to loop or something a temp distinct table to sum up the totals Pin
jkirkerx6-Jan-14 6:06
professionaljkirkerx6-Jan-14 6:06 
GeneralRe: How to loop or something a temp distinct table to sum up the totals Pin
Jörgen Andersson5-Jan-14 22:16
professionalJörgen Andersson5-Jan-14 22:16 
GeneralRe: How to loop or something a temp distinct table to sum up the totals Pin
jkirkerx6-Jan-14 6:15
professionaljkirkerx6-Jan-14 6:15 
GeneralRe: How to loop or something a temp distinct table to sum up the totals Pin
Jörgen Andersson6-Jan-14 8:17
professionalJörgen Andersson6-Jan-14 8:17 
GeneralRe: How to loop or something a temp distinct table to sum up the totals Pin
jkirkerx6-Jan-14 7:01
professionaljkirkerx6-Jan-14 7:01 
GeneralRe: How to loop or something a temp distinct table to sum up the totals Pin
Jörgen Andersson6-Jan-14 8:36
professionalJörgen Andersson6-Jan-14 8:36 
GeneralRe: How to loop or something a temp distinct table to sum up the totals Pin
jkirkerx6-Jan-14 9:02
professionaljkirkerx6-Jan-14 9:02 
QuestionNot work correctly Pin
Member 100045982-Jan-14 23:05
Member 100045982-Jan-14 23:05 
AnswerRe: Not work correctly Pin
Shameel2-Jan-14 23:14
professionalShameel2-Jan-14 23:14 
GeneralRe: Not work correctly Pin
Member 100045983-Jan-14 0:07
Member 100045983-Jan-14 0:07 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.